summaryrefslogtreecommitdiff
path: root/kurator/src/inspect.cpp
blob: 47950682c6ba770b70a846b7813fd75275e795f3 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "inspect.h"

#include <utility>

#include <entt/entt.hpp>
#include <imgui.h>

#include <kurator/sim/components.h>
#include <kurator/sim/FloatingMovement.h>
#include <kurator/sim/HitPoints.h>
#include <kurator/sim/TurretControl.h>
#include <kurator/universe/ShipType.h>
#include <kurator/universe/TurretType.h>
#include <kurator/universe/UniqueIdentifier.h>

#include "markers.h"


namespace kurator
{


template <typename Type> void inspect(entt::handle& entity);
template <typename Type> void inspect(entt::handle& entity, Type& component);


template <typename Type>
void
inspect(entt::handle& entity)
{
	if (entity.all_of<Type>()) {
		inspect(entity, entity.get<Type>());
		ImGui::Separator();
	}
}


void
InspectionWindow::show()
{
	if (!open)
		return;
	if (ImGui::Begin("Inspect", &open)) {
		auto it = selected.begin();
		while (it != selected.end()) {
			ImGui::PushID(it - selected.begin());
			if (!it->valid()) {
				it = selected.erase(it);
				continue;
			}
			inspect<universe::UniqueIdentifier>(*it);
			inspect<universe::ShipType>(*it);
			inspect<universe::TurretType>(*it);
			inspect<sim::FloatingMovement>(*it);
			inspect<sim::HitPoints>(*it);
			inspect<TurretVisuals>(*it);
			it = std::next(it);
			ImGui::PopID();
		}
		if (selected.empty())
			ImGui::Text("Nothing selected");
	}
	ImGui::End();
	if (!open)
		deselect();
}


void
InspectionWindow::select(entt::handle entity)
{
	deselect();
	if (!entity)
		return;
	entity.get<Marker>().selected = true;
	auto* registry = entity.registry();
	auto turrets = registry->view<sim::TurretControl>();
	selected.push_back(std::move(entity));
	for (auto&& [entity_, turret] : turrets.each()) {
		if (turret.owner == entity) {
			registry->emplace<TurretVisuals>(entity_);
			selected.push_back(entt::handle{*registry, entity_});
		}
	}
	open = true;
}


void
InspectionWindow::deselect()
{
	for (auto&& entity_ : selected) {
		if (entity_.all_of<Marker>())
			entity_.get<Marker>().selected = false;
		if (entity_.all_of<TurretVisuals>())
			entity_.remove<TurretVisuals>();
	}
	selected.clear();
}


template <>
void
inspect(entt::handle&, universe::UniqueIdentifier& identifier)
{
	ImGui::Text("Selected: %d", identifier.id);
}


template <>
void
inspect(entt::handle&, universe::ShipType& type)
{
	ImGui::Text("Type: %s", type.name.c_str());
}


template <>
void
inspect(entt::handle&, universe::TurretType& type)
{
	ImGui::Text("Type: %s", type.name.c_str());
}


template <>
void
inspect(entt::handle&, sim::FloatingMovement& movement)
{
	ImGui::Text("Speed: %.2f", movement.speed.magnitude());
	ImGui::InputDouble("Maximum Speed", &movement.max_speed, 0, 0, "%.1f");
}


template <>
void
inspect(entt::handle&, sim::HitPoints& points)
{
	ImGui::Text("Total: %.2f", points.total());
	ImGui::InputDouble("Shield", &points.shield.points, 0, 0, "%.1f");
	ImGui::InputDouble("Armour", &points.armour.points, 0, 0, "%.1f");
	ImGui::InputDouble("Structure", &points.structure.points, 0, 0, "%.1f");
}


template <>
void
inspect(entt::handle&, TurretVisuals& visuals)
{
	ImGui::Checkbox("Show Turret Ranges", &visuals.visible);
}


static Color OPTIMAL {0x88, 0x22, 0x22, 0xff};
static Color EFFECTIVE {0x77, 0x50, 0x22, 0xff};

void
draw_turret_visuals(const sim::State& ctx)
{
	const auto helpers = ctx.registry.view<TurretVisuals, universe::TurretType, sim::TurretControl>();
	for (const auto& [entity, visuals, type, turret] : helpers.each()) {
		if (!visuals.visible)
			continue;
		const auto& transform = ctx.registry.get<sim::Transform>(turret.owner);
		const auto center = ctx.camera.to_screen(transform.position);
		const auto optimal = type.optimal_range * ctx.camera.scale;
		const auto effective = type.effective_range() * ctx.camera.scale;
		DrawCircleLines(center.x, center.y, optimal, OPTIMAL);
		DrawCircleLines(center.x, center.y, effective, EFFECTIVE);
	}
}


}  // namespace kurator