aboutsummaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-08-01 22:19:59 +0200
committerdec05eba <dec05eba@protonmail.com>2024-08-01 22:22:45 +0200
commit10a5669330ba978c37d984a312ba1de7a2542503 (patch)
tree0ff8cc9660841ce3979ad87da954fb7e14de7d16 /src/gui
parent67216467d4dc43134a07f7f843a80a1adf688acd (diff)
Draw and event position relative to parent (for pages)
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/Button.cpp70
-rw-r--r--src/gui/ComboBox.cpp17
-rw-r--r--src/gui/DropdownButton.cpp29
-rw-r--r--src/gui/Page.cpp23
-rw-r--r--src/gui/ScrollablePage.cpp53
-rw-r--r--src/gui/StaticPage.cpp33
6 files changed, 127 insertions, 98 deletions
diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp
index 3cad31f..48ab085 100644
--- a/src/gui/Button.cpp
+++ b/src/gui/Button.cpp
@@ -1,4 +1,5 @@
#include "../../include/gui/Button.hpp"
+#include "../../include/gui/Utils.hpp"
#include "../../include/Theme.hpp"
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/window/Window.hpp>
@@ -6,80 +7,39 @@
#include <mglpp/system/FloatRect.hpp>
namespace gsr {
- Button::Button(mgl::vec2f size) : size(size) {
+ Button::Button(mgl::Font *font, const char *text, mgl::vec2f size, mgl::Color bg_color) : size(size), bg_color(bg_color), text(text, *font) {
}
- bool Button::on_event(mgl::Event &event, mgl::Window&) {
+ bool Button::on_event(mgl::Event &event, mgl::Window&, mgl::vec2f offset) {
if(event.type == mgl::Event::MouseMoved) {
- const mgl::vec2f collision_margin(1.0f, 1.0f); // Makes sure that multiple buttons that are next to each other wont activate at the same time when the cursor is right between them
- const bool inside = mgl::FloatRect(position + collision_margin, size - collision_margin).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y });
+ const bool inside = mgl::FloatRect(position + offset, size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y });
if(mouse_inside && !inside) {
mouse_inside = false;
} else if(!mouse_inside && inside) {
mouse_inside = true;
}
} 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;
+ const bool clicked_inside = mouse_inside;
if(clicked_inside && on_click)
on_click();
}
return true;
}
- void Button::draw(mgl::Window &window) {
- if(mouse_inside) {
- // Background
- {
- mgl::Rectangle rect(size);
- rect.set_position(position);
- rect.set_color(mgl::Color(0, 0, 0, 255));
- window.draw(rect);
- }
+ void Button::draw(mgl::Window &window, mgl::vec2f offset) {
+ mgl::Rectangle background(size);
+ background.set_position(position + offset);
+ background.set_color(bg_color);
+ window.draw(background);
+
+ text.set_position((position + offset + size * 0.5f - text.get_bounds().size * 0.5f).floor());
+ window.draw(text);
+ if(mouse_inside) {
const int border_size = 5;
const mgl::Color border_color = gsr::get_theme().tint_color;
-
- // Green line at top
- {
- mgl::Rectangle rect({ size.x, border_size });
- rect.set_position(position);
- rect.set_color(border_color);
- window.draw(rect);
- }
-
- // Green line at bottom
- {
- mgl::Rectangle rect({ size.x, border_size });
- rect.set_position(position + mgl::vec2f(0.0f, size.y - border_size));
- rect.set_color(border_color);
- window.draw(rect);
- }
-
- // Green line at left
- {
- 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 - border_size * 2 });
- rect.set_position(position + mgl::vec2f(size.x - border_size, border_size));
- rect.set_color(border_color);
- window.draw(rect);
- }
- } else {
- // Background
- mgl::Rectangle rect(size);
- rect.set_position(position);
- rect.set_color(mgl::Color(0, 0, 0, 220));
- window.draw(rect);
+ draw_rectangle_outline(window, position, size, border_color, border_size);
}
}
} \ No newline at end of file
diff --git a/src/gui/ComboBox.cpp b/src/gui/ComboBox.cpp
index dd683e1..6b8ec26 100644
--- a/src/gui/ComboBox.cpp
+++ b/src/gui/ComboBox.cpp
@@ -17,13 +17,14 @@ namespace gsr {
assert(font);
}
- bool ComboBox::on_event(mgl::Event &event, mgl::Window&) {
+ bool ComboBox::on_event(mgl::Event &event, mgl::Window&, mgl::vec2f offset) {
if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) {
+ const mgl::vec2f draw_pos = position + offset;
const mgl::vec2f mouse_pos = { (float)event.mouse_button.x, (float)event.mouse_button.y };
const mgl::vec2f item_size(max_size.x, font->get_character_size() + padding_top + padding_bottom);
if(show_dropdown && !items.empty()) {
- mgl::vec2f pos = position + mgl::vec2f(padding_left, padding_top);
+ mgl::vec2f pos = draw_pos + mgl::vec2f(padding_left, padding_top);
pos.y += items[selected_item].text.get_bounds().size.y + padding_top + padding_bottom;
for(size_t i = 0; i < items.size(); ++i) {
@@ -38,7 +39,7 @@ namespace gsr {
}
}
- if(mgl::FloatRect(position, item_size).contains(mouse_pos)) {
+ if(mgl::FloatRect(draw_pos, item_size).contains(mouse_pos)) {
show_dropdown = !show_dropdown;
move_to_top = true;
} else {
@@ -48,26 +49,28 @@ namespace gsr {
return true;
}
- void ComboBox::draw(mgl::Window &window) {
+ void ComboBox::draw(mgl::Window &window, mgl::vec2f offset) {
update_if_dirty();
if(items.empty())
return;
+ const mgl::vec2f draw_pos = position + offset;
+
const mgl::vec2f item_size(max_size.x, font->get_character_size() + padding_top + padding_bottom);
const mgl::vec2i mouse_pos = window.get_mouse_position();
bool inside = false;
- mgl::Rectangle background(position, mgl::vec2f(max_size.x, item_size.y));
+ mgl::Rectangle background(draw_pos, mgl::vec2f(max_size.x, item_size.y));
if(show_dropdown) {
background.set_size(max_size);
background.set_color(mgl::Color(0, 0, 0));
} else {
- background.set_color(mgl::Color(0, 0, 0, 220));
+ background.set_color(mgl::Color(0, 0, 0, 120));
}
window.draw(background);
- mgl::vec2f pos = position + mgl::vec2f(padding_left, padding_top);
+ mgl::vec2f pos = draw_pos + mgl::vec2f(padding_left, padding_top);
Item &item = items[selected_item];
item.text.set_position(pos.floor());
diff --git a/src/gui/DropdownButton.cpp b/src/gui/DropdownButton.cpp
index 9610e26..ecc284a 100644
--- a/src/gui/DropdownButton.cpp
+++ b/src/gui/DropdownButton.cpp
@@ -25,10 +25,11 @@ namespace gsr {
this->description.set_color(mgl::Color(150, 150, 150));
}
- bool DropdownButton::on_event(mgl::Event &event, mgl::Window&) {
+ bool DropdownButton::on_event(mgl::Event &event, mgl::Window&, mgl::vec2f offset) {
if(event.type == mgl::Event::MouseMoved) {
+ const mgl::vec2f draw_pos = position + offset;
const mgl::vec2f collision_margin(1.0f, 1.0f); // Makes sure that multiple buttons that are next to each other wont activate at the same time when the cursor is right between them
- const bool inside = mgl::FloatRect(position + collision_margin, size - collision_margin).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y });
+ const bool inside = mgl::FloatRect(draw_pos + collision_margin, size - collision_margin).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y });
if(mouse_inside && !inside) {
mouse_inside = false;
} else if(!mouse_inside && inside) {
@@ -43,14 +44,16 @@ namespace gsr {
return true;
}
- void DropdownButton::draw(mgl::Window &window) {
+ void DropdownButton::draw(mgl::Window &window, mgl::vec2f offset) {
update_if_dirty();
+ const mgl::vec2f draw_pos = position + offset;
+
if(show_dropdown) {
// Background
{
mgl::Rectangle rect(size);
- rect.set_position(position);
+ rect.set_position(draw_pos);
rect.set_color(mgl::Color(0, 0, 0, 255));
window.draw(rect);
}
@@ -60,7 +63,7 @@ namespace gsr {
// Green line at top
{
mgl::Rectangle rect({ size.x, border_size });
- rect.set_position(position);
+ rect.set_position(draw_pos);
rect.set_color(border_color);
window.draw(rect);
}
@@ -68,33 +71,33 @@ namespace gsr {
// Background
{
mgl::Rectangle rect(size);
- rect.set_position(position);
+ rect.set_position(draw_pos);
rect.set_color(mgl::Color(0, 0, 0, 255));
window.draw(rect);
}
const mgl::Color border_color = gsr::get_theme().tint_color;
- draw_rectangle_outline(window, position, size, border_color, border_size);
+ draw_rectangle_outline(window, draw_pos, size, border_color, border_size);
} else {
// Background
mgl::Rectangle rect(size);
- rect.set_position(position);
- rect.set_color(mgl::Color(0, 0, 0, 220));
+ rect.set_position(draw_pos);
+ rect.set_color(mgl::Color(0, 0, 0, 120));
window.draw(rect);
}
const int text_margin = size.y * 0.085;
const auto title_bounds = title.get_bounds();
- title.set_position((position + mgl::vec2f(size.x * 0.5f - title_bounds.size.x * 0.5f, text_margin)).floor());
+ title.set_position((draw_pos + mgl::vec2f(size.x * 0.5f - title_bounds.size.x * 0.5f, text_margin)).floor());
window.draw(title);
const auto description_bounds = description.get_bounds();
- description.set_position((position + mgl::vec2f(size.x * 0.5f - description_bounds.size.x * 0.5f, size.y - description_bounds.size.y - text_margin)).floor());
+ description.set_position((draw_pos + mgl::vec2f(size.x * 0.5f - description_bounds.size.x * 0.5f, size.y - description_bounds.size.y - text_margin)).floor());
window.draw(description);
if(icon_sprite.get_texture()->is_valid()) {
- icon_sprite.set_position((position + size * 0.5f - icon_sprite.get_size() * 0.5f).floor());
+ icon_sprite.set_position((draw_pos + size * 0.5f - icon_sprite.get_size() * 0.5f).floor());
window.draw(icon_sprite);
}
@@ -103,7 +106,7 @@ namespace gsr {
const mgl::vec2i mouse_pos = window.get_mouse_position();
mgl::Rectangle dropdown_bg(max_size);
- dropdown_bg.set_position(position + mgl::vec2f(0.0f, size.y));
+ dropdown_bg.set_position(draw_pos + mgl::vec2f(0.0f, size.y));
dropdown_bg.set_color(mgl::Color(0, 0, 0));
window.draw(dropdown_bg);
diff --git a/src/gui/Page.cpp b/src/gui/Page.cpp
index 813c2bd..19e3f2c 100644
--- a/src/gui/Page.cpp
+++ b/src/gui/Page.cpp
@@ -1,30 +1,7 @@
#include "../../include/gui/Page.hpp"
-#include "../../include/gui/Widget.hpp"
namespace gsr {
void Page::add_widget(std::unique_ptr<Widget> widget) {
widgets.push_back(std::move(widget));
}
-
- void Page::on_event(mgl::Event &event, mgl::Window &window) {
- // Process widgets by visibility (backwards)
- for(auto it = widgets.rbegin(), end = widgets.rend(); it != end; ++it) {
- if(!(*it)->on_event(event, window))
- return;
- }
- }
-
- void Page::draw(mgl::Window &window) {
- for(auto &widget : widgets) {
- if(widget->move_to_top) {
- widget->move_to_top = false;
- std::swap(widget, widgets.back());
- }
- widget->draw(window);
- }
-
- for(auto &widget : widgets) {
- widget->draw(window);
- }
- }
} \ No newline at end of file
diff --git a/src/gui/ScrollablePage.cpp b/src/gui/ScrollablePage.cpp
new file mode 100644
index 0000000..4a4f88b
--- /dev/null
+++ b/src/gui/ScrollablePage.cpp
@@ -0,0 +1,53 @@
+#include "../../include/gui/ScrollablePage.hpp"
+#include "../../include/Theme.hpp"
+
+#include <mglpp/graphics/Rectangle.hpp>
+#include <mglpp/window/Window.hpp>
+
+namespace gsr {
+ ScrollablePage::ScrollablePage(mgl::vec2f size) : size(size) {}
+
+ bool ScrollablePage::on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) {
+ const mgl::vec2f draw_pos = position + offset;
+ offset = draw_pos + mgl::vec2f(0.0f, get_border_size(window)).floor();
+
+ // Process widgets by visibility (backwards)
+ for(auto it = widgets.rbegin(), end = widgets.rend(); it != end; ++it) {
+ if(!(*it)->on_event(event, window, offset))
+ return false;
+ }
+
+ return true;
+ }
+
+ void ScrollablePage::draw(mgl::Window &window, mgl::vec2f offset) {
+ const mgl::vec2f draw_pos = position + offset;
+ offset = draw_pos + mgl::vec2f(0.0f, get_border_size(window)).floor();
+
+ mgl::Rectangle background(size);
+ background.set_position(draw_pos);
+ background.set_color(get_theme().scrollable_page_bg_color);
+ window.draw(background);
+
+ mgl::Rectangle border(mgl::vec2f(size.x, get_border_size(window)).floor());
+ border.set_position(draw_pos);
+ border.set_color(get_theme().tint_color);
+ window.draw(border);
+
+ for(auto &widget : widgets) {
+ if(widget->move_to_top) {
+ widget->move_to_top = false;
+ std::swap(widget, widgets.back());
+ }
+ widget->draw(window, offset);
+ }
+
+ for(auto &widget : widgets) {
+ widget->draw(window, offset);
+ }
+ }
+
+ float ScrollablePage::get_border_size(mgl::Window &window) const {
+ return window.get_size().y * 0.004f;
+ }
+} \ No newline at end of file
diff --git a/src/gui/StaticPage.cpp b/src/gui/StaticPage.cpp
new file mode 100644
index 0000000..78fa497
--- /dev/null
+++ b/src/gui/StaticPage.cpp
@@ -0,0 +1,33 @@
+#include "../../include/gui/StaticPage.hpp"
+
+namespace gsr {
+ bool StaticPage::on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) {
+ const mgl::vec2f draw_pos = position + offset;
+ offset = draw_pos;
+
+ // Process widgets by visibility (backwards)
+ for(auto it = widgets.rbegin(), end = widgets.rend(); it != end; ++it) {
+ if(!(*it)->on_event(event, window, offset))
+ return false;
+ }
+
+ return true;
+ }
+
+ void StaticPage::draw(mgl::Window &window, mgl::vec2f offset) {
+ const mgl::vec2f draw_pos = position + offset;
+ offset = draw_pos;
+
+ for(auto &widget : widgets) {
+ if(widget->move_to_top) {
+ widget->move_to_top = false;
+ std::swap(widget, widgets.back());
+ }
+ widget->draw(window, offset);
+ }
+
+ for(auto &widget : widgets) {
+ widget->draw(window, offset);
+ }
+ }
+} \ No newline at end of file