summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ui/builders/straight.cpp40
-rw-r--r--ui/builders/straight.h15
-rw-r--r--ui/editNetwork.cpp48
-rw-r--r--ui/editNetwork.h45
-rw-r--r--ui/gameMainSelector.cpp79
-rw-r--r--ui/gameMainSelector.h17
-rw-r--r--ui/gameMainWindow.cpp3
7 files changed, 201 insertions, 46 deletions
diff --git a/ui/builders/straight.cpp b/ui/builders/straight.cpp
new file mode 100644
index 0000000..7024746
--- /dev/null
+++ b/ui/builders/straight.cpp
@@ -0,0 +1,40 @@
+#include "straight.h"
+#include <game/geoData.h>
+
+void
+BuilderStraight::render(const Shader &) const
+{
+}
+
+std::string
+BuilderStraight::hint() const
+{
+ if (p1) {
+ return "Pick straight end point";
+ }
+ return "Pick straight start point";
+}
+
+void
+BuilderStraight::click(Network * network, const GeoData * geoData, const SDL_MouseButtonEvent & e, const Ray & ray)
+{
+ switch (e.button) {
+ case SDL_BUTTON_LEFT:
+ if (const auto p = geoData->intersectRay(ray)) {
+ if (p1) {
+ create(network, *p1, *p);
+ }
+ p1 = *p;
+ }
+ return;
+ case SDL_BUTTON_MIDDLE:
+ p1.reset();
+ return;
+ }
+}
+
+void
+BuilderStraight::create(Network * network, glm::vec3 p1, glm::vec3 p2) const
+{
+ network->addStraight(p1, p2);
+}
diff --git a/ui/builders/straight.h b/ui/builders/straight.h
new file mode 100644
index 0000000..77847dd
--- /dev/null
+++ b/ui/builders/straight.h
@@ -0,0 +1,15 @@
+#pragma once
+#include "../editNetwork.h"
+
+class Network;
+class GeoData;
+
+class BuilderStraight : public EditNetwork::Builder {
+ void render(const Shader &) const override;
+ std::string hint() const override;
+ void click(Network * network, const GeoData * geoData, const SDL_MouseButtonEvent & e, const Ray & ray) override;
+
+ void create(Network * network, glm::vec3 p1, glm::vec3 p2) const;
+
+ std::optional<glm::vec3> p1;
+};
diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp
index 6cf0eae..a51d7fb 100644
--- a/ui/editNetwork.cpp
+++ b/ui/editNetwork.cpp
@@ -1,17 +1,53 @@
#include "editNetwork.h"
+#include "builders/straight.h"
+#include "text.h"
+#include <game/gamestate.h>
+#include <game/geoData.h>
-void
-EditNetwork::click(const Ray &)
+EditNetwork::EditNetwork(Network * n) :
+ network {n}, builderToolbar {
+ {"ui/icon/network.png", mode.toggle<BuilderStraight>()},
+ }
{
}
-void
-EditNetwork::move(const Ray &)
+EditNetwork::~EditNetwork() = default;
+
+bool
+EditNetwork::click(const SDL_MouseButtonEvent & e, const Ray & ray)
+{
+ if (builder) {
+ builder->click(network, gameState->geoData.get(), e, ray);
+ return true;
+ }
+ return false;
+}
+
+bool
+EditNetwork::move(const SDL_MouseMotionEvent &, const Ray &)
{
+ return false;
}
bool
-EditNetwork::handleMove()
+EditNetwork::handleInput(const SDL_Event & e, const UIComponent::Position & parentPos)
+{
+ return builderToolbar.handleInput(e, parentPos);
+}
+
+void
+EditNetwork::render(const Shader & shader) const
+{
+ if (builder) {
+ builder->render(shader);
+ }
+}
+
+void
+EditNetwork::render(const UIShader & shader, const UIComponent::Position & parentPos) const
{
- return true;
+ if (builder) {
+ Text {builder->hint(), {{50, 10}, {0, 15}}, {1, 1, 0}}.render(shader, parentPos);
+ }
+ builderToolbar.render(shader, parentPos);
}
diff --git a/ui/editNetwork.h b/ui/editNetwork.h
index 0a27609..72e0955 100644
--- a/ui/editNetwork.h
+++ b/ui/editNetwork.h
@@ -1,12 +1,49 @@
#pragma once
#include "gameMainSelector.h"
+#include "modeHelper.hpp"
+#include "toolbar.h"
+#include "worldOverlay.h"
+#include <game/gamestate.h>
+#include <game/network/network.h>
+#include <optional>
class Ray;
-class EditNetwork : public GameMainSelector::ClickReceiver {
+class EditNetwork : public GameMainSelector::Component, public WorldOverlay {
public:
- void click(const Ray &) override;
- void move(const Ray &) override;
- bool handleMove() override;
+ explicit EditNetwork(Network *);
+ virtual ~EditNetwork();
+
+ bool click(const SDL_MouseButtonEvent & e, const Ray &) override;
+ bool move(const SDL_MouseMotionEvent & e, const Ray &) override;
+ bool handleInput(const SDL_Event & e, const UIComponent::Position &) override;
+ void render(const Shader &) const override;
+ void render(const UIShader & shader, const UIComponent::Position & pos) const override;
+
+ using NetworkClickPos = std::variant<glm::vec3, NodePtr>;
+
+ class Builder {
+ public:
+ virtual ~Builder() = default;
+ virtual void render(const Shader & shader) const = 0;
+ virtual std::string hint() const = 0;
+ virtual void click(Network *, const GeoData *, const SDL_MouseButtonEvent &, const Ray &) = 0;
+ };
+
+private:
+ using BuilderPtr = std::unique_ptr<Builder>;
+
+ Network * network;
+ BuilderPtr builder;
+ Mode<BuilderPtr, ModeSecondClick::NoAction> mode {builder};
+ Toolbar builderToolbar;
+};
+
+template<typename T> class EditNetworkOf : public EditNetwork {
+public:
+ template<typename... P>
+ EditNetworkOf(P &&... p) : EditNetwork(gameState->world.findOrCreate<T>(), std::forward<P>(p)...)
+ {
+ }
};
diff --git a/ui/gameMainSelector.cpp b/ui/gameMainSelector.cpp
index 5011767..ff9afb7 100644
--- a/ui/gameMainSelector.cpp
+++ b/ui/gameMainSelector.cpp
@@ -15,43 +15,47 @@
#include <vector>
GameMainSelector::GameMainSelector(const Camera * c, glm::vec2 size) : UIComponent {{{}, size}}, camera {c} { }
+constexpr glm::vec2 TargetPos {5, 45};
void
-GameMainSelector::render(const UIShader & shader, const Position & pos) const
+GameMainSelector::render(const UIShader & shader, const Position & parentPos) const
{
+ if (target) {
+ target->render(shader, parentPos + position + TargetPos);
+ }
if (!clicked.empty()) {
- Text {clicked, {{50, 10}, {0, 15}}, {1, 1, 0}}.render(shader, pos);
+ Text {clicked, {{50, 10}, {0, 15}}, {1, 1, 0}}.render(shader, parentPos);
}
}
bool
-GameMainSelector::handleInput(const SDL_Event & e, const Position &)
+GameMainSelector::handleInput(const SDL_Event & e, const Position & parentPos)
{
- 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);
+ const auto getRay = [this](const auto & e) {
+ const auto mouse = glm::vec2 {e.x, e.y} / position.size;
+ return camera->unProject(mouse);
+ };
+ if (target) {
+ switch (e.type) {
+ case SDL_MOUSEBUTTONDOWN:
+ if (target->click(e.button, getRay(e.button))) {
+ return true;
}
- else {
- defaultClick(ray);
+ break;
+ case SDL_MOUSEMOTION:
+ if (target->move(e.motion, getRay(e.motion))) {
+ return true;
}
- 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);
-
- target->move(ray);
- return true;
- }
- break;
+ break;
+ }
+ return target->handleInput(e, parentPos + position + TargetPos);
+ }
+ else {
+ switch (e.type) {
+ case SDL_MOUSEBUTTONDOWN:
+ defaultClick(getRay(e.button));
+ break;
+ }
}
return false;
}
@@ -75,3 +79,26 @@ GameMainSelector::defaultClick(const Ray & ray)
clicked.clear();
}
}
+
+bool
+GameMainSelector::Component::click(const SDL_MouseButtonEvent &, const Ray &)
+{
+ return false;
+}
+
+bool
+GameMainSelector::Component::move(const SDL_MouseMotionEvent &, const Ray &)
+{
+ return false;
+}
+
+bool
+GameMainSelector::Component::handleInput(const SDL_Event &, const Position &)
+{
+ return false;
+}
+
+void
+GameMainSelector::Component::render(const UIShader &, const UIComponent::Position &) const
+{
+}
diff --git a/ui/gameMainSelector.h b/ui/gameMainSelector.h
index e0715c9..04f2fd7 100644
--- a/ui/gameMainSelector.h
+++ b/ui/gameMainSelector.h
@@ -1,5 +1,6 @@
#pragma once
+#include "SDL_events.h"
#include "special_members.hpp"
#include "uiComponent.h"
#include <glm/glm.hpp>
@@ -7,20 +8,18 @@
#include <string>
class Ray;
class UIShader;
-union SDL_Event;
class Camera;
class GameMainSelector : public UIComponent {
public:
- class ClickReceiver {
+ class Component {
public:
- ClickReceiver() = default;
- virtual ~ClickReceiver() = default;
- DEFAULT_MOVE_COPY(ClickReceiver);
+ virtual ~Component() = default;
- virtual void click(const Ray &) = 0;
- virtual void move(const Ray &) = 0;
- virtual bool handleMove() = 0;
+ virtual bool click(const SDL_MouseButtonEvent &, const Ray &);
+ virtual bool move(const SDL_MouseMotionEvent &, const Ray &);
+ virtual bool handleInput(const SDL_Event &, const Position & pos);
+ virtual void render(const UIShader & shader, const Position & pos) const;
};
GameMainSelector(const Camera * c, glm::vec2 size);
@@ -31,7 +30,7 @@ public:
void defaultClick(const Ray & ray);
- std::unique_ptr<ClickReceiver> target;
+ std::unique_ptr<Component> target;
private:
const Camera * camera;
diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp
index daa5494..84d0ca6 100644
--- a/ui/gameMainWindow.cpp
+++ b/ui/gameMainWindow.cpp
@@ -11,6 +11,7 @@
#include <SDL2/SDL.h>
#include <collection.hpp>
#include <game/gamestate.h>
+#include <game/network/rail.h>
#include <game/worldobject.h> // IWYU pragma: keep
#include <gfx/renderable.h>
#include <glm/glm.hpp>
@@ -22,7 +23,7 @@ public:
explicit GameMainToolbar(GameMainSelector * gms_) :
Mode<decltype(GameMainSelector::target)> {gms_->target}, Toolbar {
{"ui/icon/network.png",
- toggle<EditNetwork>()},
+ toggle<EditNetworkOf<RailLinks>>()},
}
{
}