Skip to content

Commit c2a920d

Browse files
committed
use the ui::EventHandleType::RequestAction method instead of an callback for the enter press of the textinput
1 parent c787c94 commit c2a920d

File tree

5 files changed

+61
-35
lines changed

5 files changed

+61
-35
lines changed

src/scenes/online_lobby/online_lobby.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "online_lobby.hpp"
22
#include "graphics/window.hpp"
33
#include "helper/constants.hpp"
4+
#include "helper/errors.hpp"
45
#include "manager/music_manager.hpp"
56
#include "manager/resource_manager.hpp"
67
#include "ui/components/textinput.hpp"
@@ -51,10 +52,7 @@ namespace scenes {
5152
service_provider->fonts().get(FontId::Symbola), Color::white(), focus_helper.focus_id(),
5253
std::pair<double, double>{ 0.9, 0.9 },
5354
ui::Alignment{ ui::AlignmentHorizontal::Middle, ui::AlignmentVertical::Center },
54-
ui::TextInputMode::Scroll,
55-
[i](const std::string& value) -> void {
56-
spdlog::info("Pressed Enter on TextInput {}: {}", i, value);
57-
}
55+
ui::TextInputMode::Scroll
5856
);
5957
} else {
6058
scroll_layout->add<ui::TextButton>(
@@ -121,7 +119,32 @@ namespace scenes {
121119
// description of intentional behaviour of this scene, even if it seems off:
122120
// the return button or the scroll layout can have the focus, if the scroll_layout has the focus, it can be scrolled by the scroll wheel and you can move around the focused item of the scroll_layout with up and down, but not with TAB, with tab you can change the focus to the return button, where you can't use the scroll wheel or up / down to change the scroll items, but you still can use click events, they are not affected by focus
123121

124-
if (m_main_layout.handle_event(event, window)) {
122+
if (const auto event_result = m_main_layout.handle_event(event, window)) {
123+
124+
if (const auto additional = event_result.get_additional(); additional.has_value()) {
125+
const auto value = additional.value();
126+
127+
if (value.first == ui::EventHandleType::RequestAction) {
128+
129+
130+
if (auto* text_input = dynamic_cast<ui::TextInput*>(value.second); text_input != nullptr) {
131+
spdlog::info("Pressed Enter on TextInput {}", text_input->get_text());
132+
133+
if (text_input->has_focus()) {
134+
text_input->unfocus();
135+
}
136+
return true;
137+
}
138+
139+
140+
throw std::runtime_error("Requested action on unknown widget, this is a fatal error");
141+
}
142+
143+
throw helper::FatalError(
144+
fmt::format("Unsupported Handle Type: {}", magic_enum::enum_name(additional->first))
145+
);
146+
}
147+
125148
return true;
126149
}
127150

src/scenes/settings_menu/color_setting_row.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

22
#include "color_setting_row.hpp"
3+
#include "helper/errors.hpp"
34
#include "helper/utils.hpp"
45
#include "ui/components/label.hpp"
56
#include "ui/focusable.hpp"
67
#include "ui/hoverable.hpp"
78
#include "ui/layout.hpp"
89
#include "ui/widget.hpp"
10+
911
#include <functional>
1012

1113
detail::ColorSettingRectangle::ColorSettingRectangle(
@@ -168,6 +170,8 @@ custom_ui::ColorSettingRow::handle_event(const SDL_Event& event, const Window* w
168170
{ui::EventHandleType::RequestAction, this}
169171
};
170172
}
173+
174+
throw helper::FatalError(fmt::format("Unsupported Handle Type: {}", magic_enum::enum_name(additional->first)));
171175
}
172176

173177
return result;

src/ui/components/color_picker.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -403,21 +403,6 @@ ui::ColorPicker::ColorPicker(
403403
service_provider, service_provider->fonts().get(FontId::Default), Color::white(), focus_id_unused,
404404
std::pair<double, double>{ 0.9, 0.9 },
405405
ui::Alignment{ ui::AlignmentHorizontal::Middle, ui::AlignmentVertical::Center }, ui::TextInputMode::Scale,
406-
[this](const std::string& value) -> void {
407-
const auto maybe_color = HSVColor::from_string(value);
408-
409-
if (not maybe_color.has_value()) {
410-
//TODO: maybe inform the user, that the input is incorrect?
411-
//m_color_text->display_error();
412-
413-
return;
414-
}
415-
416-
const auto color = maybe_color.value();
417-
m_color = Color{ color };
418-
419-
after_color_change(detail::ColorChangeOrigin::TextInput, color);
420-
},
421406
textinput_layout, false
422407
);
423408

@@ -506,9 +491,27 @@ ui::ColorPicker::handle_event(const SDL_Event& event, const Window* window) {
506491
m_color_text->unfocus();
507492
}
508493
break;
509-
case ui::EventHandleType::RequestAction:
510-
//TODO use instead of callback
511-
throw std::runtime_error("TODO");
494+
case ui::EventHandleType::RequestAction: {
495+
const auto maybe_color = HSVColor::from_string(m_color_text->get_text());
496+
497+
if (m_color_text->has_focus()) {
498+
m_color_text->unfocus();
499+
}
500+
501+
if (not maybe_color.has_value()) {
502+
//TODO: maybe inform the user, that the input is incorrect?
503+
//m_color_text->display_error();
504+
505+
break;
506+
}
507+
508+
const auto color = maybe_color.value();
509+
m_color = Color{ color };
510+
511+
after_color_change(detail::ColorChangeOrigin::TextInput, color);
512+
513+
break;
514+
}
512515
default:
513516
utils::unreachable();
514517
}

src/ui/components/textinput.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ ui::TextInput::TextInput(
1717
u32 focus_id,
1818
const shapes::URect& fill_rect,
1919
TextInputMode mode,
20-
EnterCallback enter_callback,
2120
const Layout& layout,
2221
bool is_top_level
2322
)
@@ -33,8 +32,7 @@ ui::TextInput::TextInput(
3332
) },
3433
m_scaled_text_size{ 0 },
3534
m_timer{ [this]() { this->m_cursor_shown = !this->m_cursor_shown; }, 500ms },
36-
m_mode{ mode },
37-
m_enter_callback{ std::move(enter_callback) } {
35+
m_mode{ mode } {
3836
recalculate_textures(false);
3937

4038
// if on top. we give us focus automatically
@@ -51,7 +49,6 @@ ui::TextInput::TextInput(
5149
std::pair<double, double> size,
5250
Alignment alignment,
5351
TextInputMode mode,
54-
EnterCallback enter_callback,
5552
const Layout& layout,
5653
bool is_top_level
5754
)
@@ -66,7 +63,6 @@ ui::TextInput::TextInput(
6663
alignment
6764
),
6865
mode,
69-
std::move(enter_callback),
7066
layout,
7167
is_top_level } { }
7268

@@ -127,11 +123,10 @@ ui::TextInput::handle_event( // NOLINT(readability-function-cognitive-complexity
127123
case SDL_KEYDOWN: {
128124
switch (event.key.keysym.sym) {
129125
case SDLK_RETURN: {
130-
m_enter_callback(m_text);
131126
on_unfocus();
132127
return {
133128
true,
134-
{EventHandleType::RequestUnFocus, this}
129+
{EventHandleType::RequestAction, this}
135130
};
136131
}
137132
case SDLK_BACKSPACE: {
@@ -247,6 +242,10 @@ void ui::TextInput::set_text(const std::string& text) {
247242
}
248243
}
249244

245+
[[nodiscard]] const std::string& ui::TextInput::get_text() const {
246+
return m_text;
247+
}
248+
250249
void ui::TextInput::recalculate_textures(bool text_changed) {
251250

252251
const auto& renderer = m_service_provider->renderer();

src/ui/components/textinput.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ namespace ui {
1515
enum class TextInputMode { Scroll, Scale };
1616

1717
struct TextInput final : public Widget, public Focusable, public Hoverable {
18-
using EnterCallback = std::function<void(const std::string& value)>;
19-
2018
private:
2119
std::string m_text{};
2220
u32 m_cursor_position{ 0 };
@@ -31,7 +29,6 @@ namespace ui {
3129
bool m_cursor_shown{ true };
3230
helper::Timer m_timer;
3331
TextInputMode m_mode;
34-
EnterCallback m_enter_callback;
3532

3633
explicit TextInput(
3734
ServiceProvider* service_provider,
@@ -40,7 +37,6 @@ namespace ui {
4037
u32 focus_id,
4138
const shapes::URect& fill_rect,
4239
TextInputMode mode,
43-
EnterCallback enter_callback,
4440
const Layout& layout,
4541
bool is_top_level
4642
);
@@ -54,7 +50,6 @@ namespace ui {
5450
std::pair<double, double> size,
5551
Alignment alignment,
5652
TextInputMode mode,
57-
EnterCallback enter_callback,
5853
const Layout& layout,
5954
bool is_top_level
6055
);
@@ -73,6 +68,8 @@ namespace ui {
7368

7469
void set_text(const std::string& text);
7570

71+
[[nodiscard]] const std::string& get_text() const;
72+
7673
private:
7774
void recalculate_textures(bool text_changed);
7875

0 commit comments

Comments
 (0)