Impl. storing color changes in history
All checks were successful
Run UnitTests / build-and-run-tests (push) Successful in 28s

This commit is contained in:
Stephen Seo 2024-02-29 17:10:57 +09:00
parent b5efecd43d
commit 25dafb26b5
5 changed files with 47 additions and 13 deletions

View file

@ -26,6 +26,7 @@ set(Triangles_LIB_SOURCES
src/triangle.cpp src/triangle.cpp
src/circle.cpp src/circle.cpp
src/raygui.cpp src/raygui.cpp
src/helpers.cpp
) )
add_library(TrianglesLib STATIC ${Triangles_LIB_SOURCES}) add_library(TrianglesLib STATIC ${Triangles_LIB_SOURCES})

12
src/helpers.cpp Normal file
View file

@ -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);
}

View file

@ -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 #endif

View file

@ -33,6 +33,7 @@ drawCache(),
pi(std::acos(-1.0f)), pi(std::acos(-1.0f)),
selectedTri(), selectedTri(),
selectedTriColor{255, 255, 255, 255}, selectedTriColor{255, 255, 255, 255},
prevTriColor{255, 255, 255, 255},
selectedTriBlinkTimer(), selectedTriBlinkTimer(),
inputWidth(800), inputWidth(800),
inputHeight(600), inputHeight(600),
@ -84,6 +85,11 @@ data()
init(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) { void Tri::State::Action::init(float *data) {
switch(type) { switch(type) {
case AT_TRI: case AT_TRI:
@ -96,6 +102,8 @@ void Tri::State::Action::init(float *data) {
this->data.point[0] = data[0]; this->data.point[0] = data[0];
this->data.point[1] = data[1]; this->data.point[1] = data[1];
break; break;
case AT_COLOR:
break;
default: default:
type = AT_NONE; type = AT_NONE;
idx = 0; idx = 0;
@ -161,6 +169,9 @@ void Tri::State::handle_events() {
&& "There must be a point to undo a point"); && "There must be a point to undo a point");
currentTri_state = static_cast<CurrentState>(currentTri_state - 1); currentTri_state = static_cast<CurrentState>(currentTri_state - 1);
break; break;
case Action::AT_COLOR:
tris.at(history[history_idx-1].idx).fillColor = history[history_idx-1].color;
break;
default: default:
assert(!"Unreachable code"); assert(!"Unreachable code");
break; break;
@ -208,6 +219,9 @@ void Tri::State::handle_events() {
currentTri_state + 1); currentTri_state + 1);
pointCircle.fillColor = history[history_idx].color; pointCircle.fillColor = history[history_idx].color;
break; break;
case Action::AT_COLOR:
tris.at(history[history_idx].idx).fillColor = history[history_idx].data.newColor;
break;
default: default:
assert(!"Unreachable code"); assert(!"Unreachable code");
break; break;
@ -359,6 +373,7 @@ void Tri::State::handle_events() {
flags.set(F_TRI_EDIT_DRAW_TRI); flags.set(F_TRI_EDIT_DRAW_TRI);
selectedTriBlinkTimer = 1.0f; selectedTriBlinkTimer = 1.0f;
selectedTriColor = tris[i].fillColor; selectedTriColor = tris[i].fillColor;
prevTriColor = tris[i].fillColor;
selectedTri = i; selectedTri = i;
clickTimeout = CLICK_TIMEOUT_TIME; clickTimeout = CLICK_TIMEOUT_TIME;
break; break;
@ -664,22 +679,19 @@ Color& Tri::State::get_selected_tri_color() {
} }
void Tri::State::close_selected_tri_mode() { 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; tris.at(selectedTri).fillColor = selectedTriColor;
flags.set(F_DRAW_CACHE_DIRTY); 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(); reset_modes();
} }

View file

@ -49,6 +49,7 @@ namespace Tri {
AT_TRI, AT_TRI,
AT_TRI_DEL, AT_TRI_DEL,
AT_POINT, AT_POINT,
AT_COLOR,
AT_NONE, AT_NONE,
}; };
@ -68,8 +69,11 @@ namespace Tri {
union Data { union Data {
float tri[6]; float tri[6];
float point[2]; float point[2];
Color newColor;
} data; } data;
Action &setNewColor(Color color);
private: private:
void init(float *data); void init(float *data);
}; };
@ -102,6 +106,7 @@ namespace Tri {
unsigned int selectedTri; unsigned int selectedTri;
Color selectedTriColor; Color selectedTriColor;
Color prevTriColor;
float selectedTriBlinkTimer; float selectedTriBlinkTimer;
int inputWidth; int inputWidth;