From 1c5acb13bb8ad5a0e62c7abf313fc02c9890fd00 Mon Sep 17 00:00:00 2001 From: "Alejandro W. Sior" Date: Thu, 2 Feb 2023 11:38:49 +0100 Subject: chisel: rename event dispatching and sockets in box --- chisel/box.cpp | 139 +++++++----------------------------------------- chisel/box.hpp | 27 +++++----- chisel/button.cpp | 8 +-- chisel/kiosk.cpp | 2 +- chisel/sdl_window.cpp | 14 ++--- chisel/sdl_window.hpp | 2 +- chisel/vertical_box.cpp | 2 +- main.cpp | 24 ++++----- 8 files changed, 56 insertions(+), 162 deletions(-) diff --git a/chisel/box.cpp b/chisel/box.cpp index 3215c24..e8e3207 100644 --- a/chisel/box.cpp +++ b/chisel/box.cpp @@ -48,85 +48,51 @@ namespace chisel { children.push_back(child); } - void Box::event_on_mouse_button_up(int x, int y, int button) { - mouse_button_up(x, y, button); + void Box::dispatch_on_mouse_button_up(int x, int y, int button) { + on_mouse_button_up(x, y, button); for (auto& child : children) if (x >= child->x && x < child->x + child->width && y >= child->y && y < child->y + child->height) - child->event_on_mouse_button_up(x - child->x, y - child->y, button); + child->dispatch_on_mouse_button_up(x - child->x, y - child->y, button); } - void Box::event_on_mouse_button_down(int x, int y, int button) { - mouse_button_down(x, y, button); + void Box::dispatch_on_mouse_button_down(int x, int y, int button) { + on_mouse_button_down(x, y, button); for (auto& child : children) if (x >= child->x && x < child->x + child->width && y >= child->y && y < child->y + child->height) - child->event_on_mouse_button_down(x - child->x, y - child->y, button); + child->dispatch_on_mouse_button_down(x - child->x, y - child->y, button); } - void Box::event_on_mouse_motion(int x, int y) { - mouse_motion(x, y); + void Box::dispatch_on_mouse_motion(int x, int y) { + on_mouse_motion(x, y); for (auto& child : children) if (x >= child->x && x < child->x + child->width && y >= child->y && y < child->y + child->height) { - child->event_on_mouse_motion(x - child->x, y - child->y); + child->dispatch_on_mouse_motion(x - child->x, y - child->y); if (!child->hovered) - child->event_on_mouse_enter(x - child->x, y - child->y); + child->dispatch_on_mouse_enter(x - child->x, y - child->y); } else if (child->hovered) - child->event_on_mouse_exit(x - child->x, y - child->y); + child->dispatch_on_mouse_exit(x - child->x, y - child->y); } - void Box::event_on_mouse_enter(int x, int y) { + void Box::dispatch_on_mouse_enter(int x, int y) { hovered = true; - mouse_enter(x, y); + on_mouse_enter(x, y); } - void Box::event_on_mouse_exit(int x, int y) { + void Box::dispatch_on_mouse_exit(int x, int y) { hovered = false; - mouse_exit(x, y); + on_mouse_exit(x, y); for (auto &child : children) if (child->hovered) - child->event_on_mouse_exit(x - child->x, y - child->y); + child->dispatch_on_mouse_exit(x - child->x, y - child->y); } - void Box::event_on_resize() { + void Box::dispatch_on_resize() { arrange_pass0(); - //arrange_pass1(); - //arrange_pass2(); - // Broadcast resize event on socket - //resize(); - - // Make sure everything respects padding - /*int padding_width = width - padding_left - padding_right; - int padding_height = height - padding_top - padding_bottom; - - for (auto &child : children) { - int cx = child->x; - int cy = child->y; - int cw = child->width; - int ch = child->height; - - if (cx < padding_left) - cx = padding_left; - if (cy < padding_top) - cy = padding_top; - if (cx - padding_left + cw >= padding_width) - cw = padding_width + padding_left - cx; - if (cy - padding_top + ch >= padding_height) - ch = padding_height + padding_top - cy; - - if (child->x != cx || child->y != cy || child->width != cw || child->height != ch) { - damage(); - - child->x = cx; - child->y = cy; - child->width = cw; - child->height = ch; - child->event_on_resize(); - } - }*/ } void Box::arrange_pass0() { @@ -211,75 +177,6 @@ namespace chisel { void Box::arrange_pass1() { //std::cout << "noice" << std::endl; - resize(); - } - - void Box::arrange_pass2() { - int parent_padding_width = virtual_width; - int parent_padding_height = virtual_height; - int parent_padding_left = 0; - int parent_padding_top = 0; - if (parent) { - parent_padding_width = std::max(0, parent->virtual_width - parent->padding_left - parent->padding_right); - parent_padding_height = std::max(0, parent->virtual_height - parent->padding_top - parent->padding_bottom); - parent_padding_left = parent->padding_left; - parent_padding_top = parent->padding_top; - } - - // FIXME - int min_x = -1, min_y = -1; - int max_x = -1, max_y = -1; - for (auto &child : children) { - if (min_x == -1) { - min_x = child->x; - min_y = child->y; - } - - min_x = std::min(min_x, child->x); - min_y = std::min(min_y, child->y); - max_x = std::max(max_x, child->x + child->virtual_width); - max_y = std::max(max_y, child->y + child->virtual_height); - } - - if (virtual_width == 0) - virtual_width = max_x - min_x + padding_left + padding_right; - if (virtual_height == 0) - virtual_height = max_y - min_y + padding_top + padding_bottom; - - int cx = x; - int cy = y; - int cw = virtual_width; - int ch = virtual_height; - - if (cx < parent_padding_left) - cx = parent_padding_left; - if (cy < parent_padding_top) - cy = parent_padding_top; - if (cx - parent_padding_top + cw >= parent_padding_width) - cw = parent_padding_width + parent_padding_top - cx; - if (cy - parent_padding_top + ch >= parent_padding_height) - ch = parent_padding_height + parent_padding_top - cy; - - std::cout << "parent be like: " << parent_padding_width << "x" << parent_padding_height << std::endl; - if (children.empty()) { - std::cout << "hi, i am innermost component, my dims are: " << virtual_width << "x" << virtual_height << std::endl; - } - - if (virtual_width != cw || virtual_height != ch) { - virtual_width = cw; - virtual_height = ch; - width = cw; - height = ch; - - // Should i do this? perhaps in layouting or something - - arrange_pass1(); - - // ? - damage(); - } - - for (auto &child : children) - child->arrange_pass2(); + on_resize(); } } diff --git a/chisel/box.hpp b/chisel/box.hpp index b9787b8..f879584 100644 --- a/chisel/box.hpp +++ b/chisel/box.hpp @@ -16,12 +16,12 @@ namespace chisel { std::string id; - socket::Socket mouse_button_down; - socket::Socket mouse_button_up; - socket::Socket mouse_motion; - socket::Socket mouse_enter; - socket::Socket mouse_exit; - socket::Socket resize; + socket::Socket on_mouse_button_down; + socket::Socket on_mouse_button_up; + socket::Socket on_mouse_motion; + socket::Socket on_mouse_enter; + socket::Socket on_mouse_exit; + socket::Socket on_resize; Window *window; Box *parent = nullptr; @@ -56,20 +56,17 @@ namespace chisel { void add_child(Box *child); - void event_on_mouse_button_up(int x, int y, int button); - void event_on_mouse_button_down(int x, int y, int button); - void event_on_mouse_motion(int x, int y); - void event_on_mouse_enter(int x, int y); - void event_on_mouse_exit(int x, int y); - void event_on_resize(); + void dispatch_on_mouse_button_up(int x, int y, int button); + void dispatch_on_mouse_button_down(int x, int y, int button); + void dispatch_on_mouse_motion(int x, int y); + void dispatch_on_mouse_enter(int x, int y); + void dispatch_on_mouse_exit(int x, int y); + void dispatch_on_resize(); // Self determination of current and children dims void arrange_pass0(); // Layoutting of current void arrange_pass1(); - - // Constraining the children - void arrange_pass2(); }; } diff --git a/chisel/button.cpp b/chisel/button.cpp index fa90713..0bf7425 100644 --- a/chisel/button.cpp +++ b/chisel/button.cpp @@ -2,10 +2,10 @@ namespace chisel { Button::Button() { - mouse_enter.connect(this, &Button::hover); - mouse_exit.connect(this, &Button::hover); - mouse_button_down.connect(this, &Button::press); - mouse_button_up.connect(this, &Button::depress); + on_mouse_enter.connect(this, &Button::hover); + on_mouse_exit.connect(this, &Button::hover); + on_mouse_button_down.connect(this, &Button::press); + on_mouse_button_up.connect(this, &Button::depress); pressed = false; r = 255; g = 255; diff --git a/chisel/kiosk.cpp b/chisel/kiosk.cpp index 0323822..d2b55e4 100644 --- a/chisel/kiosk.cpp +++ b/chisel/kiosk.cpp @@ -3,7 +3,7 @@ namespace chisel { Kiosk::Kiosk() { - resize.connect(this, &Kiosk::layout); + on_resize.connect(this, &Kiosk::layout); } Kiosk::~Kiosk() {} diff --git a/chisel/sdl_window.cpp b/chisel/sdl_window.cpp index a969767..216becb 100644 --- a/chisel/sdl_window.cpp +++ b/chisel/sdl_window.cpp @@ -50,7 +50,7 @@ namespace chisel { SDL_Quit(); } - void SDLWindow::on_sdl_event(SDL_Event *event) { + void SDLWindow::dispatch_on_sdl_event(SDL_Event *event) { // If the event does not relate to window, exit if (SDL_GetWindowFromID(event->window.windowID) != sdl_window) return; @@ -72,33 +72,33 @@ namespace chisel { title(s.str()); damage(); - event_on_resize(); + dispatch_on_resize(); } // If mouse button up event if (event->type == SDL_MOUSEBUTTONUP) - event_on_mouse_button_up(event->button.x, event->button.y, event->button.button); + dispatch_on_mouse_button_up(event->button.x, event->button.y, event->button.button); // If mouse button down event if (event->type == SDL_MOUSEBUTTONDOWN) - event_on_mouse_button_down(event->button.x, event->button.y, event->button.button); + dispatch_on_mouse_button_down(event->button.x, event->button.y, event->button.button); // If mouse motion if (event->type == SDL_MOUSEMOTION) - event_on_mouse_motion(event->motion.x, event->motion.y); + dispatch_on_mouse_motion(event->motion.x, event->motion.y); } void SDLWindow::run() { running = true; SDL_GetWindowSize(sdl_window, &width, &height); - event_on_resize(); + dispatch_on_resize(); while (running) { if (damaged_set.empty()) { SDL_Event event; SDL_WaitEvent(&event); - on_sdl_event(&event); + dispatch_on_sdl_event(&event); } // Redraw damaged stuff diff --git a/chisel/sdl_window.hpp b/chisel/sdl_window.hpp index d809f54..36a83cf 100644 --- a/chisel/sdl_window.hpp +++ b/chisel/sdl_window.hpp @@ -14,7 +14,7 @@ namespace chisel { bool running; - void on_sdl_event(SDL_Event* event); + void dispatch_on_sdl_event(SDL_Event* event); public: SDLWindow(std::string title); virtual ~SDLWindow(); diff --git a/chisel/vertical_box.cpp b/chisel/vertical_box.cpp index 0dd474a..97e3fbb 100644 --- a/chisel/vertical_box.cpp +++ b/chisel/vertical_box.cpp @@ -3,7 +3,7 @@ namespace chisel { VerticalBox::VerticalBox() { - resize.connect(this, &VerticalBox::layout); + on_resize.connect(this, &VerticalBox::layout); } VerticalBox::~VerticalBox() {} diff --git a/main.cpp b/main.cpp index c1c14ca..7e54192 100644 --- a/main.cpp +++ b/main.cpp @@ -41,10 +41,10 @@ int main() { .type = chisel::PR, .pr = 1.0 }; - a->mouse_button_up.connect(mouse_button_up); - a->mouse_button_down.connect(mouse_button_down); - a->mouse_enter.connect(mouse_enter); - a->mouse_exit.connect(mouse_exit); + a->on_mouse_button_up.connect(mouse_button_up); + a->on_mouse_button_down.connect(mouse_button_down); + a->on_mouse_enter.connect(mouse_enter); + a->on_mouse_exit.connect(mouse_exit); window.add_child(a); chisel::VerticalBox *b = new chisel::VerticalBox(); b->id = "B"; @@ -61,10 +61,10 @@ int main() { .type = chisel::PR, .pr = 1.0 }; - b->mouse_button_up.connect(mouse_button_up); - b->mouse_button_down.connect(mouse_button_down); - b->mouse_enter.connect(mouse_enter); - b->mouse_exit.connect(mouse_exit); + b->on_mouse_button_up.connect(mouse_button_up); + b->on_mouse_button_down.connect(mouse_button_down); + b->on_mouse_enter.connect(mouse_enter); + b->on_mouse_exit.connect(mouse_exit); a->add_child(b); for (int i = 0; i < 5; i++) { @@ -77,10 +77,10 @@ int main() { .type = chisel::PR, .pr = 0.08 }; - c->mouse_button_up.connect(mouse_button_up); - c->mouse_button_down.connect(mouse_button_down); - c->mouse_enter.connect(mouse_enter); - c->mouse_exit.connect(mouse_exit); + c->on_mouse_button_up.connect(mouse_button_up); + c->on_mouse_button_down.connect(mouse_button_down); + c->on_mouse_enter.connect(mouse_enter); + c->on_mouse_exit.connect(mouse_exit); b->add_child(c); } -- cgit v1.2.3