Impl undo/redo with U/R keys
This commit is contained in:
parent
d7b8dd23a9
commit
20418c9a5a
2 changed files with 16 additions and 2 deletions
|
@ -11,6 +11,7 @@ width(800),
|
||||||
height(600),
|
height(600),
|
||||||
dt(sf::microseconds(16666)),
|
dt(sf::microseconds(16666)),
|
||||||
window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close),
|
window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close),
|
||||||
|
trisIndex(0),
|
||||||
currentTri_state(CurrentState::NONE)
|
currentTri_state(CurrentState::NONE)
|
||||||
{
|
{
|
||||||
flags.set(1); // is running
|
flags.set(1); // is running
|
||||||
|
@ -38,6 +39,14 @@ void Tri::State::handle_events() {
|
||||||
} else if(event.type == sf::Event::KeyPressed) {
|
} else if(event.type == sf::Event::KeyPressed) {
|
||||||
if(event.key.code == sf::Keyboard::H) {
|
if(event.key.code == sf::Keyboard::H) {
|
||||||
flags.flip(0);
|
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) {
|
} else if(event.type == sf::Event::MouseButtonPressed) {
|
||||||
switch(currentTri_state) {
|
switch(currentTri_state) {
|
||||||
|
@ -51,6 +60,10 @@ void Tri::State::handle_events() {
|
||||||
break;
|
break;
|
||||||
case CurrentState::SECOND:
|
case CurrentState::SECOND:
|
||||||
currentTri[2] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y);
|
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.emplace_back(sf::ConvexShape(3));
|
||||||
tris.back().setPoint(0, currentTri[0]);
|
tris.back().setPoint(0, currentTri[0]);
|
||||||
tris.back().setPoint(1, currentTri[1]);
|
tris.back().setPoint(1, currentTri[1]);
|
||||||
|
@ -77,8 +90,8 @@ void Tri::State::draw() {
|
||||||
ImGui::SFML::Render(window);
|
ImGui::SFML::Render(window);
|
||||||
|
|
||||||
// draw tris
|
// draw tris
|
||||||
for(auto& tri : tris) {
|
for(unsigned int i = 0; i < trisIndex; ++i) {
|
||||||
window.draw(tri);
|
window.draw(tris[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw points
|
// draw points
|
||||||
|
|
|
@ -25,6 +25,7 @@ namespace Tri {
|
||||||
|
|
||||||
sf::RenderWindow window;
|
sf::RenderWindow window;
|
||||||
std::vector<sf::ConvexShape> tris;
|
std::vector<sf::ConvexShape> tris;
|
||||||
|
unsigned int trisIndex;
|
||||||
sf::Vector2f currentTri[3];
|
sf::Vector2f currentTri[3];
|
||||||
enum CurrentState { NONE = 0, FIRST = 1, SECOND = 2} currentTri_state;
|
enum CurrentState { NONE = 0, FIRST = 1, SECOND = 2} currentTri_state;
|
||||||
sf::CircleShape pointCircle;
|
sf::CircleShape pointCircle;
|
||||||
|
|
Loading…
Reference in a new issue