From 20418c9a5a7afc83f3f2097b79193348eb989ce5 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 22 Jul 2020 15:33:09 +0900 Subject: [PATCH] Impl undo/redo with U/R keys --- src/state.cpp | 17 +++++++++++++++-- src/state.hpp | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/state.cpp b/src/state.cpp index be6e256..5e84d8d 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -11,6 +11,7 @@ width(800), height(600), dt(sf::microseconds(16666)), window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close), +trisIndex(0), currentTri_state(CurrentState::NONE) { flags.set(1); // is running @@ -38,6 +39,14 @@ void Tri::State::handle_events() { } else if(event.type == sf::Event::KeyPressed) { if(event.key.code == sf::Keyboard::H) { flags.flip(0); + } else if(event.key.code == sf::Keyboard::U) { + if(trisIndex > 0) { + --trisIndex; + } + } else if(event.key.code == sf::Keyboard::R) { + if(tris.size() > trisIndex) { + ++trisIndex; + } } } else if(event.type == sf::Event::MouseButtonPressed) { switch(currentTri_state) { @@ -51,6 +60,10 @@ void Tri::State::handle_events() { break; case CurrentState::SECOND: currentTri[2] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); + if(trisIndex < tris.size()) { + tris.resize(trisIndex); + } + ++trisIndex; tris.emplace_back(sf::ConvexShape(3)); tris.back().setPoint(0, currentTri[0]); tris.back().setPoint(1, currentTri[1]); @@ -77,8 +90,8 @@ void Tri::State::draw() { ImGui::SFML::Render(window); // draw tris - for(auto& tri : tris) { - window.draw(tri); + for(unsigned int i = 0; i < trisIndex; ++i) { + window.draw(tris[i]); } // draw points diff --git a/src/state.hpp b/src/state.hpp index b0c43ae..c6a0147 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -25,6 +25,7 @@ namespace Tri { sf::RenderWindow window; std::vector tris; + unsigned int trisIndex; sf::Vector2f currentTri[3]; enum CurrentState { NONE = 0, FIRST = 1, SECOND = 2} currentTri_state; sf::CircleShape pointCircle;