From 187c52bf5fa04af3bb71de650c03dc8362943d42 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 22 Jul 2020 16:26:57 +0900 Subject: [PATCH] Impl choosing colors by color picker menu "C" --- src/imgui_helper.hpp | 11 +++++- src/state.cpp | 82 +++++++++++++++++++++++++++----------------- src/state.hpp | 6 ++++ 3 files changed, 67 insertions(+), 32 deletions(-) diff --git a/src/imgui_helper.hpp b/src/imgui_helper.hpp index 9e4186f..a2eb0af 100644 --- a/src/imgui_helper.hpp +++ b/src/imgui_helper.hpp @@ -23,10 +23,11 @@ namespace Tri { state->get_height() - 20.0f)); ImGui::SetNextWindowBgAlpha(0.7f); ImGui::Begin("Help Window", nullptr, ImGuiWindowFlags_NoDecoration); - ImGui::Text("This is the help window. Press \"H\" to toggle this 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::Text("Press \"C\" to change colors"); ImGui::End(); } } @@ -50,6 +51,14 @@ namespace Tri { ImGui::PopStyleVar(); } } + + inline void draw_color_picker(Tri::State *state) { + if(state->get_flags().test(2)) { + ImGui::Begin("Color Picker"); + ImGui::ColorPicker4("Tri Color", state->get_color()); + ImGui::End(); + } + } } #endif diff --git a/src/state.cpp b/src/state.cpp index fae405e..e709f19 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -21,7 +21,8 @@ starting_help_alpha(1.0f), window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close), trisIndex(0), currentTri_state(CurrentState::NONE), -currentTri_maxState(CurrentState::NONE) +currentTri_maxState(CurrentState::NONE), +colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f} { flags.set(1); // is running ImGui::SFML::Init(window); @@ -93,39 +94,43 @@ void Tri::State::handle_events() { break; } } + } else if(event.key.code == sf::Keyboard::C) { + flags.flip(2); } } else if(event.type == sf::Event::MouseButtonPressed) { - switch(currentTri_state) { - case CurrentState::NONE: - currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); - if(trisIndex < tris.size()) { - tris.resize(trisIndex); + if(!flags.test(2)) { + switch(currentTri_state) { + case CurrentState::NONE: + currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); + if(trisIndex < tris.size()) { + tris.resize(trisIndex); + } + currentTri_state = CurrentState::FIRST; + currentTri_maxState = CurrentState::FIRST; + break; + case CurrentState::FIRST: + currentTri[1] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); + if(trisIndex < tris.size()) { + tris.resize(trisIndex); + } + currentTri_state = CurrentState::SECOND; + currentTri_maxState = CurrentState::SECOND; + 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]); + tris.back().setPoint(2, currentTri[2]); + tris.back().setFillColor(pointCircle.getFillColor()); + currentTri_state = CurrentState::NONE; + currentTri_maxState = CurrentState::NONE; + break; } - currentTri_state = CurrentState::FIRST; - currentTri_maxState = CurrentState::FIRST; - break; - case CurrentState::FIRST: - currentTri[1] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); - if(trisIndex < tris.size()) { - tris.resize(trisIndex); - } - currentTri_state = CurrentState::SECOND; - currentTri_maxState = CurrentState::SECOND; - 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]); - tris.back().setPoint(2, currentTri[2]); - tris.back().setFillColor(sf::Color::White); // TODO use chosen color - currentTri_state = CurrentState::NONE; - currentTri_maxState = CurrentState::NONE; - break; } } } @@ -141,8 +146,18 @@ void Tri::State::update() { } } + if(flags.test(3)) { + flags.reset(3); + pointCircle.setFillColor(sf::Color( + (unsigned char)(255 * colorPickerColor[0]), + (unsigned char)(255 * colorPickerColor[1]), + (unsigned char)(255 * colorPickerColor[2]), + (unsigned char)(255 * colorPickerColor[3]))); + } + // Seems misleading, but imgui handles setting up the window during update Tri::draw_show_help(this); + Tri::draw_color_picker(this); Tri::draw_help(this); ImGui::EndFrame(); @@ -183,3 +198,8 @@ const Tri::State::BitsetType Tri::State::get_flags() const { float Tri::State::get_starting_help_alpha() const { return starting_help_alpha; } + +float* Tri::State::get_color() { + flags.set(3); + return colorPickerColor; +} diff --git a/src/state.hpp b/src/state.hpp index e64cb1a..fa3feca 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -18,6 +18,8 @@ namespace Tri { /* * 0 - display help * 1 - is running + * 2 - display color picker + * 3 - color picker color dirty */ typedef std::bitset<64> BitsetType; BitsetType flags; @@ -36,6 +38,8 @@ namespace Tri { sf::Event event; + float colorPickerColor[4]; + public: void handle_events(); void update(); @@ -47,6 +51,8 @@ namespace Tri { const BitsetType get_flags() const; float get_starting_help_alpha() const; + + float* get_color(); }; }