From 2aea0635f46d4356953b1885bf6f8510cb5b038f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 31 Mar 2022 13:38:02 +0200 Subject: wip --- src/gui/Button.cpp | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'src/gui') diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index 2250560..d7df26a 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -10,7 +10,6 @@ namespace gsr { } void Button::on_event(mgl::Event &event, mgl::Window&) { - /* if(event.type == mgl::Event::MouseMoved) { const bool inside = mgl::FloatRect(position, size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); if(mouse_inside && !inside) { @@ -18,34 +17,25 @@ namespace gsr { } else if(!mouse_inside && inside) { mouse_inside = true; } - } else if(event.type == mgl::Event::MouseButtonPressed && mouse_inside) { - - } - */ - if(event.type == mgl::Event::MouseButtonPressed && mouse_inside) { - if(on_click) + } else if(event.type == mgl::Event::MouseButtonPressed) { + pressed_inside = mouse_inside; + } else if(event.type == mgl::Event::MouseButtonReleased) { + const bool clicked_inside = pressed_inside && mouse_inside; + pressed_inside = false; + if(clicked_inside && on_click) on_click(); } } void Button::draw(mgl::Window &window) { - const bool inside = mgl::FloatRect(position, size).contains(window.get_mouse_position().to_vec2f()); - if(mouse_inside && !inside) { - mouse_inside = false; - } else if(!mouse_inside && inside) { - mouse_inside = true; - } - if(mouse_inside) { // Background - /* { mgl::Rectangle rect(size); rect.set_position(position); rect.set_color(mgl::Color(20, 20, 20, 255)); window.draw(rect); } - */ const int border_size = 5; const mgl::Color border_color(118, 185, 0); @@ -68,16 +58,16 @@ namespace gsr { // Green line at left { - mgl::Rectangle rect({ border_size, size.y }); - rect.set_position(position); + mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); + rect.set_position(position + mgl::vec2f(0, border_size)); rect.set_color(border_color); window.draw(rect); } // Green line at right { - mgl::Rectangle rect({ border_size, size.y }); - rect.set_position(position + mgl::vec2f(size.x - border_size, 0.0f)); + mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); + rect.set_position(position + mgl::vec2f(size.x - border_size, border_size)); rect.set_color(border_color); window.draw(rect); } -- cgit v1.2.3