Impl choosing colors by color picker menu "C"

This commit is contained in:
Stephen Seo 2020-07-22 16:26:57 +09:00
parent 7c71ce4d1b
commit 187c52bf5f
3 changed files with 67 additions and 32 deletions

View file

@ -23,10 +23,11 @@ namespace Tri {
state->get_height() - 20.0f)); state->get_height() - 20.0f));
ImGui::SetNextWindowBgAlpha(0.7f); 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. 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("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 \"U\" to undo. Clicking will remove all future undo history");
ImGui::Text("Press \"R\" to undo."); ImGui::Text("Press \"R\" to undo.");
ImGui::Text("Press \"C\" to change colors");
ImGui::End(); ImGui::End();
} }
} }
@ -50,6 +51,14 @@ namespace Tri {
ImGui::PopStyleVar(); 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 #endif

View file

@ -21,7 +21,8 @@ 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) currentTri_maxState(CurrentState::NONE),
colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f}
{ {
flags.set(1); // is running flags.set(1); // is running
ImGui::SFML::Init(window); ImGui::SFML::Init(window);
@ -93,39 +94,43 @@ void Tri::State::handle_events() {
break; break;
} }
} }
} else if(event.key.code == sf::Keyboard::C) {
flags.flip(2);
} }
} else if(event.type == sf::Event::MouseButtonPressed) { } else if(event.type == sf::Event::MouseButtonPressed) {
switch(currentTri_state) { if(!flags.test(2)) {
case CurrentState::NONE: switch(currentTri_state) {
currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); case CurrentState::NONE:
if(trisIndex < tris.size()) { currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y);
tris.resize(trisIndex); 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 // Seems misleading, but imgui handles setting up the window during update
Tri::draw_show_help(this); Tri::draw_show_help(this);
Tri::draw_color_picker(this);
Tri::draw_help(this); Tri::draw_help(this);
ImGui::EndFrame(); ImGui::EndFrame();
@ -183,3 +198,8 @@ const Tri::State::BitsetType Tri::State::get_flags() const {
float Tri::State::get_starting_help_alpha() const { float Tri::State::get_starting_help_alpha() const {
return starting_help_alpha; return starting_help_alpha;
} }
float* Tri::State::get_color() {
flags.set(3);
return colorPickerColor;
}

View file

@ -18,6 +18,8 @@ namespace Tri {
/* /*
* 0 - display help * 0 - display help
* 1 - is running * 1 - is running
* 2 - display color picker
* 3 - color picker color dirty
*/ */
typedef std::bitset<64> BitsetType; typedef std::bitset<64> BitsetType;
BitsetType flags; BitsetType flags;
@ -36,6 +38,8 @@ namespace Tri {
sf::Event event; sf::Event event;
float colorPickerColor[4];
public: public:
void handle_events(); void handle_events();
void update(); void update();
@ -47,6 +51,8 @@ namespace Tri {
const BitsetType get_flags() const; const BitsetType get_flags() const;
float get_starting_help_alpha() const; float get_starting_help_alpha() const;
float* get_color();
}; };
} }