Skip to content

Commit ba3f299

Browse files
committed
UI and constant folder overhaul
1 parent 1d123a6 commit ba3f299

8 files changed

Lines changed: 248 additions & 87 deletions

File tree

‎src/graphics/ui.cpp‎

Lines changed: 114 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <parser/parser.h>
33
#include <transformer/transformer.h>
44
#include <graphics/graphics.h>
5+
#include <interactions/interactions.h>
56
#include <string>
67
#include <iostream>
78

@@ -56,46 +57,118 @@ void compile(FunctionState& state, CompilerShader& compiler_shader) {
5657
state.is_interpreted = false;
5758
}
5859

59-
void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader){
60-
ImGui::Begin("Function editor");
61-
ImGui::Text("Enter a complex function f(z)=");
62-
ImGui::SetNextItemWidth(-FLT_MIN);
63-
bool pressed_enter = ImGui::InputText("##source",
64-
static_cast<char*>(state.expression.data()),
65-
state.expression.capacity() + 1,
66-
ImGuiInputTextFlags_CallbackResize |
67-
ImGuiInputTextFlags_EnterReturnsTrue,
68-
input_text_callback,
69-
&state.expression);
70-
bool typed = ImGui::IsItemEdited();
71-
72-
if (typed && !pressed_enter) {
73-
state.last_typing_time = glfwGetTime();
74-
state.needs_reparse = true;
75-
}
76-
77-
const float current_time = glfwGetTime();
78-
const float time_dif = current_time - state.last_typing_time;
79-
const bool debounced = time_dif > DEBOUNCE_DELAY;
60+
void change_3d_mode(ViewState& view_state) {
8061

81-
if (pressed_enter) {
82-
compile(state, compiler_shader);
83-
}
84-
if (state.needs_reparse && debounced && !pressed_enter) {
85-
render(state, op_tex, const_tex, interpreter_shader);
86-
}
87-
if (!state.error_message.empty()) {
88-
ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "Error: %s", state.error_message.c_str());
89-
}
90-
else {
91-
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "Valid function!");
92-
}
93-
ImGui::Separator();
94-
ImGui::Text("Controls:");
95-
ImGui::BulletText("Pan: Left Click + Drag");
96-
ImGui::BulletText("Zoom: Scroll Wheel");
97-
ImGui::BulletText("Enter: Compile (increases performance)");
98-
ImGui::BulletText("T: Toggle 3D mode");
99-
100-
ImGui::End();
10162
}
63+
64+
65+
void render_and_update(FunctionState& state, ViewState& view_state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader, bool& should_switch) {
66+
ImGui::Begin("Function Control", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
67+
ImGui::SameLine();
68+
if (state.is_interpreted) {
69+
ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.0f, 1.0f), "[ INTERPRETED ]");
70+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("May be slow for large expressions. Press enter to increase performance");
71+
}
72+
else {
73+
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "[ COMPILED ]");
74+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Faster for large expressions");
75+
}
76+
ImGui::Separator();
77+
78+
ImGui::Text("f(z) = ");
79+
ImGui::SameLine();
80+
81+
static bool auto_reparse = true;
82+
83+
bool pressed_enter = ImGui::InputText("##source",
84+
static_cast<char*>(state.expression.data()),
85+
state.expression.capacity() + 1,
86+
ImGuiInputTextFlags_CallbackResize | ImGuiInputTextFlags_EnterReturnsTrue,
87+
input_text_callback,
88+
&state.expression);
89+
90+
bool typed = ImGui::IsItemEdited();
91+
92+
if (typed && !pressed_enter && auto_reparse) {
93+
state.last_typing_time = glfwGetTime();
94+
state.needs_reparse = true;
95+
}
96+
97+
ImGui::SameLine();
98+
if (ImGui::Button("Compile")) {
99+
pressed_enter = true;
100+
}
101+
102+
const float current_time = glfwGetTime();
103+
const float time_dif = current_time - state.last_typing_time;
104+
const bool debounced = time_dif > DEBOUNCE_DELAY;
105+
106+
if (pressed_enter) {
107+
compile(state, compiler_shader);
108+
}
109+
if (state.needs_reparse && debounced && !pressed_enter) {
110+
render(state, op_tex, const_tex, interpreter_shader);
111+
}
112+
113+
if (!state.error_message.empty()) {
114+
ImGui::Spacing();
115+
ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "Error: %s", state.error_message.c_str());
116+
ImGui::Spacing();
117+
}
118+
119+
ImGui::Spacing();
120+
ImGui::Separator();
121+
if (ImGui::CollapsingHeader("Settings", ImGuiTreeNodeFlags_DefaultOpen)) {
122+
ImGui::Checkbox("Auto-Reparse (Live Edit)", &auto_reparse);
123+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("If disabled, you must press Enter to see changes.");
124+
125+
ImGui::Text("View Mode:");
126+
ImGui::SameLine();
127+
if (ImGui::RadioButton("2D Plane", !state.is_3d)) {
128+
view_state.is_3d = false;
129+
}
130+
ImGui::SameLine();
131+
if (ImGui::RadioButton("3D Surface", state.is_3d)) {
132+
view_state.is_3d = true;
133+
}
134+
}
135+
136+
if (ImGui::CollapsingHeader("3D Keybinds")) {
137+
ImGui::BulletText("WASD: Move");
138+
ImGui::BulletText("Right click + Drag: Move camera");
139+
ImGui::BulletText("Shift/Spacebar: Go up");
140+
ImGui::BulletText("Ctrl: Go down");
141+
}
142+
if (ImGui::CollapsingHeader("Help & Keybinds")) {
143+
ImGui::BulletText("Left Click + Drag: Pan Camera");
144+
ImGui::BulletText("Scroll Wheel: Zoom");
145+
ImGui::BulletText("Enter: Compile (High Performance)");
146+
ImGui::BulletText("T: Quick Toggle 2D/3D");
147+
ImGui::TextDisabled("Supported Symbols:");
148+
static string supported_operators = []() {
149+
string out;
150+
for (const auto& op : full_operators) {
151+
if (op.token_operator.op != Operator::VARIABLEPLACEHOLDER && op.token_operator.op != Operator::SECONDVARIABLEPLACEHOLDER)
152+
out += op.token_operator.str_repr + ", ";
153+
}
154+
return out;
155+
}();
156+
ImGui::TextWrapped(supported_operators.c_str());
157+
}
158+
if (ImGui::CollapsingHeader("Presets")) {
159+
static const char* presets[] = {
160+
"z",
161+
"sin(z)",
162+
"(z^2+1)/(z^2-1)",
163+
"exp(1/z)",
164+
"sin(z)*cos(10/z)"
165+
};
166+
static int current_preset = -1;
167+
if (ImGui::Combo("Choose", & current_preset, presets, IM_ARRAYSIZE(presets))) {
168+
state.expression = string(presets[current_preset]);
169+
state.needs_reparse = true;
170+
}
171+
}
172+
173+
ImGui::End();
174+
}

