diff options
-rw-r--r-- | game/network/network.h | 10 | ||||
-rw-r--r-- | game/network/network.impl.h | 14 | ||||
-rw-r--r-- | ui/builders/freeExtend.cpp | 57 | ||||
-rw-r--r-- | ui/builders/freeExtend.h | 13 | ||||
-rw-r--r-- | ui/editNetwork.cpp | 2 |
5 files changed, 94 insertions, 2 deletions
diff --git a/game/network/network.h b/game/network/network.h index 773426e..b95dfdc 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -33,8 +33,10 @@ public: virtual Link::CCollection candidateStraight(glm::vec3, glm::vec3) = 0; virtual Link::CCollection candidateJoins(glm::vec3, glm::vec3) = 0; + virtual Link::CCollection candidateExtend(glm::vec3, glm::vec3) = 0; virtual Link::CCollection addStraight(glm::vec3, glm::vec3) = 0; virtual Link::CCollection addJoins(glm::vec3, glm::vec3) = 0; + virtual Link::CCollection addExtend(glm::vec3, glm::vec3) = 0; protected: static void joinLinks(const Link::Ptr & l, const Link::Ptr & ol); @@ -57,7 +59,8 @@ protected: public: template<typename L, typename... Params> std::shared_ptr<L> - candidateLink(glm::vec3 a, glm::vec3 b, Params &&... params) requires std::is_base_of_v<T, L> + candidateLink(glm::vec3 a, glm::vec3 b, Params &&... params) + requires std::is_base_of_v<T, L> { const auto node1 = candidateNodeAt(a).first, node2 = candidateNodeAt(b).first; return std::make_shared<L>(node1, node2, std::forward<Params>(params)...); @@ -65,7 +68,8 @@ public: template<typename L, typename... Params> std::shared_ptr<L> - addLink(glm::vec3 a, glm::vec3 b, Params &&... params) requires std::is_base_of_v<T, L> + addLink(glm::vec3 a, glm::vec3 b, Params &&... params) + requires std::is_base_of_v<T, L> { const auto node1 = nodeAt(a), node2 = nodeAt(b); auto l {links.template create<L>(node1, node2, std::forward<Params>(params)...)}; @@ -75,8 +79,10 @@ public: Link::CCollection candidateStraight(glm::vec3 n1, glm::vec3 n2) override; Link::CCollection candidateJoins(glm::vec3, glm::vec3) override; + Link::CCollection candidateExtend(glm::vec3, glm::vec3) override; Link::CCollection addStraight(glm::vec3 n1, glm::vec3 n2) override; Link::CCollection addJoins(glm::vec3, glm::vec3) override; + Link::CCollection addExtend(glm::vec3, glm::vec3) override; void render(const Shader &) const override; }; diff --git a/game/network/network.impl.h b/game/network/network.impl.h index 6769320..d112bf7 100644 --- a/game/network/network.impl.h +++ b/game/network/network.impl.h @@ -53,6 +53,13 @@ NetworkOf<T>::candidateJoins(glm::vec3 n1, glm::vec3 n2) template<typename T> Link::CCollection +NetworkOf<T>::candidateExtend(glm::vec3 n1, glm::vec3 n2) +{ + return {candidateLink<typename T::StraightLink>(n1, n2)}; +} + +template<typename T> +Link::CCollection NetworkOf<T>::addStraight(glm::vec3 n1, glm::vec3 n2) { return {addLink<typename T::StraightLink>(n1, n2)}; @@ -64,3 +71,10 @@ NetworkOf<T>::addJoins(glm::vec3 n1, glm::vec3 n2) { return {addLink<typename T::StraightLink>(n1, n2)}; } + +template<typename T> +Link::CCollection +NetworkOf<T>::addExtend(glm::vec3 n1, glm::vec3 n2) +{ + return {addLink<typename T::StraightLink>(n1, n2)}; +} diff --git a/ui/builders/freeExtend.cpp b/ui/builders/freeExtend.cpp new file mode 100644 index 0000000..1520421 --- /dev/null +++ b/ui/builders/freeExtend.cpp @@ -0,0 +1,57 @@ +#include "freeExtend.h" +#include <game/geoData.h> + +std::string +BuilderFreeExtend::hint() const +{ + if (p1) { + return "Pick next point"; + } + return "Pick start node"; +} + +void +BuilderFreeExtend::move(Network * network, const GeoData * geoData, const SDL_MouseMotionEvent &, const Ray & ray) +{ + if (p1) { + if (const auto p = network->intersectRayNodes(ray)) { + candidateLinks.objects = network->candidateJoins(*p1, p->pos); + } + else if (const auto p = geoData->intersectRay(ray)) { + candidateLinks.objects = network->candidateExtend(*p1, *p); + } + else { + candidateLinks.removeAll(); + } + } + else { + candidateLinks.removeAll(); + } +} + +void +BuilderFreeExtend::click(Network * network, const GeoData * geoData, const SDL_MouseButtonEvent & e, const Ray & ray) +{ + switch (e.button) { + case SDL_BUTTON_LEFT: + if (p1) { + if (const auto p = network->intersectRayNodes(ray)) { + network->addJoins(*p1, p->pos); + p1 = p->pos; + } + else if (const auto p = geoData->intersectRay(ray)) { + network->addExtend(*p1, *p); + p1 = *p; + } + } + else { + if (const auto p = network->intersectRayNodes(ray)) { + p1 = p->pos; + } + } + return; + case SDL_BUTTON_MIDDLE: + p1.reset(); + return; + } +} diff --git a/ui/builders/freeExtend.h b/ui/builders/freeExtend.h new file mode 100644 index 0000000..55fe7ff --- /dev/null +++ b/ui/builders/freeExtend.h @@ -0,0 +1,13 @@ +#pragma once +#include "../editNetwork.h" + +class Network; +class GeoData; + +class BuilderFreeExtend : public EditNetwork::Builder { + std::string hint() const override; + void click(Network * network, const GeoData * geoData, const SDL_MouseButtonEvent & e, const Ray & ray) override; + void move(Network * network, const GeoData * geoData, const SDL_MouseMotionEvent & e, const Ray & ray) override; + + std::optional<glm::vec3> p1; +}; diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp index 7e007df..df1be8f 100644 --- a/ui/editNetwork.cpp +++ b/ui/editNetwork.cpp @@ -1,4 +1,5 @@ #include "editNetwork.h" +#include "builders/freeExtend.h" #include "builders/join.h" #include "builders/straight.h" #include "text.h" @@ -14,6 +15,7 @@ EditNetwork::EditNetwork(Network * n) : builderToolbar { {"ui/icon/network.png", mode.toggle<BuilderStraight>()}, {"ui/icon/network.png", mode.toggle<BuilderJoin>()}, + {"ui/icon/network.png", mode.toggle<BuilderFreeExtend>()}, }, blue {1, 1, &TRANSPARENT_BLUE} { |