summaryrefslogtreecommitdiff
path: root/ui/gameMainWindow.cpp
blob: 1724927d7f2dcf71b19415631d00883422980a30 (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
#include "gameMainWindow.h"
#include "gfx/camera_controller.h"
#include "manualCameraController.h"
#include "maths.h"
#include "ray.hpp"
#include "text.h"
#include "toolbar.h"
#include "ui/uiComponent.h"
#include "window.h"
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <collection.hpp>
#include <game/gamestate.h>
#include <game/geoData.h>
#include <game/selectable.h>
#include <game/worldobject.h> // IWYU pragma: keep
#include <gfx/renderable.h>
#include <glm/glm.hpp>
#include <memory>
#include <span>
#include <stdexcept>
#include <string>
#include <typeinfo>
#include <vector>

class UIShader;

class GameMainToolbar : public Toolbar {
public:
	GameMainToolbar() :
		Toolbar {
				{"ui/icon/network.png",
						[](const SDL_Event &) {
							// fprintf(stderr, "network click\n");
						}},
		}
	{
	}
};

#include <stream_support.hpp>

class GameMainSelector : public UIComponent {
public:
	GameMainSelector(const Camera * c, glm::vec2 size) : UIComponent {{{}, size}}, camera {c} { }

	void
	render(const UIShader & shader, const Position & pos) const override
	{
		if (!clicked.empty()) {
			Text {clicked, {{50, 10}, {0, 15}}, {1, 1, 0}}.render(shader, pos);
		}
	}

	bool
	handleInput(const SDL_Event & e, const Position &) override
	{
		switch (e.type) {
			case SDL_MOUSEBUTTONDOWN:
				if (e.button.button == SDL_BUTTON_LEFT) {
					const auto mouse = glm::vec2 {e.button.x, e.button.y} / position.size;

					glm::vec2 baryPos {};
					float eh;

					const auto ray = camera->unProject(mouse);
					if (const auto selected
							= gameState->world.applyOne<Selectable>(&Selectable::intersectRay, ray, &baryPos, &eh);
							selected != gameState->world.end()) {
						const auto & ref = *selected.base()->get();
						clicked = typeid(ref).name();
					}
					else if (const auto pos = gameState->geoData->intersectRay(ray)) {
						clicked = streamed_string(*pos);
					}
					else {
						clicked.clear();
					}
				}
		}
		return false;
	}

private:
	const Camera * camera;
	std::string clicked;
};

GameMainWindow::GameMainWindow(size_t w, size_t h) :
	Window {w, h, "I Like Trains"}, camera {{-1250.0F, -1250.0F, 35.0F}, quarter_pi, rdiv(w, h), 0.1F, 10000.0F}
{
	uiComponents.create<GameMainSelector>(&camera, glm::vec2 {w, h});
	uiComponents.create<GameMainToolbar>();
	uiComponents.create<ManualCameraController>(glm::vec2 {-1150, -1150});

	shader.setUniform("lightDirection", glm::normalize(glm::vec3 {1, 0, -1}));
	shader.setUniform("lightColor", {.6, .6, .6});
	shader.setUniform("ambientColor", {0.5, 0.5, 0.5});
}

void
GameMainWindow::tick(TickDuration)
{
	uiComponents.apply<CameraController>(&CameraController::updateCamera, &camera);
	shader.setView(camera.GetViewProjection());
}

void
GameMainWindow::render() const
{
	glEnable(GL_DEPTH_TEST);
	gameState->world.apply<Renderable>(&Renderable::render, shader);
	Window::render();
}