From 099b0e65d698b6bfda375f228e0d0d9eedd74044 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 22 Jul 2020 17:46:20 +0900 Subject: [PATCH] Impl changing background color, with "B" --- src/imgui_helper.hpp | 11 ++++++++++- src/state.cpp | 23 ++++++++++++++++++++--- src/state.hpp | 5 +++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/imgui_helper.hpp b/src/imgui_helper.hpp index dc80e51..eada1a0 100644 --- a/src/imgui_helper.hpp +++ b/src/imgui_helper.hpp @@ -28,6 +28,7 @@ namespace Tri { 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::Text("Press \"B\" to change background color"); ImGui::End(); } } @@ -54,11 +55,19 @@ namespace Tri { inline void draw_color_picker(Tri::State *state) { if(state->get_flags().test(2)) { - ImGui::Begin("Color Picker"); + ImGui::Begin("Tri Color Picker"); ImGui::ColorPicker4("Tri Color", state->get_color()); ImGui::End(); } } + + inline void draw_bg_color_picker(Tri::State *state) { + if(state->get_flags().test(5)) { + ImGui::Begin("BG Color Picker"); + ImGui::ColorPicker3("BG Color", state->get_bg_color()); + ImGui::End(); + } + } } #endif diff --git a/src/state.cpp b/src/state.cpp index e709f19..70e5c94 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -22,7 +22,9 @@ window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Cl trisIndex(0), currentTri_state(CurrentState::NONE), currentTri_maxState(CurrentState::NONE), -colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f} +colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f}, +bgColorPickerColor{0.0f, 0.0f, 0.0f}, +bgColor(sf::Color::Black) { flags.set(1); // is running ImGui::SFML::Init(window); @@ -96,9 +98,11 @@ void Tri::State::handle_events() { } } else if(event.key.code == sf::Keyboard::C) { flags.flip(2); + } else if(event.key.code == sf::Keyboard::B) { + flags.flip(5); } } else if(event.type == sf::Event::MouseButtonPressed) { - if(!flags.test(2)) { + if(!flags.test(2) && !flags.test(5)) { switch(currentTri_state) { case CurrentState::NONE: currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); @@ -155,16 +159,24 @@ void Tri::State::update() { (unsigned char)(255 * colorPickerColor[3]))); } + if(flags.test(4)) { + flags.reset(4); + bgColor.r = (unsigned char)(255 * bgColorPickerColor[0]); + bgColor.g = (unsigned char)(255 * bgColorPickerColor[1]); + bgColor.b = (unsigned char)(255 * bgColorPickerColor[2]); + } + // Seems misleading, but imgui handles setting up the window during update Tri::draw_show_help(this); Tri::draw_color_picker(this); + Tri::draw_bg_color_picker(this); Tri::draw_help(this); ImGui::EndFrame(); } void Tri::State::draw() { - window.clear(); + window.clear(bgColor); // draw tris for(unsigned int i = 0; i < trisIndex; ++i) { @@ -203,3 +215,8 @@ float* Tri::State::get_color() { flags.set(3); return colorPickerColor; } + +float* Tri::State::get_bg_color() { + flags.set(4); + return bgColorPickerColor; +} diff --git a/src/state.hpp b/src/state.hpp index fa3feca..aac3d77 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -20,6 +20,8 @@ namespace Tri { * 1 - is running * 2 - display color picker * 3 - color picker color dirty + * 4 - bg color picker color dirty + * 5 - display bg color picker */ typedef std::bitset<64> BitsetType; BitsetType flags; @@ -39,6 +41,8 @@ namespace Tri { sf::Event event; float colorPickerColor[4]; + float bgColorPickerColor[3]; + sf::Color bgColor; public: void handle_events(); @@ -53,6 +57,7 @@ namespace Tri { float get_starting_help_alpha() const; float* get_color(); + float* get_bg_color(); }; }