From 6e77785ce552a72269f3d76e9d8730cffb6b750d Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 7 Jun 2021 19:20:43 +0200 Subject: [PATCH] Added stretching related things to Widget. Also fixed Button. --- app/impl_application.h | 3 ++- app/main_scene.cpp | 6 +++--- custom_modules/sdl/widgets/button.cpp | 16 +++++++------- custom_modules/sdl/widgets/button.h | 30 +++++++++++++-------------- custom_modules/sdl/widgets/widget.cpp | 14 ++++++------- custom_modules/sdl/widgets/widget.h | 23 ++++++++++++++++---- 6 files changed, 52 insertions(+), 40 deletions(-) diff --git a/app/impl_application.h b/app/impl_application.h index 48db7d6..c5f547e 100644 --- a/app/impl_application.h +++ b/app/impl_application.h @@ -9,7 +9,8 @@ class ImplApplication : public Application { public: ImplApplication() : Application() { - scene = new VLCScene(); + //scene = new VLCScene(); + scene = new MainScene(); } ~ImplApplication() { delete scene; diff --git a/app/main_scene.cpp b/app/main_scene.cpp index 7283956..425c70d 100644 --- a/app/main_scene.cpp +++ b/app/main_scene.cpp @@ -47,7 +47,7 @@ MainScene::MainScene() { _ts->set_y(50); b1 = new Button(); - b1->transform = Rect2(0, 0, 100, 100); + b1->rect = Rect2(0, 0, 100, 100); b1->up = new Sprite(_texture); b1->down = new Sprite(_texture, Color(100, 100, 100)); b1->hover = new Sprite(_texture, Color(200, 200, 200)); @@ -55,12 +55,12 @@ MainScene::MainScene() { //b1->on_click = [this]() -> void { this->member_print(); }; b2 = new Button(); - b2->transform = Rect2(0, 110, 100, 100); + b2->rect = Rect2(0, 110, 100, 100); b2->off = new Sprite(_texture, Color(50, 50, 50)); b2->state = Button::BUTTON_STATE_OFF; b3 = new Button(); - b3->transform = Rect2(0, 220, 100, 100); + b3->rect = Rect2(0, 220, 100, 100); b3->up = new Sprite(_texture); b3->down = new Sprite(_texture, Color(100, 100, 100)); b3->hover = new Sprite(_texture, Color(200, 200, 200)); diff --git a/custom_modules/sdl/widgets/button.cpp b/custom_modules/sdl/widgets/button.cpp index 42cd6ac..84163f7 100644 --- a/custom_modules/sdl/widgets/button.cpp +++ b/custom_modules/sdl/widgets/button.cpp @@ -12,7 +12,7 @@ void Button::_event(const SDL_Event &ev) { int x = ev.motion.x; int y = ev.motion.y; - if (transform.x < x && transform.x + transform.w > x && transform.y < y && transform.y + transform.w > y) { + if (rect.x < x && rect.x + rect.w > x && rect.y < y && rect.y + rect.w > y) { state = BUTTON_STATE_HOVER; } else { state = BUTTON_STATE_UP; @@ -24,7 +24,7 @@ void Button::_event(const SDL_Event &ev) { int x = ev.motion.x; int y = ev.motion.y; - if (transform.x < x && transform.x + transform.w > x && transform.y < y && transform.y + transform.w > y) { + if (rect.x < x && rect.x + rect.w > x && rect.y < y && rect.y + rect.w > y) { state = BUTTON_STATE_DOWN; } @@ -34,7 +34,7 @@ void Button::_event(const SDL_Event &ev) { int x = ev.motion.x; int y = ev.motion.y; - if (transform.x < x && transform.x + transform.w > x && transform.y < y && transform.y + transform.w > y) { + if (rect.x < x && rect.x + rect.w > x && rect.y < y && rect.y + rect.w > y) { state = BUTTON_STATE_HOVER; if (on_click) { @@ -55,28 +55,28 @@ void Button::_update(float delta) { void Button::_render() { if (state == BUTTON_STATE_UP) { if (up) { - up->set_transform(transform); + up->set_transform(rect); up->draw(); } } else if (state == BUTTON_STATE_HOVER) { if (hover) { - hover->set_transform(transform); + hover->set_transform(rect); hover->draw(); } } else if (state == BUTTON_STATE_DOWN) { if (down) { - down->set_transform(transform); + down->set_transform(rect); down->draw(); } } else if (state == BUTTON_STATE_OFF) { if (off) { - off->set_transform(transform); + off->set_transform(rect); off->draw(); } } } -Button::Button() { +Button::Button() : Widget() { state = BUTTON_STATE_UP; up = nullptr; diff --git a/custom_modules/sdl/widgets/button.h b/custom_modules/sdl/widgets/button.h index 9103cd3..f776e68 100644 --- a/custom_modules/sdl/widgets/button.h +++ b/custom_modules/sdl/widgets/button.h @@ -13,30 +13,28 @@ class Button : public Widget { public: - enum ButtonState { - BUTTON_STATE_UP, - BUTTON_STATE_HOVER, - BUTTON_STATE_DOWN, - BUTTON_STATE_OFF, - }; + enum ButtonState { + BUTTON_STATE_UP, + BUTTON_STATE_HOVER, + BUTTON_STATE_DOWN, + BUTTON_STATE_OFF, + }; void _event(const SDL_Event &ev); void _update(float delta); void _render(); - std::function on_click; + std::function on_click; - Button(); - virtual ~Button(); + Button(); + virtual ~Button(); - ButtonState state; + ButtonState state; - Rect2 transform; - - Sprite *up; - Sprite *down; - Sprite *hover; - Sprite *off; + Sprite *up; + Sprite *down; + Sprite *hover; + Sprite *off; }; #endif diff --git a/custom_modules/sdl/widgets/widget.cpp b/custom_modules/sdl/widgets/widget.cpp index 2ff1cc6..14fea06 100644 --- a/custom_modules/sdl/widgets/widget.cpp +++ b/custom_modules/sdl/widgets/widget.cpp @@ -1,28 +1,26 @@ #include "widget.h" void Widget::event(const SDL_Event &ev) { - + _event(ev); } void Widget::update(float delta) { - + _update(delta); } void Widget::render() { - + _render(); } void Widget::_event(const SDL_Event &ev) { - } void Widget::_update(float delta) { - } void Widget::_render() { - } Widget::Widget() { - + min_size = 0; + stretch_flags = STRETCH_OFF; + stretch_ratio = 1; } Widget::~Widget() { - } diff --git a/custom_modules/sdl/widgets/widget.h b/custom_modules/sdl/widgets/widget.h index ddb9619..4defc97 100644 --- a/custom_modules/sdl/widgets/widget.h +++ b/custom_modules/sdl/widgets/widget.h @@ -4,23 +4,38 @@ #include +#include "core/rect2.h" #include "core/vector.h" class Widget { public: + enum StretchFlags { + STRETCH_HORIZONTAL = 1 << 0, + STRETCH_VERTICAL = 1 << 1, + + STRETCH_OFF = 0, + STRETCH_BOTH = STRETCH_HORIZONTAL | STRETCH_VERTICAL, + }; + void event(const SDL_Event &ev); void update(float delta); void render(); - virtual void _event(const SDL_Event &ev); + virtual void _event(const SDL_Event &ev); virtual void _update(float delta); virtual void _render(); - Widget(); - virtual ~Widget(); + Widget(); + virtual ~Widget(); + + float min_size; + int stretch_flags; + float stretch_ratio; + + Rect2 rect; private: - Vector _children; + Vector _children; }; #endif