summaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-12-28 15:42:50 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-12-28 15:42:50 +0000
commitde9909df038d1b6bb47bcc82fea5f8dc9dec2491 (patch)
treeb1f1337603c1bda68760d3f5f5147bc345e3e77f /ui
parentComponent position mouse click check should only match mouse button events (diff)
downloadilt-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')
-rw-r--r--ui/gameMainWindow.cpp45
-rw-r--r--ui/gameMainWindow.h2
-rw-r--r--ui/manualCameraController.cpp12
3 files changed, 56 insertions, 3 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});
diff --git a/ui/gameMainWindow.h b/ui/gameMainWindow.h
index 8b88f11..b85b8a8 100644
--- a/ui/gameMainWindow.h
+++ b/ui/gameMainWindow.h
@@ -11,7 +11,7 @@ class GameState;
class GameMainWindow : public Window {
public:
- GameMainWindow(size_t w, size_t h);
+ GameMainWindow(size_t w, size_t h, const GameState *);
void tick(TickDuration) override;
diff --git a/ui/manualCameraController.cpp b/ui/manualCameraController.cpp
index 568c547..74b526b 100644
--- a/ui/manualCameraController.cpp
+++ b/ui/manualCameraController.cpp
@@ -22,6 +22,18 @@ ManualCameraController::handleInput(const SDL_Event & e, const Position &)
case SDLK_RCTRL:
ctrl = false;
return true;
+ case SDLK_KP_8:
+ direction = 0;
+ break;
+ case SDLK_KP_4:
+ direction = -half_pi;
+ break;
+ case SDLK_KP_6:
+ direction = half_pi;
+ break;
+ case SDLK_KP_2:
+ direction = pi;
+ break;
}
break;
case SDL_MOUSEBUTTONDOWN: