Add starting help info, impl undo tri in progress

This commit is contained in:
Stephen Seo 2020-07-22 16:12:21 +09:00
parent 20418c9a5a
commit 7c71ce4d1b
3 changed files with 117 additions and 11 deletions

View file

@ -5,6 +5,13 @@
#include "state.hpp" #include "state.hpp"
#define SHOW_HELP_WIDTH (state->get_width() / 2.0f)
#define SHOW_HELP_HEIGHT (state->get_height() / 2.0f)
#ifndef NDEBUG
# include <cstdio>
#endif
namespace Tri { namespace Tri {
// Seems misleading, but imgui handles setting up the window during update // Seems misleading, but imgui handles setting up the window during update
// so this should be called during update, not draw // so this should be called during update, not draw
@ -14,11 +21,35 @@ namespace Tri {
ImGui::SetNextWindowSize(sf::Vector2f( ImGui::SetNextWindowSize(sf::Vector2f(
state->get_width() - 20.0f, state->get_width() - 20.0f,
state->get_height() - 20.0f)); state->get_height() - 20.0f));
ImGui::SetNextWindowBgAlpha(0.7f);
ImGui::Begin("Help Window", nullptr, ImGuiWindowFlags_NoDecoration); ImGui::Begin("Help Window", nullptr, ImGuiWindowFlags_NoDecoration);
ImGui::Text("This is the help window."); ImGui::Text("This is the help window. Press \"H\" to toggle this window.");
ImGui::Text("Click anywhere to create triangles, one point at a time");
ImGui::Text("Press \"U\" to undo. Clicking will remove all future undo history");
ImGui::Text("Press \"R\" to undo.");
ImGui::End(); ImGui::End();
} }
} }
inline void draw_show_help(Tri::State *state) {
float alpha = state->get_starting_help_alpha();
if(alpha > 0.0f) {
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, alpha);
ImGui::SetNextWindowPos(sf::Vector2f(
(state->get_width() - SHOW_HELP_WIDTH) / 2.0f,
(state->get_height() - SHOW_HELP_HEIGHT) / 2.0f));
ImGui::SetNextWindowSize(sf::Vector2f(
SHOW_HELP_WIDTH,
SHOW_HELP_HEIGHT));
ImGui::Begin("Use Help Window", nullptr, ImGuiWindowFlags_NoDecoration);
ImGui::SetWindowFontScale(3.0f);
ImGui::Text("Press \"H\" for help");
ImGui::End();
ImGui::PopStyleVar();
}
}
} }
#endif #endif

View file

