Added stretching related things to Widget. Also fixed Button.

This commit is contained in:
Relintai 2021-06-07 19:20:43 +02:00
parent f0ac7c40cf
commit 6e77785ce5
6 changed files with 52 additions and 40 deletions

View File

@ -9,7 +9,8 @@
class ImplApplication : public Application { class ImplApplication : public Application {
public: public:
ImplApplication() : Application() { ImplApplication() : Application() {
scene = new VLCScene(); //scene = new VLCScene();
scene = new MainScene();
} }
~ImplApplication() { ~ImplApplication() {
delete scene; delete scene;

View File

@ -47,7 +47,7 @@ MainScene::MainScene() {
_ts->set_y(50); _ts->set_y(50);
b1 = new Button(); b1 = new Button();
b1->transform = Rect2(0, 0, 100, 100); b1->rect = Rect2(0, 0, 100, 100);
b1->up = new Sprite(_texture); b1->up = new Sprite(_texture);
b1->down = new Sprite(_texture, Color(100, 100, 100)); b1->down = new Sprite(_texture, Color(100, 100, 100));
b1->hover = new Sprite(_texture, Color(200, 200, 200)); b1->hover = new Sprite(_texture, Color(200, 200, 200));
@ -55,12 +55,12 @@ MainScene::MainScene() {
//b1->on_click = [this]() -> void { this->member_print(); }; //b1->on_click = [this]() -> void { this->member_print(); };
b2 = new Button(); 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->off = new Sprite(_texture, Color(50, 50, 50));
b2->state = Button::BUTTON_STATE_OFF; b2->state = Button::BUTTON_STATE_OFF;
b3 = new Button(); b3 = new Button();
b3->transform = Rect2(0, 220, 100, 100); b3->rect = Rect2(0, 220, 100, 100);
b3->up = new Sprite(_texture); b3->up = new Sprite(_texture);
b3->down = new Sprite(_texture, Color(100, 100, 100)); b3->down = new Sprite(_texture, Color(100, 100, 100));
b3->hover = new Sprite(_texture, Color(200, 200, 200)); b3->hover = new Sprite(_texture, Color(200, 200, 200));

View File

@ -12,7 +12,7 @@ void Button::_event(const SDL_Event &ev) {
int x = ev.motion.x; int x = ev.motion.x;
int y = ev.motion.y; 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; state = BUTTON_STATE_HOVER;
} else { } else {
state = BUTTON_STATE_UP; state = BUTTON_STATE_UP;
@ -24,7 +24,7 @@ void Button::_event(const SDL_Event &ev) {
int x = ev.motion.x; int x = ev.motion.x;
int y = ev.motion.y; 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; state = BUTTON_STATE_DOWN;
} }
@ -34,7 +34,7 @@ void Button::_event(const SDL_Event &ev) {
int x = ev.motion.x; int x = ev.motion.x;
int y = ev.motion.y; 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; state = BUTTON_STATE_HOVER;
if (on_click) { if (on_click) {
@ -55,28 +55,28 @@ void Button::_update(float delta) {
void Button::_render() { void Button::_render() {
if (state == BUTTON_STATE_UP) { if (state == BUTTON_STATE_UP) {
if (up) { if (up) {
up->set_transform(transform); up->set_transform(rect);
up->draw(); up->draw();
} }
} else if (state == BUTTON_STATE_HOVER) { } else if (state == BUTTON_STATE_HOVER) {
if (hover) { if (hover) {
hover->set_transform(transform); hover->set_transform(rect);
hover->draw(); hover->draw();
} }
} else if (state == BUTTON_STATE_DOWN) { } else if (state == BUTTON_STATE_DOWN) {
if (down) { if (down) {
down->set_transform(transform); down->set_transform(rect);
down->draw(); down->draw();
} }
} else if (state == BUTTON_STATE_OFF) { } else if (state == BUTTON_STATE_OFF) {
if (off) { if (off) {
off->set_transform(transform); off->set_transform(rect);
off->draw(); off->draw();
} }
} }
} }
Button::Button() { Button::Button() : Widget() {
state = BUTTON_STATE_UP; state = BUTTON_STATE_UP;
up = nullptr; up = nullptr;

View File

@ -13,30 +13,28 @@
class Button : public Widget { class Button : public Widget {
public: public:
enum ButtonState { enum ButtonState {
BUTTON_STATE_UP, BUTTON_STATE_UP,
BUTTON_STATE_HOVER, BUTTON_STATE_HOVER,
BUTTON_STATE_DOWN, BUTTON_STATE_DOWN,
BUTTON_STATE_OFF, BUTTON_STATE_OFF,
}; };
void _event(const SDL_Event &ev); void _event(const SDL_Event &ev);
void _update(float delta); void _update(float delta);
void _render(); void _render();
std::function<void(void)> on_click; std::function<void(void)> on_click;
Button(); Button();
virtual ~Button(); virtual ~Button();
ButtonState state; ButtonState state;
Rect2 transform; Sprite *up;
Sprite *down;
Sprite *up; Sprite *hover;
Sprite *down; Sprite *off;
Sprite *hover;
Sprite *off;
}; };
#endif #endif

View File

@ -1,28 +1,26 @@
#include "widget.h" #include "widget.h"
void Widget::event(const SDL_Event &ev) { void Widget::event(const SDL_Event &ev) {
_event(ev);
} }
void Widget::update(float delta) { void Widget::update(float delta) {
_update(delta);
} }
void Widget::render() { void Widget::render() {
_render();
} }
void Widget::_event(const SDL_Event &ev) { void Widget::_event(const SDL_Event &ev) {
} }
void Widget::_update(float delta) { void Widget::_update(float delta) {
} }
void Widget::_render() { void Widget::_render() {
} }
Widget::Widget() { Widget::Widget() {
min_size = 0;
stretch_flags = STRETCH_OFF;
stretch_ratio = 1;
} }
Widget::~Widget() { Widget::~Widget() {
} }

View File

@ -4,23 +4,38 @@
#include <SDL.h> #include <SDL.h>
#include "core/rect2.h"
#include "core/vector.h" #include "core/vector.h"
class Widget { class Widget {
public: 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 event(const SDL_Event &ev);
void update(float delta); void update(float delta);
void render(); void render();
virtual void _event(const SDL_Event &ev); virtual void _event(const SDL_Event &ev);
virtual void _update(float delta); virtual void _update(float delta);
virtual void _render(); virtual void _render();
Widget(); Widget();
virtual ~Widget(); virtual ~Widget();
float min_size;
int stretch_flags;
float stretch_ratio;
Rect2 rect;
private: private:
Vector<Widget *> _children; Vector<Widget *> _children;
}; };
#endif #endif