From 917b3049a10dcc41d6d02f332e81b5dd05aff33d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 18 Jan 2022 02:07:33 +0000 Subject: Beginnings of network editor Pushes gameMainSelector clicks/moves into a different target --- ui/gameMainSelector.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 ui/gameMainSelector.cpp (limited to 'ui/gameMainSelector.cpp') diff --git a/ui/gameMainSelector.cpp b/ui/gameMainSelector.cpp new file mode 100644 index 0000000..635dce0 --- /dev/null +++ b/ui/gameMainSelector.cpp @@ -0,0 +1,79 @@ +#include "gameMainSelector.h" +#include "collection.hpp" +#include "text.h" +#include "ui/uiComponent.h" +#include +#include +#include +#include +#include // IWYU pragma: keep +#include +#include +#include +#include +#include +#include + +GameMainSelector::GameMainSelector(const Camera * c, glm::vec2 size) : UIComponent {{{}, size}}, camera {c} { } + +void +GameMainSelector::render(const UIShader & shader, const Position & pos) const +{ + if (!clicked.empty()) { + Text {clicked, {{50, 10}, {0, 15}}, {1, 1, 0}}.render(shader, pos); + } +} + +bool +GameMainSelector::handleInput(const SDL_Event & e, const Position &) +{ + 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; + const auto ray = camera->unProject(mouse); + + if (target) { + target->click(ray); + } + else { + defaultClick(ray); + } + return true; + } + break; + + case SDL_MOUSEMOTION: + if (target && target->handleMove()) { + const auto mouse = glm::vec2 {e.motion.x, e.motion.y} / position.size; + const auto ray = camera->unProject(mouse); + + if (target) { + target->move(ray); + } + return true; + } + break; + } + return false; +} + +void +GameMainSelector::defaultClick(const Ray & ray) +{ + glm::vec2 baryPos {}; + float distance; + + if (const auto selected + = gameState->world.applyOne(&Selectable::intersectRay, ray, &baryPos, &distance); + selected != gameState->world.end()) { + const auto & ref = *selected.base()->get(); + clicked = typeid(ref).name(); + } + else if (const auto pos = gameState->geoData->intersectRay(ray)) { + clicked = streamed_string(*pos); + } + else { + clicked.clear(); + } +} -- cgit v1.2.3