Skip to content

Commit 63102ba

Browse files
committed
fix: allow render button to be pressed
this required some refactoring, each RequestAction EventHandleType returns a void*, that cAn contain any data, in the render case it's a reference to the selector, which has a recordings path
1 parent 80ec375 commit 63102ba

22 files changed

+172
-69
lines changed

src/game/game.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void Game::render(const ServiceProvider& service_provider) const {
7575
m_tetrion->render(service_provider);
7676
}
7777

78-
[[nodiscard]] helper::BoolWrapper<std::pair<ui::EventHandleType, ui::Widget*>>
78+
[[nodiscard]] ui::Widget::EventHandleResult
7979
Game::handle_event(const std::shared_ptr<input::InputManager>& /*input_manager*/, const SDL_Event& /*event*/) {
8080
return false;
8181
}

src/game/grid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void Grid::render(const ServiceProvider& service_provider) const {
4141
draw_playing_field_background(service_provider);
4242
}
4343

44-
[[nodiscard]] helper::BoolWrapper<std::pair<ui::EventHandleType, ui::Widget*>>
44+
[[nodiscard]] ui::Widget::EventHandleResult
4545
Grid::handle_event(const std::shared_ptr<input::InputManager>& /*input_manager*/, const SDL_Event& /*event*/) {
4646
return false;
4747
}

src/game/grid.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct Grid final : public ui::Widget {
3232

3333
OOPETRIS_GRAPHICS_EXPORTED void render(const ServiceProvider& service_provider) const override;
3434

35-
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] helper::BoolWrapper<std::pair<ui::EventHandleType, ui::Widget*>>
35+
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] Widget::EventHandleResult
3636
handle_event(const std::shared_ptr<input::InputManager>& input_manager, const SDL_Event& event) override;
3737

3838
private:

src/game/tetrion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void Tetrion::render(const ServiceProvider& service_provider) const {
110110
}
111111
}
112112

113-
[[nodiscard]] helper::BoolWrapper<std::pair<ui::EventHandleType, ui::Widget*>>
113+
[[nodiscard]] ui::Widget::EventHandleResult
114114
Tetrion::handle_event(const std::shared_ptr<input::InputManager>& /*input_manager*/, const SDL_Event& /*event*/) {
115115
return false;
116116
}

