summaryrefslogtreecommitdiff
path: root/ui/editNetwork.cpp
blob: 98a60d32d9ec054e9dce3b46592d241bee70c3de (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "editNetwork.h"
#include "imgui_wrap.h"
#include <game/gamestate.h>
#include <game/terrain.h>
#include <gfx/gl/sceneShader.h>
#include <gfx/models/texture.h>

EditNetwork::EditNetwork(Network * n) : network {n} { }

bool
EditNetwork::click(const SDL_MouseButtonEvent & event, const Ray<GlobalPosition3D> & ray)
{
	switch (event.button) {
		case SDL_BUTTON_MIDDLE:
			currentStart.reset();
			candidates.clear();
			return true;
		case SDL_BUTTON_LEFT:
			if (!currentStart) {
				currentStart = resolveRay(ray);
			}
			else {
				if (const auto def = resolveRay(ray)) {
					candidates = network->create(
							gameState->terrain.get(), CreationDefinition {.fromEnd = *currentStart, .toEnd = *def});
					for (const auto & link : candidates) {
						network->add(gameState->terrain.get(), link);
					}
					if (continuousMode) {
						currentStart = def;
						currentStart->direction = candidates.back()->endAt(def->position)->dir;
					}
					else {
						currentStart.reset();
					}
					candidates.clear();
				}
			}
			return true;
		default:
			return false;
	}
}

bool
EditNetwork::move(const SDL_MouseMotionEvent &, const Ray<GlobalPosition3D> & ray)
{
	if (currentStart) {
		if (const auto def = resolveRay(ray)) {
			candidates = network->create(
					gameState->terrain.get(), CreationDefinition {.fromEnd = *currentStart, .toEnd = *def});
		}
	}
	return false;
}

bool
EditNetwork::handleInput(const SDL_Event &)
{
	return false;
}

void
EditNetwork::render(const SceneShader &, const Frustum &) const
{
}

std::optional<CreationDefinitionEnd>
EditNetwork::resolveRay(const Ray<GlobalPosition3D> & ray) const
{
	if (const auto position = gameState->terrain->intersectRay(ray)) {
		const auto node = network->intersectRayNodes(ray);
		if (node) {
			const auto direction = network->findNodeDirection(node);
			return CreationDefinitionEnd {.position = node->pos, .direction = direction};
		}
		return CreationDefinitionEnd {.position = position->first, .direction = std::nullopt};
	}
	return {};
}

void
EditNetwork::render(bool & open)
{
	ImGui::SetNextWindowSize({-1, -1});
	ImGui::Begin("Edit Network", &open);
	ImGui::Checkbox("Continuous mode", &continuousMode);
	ImGui::End();
}