Skip to content

Commit 3dbbeb1

Browse files
committed
feat(userInterface): connect functions panel to renderer
1 parent 638e7f2 commit 3dbbeb1

4 files changed

Lines changed: 117 additions & 29 deletions

File tree

‎include/mathplotter/application.hpp‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
#include <SFML/Graphics/RenderWindow.hpp>
1212
#include <SFML/Graphics/View.hpp>
1313

14+
#include <memory>
1415
#include <vector>
1516

1617
namespace mathplotter {
1718

1819
class Application {
1920
private:
20-
sf::RenderWindow m_window;
21-
sf::Font m_font;
22-
sf::View m_interfaceView;
23-
Theme m_theme;
24-
CameraController m_cameraController;
25-
GridRenderer m_gridRenderer;
26-
std::vector<FunctionRenderer> m_functionRenderers;
27-
UserInterface m_userInterface;
21+
sf::RenderWindow m_window;
22+
sf::Font m_font;
23+
sf::View m_interfaceView;
24+
Theme m_theme;
25+
CameraController m_cameraController;
26+
GridRenderer m_gridRenderer;
27+
std::vector<std::unique_ptr<FunctionRenderer>> m_functionRenderers;
28+
UserInterface m_userInterface;
2829

2930
void ProcessEvents();
3031
public:

‎include/mathplotter/userInterface.hpp‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,44 @@
99
#include <SFML/Graphics/Text.hpp>
1010
#include <imgui.h>
1111

12+
#include <cstddef>
13+
#include <optional>
1214
#include <string>
1315
#include <vector>
1416

1517
namespace mathplotter {
1618

19+
struct FunctionSubmission {
20+
std::size_t rowIndex;
21+
std::string expression;
22+
sf::Color color;
23+
};
24+
1725
class UserInterface {
1826
private:
1927
struct FunctionRow {
2028
std::string expression;
2129
ImVec4 color{1.f, 0.f, 0.f, 1.f};
30+
bool isSubmitted{false};
2231
};
2332

2433
sf::Text m_title;
2534
int m_selectedThemeIndex{0};
2635
std::vector<FunctionRow> m_functionRows;
2736

28-
void DrawFunctionPanel(const Theme& theme);
37+
std::optional<FunctionSubmission> DrawFunctionPanel(
38+
const Theme& theme
39+
);
2940
void ConfigureTitle(const Theme& theme);
3041
void SelectTheme(Theme& theme);
3142
ImVec4 ToImGuiColor(const sf::Color& color);
43+
sf::Color ToSfmlColor(const ImVec4& color);
3244
public:
3345
UserInterface(const sf::Font& font, const Theme& theme);
34-
void Draw(sf::RenderWindow& window, Theme& theme);
46+
std::optional<FunctionSubmission> Draw(
47+
sf::RenderWindow& window,
48+
Theme& theme
49+
);
3550
};
3651

3752
} // namespace mathplotter

‎src/application.cpp‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ namespace mathplotter {
7979
void Application::Run() {
8080
sf::Clock deltaClock;
8181

82-
m_functionRenderers.emplace_back("x^3", m_theme.GetFunctionColor());
8382
while (m_window.isOpen()) {
8483
ProcessEvents();
8584
ImGui::SFML::Update(m_window, deltaClock.restart());
@@ -110,13 +109,42 @@ namespace mathplotter {
110109
);
111110

112111
// Render Function
113-
for (auto& function: m_functionRenderers) {
114-
function.Draw(m_window, bounds, m_gridRenderer.GetMajorGridStep());
112+
for (auto& function : m_functionRenderers) {
113+
if (function) {
114+
function->Draw(
115+
m_window,
116+
bounds,
117+
m_gridRenderer.GetMajorGridStep()
118+
);
119+
}
120+
}
121+
122+
const auto submission{
123+
m_userInterface.Draw(
124+
m_window,
125+
m_theme
126+
)
127+
};
128+
129+
if (submission.has_value()) {
130+
if (
131+
m_functionRenderers.size() <=
132+
submission->rowIndex
133+
) {
134+
m_functionRenderers.resize(
135+
submission->rowIndex + 1
136+
);
137+
}
138+
139+
m_functionRenderers[submission->rowIndex] =
140+
std::make_unique<FunctionRenderer>(
141+
submission->expression,
142+
submission->color
143+
);
115144
}
116145

117146
// Render User Interface
118147
m_window.setView(m_interfaceView);
119-
m_userInterface.Draw(m_window, m_theme);
120148

121149
ImGui::SFML::Render(m_window);
122150
m_window.display();

‎src/userInterface.cpp‎

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
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

1215
namespace 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

Comments
 (0)