hud_system.cpp 3.3 KB
Newer Older
1
2
3
#include "hud_system.hpp"

#include <imgui_internal.h>
Florian Oetke's avatar
Florian Oetke committed
4
#include <glm/glm.hpp>
5
6
7
8
9
10
11
12
13
14
15
#include <glm/vec2.hpp>
#include <glm/vec4.hpp>

#include <string>
#include <unordered_map>

namespace phase_shifter::ui {

	using namespace mirrage::gui::literals;

	namespace {
Florian Oetke's avatar
Florian Oetke committed
16
17
18
19
		constexpr auto hud_height      = 100;
		constexpr auto bar_height      = 90;
		constexpr auto beat_bar_height = 80;
		constexpr auto bar_speed       = 100;
Kevin Balz's avatar
Kevin Balz committed
20
	} // namespace
21

Kevin Balz's avatar
Kevin Balz committed
22
23
	Hud_system::Hud_system(mirrage::gui::Gui&                          gui,
	                       mirrage::ecs::Entity_manager&               ecs,
24
	                       const phase_shifter::gameplay::Beat_system& beat_system)
25
	  : _gui(gui), _ecs(ecs), _beat_system(beat_system)
26
	{
Kevin Balz's avatar
Kevin Balz committed
27
		//_circle_texture = _gui.load_texture("tex:circle"_aid);
28
29
	}

Kevin Balz's avatar
Kevin Balz committed
30
31
	void Hud_system::update(mirrage::util::Time dt)
	{
Florian Oetke's avatar
Florian Oetke committed
32
		_offset = _beat_system.beat_state().time_to_beat * bar_speed;
33
34
35
36
37
38
39
40
41
42
43
	}

	void Hud_system::draw()
	{
		auto viewport = _gui.viewport();
		auto font     = _gui.find_font("default"_strid);

		if(font.is_some())
			ImGui::PushFont(font.get_or_throw());

		ImGui::PositionNextWindow(glm::vec2(viewport.z, hud_height),
44
45
		                          ImGui::WindowPosition_X::left,
		                          ImGui::WindowPosition_Y::top);
46
		if(ImGui::Begin("hud",
Kevin Balz's avatar
Kevin Balz committed
47
48
49
50
		                nullptr,
		                ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
		                        | ImGuiWindowFlags_NoBackground)) {

51
			ImGui::Text("%i", _beat_system.beat_state().beats_left);
52
53
54
55

			ImGui::End();
		}

Florian Oetke's avatar
Florian Oetke committed
56
57
58
		ImGui::PositionNextWindow(glm::vec2(viewport.z, hud_height),
		                          ImGui::WindowPosition_X::center,
		                          ImGui::WindowPosition_Y::bottom);
59
60
		if(ImGui::Begin("beat",
		                nullptr,
Kevin Balz's avatar
Kevin Balz committed
61
62
63
		                ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
		                        | ImGuiWindowFlags_NoBackground)) {
			ImGui::PushStyleColor(ImGuiCol_Border, "#00000000"_imcolor.Value); // Hide Border
Florian Oetke's avatar
Florian Oetke committed
64
65
66
67
68
69
			ImVec2 cursor = ImGui::GetCursorScreenPos();
			cursor.y += hud_height / 2.f - 10;
			auto beat_state  = _beat_system.beat_state();
			auto line_offset = bar_speed * beat_state.avg_beat_time;
			auto beats       = std::clamp(beat_state.beats_left, 0, 5);
			ImGui::BeginChild("Line", {viewport.z, bar_height}, true);
70
			ImDrawList* drawList = ImGui::GetWindowDrawList();
Florian Oetke's avatar
Florian Oetke committed
71
72
73
			for(int i = 0; i < beats; i++) {
				auto x_offset = _offset + i * line_offset;
				auto height   = beat_bar_height * (1.f - glm::smoothstep(0.f, beats * line_offset, x_offset));
Kevin Balz's avatar
Kevin Balz committed
74

Florian Oetke's avatar
Florian Oetke committed
75
76
77
78
79
				ImVec2 p(cursor.x + viewport.z / 2 - x_offset, cursor.y - height / 2.f);
				drawList->AddLine(p, {p.x, p.y + height}, 0xFFFFFFFF, 1);

				p.x = cursor.x + viewport.z / 2 + x_offset;
				drawList->AddLine(p, {p.x, p.y + height}, 0xFFFFFFFF, 1);
Kevin Balz's avatar
Kevin Balz committed
80
			}
Florian Oetke's avatar
Florian Oetke committed
81
82
83
84

			ImVec2 middle(cursor.x + viewport.z / 2, cursor.y - bar_height / 2.f);
			drawList->AddLine(middle, {middle.x, middle.y + bar_height}, 0xFFFF0000, 4);

85
			ImGui::EndChild();
Kevin Balz's avatar
Kevin Balz committed
86
87
			ImGui::PopStyleColor();

88
89

			ImGui::End();
Kevin Balz's avatar
Kevin Balz committed
90
91
92
93
94
95

			if(beat_state.beat || beat_state.avg_beat_time - beat_state.time_since_beat < 0.1f
			   || beat_state.time_to_beat < 0.1f) {
				//TODO: Flash image to beat
				//ImGui::Image(_circle_texture.get(), {10, 10});
			}
96
97
98
99
100
101
102
		}

		if(font.is_some())
			ImGui::PopFont();
	}

} // namespace phase_shifter::ui