‎src/graphics/ui.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <compiler/compiler_shader.h>
55
#include <graphics/ui_init.h>
66
#include <string>
7+
#include <interactions/interactions.h>
78

89
typedef struct FunctionState {
910
Shader* current_shader = nullptr;
@@ -17,4 +18,6 @@ typedef struct FunctionState {
1718

1819
void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader);
1920

20-
void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader);
21+
void compile(FunctionState& state, CompilerShader& compiler_shader);
22+
23+
void render_and_update(FunctionState& state,ViewState& view_state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader, bool& should_switch);

‎src/interactions/interactions.cpp‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ViewState* get_state(GLFWwindow* window) {
1010

1111
void scroll_callback(GLFWwindow* window, const double xoffset, const double yoffset) {
1212
ViewState* state = get_state(window);
13+
if (ImGui::GetIO().WantCaptureMouse) return;
1314
if (!state) return;
1415
const float zoom_factor = 0.9f;
1516
double x, y;
@@ -24,6 +25,8 @@ void scroll_callback(GLFWwindow* window, const double xoffset, const double yoff
2425
}
2526
else
2627
state->range /= zoom_factor;
28+
if (state->is_3d) return;
29+
2730
state->shift += (prev_range - state->range) * norm;
2831
}
2932

@@ -74,18 +77,3 @@ void window_size_callback(GLFWwindow* window, const int width, const int height)
7477
state->height = static_cast<float>(height);
7578
glViewport(0, 0, width, height);
7679
}
77-
78-
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
79-
{
80-
ViewState* state = get_state(window);
81-
if (action != GLFW_PRESS) return;
82-
if (key == GLFW_KEY_ESCAPE) {
83-
glfwSetWindowShouldClose(window, true);
84-
}
85-
if (key == GLFW_KEY_T) {
86-
state->is_3d = !state->is_3d;
87-
}
88-
if (key == GLFW_KEY_SPACE) {
89-
90-
}
91-
}

