Impl "Action" object for undo/redo history
All checks were successful
Run UnitTests / build-and-run-tests (push) Successful in 50s
All checks were successful
Run UnitTests / build-and-run-tests (push) Successful in 50s
TODO: Store undo/redo history for color editing, allow deleting individual triangles and undo/redo of them.
This commit is contained in:
parent
1da2ddb125
commit
852a099930
2 changed files with 227 additions and 68 deletions
255
src/state.cpp
255
src/state.cpp
|
@ -22,11 +22,9 @@ dt(1.0f/60.0f),
|
||||||
notificationAlpha(1.0f),
|
notificationAlpha(1.0f),
|
||||||
notificationText(),
|
notificationText(),
|
||||||
tris(),
|
tris(),
|
||||||
trisIndex(0),
|
|
||||||
currentTri(),
|
currentTri(),
|
||||||
currentTri_state(CurrentState::NONE),
|
currentTri_state(CurrentState::NONE),
|
||||||
currentTri_maxState(CurrentState::NONE),
|
pointCircle({7.0F, 7.0F}, 7.0F, WHITE),
|
||||||
pointCircle(),
|
|
||||||
colorPickerColor{255, 255, 255, 255},
|
colorPickerColor{255, 255, 255, 255},
|
||||||
bgColorPickerColor{0, 0, 0, 255},
|
bgColorPickerColor{0, 0, 0, 255},
|
||||||
bgColor(BLACK),
|
bgColor(BLACK),
|
||||||
|
@ -38,7 +36,9 @@ selectedTri(),
|
||||||
selectedTriColor{255, 255, 255, 255},
|
selectedTriColor{255, 255, 255, 255},
|
||||||
selectedTriBlinkTimer(),
|
selectedTriBlinkTimer(),
|
||||||
inputWidth(800),
|
inputWidth(800),
|
||||||
inputHeight(600)
|
inputHeight(600),
|
||||||
|
history(),
|
||||||
|
history_idx(0)
|
||||||
{
|
{
|
||||||
InitWindow(width, height, "Triangles");
|
InitWindow(width, height, "Triangles");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
@ -47,11 +47,6 @@ inputHeight(600)
|
||||||
|
|
||||||
set_notification_text("Press \"H\" for help");
|
set_notification_text("Press \"H\" for help");
|
||||||
|
|
||||||
pointCircle.setRadius(7.0f);
|
|
||||||
pointCircle.translate({7.0f, 7.0f});
|
|
||||||
pointCircle.fillColor = WHITE;
|
|
||||||
pointCircle.outlineColor = BLACK;
|
|
||||||
|
|
||||||
saveFilenameBuffer.fill(0);
|
saveFilenameBuffer.fill(0);
|
||||||
|
|
||||||
drawCache = LoadRenderTexture(width, height);
|
drawCache = LoadRenderTexture(width, height);
|
||||||
|
@ -62,6 +57,52 @@ inputHeight(600)
|
||||||
}
|
}
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
|
|
||||||
|
Tri::State::Action::Action() :
|
||||||
|
type(Tri::State::Action::AT_NONE),
|
||||||
|
idx(0),
|
||||||
|
color(BLACK),
|
||||||
|
data{{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}}
|
||||||
|
{}
|
||||||
|
|
||||||
|
Tri::State::Action::Action(const Type& type, IndexT idx, const Color& color, float *data) :
|
||||||
|
type(type),
|
||||||
|
idx(idx),
|
||||||
|
color(color),
|
||||||
|
data()
|
||||||
|
{
|
||||||
|
init(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
Tri::State::Action::Action(Type&& type, IndexT idx, Color&& color, float *data) :
|
||||||
|
type(type),
|
||||||
|
idx(idx),
|
||||||
|
color(color),
|
||||||
|
data()
|
||||||
|
{
|
||||||
|
init(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Tri::State::Action::init(float *data) {
|
||||||
|
switch(type) {
|
||||||
|
case AT_TRI:
|
||||||
|
case AT_TRI_DEL:
|
||||||
|
for(unsigned int i = 0; i < 6; ++i) {
|
||||||
|
this->data.tri[i] = data[i];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case AT_POINT:
|
||||||
|
this->data.point[0] = data[0];
|
||||||
|
this->data.point[1] = data[1];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
type = AT_NONE;
|
||||||
|
idx = 0;
|
||||||
|
color = BLACK;
|
||||||
|
this->data = {{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Tri::State::~State() {
|
Tri::State::~State() {
|
||||||
UnloadRenderTexture(drawCache);
|
UnloadRenderTexture(drawCache);
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
@ -81,51 +122,91 @@ void Tri::State::handle_events() {
|
||||||
break;
|
break;
|
||||||
case KEY_U:
|
case KEY_U:
|
||||||
flags.set(F_DRAW_CACHE_DIRTY);
|
flags.set(F_DRAW_CACHE_DIRTY);
|
||||||
if(currentTri_state > 0) {
|
if (history_idx > 0) {
|
||||||
switch(currentTri_state) {
|
switch(history[history_idx-1].type) {
|
||||||
case FIRST:
|
case Action::AT_TRI:
|
||||||
|
tris.erase(
|
||||||
|
tris.cbegin() + history[history_idx-1].idx);
|
||||||
|
restore_points_on_tri_del(history_idx - 1);
|
||||||
|
break;
|
||||||
|
case Action::AT_TRI_DEL:
|
||||||
|
{
|
||||||
|
tris.emplace(
|
||||||
|
tris.cbegin() + history[history_idx].idx,
|
||||||
|
Triangle(
|
||||||
|
{{
|
||||||
|
{history[history_idx-1].data.tri[0],
|
||||||
|
history[history_idx-1].data.tri[1]},
|
||||||
|
{history[history_idx-1].data.tri[2],
|
||||||
|
history[history_idx-1].data.tri[3]},
|
||||||
|
{history[history_idx-1].data.tri[4],
|
||||||
|
history[history_idx-1].data.tri[5]},
|
||||||
|
}},
|
||||||
|
history[history_idx-1].color
|
||||||
|
)
|
||||||
|
);
|
||||||
currentTri_state = CurrentState::NONE;
|
currentTri_state = CurrentState::NONE;
|
||||||
break;
|
break;
|
||||||
case SECOND:
|
}
|
||||||
currentTri_state = CurrentState::FIRST;
|
case Action::AT_POINT:
|
||||||
|
assert(history[history_idx-1].idx + 1 == currentTri_state
|
||||||
|
&& "Point in history must match point index");
|
||||||
|
assert(currentTri_state > 0
|
||||||
|
&& "There must be a point to undo a point");
|
||||||
|
currentTri_state = static_cast<CurrentState>(currentTri_state - 1);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(!"Unreachable code");
|
assert(!"Unreachable code");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if(trisIndex > 0) {
|
--history_idx;
|
||||||
--trisIndex;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case KEY_R:
|
case KEY_R:
|
||||||
flags.set(F_DRAW_CACHE_DIRTY);
|
flags.set(F_DRAW_CACHE_DIRTY);
|
||||||
if(currentTri_state != CurrentState::NONE
|
if(history_idx < history.size()) {
|
||||||
&& currentTri_state < currentTri_maxState) {
|
switch(history[history_idx].type) {
|
||||||
switch(currentTri_state) {
|
case Action::AT_TRI:
|
||||||
case NONE:
|
{
|
||||||
currentTri_state = CurrentState::FIRST;
|
tris.emplace(
|
||||||
|
tris.cbegin() + history[history_idx].idx,
|
||||||
|
Triangle(
|
||||||
|
{{
|
||||||
|
{history[history_idx].data.tri[0],
|
||||||
|
history[history_idx].data.tri[1]},
|
||||||
|
{history[history_idx].data.tri[2],
|
||||||
|
history[history_idx].data.tri[3]},
|
||||||
|
{history[history_idx].data.tri[4],
|
||||||
|
history[history_idx].data.tri[5]},
|
||||||
|
}},
|
||||||
|
history[history_idx].color
|
||||||
|
)
|
||||||
|
);
|
||||||
|
currentTri_state = CurrentState::NONE;
|
||||||
break;
|
break;
|
||||||
case FIRST:
|
}
|
||||||
currentTri_state = CurrentState::SECOND;
|
break;
|
||||||
break;
|
case Action::AT_TRI_DEL:
|
||||||
default:
|
tris.erase(
|
||||||
assert(!"Unreachable code");
|
tris.cbegin() + history[history_idx].idx);
|
||||||
break;
|
restore_points_on_tri_del(history_idx);
|
||||||
}
|
break;
|
||||||
} else if(tris.size() > trisIndex) {
|
case Action::AT_POINT:
|
||||||
++trisIndex;
|
assert(history[history_idx].idx == currentTri_state
|
||||||
} else if(currentTri_state < currentTri_maxState) {
|
&& "Point in history must match point index");
|
||||||
switch(currentTri_state) {
|
assert(currentTri_state < CurrentState::SECOND
|
||||||
case NONE:
|
&& "Current point state must be 0 or 1");
|
||||||
currentTri_state = CurrentState::FIRST;
|
currentTri[currentTri_state].x = history[history_idx].data.point[0];
|
||||||
break;
|
currentTri[currentTri_state].y = history[history_idx].data.point[1];
|
||||||
case FIRST:
|
currentTri_state = static_cast<CurrentState>(
|
||||||
currentTri_state = CurrentState::SECOND;
|
currentTri_state + 1);
|
||||||
|
pointCircle.fillColor = history[history_idx].color;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(!"Unreachable code");
|
assert(!"Unreachable code");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
++history_idx;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case KEY_C:
|
case KEY_C:
|
||||||
|
@ -187,34 +268,53 @@ void Tri::State::handle_events() {
|
||||||
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
||||||
if(can_draw()) {
|
if(can_draw()) {
|
||||||
switch(currentTri_state) {
|
switch(currentTri_state) {
|
||||||
case CurrentState::NONE:
|
case CurrentState::NONE: {
|
||||||
currentTri[0] = {GetMouseX(), GetMouseY()};
|
currentTri[0] = {GetMouseX(), GetMouseY()};
|
||||||
if(trisIndex < tris.size()) {
|
|
||||||
tris.resize(trisIndex);
|
|
||||||
}
|
|
||||||
currentTri_state = CurrentState::FIRST;
|
currentTri_state = CurrentState::FIRST;
|
||||||
currentTri_maxState = CurrentState::FIRST;
|
if(history_idx < history.size()) {
|
||||||
break;
|
history.resize(history_idx);
|
||||||
case CurrentState::FIRST:
|
}
|
||||||
|
float points[2] = {currentTri[0].x, currentTri[0].y};
|
||||||
|
history.push_back(Action(Action::AT_POINT,
|
||||||
|
0,
|
||||||
|
pointCircle.fillColor,
|
||||||
|
points));
|
||||||
|
++history_idx;
|
||||||
|
break; }
|
||||||
|
case CurrentState::FIRST: {
|
||||||
currentTri[1] = {GetMouseX(), GetMouseY()};
|
currentTri[1] = {GetMouseX(), GetMouseY()};
|
||||||
if(trisIndex < tris.size()) {
|
|
||||||
tris.resize(trisIndex);
|
|
||||||
}
|
|
||||||
currentTri_state = CurrentState::SECOND;
|
currentTri_state = CurrentState::SECOND;
|
||||||
currentTri_maxState = CurrentState::SECOND;
|
if(history_idx < history.size()) {
|
||||||
break;
|
history.resize(history_idx);
|
||||||
case CurrentState::SECOND:
|
|
||||||
currentTri[2] = {GetMouseX(), GetMouseY()};
|
|
||||||
if(trisIndex < tris.size()) {
|
|
||||||
tris.resize(trisIndex);
|
|
||||||
}
|
}
|
||||||
++trisIndex;
|
float points[2] = {currentTri[1].x, currentTri[1].y};
|
||||||
|
history.push_back(Action(Action::AT_POINT,
|
||||||
|
1,
|
||||||
|
pointCircle.fillColor,
|
||||||
|
points));
|
||||||
|
++history_idx;
|
||||||
|
break; }
|
||||||
|
case CurrentState::SECOND: {
|
||||||
|
currentTri[2] = {GetMouseX(), GetMouseY()};
|
||||||
make_counter_clockwise(currentTri);
|
make_counter_clockwise(currentTri);
|
||||||
tris.emplace_back(currentTri, pointCircle.fillColor);
|
tris.emplace_back(currentTri, pointCircle.fillColor);
|
||||||
currentTri_state = CurrentState::NONE;
|
currentTri_state = CurrentState::NONE;
|
||||||
currentTri_maxState = CurrentState::NONE;
|
|
||||||
flags.set(F_DRAW_CACHE_DIRTY);
|
flags.set(F_DRAW_CACHE_DIRTY);
|
||||||
break;
|
|
||||||
|
if(history_idx < history.size()) {
|
||||||
|
history.resize(history_idx);
|
||||||
|
}
|
||||||
|
float points[6] = {
|
||||||
|
currentTri[0].x, currentTri[0].y,
|
||||||
|
currentTri[1].x, currentTri[1].y,
|
||||||
|
currentTri[2].x, currentTri[2].y,
|
||||||
|
};
|
||||||
|
history.push_back(Action(Action::AT_TRI,
|
||||||
|
tris.size()-1,
|
||||||
|
pointCircle.fillColor,
|
||||||
|
points));
|
||||||
|
++history_idx;
|
||||||
|
break; }
|
||||||
}
|
}
|
||||||
} else if(flags.test(F_COPY_COLOR_MODE)) {
|
} else if(flags.test(F_COPY_COLOR_MODE)) {
|
||||||
check_draw_cache();
|
check_draw_cache();
|
||||||
|
@ -245,15 +345,14 @@ void Tri::State::handle_events() {
|
||||||
if(my < 0) { my = 0; }
|
if(my < 0) { my = 0; }
|
||||||
else if(my >= (int)height) { my = height - 1; }
|
else if(my >= (int)height) { my = height - 1; }
|
||||||
|
|
||||||
for(unsigned int i = trisIndex; i-- > 0; ) {
|
for (auto &tri : tris) {
|
||||||
if(is_within_shape(tris.at(i), {mx, my})) {
|
if(is_within_shape(tri, {mx, my})) {
|
||||||
selectedTri = i;
|
tri.outlineColor = invert_color(tri.fillColor);
|
||||||
tris[i].outlineColor = invert_color(tris[i].fillColor);
|
|
||||||
flags.reset(F_SELECT_TRI_MODE);
|
flags.reset(F_SELECT_TRI_MODE);
|
||||||
flags.set(F_TRI_EDIT_MODE);
|
flags.set(F_TRI_EDIT_MODE);
|
||||||
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 = tri.fillColor;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -325,9 +424,7 @@ void Tri::State::draw() {
|
||||||
|
|
||||||
if(can_draw()) {
|
if(can_draw()) {
|
||||||
for(unsigned int i = 0; i < currentTri_state; ++i) {
|
for(unsigned int i = 0; i < currentTri_state; ++i) {
|
||||||
pointCircle.resetTransform();
|
DrawCircle(currentTri[i].x, currentTri[i].y, pointCircle.getRadius(), pointCircle.fillColor);
|
||||||
pointCircle.translate(currentTri[i]);
|
|
||||||
pointCircle.draw();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,7 +445,7 @@ void Tri::State::draw_to_target(RenderTexture2D *target) {
|
||||||
ClearBackground(bgColor);
|
ClearBackground(bgColor);
|
||||||
|
|
||||||
// draw tris
|
// draw tris
|
||||||
for(unsigned int i = 0; i < trisIndex; ++i) {
|
for(unsigned int i = 0; i < tris.size(); ++i) {
|
||||||
tris[i].draw();
|
tris[i].draw();
|
||||||
}
|
}
|
||||||
EndTextureMode();
|
EndTextureMode();
|
||||||
|
@ -357,7 +454,7 @@ void Tri::State::draw_to_target(RenderTexture2D *target) {
|
||||||
ClearBackground(bgColor);
|
ClearBackground(bgColor);
|
||||||
|
|
||||||
// draw tris
|
// draw tris
|
||||||
for(unsigned int i = 0; i < trisIndex; ++i) {
|
for(unsigned int i = 0; i < tris.size(); ++i) {
|
||||||
tris[i].draw();
|
tris[i].draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -563,3 +660,29 @@ int* Tri::State::get_input_width() {
|
||||||
int* Tri::State::get_input_height() {
|
int* Tri::State::get_input_height() {
|
||||||
return &inputHeight;
|
return &inputHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Tri::State::restore_points_on_tri_del(Action::IndexT end) {
|
||||||
|
assert(end < history.size()
|
||||||
|
&& "Index on history must be in range");
|
||||||
|
currentTri[2].x = history[end].data.tri[4];
|
||||||
|
currentTri[2].y = history[end].data.tri[5];
|
||||||
|
pointCircle.fillColor = history[end].color;
|
||||||
|
unsigned int currentTriIdx = 1;
|
||||||
|
while(end-- > 0) {
|
||||||
|
if(history[end].type == Action::AT_POINT) {
|
||||||
|
assert(history[end].idx == currentTriIdx
|
||||||
|
&& "Last point must be second point");
|
||||||
|
currentTri[currentTriIdx].x = history[end].data.point[0];
|
||||||
|
currentTri[currentTriIdx].y = history[end].data.point[1];
|
||||||
|
if(currentTriIdx > 0) {
|
||||||
|
--currentTriIdx;
|
||||||
|
} else {
|
||||||
|
currentTri_state = CurrentState::SECOND;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(!"Unreachable code");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
|
@ -41,6 +41,39 @@ namespace Tri {
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct Action {
|
||||||
|
public:
|
||||||
|
typedef std::vector<Action>::size_type IndexT;
|
||||||
|
|
||||||
|
enum Type {
|
||||||
|
AT_TRI,
|
||||||
|
AT_TRI_DEL,
|
||||||
|
AT_POINT,
|
||||||
|
AT_NONE,
|
||||||
|
};
|
||||||
|
|
||||||
|
Action();
|
||||||
|
Action(const Type& type,
|
||||||
|
IndexT idx,
|
||||||
|
const Color& color,
|
||||||
|
float *data);
|
||||||
|
Action(Type&& type,
|
||||||
|
IndexT idx,
|
||||||
|
Color&& color,
|
||||||
|
float *data);
|
||||||
|
|
||||||
|
Type type;
|
||||||
|
IndexT idx;
|
||||||
|
Color color;
|
||||||
|
union Data {
|
||||||
|
float tri[6];
|
||||||
|
float point[2];
|
||||||
|
} data;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void init(float *data);
|
||||||
|
};
|
||||||
|
|
||||||
// use enum FlagName
|
// use enum FlagName
|
||||||
typedef std::bitset<64> BitsetType;
|
typedef std::bitset<64> BitsetType;
|
||||||
BitsetType flags;
|
BitsetType flags;
|
||||||
|
@ -51,10 +84,8 @@ namespace Tri {
|
||||||
std::array<char, 256> notificationText;
|
std::array<char, 256> notificationText;
|
||||||
|
|
||||||
std::vector<Triangle> tris;
|
std::vector<Triangle> tris;
|
||||||
unsigned int trisIndex;
|
|
||||||
std::array<glm::vec2, 3> currentTri;
|
std::array<glm::vec2, 3> currentTri;
|
||||||
CurrentState currentTri_state;
|
CurrentState currentTri_state;
|
||||||
CurrentState currentTri_maxState;
|
|
||||||
Circle pointCircle;
|
Circle pointCircle;
|
||||||
|
|
||||||
Color colorPickerColor;
|
Color colorPickerColor;
|
||||||
|
@ -75,6 +106,9 @@ namespace Tri {
|
||||||
int inputWidth;
|
int inputWidth;
|
||||||
int inputHeight;
|
int inputHeight;
|
||||||
|
|
||||||
|
std::vector<Action> history;
|
||||||
|
Action::IndexT history_idx;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void handle_events();
|
void handle_events();
|
||||||
void update();
|
void update();
|
||||||
|
@ -129,6 +163,8 @@ namespace Tri {
|
||||||
int* get_input_width();
|
int* get_input_width();
|
||||||
int* get_input_height();
|
int* get_input_height();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void restore_points_on_tri_del(Action::IndexT end);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue