hud_system.cpp 3.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "hud_system.hpp"

#include <imgui_internal.h>
#include <glm/vec2.hpp>
#include <glm/vec4.hpp>

#include <string>
#include <unordered_map>

namespace phase_shifter::ui {

	using namespace mirrage::gui::literals;

	namespace {
		constexpr auto hud_height = 100;
16
		constexpr auto count_down = 300;
Kevin Balz's avatar
Kevin Balz committed
17
18
		constexpr auto bar_speed  = 100;
	} // namespace
19

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

Kevin Balz's avatar
Kevin Balz committed
29
30
	void Hud_system::update(mirrage::util::Time dt)
	{
31
		_timeLeft -= dt;
Kevin Balz's avatar
Kevin Balz committed
32
		auto state    = _beat_system.beat_state();
Kevin Balz's avatar
Kevin Balz committed
33
		int  max_bars = static_cast<int>(_max_distance / (bar_speed * state.avg_beat_time) + 1);
Kevin Balz's avatar
Kevin Balz committed
34
35
36
37
		if(state.beat && _beats < max_bars) {
			_beats++;
		}
		_offset = state.time_to_beat * bar_speed;
38
39
40
41
42
43
44
45
46
47
48
	}

	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),
49
50
		                          ImGui::WindowPosition_X::left,
		                          ImGui::WindowPosition_Y::top);
51
		if(ImGui::Begin("hud",
Kevin Balz's avatar
Kevin Balz committed
52
53
54
55
		                nullptr,
		                ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
		                        | ImGuiWindowFlags_NoBackground)) {

56
57
58
59
60
			ImGui::Text("%.2f", _timeLeft.value());

			ImGui::End();
		}

Kevin Balz's avatar
Kevin Balz committed
61
62
		ImGui::PositionNextWindow(
		        glm::vec2(viewport.z, 150), ImGui::WindowPosition_X::center, ImGui::WindowPosition_Y::bottom);
63
64
		if(ImGui::Begin("beat",
		                nullptr,
Kevin Balz's avatar
Kevin Balz committed
65
66
67
68
69
		                ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
		                        | ImGuiWindowFlags_NoBackground)) {
			ImGui::PushStyleColor(ImGuiCol_Border, "#00000000"_imcolor.Value); // Hide Border
			ImVec2 cursor       = ImGui::GetCursorScreenPos();
			auto   beat_state   = _beat_system.beat_state();
Kevin Balz's avatar
Kevin Balz committed
70
			int    max_bars     = static_cast<int>(_max_distance / (bar_speed * beat_state.avg_beat_time) + 1);
Kevin Balz's avatar
Kevin Balz committed
71
72
73
			auto   line_offset  = bar_speed * beat_state.avg_beat_time;
			auto   start_offset = (max_bars - _beats) * line_offset;
			ImGui::BeginChild("Line", {viewport.z, 90}, true);
74
			ImDrawList* drawList = ImGui::GetWindowDrawList();
Kevin Balz's avatar
Kevin Balz committed
75
76
77
78
79
80
81
82
83
84
85
			for(int i = 0; i < _beats; i++) {
				ImVec2 p(cursor.x + _max_distance - _offset - start_offset - i * line_offset, cursor.y + 10);
				drawList->AddLine(p, {p.x, p.y + 50}, 0xFFFFFFFF, 1);
			}

			for(int i = 0; i < _beats; i++) {
				ImVec2 p(cursor.x + viewport.z / 2 + _offset + start_offset + i * line_offset, cursor.y + 10);
				drawList->AddLine(p, {p.x, p.y + 50}, 0xFFFFFFFF, 1);
			}
			ImVec2 middle(cursor.x + viewport.z / 2, cursor.y);
			drawList->AddLine(middle, {middle.x, middle.y + 70}, 0xFFFF0000, 3);
86
			ImGui::EndChild();
Kevin Balz's avatar
Kevin Balz committed
87
88
			ImGui::PopStyleColor();

89
90

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

			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});
			}
97
98
99
100
101
102
103
		}

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

} // namespace phase_shifter::ui