Add checkbox, scale more sizes by window scale

This commit is contained in:
dec05eba
2024-08-06 08:22:42 +02:00
parent 9b6033e2b9
commit 520c61ec64
9 changed files with 125 additions and 10 deletions

View File

@@ -28,6 +28,7 @@ namespace gsr {
void Button::draw(mgl::Window &window, mgl::vec2f offset) {
const mgl::vec2f draw_pos = position + offset;
const mgl::vec2f item_size = get_size().floor();
mgl::Rectangle background(item_size);
background.set_position(draw_pos.floor());
@@ -38,11 +39,8 @@ namespace gsr {
window.draw(text);
const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f());
if(mouse_inside) {
const int border_size = 5;
const mgl::Color border_color = gsr::get_theme().tint_color;
draw_rectangle_outline(window, position, item_size, border_color, border_size);
}
if(mouse_inside)
draw_rectangle_outline(window, draw_pos, item_size, gsr::get_theme().tint_color, border_scale * gsr::get_theme().window_height);
}
mgl::vec2f Button::get_size() {
@@ -54,4 +52,8 @@ namespace gsr {
s.y = padding_top + text_bounds.y + padding_bottom;
return s;
}
void Button::set_border_scale(float scale) {
border_scale = scale;
}
}

71
src/gui/CheckBox.cpp Normal file
View File

@@ -0,0 +1,71 @@
#include "../../include/gui/CheckBox.hpp"
#include "../../include/gui/Utils.hpp"
#include "../../include/Theme.hpp"
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/window/Window.hpp>
#include <mglpp/window/Event.hpp>
#include <mglpp/system/FloatRect.hpp>
namespace gsr {
static const float spacing_scale = 0.005f;
static const float checked_margin_scale = 0.003f;
static const float border_scale = 0.001f;
CheckBox::CheckBox(mgl::Font *font, const char *text) : text(text, *font) {
}
bool CheckBox::on_event(mgl::Event &event, mgl::Window&, mgl::vec2f offset) {
if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) {
const bool clicked_inside = mgl::FloatRect(position + offset, get_size()).contains({ (float)event.mouse_button.x, (float)event.mouse_button.y });
if(clicked_inside) {
checked = !checked;
if(on_click)
on_click();
}
}
return true;
}
void CheckBox::draw(mgl::Window &window, mgl::vec2f offset) {
const mgl::vec2f draw_pos = position + offset;
const mgl::vec2f checkbox_size = get_checkbox_size();
mgl::Rectangle background(get_checkbox_size());
background.set_position(draw_pos.floor());
background.set_color(mgl::Color(0, 0, 0, 120));
window.draw(background);
if(checked) {
const float side_margin = checked_margin_scale * get_theme().window_height;
mgl::Rectangle background(get_checkbox_size() - mgl::vec2f(side_margin, side_margin).floor() * 2.0f);
background.set_position(draw_pos.floor() + mgl::vec2f(side_margin, side_margin).floor());
background.set_color(gsr::get_theme().tint_color);
window.draw(background);
}
const mgl::vec2f text_bounds = text.get_bounds().size;
text.set_position((draw_pos + mgl::vec2f(checkbox_size.x + spacing_scale * get_theme().window_height, checkbox_size.y * 0.5f - text_bounds.y * 0.5f)).floor());
window.draw(text);
const bool mouse_inside = mgl::FloatRect(draw_pos, get_size()).contains(window.get_mouse_position().to_vec2f());
if(mouse_inside) {
const int border_size = border_scale * gsr::get_theme().window_height;
const mgl::Color border_color = gsr::get_theme().tint_color;
draw_rectangle_outline(window, draw_pos, checkbox_size, border_color, border_size);
}
}
mgl::vec2f CheckBox::get_size() {
mgl::vec2f size = text.get_bounds().size;
const mgl::vec2f checkbox_size = get_checkbox_size();
size.x += checkbox_size.x + spacing_scale * get_theme().window_height;
size.y = std::max(size.y, checkbox_size.y);
return size;
}
mgl::vec2f CheckBox::get_checkbox_size() {
const mgl::vec2f text_bounds = text.get_bounds().size;
return mgl::vec2f(text_bounds.y, text_bounds.y).floor();
}
}

View File

@@ -12,6 +12,7 @@ namespace gsr {
static const float padding_bottom = 10.0f;
static const float padding_left = 10.0f;
static const float padding_right = 10.0f;
static const float border_scale = 0.0015f;
ComboBox::ComboBox(mgl::Font *font) : font(font), dropdown_arrow(&get_theme().combobox_arrow) {
assert(font);
@@ -82,12 +83,13 @@ namespace gsr {
window.draw(dropdown_arrow);
}
const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f());
mgl::vec2f pos = draw_pos + mgl::vec2f(padding_left, padding_top);
Item &item = items[selected_item];
item.text.set_position(pos.floor());
if(show_dropdown) {
const int border_size = 3;
if(show_dropdown || mouse_inside) {
const int border_size = border_scale * gsr::get_theme().window_height;
const mgl::Color border_color = gsr::get_theme().tint_color;
draw_rectangle_outline(window, pos - mgl::vec2f(padding_left, padding_top), item_size.floor(), border_color, border_size);
}

View File

@@ -12,7 +12,7 @@ namespace gsr {
static const float padding_bottom = 15.0f;
static const float padding_left = 20.0f;
static const float padding_right = 20.0f;
static const int border_size = 5;
static const float border_scale = 0.003f;
DropdownButton::DropdownButton(mgl::Font *title_font, mgl::Font *description_font, const char *title, const char *description_activated, const char *description_deactivated, mgl::Texture *icon_texture, mgl::vec2f size) :
title_font(title_font), description_font(description_font), size(size), title(title, *title_font), description(description_deactivated, *description_font),
@@ -59,6 +59,7 @@ namespace gsr {
update_if_dirty();
const mgl::vec2f draw_pos = position + offset;
const int border_size = border_scale * gsr::get_theme().window_height;
if(show_dropdown) {
// Background
@@ -73,7 +74,7 @@ namespace gsr {
// Green line at top
{
mgl::Rectangle rect({ size.x, border_size });
mgl::Rectangle rect({ size.x, (float)border_size });
rect.set_position(draw_pos);
rect.set_color(border_color);
window.draw(rect);

View File

@@ -13,6 +13,7 @@ namespace gsr {
static const float padding_bottom = 10.0f;
static const float padding_left = 10.0f;
static const float padding_right = 10.0f;
static const float border_scale = 0.0015f;
Entry::Entry(mgl::Font *font, const char *text, float max_width) : text("", *font), max_width(max_width) {
this->text.set_color(get_theme().text_color);
@@ -46,7 +47,7 @@ namespace gsr {
window.draw(background);
if(selected) {
const int border_size = 3;
const int border_size = border_scale * gsr::get_theme().window_height;
draw_rectangle_outline(window, draw_pos.floor(), get_size().floor(), get_theme().tint_color, border_size);
const int caret_width = 2;