88using string = std::string;
99using glm::vec2;
1010
11- const float DEBOUNCE_DELAY = 0 .2f ;
11+ const float DEBOUNCE_DELAY = 0 .05f ;
1212
1313int input_text_callback (ImGuiInputTextCallbackData* data) {
1414 if (data->EventFlag != ImGuiInputTextFlags_CallbackResize) {
@@ -22,7 +22,7 @@ int input_text_callback(ImGuiInputTextCallbackData* data) {
2222
2323
2424
25- void render (FunctionState& state, unsigned int & op_tex, unsigned int & const_tex) {
25+ void render (FunctionState& state, unsigned int & op_tex, unsigned int & const_tex, Shader& interpreter_shader ) {
2626 try {
2727 vector<TokenOperator> stack = parser::parse (state.expression );
2828 vector<unsigned char > operator_stack;
@@ -38,27 +38,51 @@ void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex)
3838 state.error_message = e.what ();
3939 state.needs_reparse = false ;
4040 }
41+ state.current_shader = &interpreter_shader;
42+ state.is_interpreted = true ;
4143}
4244
43- void render_and_update (FunctionState& state, unsigned int & op_tex, unsigned int & const_tex){
45+ void compile (FunctionState& state, CompilerShader& compiler_shader) {
46+ try {
47+ vector<TokenOperator> stack = parser::parse (state.expression );
48+ const string& expression = stack_to_glsl_string (stack);
49+ compiler_shader.compile (expression);
50+ }
51+ catch (const std::runtime_error& e) {
52+ state.error_message = e.what ();
53+ state.needs_reparse = false ;
54+ }
55+ state.current_shader = &compiler_shader.shader ;
56+ state.is_interpreted = false ;
57+ }
58+
59+ void render_and_update (FunctionState& state, unsigned int & op_tex, unsigned int & const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader){
4460 ImGui::Begin (" Function editor" );
4561 ImGui::Text (" Enter a complex function f(z)=" );
4662 ImGui::SetNextItemWidth (-FLT_MIN );
47- bool typed = ImGui::InputText (" ##source" ,
63+ bool pressed_enter = ImGui::InputText (" ##source" ,
4864 static_cast <char *>(state.expression .data ()),
4965 state.expression .capacity () + 1 ,
50- ImGuiInputTextFlags_CallbackResize,
66+ ImGuiInputTextFlags_CallbackResize |
67+ ImGuiInputTextFlags_EnterReturnsTrue,
5168 input_text_callback,
5269 &state.expression );
53- if (typed) {
70+ bool typed = ImGui::IsItemEdited ();
71+
72+ if (typed && !pressed_enter) {
5473 state.last_typing_time = glfwGetTime ();
5574 state.needs_reparse = true ;
5675 }
5776
5877 const float current_time = glfwGetTime ();
5978 const float time_dif = current_time - state.last_typing_time ;
60- if (state.needs_reparse && time_dif < DEBOUNCE_DELAY ) {
61- render (state, op_tex, const_tex);
79+ const bool debounced = time_dif > DEBOUNCE_DELAY ;
80+
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);
6286 }
6387 if (!state.error_message .empty ()) {
6488 ImGui::TextColored (ImVec4 (1 .0f , 0 .4f , 0 .4f , 1 .0f ), " Error: %s" , state.error_message .c_str ());
@@ -70,6 +94,7 @@ void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int&
7094 ImGui::Text (" Controls:" );
7195 ImGui::BulletText (" Pan: Left Click + Drag" );
7296 ImGui::BulletText (" Zoom: Scroll Wheel" );
97+ ImGui::BulletText (" Enter: Compile (increases performance)" );
7398
7499 ImGui::End ();
75100}
0 commit comments