From 25dafb26b54c0738cabb0be8c642c04f6ba79f99 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 29 Feb 2024 17:10:57 +0900 Subject: [PATCH] Impl. storing color changes in history --- CMakeLists.txt | 1 + src/helpers.cpp | 12 ++++++++++++ src/helpers.hpp | 4 ++++ src/state.cpp | 38 +++++++++++++++++++++++++------------- src/state.hpp | 5 +++++ 5 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 src/helpers.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d1af539..3f8c0e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ set(Triangles_LIB_SOURCES src/triangle.cpp src/circle.cpp src/raygui.cpp + src/helpers.cpp ) add_library(TrianglesLib STATIC ${Triangles_LIB_SOURCES}) diff --git a/src/helpers.cpp b/src/helpers.cpp new file mode 100644 index 0000000..126495d --- /dev/null +++ b/src/helpers.cpp @@ -0,0 +1,12 @@ +#include "helpers.hpp" + +bool operator==(const Color& a, const Color& b) { + return a.r == b.r + && a.g == b.g + && a.b == b.b + && a.a == b.a; +} + +bool operator!=(const Color& a, const Color& b) { + return !(a == b); +} diff --git a/src/helpers.hpp b/src/helpers.hpp index 6ee4339..3dc24fb 100644 --- a/src/helpers.hpp +++ b/src/helpers.hpp @@ -307,4 +307,8 @@ namespace Tri { } } +// Color operator helpers +bool operator==(const Color& a, const Color& b); +bool operator!=(const Color& a, const Color& b); + #endif diff --git a/src/state.cpp b/src/state.cpp index 3caa818..33fc8c6 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -33,6 +33,7 @@ drawCache(), pi(std::acos(-1.0f)), selectedTri(), selectedTriColor{255, 255, 255, 255}, +prevTriColor{255, 255, 255, 255}, selectedTriBlinkTimer(), inputWidth(800), inputHeight(600), @@ -84,6 +85,11 @@ data() init(data); } +Tri::State::Action &Tri::State::Action::setNewColor(Color color) { + this->data.newColor = color; + return *this; +} + void Tri::State::Action::init(float *data) { switch(type) { case AT_TRI: @@ -96,6 +102,8 @@ void Tri::State::Action::init(float *data) { this->data.point[0] = data[0]; this->data.point[1] = data[1]; break; + case AT_COLOR: + break; default: type = AT_NONE; idx = 0; @@ -161,6 +169,9 @@ void Tri::State::handle_events() { && "There must be a point to undo a point"); currentTri_state = static_cast(currentTri_state - 1); break; + case Action::AT_COLOR: + tris.at(history[history_idx-1].idx).fillColor = history[history_idx-1].color; + break; default: assert(!"Unreachable code"); break; @@ -208,6 +219,9 @@ void Tri::State::handle_events() { currentTri_state + 1); pointCircle.fillColor = history[history_idx].color; break; + case Action::AT_COLOR: + tris.at(history[history_idx].idx).fillColor = history[history_idx].data.newColor; + break; default: assert(!"Unreachable code"); break; @@ -359,6 +373,7 @@ void Tri::State::handle_events() { flags.set(F_TRI_EDIT_DRAW_TRI); selectedTriBlinkTimer = 1.0f; selectedTriColor = tris[i].fillColor; + prevTriColor = tris[i].fillColor; selectedTri = i; clickTimeout = CLICK_TIMEOUT_TIME; break; @@ -664,22 +679,19 @@ Color& Tri::State::get_selected_tri_color() { } void Tri::State::close_selected_tri_mode() { + // Set tri's new color in history. + if (prevTriColor != selectedTriColor) { + if (history_idx < history.size()) { + history.resize(history_idx); + } + history.emplace_back(Action::AT_COLOR, selectedTri, prevTriColor, nullptr); + history.back().setNewColor(selectedTriColor); + ++history_idx; + } + tris.at(selectedTri).fillColor = selectedTriColor; flags.set(F_DRAW_CACHE_DIRTY); - // Set tri's new color in history. - for (auto &action : history) { - if (action.type == Action::AT_TRI - && tris.at(selectedTri).vertices.at(0).x == action.data.tri[0] - && tris.at(selectedTri).vertices.at(0).y == action.data.tri[1] - && tris.at(selectedTri).vertices.at(1).x == action.data.tri[2] - && tris.at(selectedTri).vertices.at(1).y == action.data.tri[3] - && tris.at(selectedTri).vertices.at(2).x == action.data.tri[4] - && tris.at(selectedTri).vertices.at(2).y == action.data.tri[5]) { - action.color = selectedTriColor; - break; - } - } reset_modes(); } diff --git a/src/state.hpp b/src/state.hpp index fa77c94..7bafa2c 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -49,6 +49,7 @@ namespace Tri { AT_TRI, AT_TRI_DEL, AT_POINT, + AT_COLOR, AT_NONE, }; @@ -68,8 +69,11 @@ namespace Tri { union Data { float tri[6]; float point[2]; + Color newColor; } data; + Action &setNewColor(Color color); + private: void init(float *data); }; @@ -102,6 +106,7 @@ namespace Tri { unsigned int selectedTri; Color selectedTriColor; + Color prevTriColor; float selectedTriBlinkTimer; int inputWidth;