hud_system.cpp 3.63 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
		constexpr auto hud_height      = 100;
17
18
		constexpr auto bar_height      = 80;
		constexpr auto beat_bar_height = 79;
Florian Oetke's avatar
Florian Oetke committed
19
		constexpr auto bar_speed       = 100;
20
		constexpr auto beat_bar_length = 600.0f;
Kevin Balz's avatar
Kevin Balz committed
21
	} // namespace
22

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

31
32
	void Hud_system::update(mirrage::util::Time dt) { 
		_passed += dt.value(); 
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
			ImVec2 cursor = ImGui::GetCursorScreenPos();
			cursor.y += hud_height / 2.f - 10;
66
67
68
69
70
			auto beat_state = _beat_system.beat_state();
			auto beats      = _beat_system.time_stamps();
			auto beat_index = _beat_system.beat_index() + 1;
			        //auto line_offset = bar_speed * beat_state.avg_beat_time;
			        //auto beats       = std::clamp(beat_state.beats_left, 0, 5);
Florian Oetke's avatar
Florian Oetke committed
71
			ImGui::BeginChild("Line", {viewport.z, bar_height}, true);
72
			ImDrawList* drawList = ImGui::GetWindowDrawList();
Kevin Balz's avatar
Kevin Balz committed
73
			
Kevin Balz's avatar
Kevin Balz committed
74
			while (beat_index < static_cast<int>(beats.size())) {
75
76
				auto x_offset = (beats[beat_index] - _passed) * bar_speed;
				auto height   = beat_bar_height * (1.f - glm::smoothstep(0.f, beat_bar_length, x_offset));
Kevin Balz's avatar
Kevin Balz committed
77
				auto bar_color = beat_index < static_cast<int>(beats.size() * 0.8f) ? 0xFFFFFFFF : 0xFF0000FF;
Kevin Balz's avatar
Kevin Balz committed
78

Florian Oetke's avatar
Florian Oetke committed
79
				ImVec2 p(cursor.x + viewport.z / 2 - x_offset, cursor.y - height / 2.f);
Kevin Balz's avatar
Kevin Balz committed
80
				drawList->AddLine(p, {p.x, p.y + height}, bar_color, 1);
Florian Oetke's avatar
Florian Oetke committed
81
82

				p.x = cursor.x + viewport.z / 2 + x_offset;
Kevin Balz's avatar
Kevin Balz committed
83
				drawList->AddLine(p, {p.x, p.y + height}, bar_color, 1);
84
85
86
87
88

				if(x_offset > beat_bar_length + 50) {
					break;
				}
				beat_index++;
Kevin Balz's avatar
Kevin Balz committed
89
			}
Florian Oetke's avatar
Florian Oetke committed
90
91
92
93

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

94
			ImGui::EndChild();
Kevin Balz's avatar
Kevin Balz committed
95
96
			ImGui::PopStyleColor();

97
98

			ImGui::End();
Kevin Balz's avatar
Kevin Balz committed
99
100
101
102
103
104

			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});
			}
105
106
107
108
109
110
111
		}

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

} // namespace phase_shifter::ui