src/scenes/online_lobby/online_lobby.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ namespace scenes {
121121
if (const auto additional = event_result.get_additional(); additional.has_value()) {
122122
const auto value = additional.value();
123123

124-
if (value.first == ui::EventHandleType::RequestAction) {
124+
if (std::get<0>(value) == ui::EventHandleType::RequestAction) {
125125

126126

127-
if (auto text_input = utils::is_child_class<ui::TextInput>(value.second); text_input.has_value()) {
127+
if (auto text_input = utils::is_child_class<ui::TextInput>(std::get<1>(value));
128+
text_input.has_value()) {
128129
spdlog::info("Pressed Enter on TextInput {}", text_input.value()->get_text());
129130

130131
if (text_input.value()->has_focus()) {
@@ -138,7 +139,7 @@ namespace scenes {
138139
}
139140

140141
throw helper::FatalError(
141-
fmt::format("Unsupported Handle Type: {}", magic_enum::enum_name(additional->first))
142+
fmt::format("Unsupported Handle Type: {}", magic_enum::enum_name(std::get<0>(value)))
142143
);
143144
}
144145

src/scenes/recording_selector/recording_chooser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void custom_ui::RecordingFileChooser::render(const ServiceProvider& service_prov
9595
m_main_grid.render(service_provider);
9696
}
9797

98-
helper::BoolWrapper<std::pair<ui::EventHandleType, ui::Widget*>> custom_ui::RecordingFileChooser::handle_event(
98+
ui::Widget::EventHandleResult custom_ui::RecordingFileChooser::handle_event(
9999
const std::shared_ptr<input::InputManager>& input_manager,
100100
const SDL_Event& event
101101
) {

src/scenes/recording_selector/recording_component.cpp

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#include <fmt/format.h>
1212

13+
#if defined(_ENABLE_REPLAY_RENDERING)
14+
#include "graphics/video_renderer.hpp"
15+
#endif
1316

1417
custom_ui::RecordingComponent::RecordingComponent(
1518
ServiceProvider* service_provider,
@@ -24,7 +27,7 @@ custom_ui::RecordingComponent::RecordingComponent(
2427
ui::Direction::Horizontal,
2528
std::array<double, 1>{ 0.9 }, ui::RelativeMargin{layout.get_rect(), ui::Direction::Vertical,0.05}, std::pair<double, double>{ 0.05, 0.03 },
2629
layout,false
27-
},m_metadata{std::move(metadata)}{
30+
},m_metadata{std::move(metadata)},m_current_focus_id{m_main_layout.focus_id()}{
2831

2932

3033
auto text_layout_index = m_main_layout.add<ui::TileLayout>(
@@ -36,19 +39,26 @@ custom_ui::RecordingComponent::RecordingComponent(
3639
auto* text_layout = m_main_layout.get<ui::TileLayout>(text_layout_index);
3740

3841

39-
m_main_layout.add<ui::TextButton>(
42+
auto render_button_index = m_main_layout.add<ui::TextButton>(
4043
service_provider, "Render", service_provider->font_manager().get(FontId::Default), Color::white(),
41-
focus_helper.focus_id(),
42-
[](const ui::TextButton&) -> bool {
43-
//TODO: do rendering here, allow hover and click in this situation, it doesn't work as of now
44-
45-
return false;
46-
},
44+
focus_helper.focus_id(), [](const ui::TextButton&) -> bool { return false; },
4745
std::pair<double, double>{ 0.95, 0.85 },
4846
ui::Alignment{ ui::AlignmentHorizontal::Middle, ui::AlignmentVertical::Center },
4947
std::pair<double, double>{ 0.1, 0.1 }
5048
);
5149

50+
auto* render_button = m_main_layout.get<ui::TextButton>(render_button_index);
51+
52+
render_button->disable();
53+
54+
#if defined(_ENABLE_REPLAY_RENDERING)
55+
VideoRendererBackend::is_supported_async([render_button](bool is_supported) {
56+
if (is_supported) {
57+
render_button->enable();
58+
}
59+
});
60+
#endif
61+
5262
text_layout->add<ui::Label>(
5363
service_provider, "name: ?", service_provider->font_manager().get(FontId::Default), Color::white(),
5464
std::pair<double, double>{ 0.5, 0.5 },
@@ -96,24 +106,78 @@ void custom_ui::RecordingComponent::render(const ServiceProvider& service_provid
96106
m_main_layout.render(service_provider);
97107
}
98108

99-
helper::BoolWrapper<std::pair<ui::EventHandleType, ui::Widget*>> custom_ui::RecordingComponent::handle_event(
109+
ui::Widget::EventHandleResult custom_ui::RecordingComponent::handle_event(
100110
const std::shared_ptr<input::InputManager>& input_manager,
101111
const SDL_Event& event
102112
) {
103113

114+
auto* render_button = m_main_layout.get<ui::TextButton>(1);
115+
104116
if (has_focus() and input_manager->get_navigation_event(event) == input::NavigationEvent::OK) {
105-
return {
106-
true,
107-
{ ui::EventHandleType::RequestAction, this }
108-
};
117+
if (m_current_focus_id == m_main_layout.focus_id()) {
118+
return {
119+
true,
120+
{ ui::EventHandleType::RequestAction, this, nullptr }
121+
};
122+
}
123+
124+
if (m_current_focus_id == render_button->focus_id()) {
125+
return {
126+
true,
127+
{ ui::EventHandleType::RequestAction, render_button, nullptr }
128+
};
129+
}
130+
131+
spdlog::error("Recording selector has invalid focused element: {}", m_current_focus_id);
132+
}
133+
134+
if (has_focus()
135+
and (input_manager->get_navigation_event(event) == input::NavigationEvent::LEFT
136+
or input_manager->get_navigation_event(event) == input::NavigationEvent::RIGHT)) {
137+
138+
if (m_current_focus_id == m_main_layout.focus_id()) {
139+
m_current_focus_id = render_button->focus_id();
140+
return true;
141+
}
142+
143+
if (m_current_focus_id == render_button->focus_id()) {
144+
m_current_focus_id = m_main_layout.focus_id();
145+
return true;
146+
}
147+
148+
spdlog::error("Recording selector has invalid focused element: {}", m_current_focus_id);
109149
}
110150

111151

112152
if (const auto hover_result = detect_hover(input_manager, event); hover_result) {
153+
154+
155+
if (const auto render_button_hover_result = render_button->detect_hover(input_manager, event);
156+
render_button_hover_result) {
157+
158+
if (render_button_hover_result.is(ui::ActionType::Clicked)) {
159+
160+
if (not has_focus()) {
161+
return {
162+
true,
163+
{ ui::EventHandleType::RequestFocus, this, nullptr }
164+
};
165+
}
166+
167+
return {
168+
true,
169+
{ ui::EventHandleType::RequestAction, render_button, this }
170+
};
171+
}
172+
173+
return true;
174+
}
175+
113176
if (hover_result.is(ui::ActionType::Clicked)) {
177+
114178
return {
115179
true,
116-
{ has_focus() ? ui::EventHandleType::RequestAction : ui::EventHandleType::RequestFocus, this }
180+
{ has_focus() ? ui::EventHandleType::RequestAction : ui::EventHandleType::RequestFocus, this, nullptr }
117181
};
118182
}
119183
return true;

src/scenes/recording_selector/recording_component.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace custom_ui {
3535
private:
3636
ui::TileLayout m_main_layout;
3737
data::RecordingMetadata m_metadata;
38+
u32 m_current_focus_id;
3839

3940
public:
4041
OOPETRIS_GRAPHICS_EXPORTED explicit RecordingComponent(

src/scenes/recording_selector/recording_selector.cpp

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#include "recording_chooser.hpp"
77
#endif
88

9-
#include <recordings/utility/recording_reader.hpp>
10-
11-
#include "graphics/video_renderer.hpp"
129
#include "graphics/window.hpp"
1310
#include "helper/constants.hpp"
1411
#include "helper/graphic_utils.hpp"
@@ -20,10 +17,16 @@
2017
#include "ui/layout.hpp"
2118
#include "ui/layouts/scroll_layout.hpp"
2219
#include "ui/widget.hpp"
20+
#include <recordings/utility/recording_reader.hpp>
2321

2422
#include <filesystem>
2523
#include <stdexcept>
2624

25+
#if defined(_ENABLE_REPLAY_RENDERING)
26+
#include "graphics/video_renderer.hpp"
27+
#endif
28+
29+
2730
namespace scenes {
2831

2932
using namespace details::recording::selector; //NOLINT(google-build-using-namespace)
@@ -91,27 +94,50 @@ namespace scenes {
9194
// action is a reference to a structure inside m_next_command, so resetting it means, we need to copy everything out of it
9295
m_next_command = std::nullopt;
9396

97+
return UpdateResult{
98+
SceneUpdate::StopUpdating,
99+
Scene::RawSwitch{ "ReplayGame",
100+
std::make_unique<ReplayGame>(
101+
m_service_provider, ui::FullScreenLayout{ m_service_provider->window() },
102+
recording_path
103+
) }
104+
};
105+
}
106+
107+
108+
#if defined(_ENABLE_REPLAY_RENDERING)
109+
if (auto render_button = utils::is_child_class<ui::TextButton>(action.widget);
110+
render_button.has_value()) {
111+
112+
113+
auto recording_component = utils::is_child_class<custom_ui::RecordingComponent>(
114+
reinterpret_cast< //NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
115+
ui::Widget*>(action.data)
116+
);
117+
if (not recording_component.has_value()) {
118+
throw std::runtime_error(
119+
"Requested action on render button has invalid data, this is a fatal "
120+
"error"
121+
);
122+
}
123+
124+
const auto recording_path = recording_component.value()->metadata().path;
125+
94126
auto ren = VideoRenderer{
95127
m_service_provider, recording_path, shapes::UPoint{ 1280, 720 }
96128
};
97129

98130
//TODO: do this in a seperate thread
99131
ren.render("test.mp4", 60, [](double progress) {
100-
spdlog::info("Progress: {}", progress);
132+
// spdlog::info("Progress: {}", progress);
133+
UNUSED(progress);
101134
});
102135

103-
//TODO: do this in a seperate scene, with a loading bar
104136
return UpdateResult{ SceneUpdate::StopUpdating, std::nullopt };
105-
/*
106-
return UpdateResult{
107-
SceneUpdate::StopUpdating,
108-
Scene::RawSwitch{ "ReplayGame",
109-
std::make_unique<ReplayGame>(
110-
m_service_provider, ui::FullScreenLayout{ m_service_provider->window() },
111-
recording_path
112-
) }
113-
}; */
114137
}
138+
#endif
139+
140+
115141
#if defined(_HAVE_FILE_DIALOGS)
116142

117143
if (auto recording_file_chooser =
@@ -152,8 +178,10 @@ namespace scenes {
152178

153179
if (const auto event_result = m_main_layout.handle_event(input_manager, event); event_result) {
154180
if (const auto additional = event_result.get_additional();
155-
additional.has_value() and additional.value().first == ui::EventHandleType::RequestAction) {
156-
m_next_command = Command{ Action(additional.value().second) };
181+
additional.has_value() and std::get<0>(additional.value()) == ui::EventHandleType::RequestAction) {
182+
m_next_command = Command{
183+
Action{ .widget = std::get<1>(additional.value()), .data = std::get<2>(additional.value()) }
184+
};
157185
}
158186

159187
return true;

src/scenes/recording_selector/recording_selector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace details::recording::selector {
1313

1414
struct Action {
1515
ui::Widget* widget;
16+
void* data;
1617
};
1718

1819
struct Command {

0 commit comments

Comments
 (0)