From d11d99323d642ebb1cf84302b2b25019e95b5607 Mon Sep 17 00:00:00 2001
From: Dan Goodliffe <dan@randomdan.homeip.net>
Date: Sat, 15 Oct 2022 15:30:44 +0100
Subject: Add free extend builder with placeholder network support

---
 ui/builders/freeExtend.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 ui/builders/freeExtend.h   | 13 +++++++++++
 ui/editNetwork.cpp         |  2 ++
 3 files changed, 72 insertions(+)
 create mode 100644 ui/builders/freeExtend.cpp
 create mode 100644 ui/builders/freeExtend.h

(limited to 'ui')

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}
 {
-- 
cgit v1.2.3