summaryrefslogtreecommitdiff
path: root/ui/builders/freeExtend.cpp
blob: db127e65e3f7177ae8c2ecec06d3b6a527158ee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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<GlobalPosition3D> & 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->first);
		}
		else {
			candidateLinks.removeAll();
		}
	}
	else {
		candidateLinks.removeAll();
	}
}

void
BuilderFreeExtend::click(
		Network * network, const GeoData * geoData, const SDL_MouseButtonEvent & e, const Ray<GlobalPosition3D> & 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->first);
					p1 = p->first;
				}
			}
			else {
				if (const auto p = network->intersectRayNodes(ray)) {
					p1 = p->pos;
				}
			}
			return;
		case SDL_BUTTON_MIDDLE:
			p1.reset();
			return;
	}
}