1010#include " ui/components/textinput.hpp"
1111#include " ui/layout.hpp"
1212#include " ui/widget.hpp"
13+ #include < stdexcept>
1314
1415detail::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
478523void 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
503548void 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