Skip to content

Commit c787c94

Browse files
committed
- fix OK click on ColorSettingRow
- fix small serialization error in color - add full TextInput support to ColorPicker - Add support for on_enter_pressed callback of TextInput - add method to set text of TextInput - add TextInputMode for TextInput, it can be Scroll or Scale, scale was added, scroll was the past standard behavior
1 parent f837d5c commit c787c94

File tree

6 files changed

+181
-57
lines changed

6 files changed

+181
-57
lines changed

src/helper/color.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ helper::expected<std::pair<HSVColor, color::SerializeMode>, std::string> HSVColo
4545
return fmt::format("hsva({:.2f}, {:.5f}, {:.5f}, {:#2x})", h, s, v, a);
4646
}
4747

48-
return fmt::format("({:.2f}, {:.5f}, {:.5f})", h, s, v);
48+
return fmt::format("hsv({:.2f}, {:.5f}, {:.5f})", h, s, v);
4949
}
5050

5151

src/scenes/online_lobby/online_lobby.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@ namespace scenes {
5050
ui::RelativeItemSize{ scroll_layout->layout(), 0.2 }, service_provider,
5151
service_provider->fonts().get(FontId::Symbola), Color::white(), focus_helper.focus_id(),
5252
std::pair<double, double>{ 0.9, 0.9 },
53-
ui::Alignment{ ui::AlignmentHorizontal::Middle, ui::AlignmentVertical::Center }
53+
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+
}
5458
);
5559
} else {
5660
scroll_layout->add<ui::TextButton>(
5761
ui::RelativeItemSize{ scroll_layout->layout(), 0.2 }, service_provider,
5862
fmt::format("Button Nr.: {}", i), service_provider->fonts().get(FontId::Default),
5963
Color::white(), focus_helper.focus_id(),
6064
[i](const ui::TextButton&) -> bool {
61-
std::cout << "Pressed button: " << i << "\n";
65+
spdlog::info("Pressed button: {}", i);
6266
return false;
6367
},
6468
std::pair<double, double>{ 0.8, 1.0 },

src/scenes/settings_menu/color_setting_row.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,20 @@ void custom_ui::ColorSettingRow::inner_callback(const Color& color) {
198198

199199

200200
void custom_ui::ColorSettingRow::on_focus() {
201+
if (not m_main_layout.has_focus()) {
202+
m_main_layout.focus();
203+
}
204+
201205
if (not color_rect()->has_focus()) {
202206
color_rect()->focus();
203207
}
204208
}
205209

206210
void custom_ui::ColorSettingRow::on_unfocus() {
211+
if (m_main_layout.has_focus()) {
212+
m_main_layout.unfocus();
213+
}
214+
207215
if (color_rect()->has_focus()) {
208216
color_rect()->unfocus();
209217
}

src/ui/components/color_picker.cpp

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "ui/components/textinput.hpp"
1111
#include "ui/layout.hpp"
1212
#include "ui/widget.hpp"
13+
#include <stdexcept>
1314

1415
detail::ColorSlider::ColorSlider(
1516
ServiceProvider* service_provider,
@@ -284,6 +285,8 @@ ui::ColorPicker::ColorPicker(
284285
m_mode{ ColorMode::RGB },
285286
m_callback{ std::move(callback) } {
286287

288+
//TODO: add alpha slider at the side
289+
287290
constexpr double main_rect_height = 0.8;
288291

289292
const auto main_fill_layout = ui::RelativeLayout{ fill_rect, 0.0, 0.0, 1.0, main_rect_height };
@@ -375,7 +378,7 @@ ui::ColorPicker::ColorPicker(
375378
this->after_color_mode_change();
376379
return false;
377380
},
378-
std::pair<double, double>{ 1.0, 1.0 },
381+
std::pair<double, double>{ 0.95, 0.95 },
379382
ui::Alignment{ ui::AlignmentHorizontal::Middle, ui::AlignmentVertical::Center }, toggle_button_layout, false
380383
);
381384

@@ -388,7 +391,7 @@ ui::ColorPicker::ColorPicker(
388391
this->after_color_mode_change();
389392
return false;
390393
},
391-
std::pair<double, double>{ 1.0, 1.0 },
394+
std::pair<double, double>{ 0.95, 0.95 },
392395
ui::Alignment{ ui::AlignmentHorizontal::Middle, ui::AlignmentVertical::Center }, toggle_button_layout, false
393396
);
394397

@@ -399,7 +402,23 @@ ui::ColorPicker::ColorPicker(
399402
m_color_text = std::make_unique<ui::TextInput>(
400403
service_provider, service_provider->fonts().get(FontId::Default), Color::white(), focus_id_unused,
401404
std::pair<double, double>{ 0.9, 0.9 },
402-
ui::Alignment{ ui::AlignmentHorizontal::Middle, ui::AlignmentVertical::Center }, textinput_layout, false
405+
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+
},
421+
textinput_layout, false
403422
);
404423

405424
// using SLider, that behaviour simulates the one, that I want, since the ColorSlider already gets the change by the getter,
@@ -472,7 +491,33 @@ ui::ColorPicker::handle_event(const SDL_Event& event, const Window* window) {
472491
return handled;
473492
}
474493

475-
return m_color_text->handle_event(event, window);
494+
handled = m_color_text->handle_event(event, window);
495+
496+
if (handled) {
497+
if (const auto additional = handled.get_additional(); additional.has_value()) {
498+
switch (additional.value().first) {
499+
case ui::EventHandleType::RequestFocus:
500+
if (not m_color_text->has_focus()) {
501+
m_color_text->focus();
502+
}
503+
break;
504+
case ui::EventHandleType::RequestUnFocus:
505+
if (m_color_text->has_focus()) {
506+
m_color_text->unfocus();
507+
}
508+
break;
509+
case ui::EventHandleType::RequestAction:
510+
//TODO use instead of callback
511+
throw std::runtime_error("TODO");
512+
default:
513+
utils::unreachable();
514+
}
515+
}
516+
517+
return handled;
518+
}
519+
520+
return false;
476521
}
477522

478523
void ui::ColorPicker::after_color_change(detail::ColorChangeOrigin origin, const HSVColor& color) {
@@ -484,13 +529,13 @@ void ui::ColorPicker::after_color_change(detail::ColorChangeOrigin origin, const
484529
break;
485530
}
486531
case detail::ColorChangeOrigin::Canvas: {
487-
//TODO: change the text in the textinput!
532+
after_color_mode_change();
488533
break;
489534
}
490535
case detail::ColorChangeOrigin::Slider: {
491536
m_color_canvas->on_change(origin, color);
492537

493-
//TODO: change the text in the textinput!
538+
after_color_mode_change();
494539
break;
495540
}
496541
default:
@@ -501,6 +546,8 @@ void ui::ColorPicker::after_color_change(detail::ColorChangeOrigin origin, const
501546
}
502547

503548
void ui::ColorPicker::after_color_mode_change() {
504-
//TODO
505-
//TODO: handle textinput chnages and events and also change it's value every time the color is changed
549+
const std::string text =
550+
m_color.to_string(m_mode == ColorMode::HSV ? color::SerializeMode::HSV : color::SerializeMode::RGB, false);
551+
552+
m_color_text->set_text(text);
506553
}

0 commit comments

Comments
 (0)