summaryrefslogtreecommitdiff
path: root/ui/builders/join.cpp
blob: ee14d6382685dd0bd54542dc431156fd31f65819 (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
#include "join.h"
#include <game/geoData.h>

std::string
BuilderJoin::hint() const
{
	if (p1) {
		return "Pick second node";
	}
	return "Pick first node";
}

void
BuilderJoin::move(Network * network, const GeoData *, const SDL_MouseMotionEvent &, const Ray<GlobalPosition3D> & ray)
{
	if (p1) {
		if (const auto p = network->intersectRayNodes(ray)) {
			candidateLinks.objects = network->candidateJoins(p1->pos, p->pos);
		}
		else {
			candidateLinks.removeAll();
		}
	}
}

void
BuilderJoin::click(
		Network * network, const GeoData *, const SDL_MouseButtonEvent & e, const Ray<GlobalPosition3D> & ray)
{
	switch (e.button) {
		case SDL_BUTTON_LEFT:
			if (const auto p = network->intersectRayNodes(ray)) {
				if (p1) {
					create(network, p1, p);
					p1.reset();
					candidateLinks.removeAll();
				}
				else {
					p1 = p;
				}
			}
			return;
		case SDL_BUTTON_MIDDLE:
			p1.reset();
			return;
	}
}

Link::CCollection
BuilderJoin::create(Network * network, const Node::Ptr & p1, const Node::Ptr & p2) const
{
	const auto links = network->addJoins(p1->pos, p2->pos);
	setHeightsFor(network, links);
	return links;
}