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+ }
0 commit comments