diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-12-28 15:42:50 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-12-28 15:42:50 +0000 |
commit | de9909df038d1b6bb47bcc82fea5f8dc9dec2491 (patch) | |
tree | b1f1337603c1bda68760d3f5f5147bc345e3e77f /ui/gameMainWindow.cpp | |
parent | Component position mouse click check should only match mouse button events (diff) | |
download | ilt-de9909df038d1b6bb47bcc82fea5f8dc9dec2491.tar.bz2 ilt-de9909df038d1b6bb47bcc82fea5f8dc9dec2491.tar.xz ilt-de9909df038d1b6bb47bcc82fea5f8dc9dec2491.zip |
Initial implementation for being able to click in the main window to select something
Diffstat (limited to 'ui/gameMainWindow.cpp')
-rw-r--r-- | ui/gameMainWindow.cpp | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index e1d9763..41d55da 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -3,15 +3,20 @@ #include "manualCameraController.h" #include "maths.h" #include "toolbar.h" +#include "ui/uiComponent.h" #include "window.h" #include <GL/glew.h> #include <SDL2/SDL.h> #include <collection.hpp> #include <game/gamestate.h> +#include <game/selectable.h> #include <game/worldobject.h> // IWYU pragma: keep #include <gfx/renderable.h> #include <glm/glm.hpp> #include <memory> +#include <tuple> + +class UIShader; class GameMainToolbar : public Toolbar { public: @@ -26,9 +31,45 @@ public: } }; -GameMainWindow::GameMainWindow(size_t w, size_t h) : - Window {w, h, "I Like Trains"}, camera {{-1250.0F, -1250.0F, 35.0F}, 70.0F, rdiv(w, h), 0.1F, 10000.0F} +class GameMainSelector : public UIComponent { +public: + GameMainSelector(const Camera * c, glm::vec2 size, const GameState * gs) : + UIComponent {{{}, size}}, camera {c}, gameState {gs} + { + } + + void + render(const UIShader &, const Position &) const override + { + } + + bool + handleInput(const SDL_Event & e, const Position &) override + { + switch (e.type) { + case SDL_MOUSEBUTTONDOWN: + if (e.button.button == SDL_BUTTON_LEFT) { + const auto mouse = glm::vec2 {e.button.x, e.button.y} / position.size; + + glm::vec2 baryPos {}; + float eh; + + std::ignore = gameState->world.applyOne<Selectable>( + &Selectable::intersectRay, camera->pos, camera->unProject(mouse), &baryPos, &eh); + } + } + return false; + } + +private: + const Camera * camera; + const GameState * gameState; +}; + +GameMainWindow::GameMainWindow(size_t w, size_t h, const GameState * gameState) : + Window {w, h, "I Like Trains"}, camera {{-1250.0F, -1250.0F, 35.0F}, quarter_pi, rdiv(w, h), 0.1F, 10000.0F} { + uiComponents.create<GameMainSelector>(&camera, glm::vec2 {w, h}, gameState); uiComponents.create<GameMainToolbar>(); uiComponents.create<ManualCameraController>(glm::vec2 {-1150, -1150}); |