aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ui.cpp74
1 files changed, 43 insertions, 31 deletions
diff --git a/src/ui.cpp b/src/ui.cpp
index cb68c66..aa72ac9 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -3,6 +3,7 @@
#include "sandbox.hpp"
#include <imgui.h>
+#include <memory>
static void AddSand(Sandbox& sb) {
for (int y = 80; y <= 90; ++y) {
@@ -12,30 +13,37 @@ static void AddSand(Sandbox& sb) {
}
}
-constexpr int kWidth = 40;
-constexpr int kHeight = 100;
-constexpr float kScale = 4.0f;
-constexpr ImVec2 kSize(kWidth* kScale, kHeight* kScale);
-
-static ImVec2 TrSandbox2Screen(ImVec2 origin, Pt pt) {
- return ImVec2(origin.x + kScale * pt.x, origin.y - kScale * pt.y);
-}
-
struct ImRect {
ImVec2 top_left;
ImVec2 bottom_right;
};
-// Assuming inclusive-inclusive rectangle
-static ImRect TrSandbox2Screen(ImVec2 origin, Rect r) {
- return {
- ImVec2(origin.x + kScale * r.bl.x, origin.y - kScale * (r.tr.y+1)),
- ImVec2(origin.x + kScale * (r.tr.x + 1), origin.y - kScale * r.bl.y),
- };
-}
+
+struct SandboxCfg {
+ int width = 40, height = 100;
+ float tile_size = 4.0f;
+
+ ImVec2 tr_sandbox2screen(ImVec2 origin, Pt pt) const {
+ return ImVec2(origin.x + tile_size * pt.x, origin.y - tile_size * pt.y);
+ }
+
+ // Assuming inclusive-inclusive rectangle
+ ImRect tr_sandbox2screen(ImVec2 origin, Rect r) const {
+ return {
+ ImVec2(origin.x + tile_size * r.bl.x, origin.y - tile_size * (r.tr.y + 1)),
+ ImVec2(origin.x + tile_size * (r.tr.x + 1), origin.y - tile_size * r.bl.y),
+ };
+ }
+
+ std::unique_ptr<Sandbox> make_sandbox() const {
+ return std::make_unique<Sandbox>(width, height);
+ }
+};
void ShowEverything() {
ImGui::Begin("Sandbox");
- static Sandbox sandbox(40, 100);
+
+ static SandboxCfg cfg;
+ static std::unique_ptr<Sandbox> sandbox = cfg.make_sandbox();
static OglImage gl;
static bool running = false;
@@ -43,33 +51,37 @@ void ShowEverything() {
static bool show_dirty_rect = true;
ImGui::Checkbox("Show dirty rects", &show_dirty_rect);
- ImGui::Text("ncycle = %d", sandbox.ncycle);
+ ImGui::Text("ncycle = %d", sandbox->ncycle);
ImGui::SameLine();
bool step = ImGui::Button("Step");
if (step || running) {
- sandbox.simulate_step();
- gl.upload(reinterpret_cast<const char*>(sandbox.bitmap), kWidth, kHeight);
+ sandbox->simulate_step();
}
- // TODO this makes things very wrong (no proper assignment operator)
- // if (ImGui::Button("Reset sandbox")) {
- // sandbox = Sandbox(40, 100);
- // }
- // ImGui::SameLine();
+ // TODO popup for changing these params?
+ // TODO migrate existing content
+ ImGui::InputInt("Sandbox width", &cfg.width);
+ ImGui::InputInt("Sandbox height", &cfg.height);
+ if (ImGui::Button("Recreate")) {
+ sandbox = cfg.make_sandbox();
+ }
+ ImGui::SameLine();
if (ImGui::Button("Add sand")) {
- AddSand(sandbox);
+ AddSand(*sandbox);
}
- auto sandbox_topleft = ImGui::GetCursorScreenPos();
- ImGui::Image(gl.as_imgui(), kSize, ImVec2(0, 1), ImVec2(1, 0));
+ ImVec2 img_size(cfg.tile_size * sandbox->width, cfg.tile_size * sandbox->height);
+ auto img_topleft = ImGui::GetCursorScreenPos();
+ gl.upload(reinterpret_cast<const char*>(sandbox->bitmap), sandbox->width, sandbox->height);
+ ImGui::Image(gl.as_imgui(), img_size, ImVec2(0, 1), ImVec2(1, 0));
// bottom-left corner of the sandbox, in screen space
- auto origin = sandbox_topleft;
- origin.y += kSize.y;
+ auto origin = img_topleft;
+ origin.y += img_size.y;
if (show_dirty_rect) {
auto dl = ImGui::GetWindowDrawList();
- auto [top_left, bottom_right] = TrSandbox2Screen(origin, sandbox.dirty_writeto);
+ auto [top_left, bottom_right] = cfg.tr_sandbox2screen(origin, sandbox->dirty_writeto);
dl->AddRect(top_left, bottom_right, IM_COL32(255, 0, 0, 255));
}