@ -1,18 +1,27 @@
#include "state.hpp" #include "state.hpp"
#include <cstring> #include <cstring>
#include <cassert>
#include <imgui-SFML.h> #include <imgui-SFML.h>
#include "imgui_helper.hpp" #include "imgui_helper.hpp"
#define STARTING_HELP_FADE_RATE 0.2f
#ifndef NDEBUG
# include <cstdio>
#endif
Tri::State::State(int argc, char **argv) : Tri::State::State(int argc, char **argv) :
width(800), width(800),
height(600), height(600),
dt(sf::microseconds(16666)), dt(sf::microseconds(16666)),
starting_help_alpha(1.0f),
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), trisIndex(0),
currentTri_state(CurrentState::NONE) currentTri_state(CurrentState::NONE),
currentTri_maxState(CurrentState::NONE)
{ {
flags.set(1); // is running flags.set(1); // is running
ImGui::SFML::Init(window); ImGui::SFML::Init(window);
@ -40,23 +49,68 @@ void Tri::State::handle_events() {
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) { } else if(event.key.code == sf::Keyboard::U) {
if(trisIndex > 0) { if(currentTri_state > 0) {
switch(currentTri_state) {
case FIRST:
currentTri_state = CurrentState::NONE;
break;
case SECOND:
currentTri_state = CurrentState::FIRST;
break;
default:
assert(!"Unreachable code");
break;
}
} else if(trisIndex > 0) {
--trisIndex; --trisIndex;
} }
} else if(event.key.code == sf::Keyboard::R) { } else if(event.key.code == sf::Keyboard::R) {
if(tris.size() > trisIndex) { if(currentTri_state != CurrentState::NONE
&& currentTri_state < currentTri_maxState) {
switch(currentTri_state) {
case NONE:
currentTri_state = CurrentState::FIRST;
break;
case FIRST:
currentTri_state = CurrentState::SECOND;
break;
default:
assert(!"Unreachable code");
break;
}
} else if(tris.size() > trisIndex) {
++trisIndex; ++trisIndex;
} else if(currentTri_state < currentTri_maxState) {
switch(currentTri_state) {
case NONE:
currentTri_state = CurrentState::FIRST;
break;
case FIRST:
currentTri_state = CurrentState::SECOND;
break;
default:
assert(!"Unreachable code");
break;
}
} }
} }
} else if(event.type == sf::Event::MouseButtonPressed) { } else if(event.type == sf::Event::MouseButtonPressed) {
switch(currentTri_state) { switch(currentTri_state) {
case CurrentState::NONE: case CurrentState::NONE:
currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y);
if(trisIndex < tris.size()) {
tris.resize(trisIndex);
}
currentTri_state = CurrentState::FIRST; currentTri_state = CurrentState::FIRST;
currentTri_maxState = CurrentState::FIRST;
break; break;
case CurrentState::FIRST: case CurrentState::FIRST:
currentTri[1] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); currentTri[1] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y);
if(trisIndex < tris.size()) {
tris.resize(trisIndex);
}
currentTri_state = CurrentState::SECOND; currentTri_state = CurrentState::SECOND;
currentTri_maxState = CurrentState::SECOND;
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);
@ -70,6 +124,7 @@ void Tri::State::handle_events() {
tris.back().setPoint(2, currentTri[2]); tris.back().setPoint(2, currentTri[2]);
tris.back().setFillColor(sf::Color::White); // TODO use chosen color tris.back().setFillColor(sf::Color::White); // TODO use chosen color
currentTri_state = CurrentState::NONE; currentTri_state = CurrentState::NONE;
currentTri_maxState = CurrentState::NONE;
break; break;
} }
} }
@ -79,7 +134,15 @@ void Tri::State::handle_events() {
void Tri::State::update() { void Tri::State::update() {
ImGui::SFML::Update(window, dt); ImGui::SFML::Update(window, dt);
if(starting_help_alpha > 0.0f) {
starting_help_alpha -= dt.asSeconds() * STARTING_HELP_FADE_RATE;
if(starting_help_alpha < 0.0f) {
starting_help_alpha = 0.0f;
}
}
// Seems misleading, but imgui handles setting up the window during update // Seems misleading, but imgui handles setting up the window during update
Tri::draw_show_help(this);
Tri::draw_help(this); Tri::draw_help(this);
ImGui::EndFrame(); ImGui::EndFrame();
@ -87,7 +150,6 @@ void Tri::State::update() {
void Tri::State::draw() { void Tri::State::draw() {
window.clear(); window.clear();
ImGui::SFML::Render(window);
// draw tris // draw tris
for(unsigned int i = 0; i < trisIndex; ++i) { for(unsigned int i = 0; i < trisIndex; ++i) {
@ -100,17 +162,24 @@ void Tri::State::draw() {
window.draw(pointCircle); window.draw(pointCircle);
} }
// draw gui stuff
ImGui::SFML::Render(window);
window.display(); window.display();
} }
unsigned int Tri::State::get_width() { unsigned int Tri::State::get_width() const {
return width; return width;
} }
unsigned int Tri::State::get_height() { unsigned int Tri::State::get_height() const {
return height; return height;
} }
const Tri::State::BitsetType Tri::State::get_flags() const { const Tri::State::BitsetType Tri::State::get_flags() const {
return flags; return flags;
} }
float Tri::State::get_starting_help_alpha() const {
return starting_help_alpha;
}

View file

@ -12,22 +12,26 @@ namespace Tri {
public: public:
State(int argc, char **argv); State(int argc, char **argv);
~State(); ~State();
enum CurrentState {NONE = 0, FIRST = 1, SECOND = 2};
private:
/* /*
* 0 - display help * 0 - display help
* 1 - is running * 1 - is running
*/ */
private:
typedef std::bitset<64> BitsetType; typedef std::bitset<64> BitsetType;
BitsetType flags; BitsetType flags;
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
const sf::Time dt; const sf::Time dt;
float starting_help_alpha;
sf::RenderWindow window; sf::RenderWindow window;
std::vector<sf::ConvexShape> tris; std::vector<sf::ConvexShape> tris;
unsigned int trisIndex; unsigned int trisIndex;
sf::Vector2f currentTri[3]; sf::Vector2f currentTri[3];
enum CurrentState { NONE = 0, FIRST = 1, SECOND = 2} currentTri_state; CurrentState currentTri_state;
CurrentState currentTri_maxState;
sf::CircleShape pointCircle; sf::CircleShape pointCircle;
sf::Event event; sf::Event event;
@ -37,10 +41,12 @@ namespace Tri {
void update(); void update();
void draw(); void draw();
unsigned int get_width(); unsigned int get_width() const;
unsigned int get_height(); unsigned int get_height() const;
const BitsetType get_flags() const; const BitsetType get_flags() const;
float get_starting_help_alpha() const;
}; };
} }