77#include < imgui.h>
88#include < misc/cpp/imgui_stdlib.h>
99
10+ #include < cstddef>
11+ #include < cstdint>
12+ #include < optional>
1013#include < string>
1114
1215namespace mathplotter {
@@ -37,6 +40,15 @@ namespace mathplotter {
3740 };
3841 }
3942
43+ sf::Color UserInterface::ToSfmlColor (const ImVec4& color) {
44+ return {
45+ static_cast <std::uint8_t >(color.x * 255 .f ),
46+ static_cast <std::uint8_t >(color.y * 255 .f ),
47+ static_cast <std::uint8_t >(color.z * 255 .f ),
48+ static_cast <std::uint8_t >(color.w * 255 .f )
49+ };
50+ }
51+
4052 void UserInterface::SelectTheme ([[maybe_unused]] Theme& theme) {
4153 const ImGuiViewport* viewport{ImGui::GetMainViewport ()};
4254 const ImVec2 position{
@@ -126,7 +138,11 @@ namespace mathplotter {
126138 ImGui::End ();
127139 }
128140
129- void UserInterface::DrawFunctionPanel (const Theme& theme) {
141+ std::optional<FunctionSubmission> UserInterface::DrawFunctionPanel (
142+ const Theme& theme
143+ ) {
144+ std::optional<FunctionSubmission> submission;
145+
130146 const ImVec4 background{
131147 ToImGuiColor (theme.GetBackgroundColor ())
132148 };
@@ -348,7 +364,7 @@ namespace mathplotter {
348364
349365 ImGui::TableSetColumnIndex (1 );
350366
351- if (! m_functionRows[i].expression . empty () ) {
367+ if (m_functionRows[i].isSubmitted ) {
352368
353369 constexpr float colorSize{16 .f };
354370
@@ -407,11 +423,19 @@ namespace mathplotter {
407423
408424 ImGui::SetNextItemWidth (160 .f );
409425
410- ImGui::ColorPicker4 (
411- " ##ColorPicker" ,
412- &m_functionRows[i].color .x ,
413- colorPickerFlags
414- );
426+ if (
427+ ImGui::ColorPicker4 (
428+ " ##ColorPicker" ,
429+ &m_functionRows[i].color .x ,
430+ colorPickerFlags
431+ )
432+ ) {
433+ submission = FunctionSubmission{
434+ i,
435+ m_functionRows[i].expression ,
436+ ToSfmlColor (m_functionRows[i].color )
437+ };
438+ }
415439
416440 ImGui::EndPopup ();
417441 }
@@ -428,20 +452,33 @@ namespace mathplotter {
428452
429453 ImGui::SetNextItemWidth (-1 .f );
430454
431- const bool submitted {
455+ const bool expressionSubmitted {
432456 ImGui::InputText (
433457 " ##FunctionExpression" ,
434458 &m_functionRows[i].expression ,
435459 ImGuiInputTextFlags_EnterReturnsTrue
436460 )
437461 };
438462
463+ if (ImGui::IsItemEdited ()) {
464+ m_functionRows[i].isSubmitted = false ;
465+ }
466+
439467 if (
440- submitted &&
441- !m_functionRows[i].expression .empty () &&
442- i == m_functionRows.size () - 1
468+ expressionSubmitted &&
469+ !m_functionRows[i].expression .empty ()
443470 ) {
444- addNewRow = true ;
471+ m_functionRows[i].isSubmitted = true ;
472+
473+ submission = FunctionSubmission{
474+ i,
475+ m_functionRows[i].expression ,
476+ ToSfmlColor (m_functionRows[i].color )
477+ };
478+
479+ if (i == m_functionRows.size () - 1 ) {
480+ addNewRow = true ;
481+ }
445482 }
446483
447484 ImGui::PopID ();
@@ -476,12 +513,19 @@ namespace mathplotter {
476513
477514 ImGui::End ();
478515 ImGui::PopStyleColor (9 );
516+
517+ return submission;
479518 }
480519
481- void UserInterface::Draw ([[maybe_unused]] sf::RenderWindow& window, Theme& theme) {
482- DrawFunctionPanel (theme);
520+ std::optional<FunctionSubmission> UserInterface::Draw (
521+ [[maybe_unused]] sf::RenderWindow& window,
522+ Theme& theme
523+ ) {
524+ const auto submission{DrawFunctionPanel (theme)};
525+
483526 SelectTheme (theme);
484- // window.draw(m_title);
527+
528+ return submission;
485529 }
486530
487531} // namespace mathplotter
0 commit comments