‎src/main.cpp‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,25 @@ int main() {
9090
render(function_state, stack_tbo_texture, constants_tbo_texture,shader_program);
9191
update_camera_vectors(camera_state);
9292
bool pressing_t = false;
93+
bool should_switch = false;
94+
95+
9396
while (!glfwWindowShouldClose(window)) {
9497
glfwPollEvents();
9598
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
99+
100+
bool is_3d = view_state.is_3d;
101+
96102
float current_frame = (float)glfwGetTime();
97103
delta_time = current_frame - last_frame;
98104
last_frame = current_frame;
99105

100106
if (!pressing_t && !ImGui::GetIO().WantCaptureKeyboard && glfwGetKey(window, GLFW_KEY_T) == GLFW_PRESS) {
101-
std::cout << "pressed t";
102107
pressing_t = true;
103108
}
104109

105110
if (pressing_t && !ImGui::GetIO().WantCaptureKeyboard && glfwGetKey(window, GLFW_KEY_T) == GLFW_RELEASE) {
106111
pressing_t = false;
107-
if (view_state.is_3d) {
108-
function_state.current_shader = &shader_program;
109-
}
110112
view_state.is_3d = !view_state.is_3d;
111113
}
112114

@@ -119,11 +121,11 @@ int main() {
119121
function_state.current_shader = &shader_3d;
120122
}
121123
glEnable(GL_DEPTH_TEST);
122-
render_and_update(function_state, stack_tbo_texture, constants_tbo_texture, shader_3d, compiled_shader_3d);
124+
render_and_update(function_state,view_state, stack_tbo_texture, constants_tbo_texture, shader_3d, compiled_shader_3d, should_switch);
123125
}
124126
else {
125127
glDisable(GL_DEPTH_TEST);
126-
render_and_update(function_state, stack_tbo_texture, constants_tbo_texture, shader_program, compiled_shader);
128+
render_and_update(function_state,view_state, stack_tbo_texture, constants_tbo_texture, shader_program, compiled_shader, should_switch);
127129
}
128130
Shader* current_shader = function_state.current_shader;
129131
current_shader->use();
@@ -156,6 +158,17 @@ int main() {
156158
glDrawArrays(GL_TRIANGLES, 0, 3);
157159
}
158160

161+
if (is_3d != view_state.is_3d) {
162+
function_state.is_3d = view_state.is_3d;
163+
if (view_state.is_3d) {
164+
function_state.current_shader = &shader_3d;
165+
}
166+
else {
167+
function_state.current_shader = &shader_program;
168+
}
169+
std::cout << "Switched to " << (view_state.is_3d ? "3D" : "2D") << std::endl;
170+
}
171+
159172
render_imgui();
160173
glfwSwapBuffers(window);
161174
}

0 commit comments

Comments
 (0)