Skip to content

Commit 9de9c6f

Browse files
committed
add control panel buttons
1 parent 144adb3 commit 9de9c6f

File tree

1 file changed

+108
-31
lines changed

1 file changed

+108
-31
lines changed

UI/ui.cpp

Lines changed: 108 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,128 @@
11
#include "imgui.h"
2+
#include "imgui_internal.h"
23
#include "implot.h"
34
#include "math.h"
5+
#include <string>
46

57
#include <iostream>
68

7-
void render_loop() {
8-
static int frame_counter = 0;
9-
frame_counter++;
9+
#define DO_COUNT
10+
11+
bool ui_do_state[DO_COUNT] = {false, false, false, false, false};
12+
13+
// Helper to adjust brightness
14+
ImU32 AdjustBrightness(ImU32 color, float factor) {
15+
ImVec4 c = ImGui::ColorConvertU32ToFloat4(color);
16+
c.x = ImClamp(c.x * factor, 0.0f, 1.0f);
17+
c.y = ImClamp(c.y * factor, 0.0f, 1.0f);
18+
c.z = ImClamp(c.z * factor, 0.0f, 1.0f);
19+
return ImGui::ColorConvertFloat4ToU32(c);
20+
}
21+
22+
bool daq_button(const char *label, const ImVec2 &size,
23+
ImU32 color, float rounding = 10.0f) {
24+
ImGui::PushStyleColor(ImGuiCol_Button, color);
25+
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, AdjustBrightness(color, 1.2));
26+
ImGui::PushStyleColor(ImGuiCol_ButtonActive, AdjustBrightness(color, 0.7));
27+
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, rounding);
28+
29+
bool clicked = ImGui::Button(label, size);
30+
31+
ImGui::PopStyleVar();
32+
ImGui::PopStyleColor(3);
33+
34+
return clicked;
35+
}
36+
37+
void toggle_button(const char *label, const ImVec2 &size, ImU32 active_color, ImU32 deactive_color, bool *control_var, float rounding = 10.0f) {
38+
ImU32 color;
39+
ImU32 clicked_color;
40+
if (*control_var) {
41+
color = active_color;
42+
clicked_color = deactive_color;
43+
} else {
44+
color = deactive_color;
45+
clicked_color = active_color;
46+
}
47+
48+
ImGui::PushStyleColor(ImGuiCol_Button, color);
49+
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, AdjustBrightness(color, 1.2));
50+
ImGui::PushStyleColor(ImGuiCol_ButtonActive, AdjustBrightness(color, 1.2)); // stops flickering
51+
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, rounding);
52+
53+
bool clicked = ImGui::Button(label, size);
54+
55+
ImGui::PopStyleVar();
56+
ImGui::PopStyleColor(3);
57+
58+
if (clicked) {
59+
std::cout << "toggle" << std::endl;
60+
*control_var = !(*control_var);
61+
}
62+
}
63+
64+
void control_panel() {
65+
ImGui::Text("Control Panel");
1066

11-
ImGuiIO io = ImGui::GetIO();
67+
if (ImGui::BeginTable("start_abort", 2)) {
68+
ImGui::TableNextColumn();
69+
if (daq_button("START", ImVec2(-1, 50), IM_COL32(33, 112, 69, 255))) {
70+
std::cout << "START" << std::endl;
71+
};
72+
ImGui::TableNextColumn();
73+
if (daq_button("ABORT", ImVec2(-1, 50), IM_COL32(204, 0, 0, 255))) {
74+
std::cout << "END" << std::endl;
75+
};
76+
ImGui::EndTable();
77+
}
78+
79+
if (ImGui::BeginTable("do_control", 5)) {
80+
ImGui::TableNextRow();
81+
for (int i = 0; i < 5; i++) {
82+
ImGui::TableSetColumnIndex(i);
83+
ImGui::PushID(i);
84+
std::string do_name = "DO " + std::to_string(i + 1);
85+
toggle_button(do_name.c_str(), ImVec2(-1, 50), IM_COL32(33, 112, 69, 255), IM_COL32(50, 50, 50, 255), &ui_do_state[i]);
86+
ImGui::PopID();
87+
}
88+
ImGui::EndTable();
89+
}
90+
}
91+
92+
void plot_panel() {
93+
for (int i = 0; i < 3; i++) {
94+
std::string plot_name = "Plot " + std::to_string(i + 1);
95+
if (ImPlot::BeginPlot(plot_name.c_str(), ImVec2(-1, 0))) { // width = fill, height auto
96+
// Example: simple sine wave
97+
static float xs[1000], ys[1000];
98+
for (int j = 0; j < 1000; ++j) {
99+
xs[j] = j * 0.01f;
100+
ys[j] = sin(xs[j] + i); // different phase for each plot
101+
}
102+
ImPlot::PlotLine("Sine", xs, ys, 1000);
103+
ImPlot::EndPlot();
104+
}
105+
}
106+
}
12107

108+
void render_loop() {
109+
ImGuiIO &io = ImGui::GetIO();
13110
ImGui::SetNextWindowPos(ImVec2(0, 0));
14-
ImGui::SetNextWindowSize(io.DisplaySize); // io = ImGui::GetIO()
111+
ImGui::SetNextWindowSize(io.DisplaySize);
112+
15113
ImGui::Begin("MainWindow", nullptr,
16114
ImGuiWindowFlags_NoTitleBar |
17115
ImGuiWindowFlags_NoResize |
18116
ImGuiWindowFlags_NoMove |
19117
ImGuiWindowFlags_NoCollapse |
20118
ImGuiWindowFlags_NoBringToFrontOnFocus |
21119
ImGuiWindowFlags_NoNavFocus);
22-
// Get available width for plots
23-
ImVec2 avail = ImGui::GetContentRegionAvail();
24-
25-
// Sine Plot
26-
static float xs[1000], ys[1000];
27-
for (int i = 0; i < 1000; ++i) {
28-
xs[i] = i * 0.01f;
29-
ys[i] = sin(xs[i] + frame_counter / 100.0);
30-
}
31-
if (ImPlot::BeginPlot("Sine Wave")) { // half height
32-
ImPlot::PlotLine("sin(x)", xs, ys, 1000);
33-
ImPlot::EndPlot();
34-
}
35120

36-
// Cosine Plot
37-
for (int j = 0; j < 5; j++) {
38-
for (int i = 0; i < 1000; ++i) {
39-
ys[i] = cos(xs[i] + (frame_counter + j * 20) / 10.0);
40-
}
41-
ImGui::PushID(j);
42-
if (ImPlot::BeginPlot("Cosine Wave", ImVec2(-1, 100))) { // half height
43-
ImPlot::PlotLine("cos(x)", xs, ys, 1000);
44-
ImPlot::EndPlot();
45-
}
46-
ImGui::PopID();
47-
}
121+
// Split into 2 sections: left (buttons) and right (plots)
122+
ImGui::Columns(2, nullptr, true); // true = resizable
123+
control_panel();
124+
ImGui::NextColumn(); // move to right column
125+
plot_panel();
48126

49-
ImGui::Text("%f", io.Framerate);
50127
ImGui::End();
51128
}

0 commit comments

Comments
 (0)