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 {
public:
ImplApplication() : Application() {
scene = new VLCScene();
//scene = new VLCScene();
scene = new MainScene();
}
~ImplApplication() {
delete scene;

View File

@ -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));

View File

@ -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;

View File

@ -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<void(void)> on_click;
std::function<void(void)> 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

View File

@ -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() {
}

View File

@ -4,23 +4,38 @@
#include <SDL.h>
#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<Widget *> _children;
Vector<Widget *> _children;
};
#endif