diff --git a/client/src/main.cpp b/client/src/main.cpp index c3ed62d..c17b438 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -5,6 +5,7 @@ #include "terminal.h" #include "gfx/shape_renderer.h" +#include "ui/desktop.h" #include "ui/ui_window.h" const int SCREEN_WIDTH = 1280; @@ -63,13 +64,17 @@ int main(int argc, char** argv) { /* Init shape renderer. */ ShapeRenderer* shape_renderer_instance = new ShapeRenderer(SCREEN_WIDTH, SCREEN_HEIGHT); - /* Create UI window. */ - UIWindow* test_window = new UIWindow("Terminal", 100, 100, 800, 500); - /* Create terminal. */ Terminal* term = new Terminal(); + + /* Create UI window and pass it a terminal. */ + UIWindow* test_window = new UIWindow("Terminal", 100, 100, 800, 500); test_window->set_content(term); + /* Create desktop and add the window. */ + Desktop* desktop = new Desktop(); + desktop->add_window(test_window); + bool running = true; while(running) { /* Event handling. */ @@ -77,6 +82,7 @@ int main(int argc, char** argv) { while(SDL_PollEvent(&event)) { if(event.type == SDL_EVENT_QUIT) { running = false; } /* Pass input to terminal. */ + desktop->handle_event(&event); term->handle_input(&event); } @@ -84,13 +90,14 @@ int main(int argc, char** argv) { glClearColor(0.1f, 0.1f, 0.1, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - test_window->render(shape_renderer_instance, txt_render_instance, SCREEN_HEIGHT); + desktop->render(shape_renderer_instance, txt_render_instance, SCREEN_HEIGHT); /* It's really odd to call it SwapWindow now, rather than SwapBuffer. */ SDL_GL_SwapWindow(window); } /* Cleanup. */ + delete desktop; delete term; delete test_window; delete shape_renderer_instance; diff --git a/client/src/ui/desktop.cpp b/client/src/ui/desktop.cpp new file mode 100644 index 0000000..e946c47 --- /dev/null +++ b/client/src/ui/desktop.cpp @@ -0,0 +1,29 @@ +#include "desktop.h" +#include "gfx/shape_renderer.h" +#include "gfx/txt_renderer.h" +#include "ui/ui_window.h" + +Desktop::Desktop(void) {} + +/* Desktop won't own the windows, so doesn't delete them. */ +Desktop::~Desktop(void) {} + +void Desktop::add_window(UIWindow* window) { + _windows.push_back(window); +} + +void Desktop::handle_event(SDL_Event* event) { + /* Just pass the even to all windows for the moment. + * TODO: Only send event to the focused window. + */ + for(auto win : _windows) { + win->handle_event(event); + } +} + +void Desktop::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, + int screen_height) { + for(auto win: _windows) { + win->render(shape_renderer, txt_renderer, screen_height); + } +} diff --git a/client/src/ui/desktop.h b/client/src/ui/desktop.h new file mode 100644 index 0000000..2f354e7 --- /dev/null +++ b/client/src/ui/desktop.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +#include "gfx/shape_renderer.h" +#include "gfx/txt_renderer.h" +#include "ui/ui_window.h" + +class Desktop { +public: + Desktop(void); + ~Desktop(void); + + void add_window(UIWindow* window); + void handle_event(SDL_Event* event); + void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height); + +private: + std::vector _windows; +}; diff --git a/client/src/ui/ui_window.cpp b/client/src/ui/ui_window.cpp index a4babaf..52180d4 100644 --- a/client/src/ui/ui_window.cpp +++ b/client/src/ui/ui_window.cpp @@ -1,4 +1,5 @@ #include "ui_window.h" +#include #include "gfx/shape_renderer.h" #include "gfx/txt_renderer.h" @@ -6,9 +7,10 @@ UIWindow::UIWindow(const char* title, int x, int y, int width, int height) { _title = title; _x = x; _y = y; - _width = width; - _height = height; - _content = nullptr; + _width = width; + _height = height; + _content = nullptr; + _is_dragging = false; } void UIWindow::set_content(Terminal* term) { @@ -45,3 +47,27 @@ void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, } } +void UIWindow::handle_event(SDL_Event* event) { + int title_bar_height = 30; + + if(event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) { + int mouse_x = event->button.x; + int mouse_y = event->button.y; + + /* Is click within title bar? */ + if(mouse_x >= _x && mouse_x <= _x + _width && + mouse_y >= _y && mouse_y <= _y + title_bar_height) { + _is_dragging = true; + _drag_offset_x = mouse_x - _x; + _drag_offset_y = mouse_y - _y; + } + } else if(event->type == SDL_EVENT_MOUSE_BUTTON_UP) { + _is_dragging = false; + } else if(event->type == SDL_EVENT_MOUSE_MOTION) { + if(_is_dragging) { + _x = event->motion.x - _drag_offset_x; + _y = event->motion.y - _drag_offset_y; + } + } +} + diff --git a/client/src/ui/ui_window.h b/client/src/ui/ui_window.h index 64eee8d..0220c5b 100644 --- a/client/src/ui/ui_window.h +++ b/client/src/ui/ui_window.h @@ -11,11 +11,15 @@ public: UIWindow(const char* title, int x, int y, int width, int height); void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height); + void handle_event(SDL_Event* event); void set_content(Terminal* term); private: int _x, _y, _width, _height; std::string _title; Terminal* _content; + + bool _is_dragging; + int _drag_offset_x, _drag_offset_y; };