Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_color_picker.hpp
1#pragma once
2
3#include "structure/design_pattern/spk_observable_value.hpp"
4#include "structure/graphics/painter/spk_color_painter.hpp"
5#include "structure/graphics/spk_color.hpp"
6#include "structure/math/spk_vector2.hpp"
7#include "structure/system/event/spk_update_event.hpp"
8#include "structure/widget/spk_frame.hpp"
9#include "structure/widget/spk_grid_layout.hpp"
10#include "structure/widget/spk_linear_layout.hpp"
11#include "structure/widget/spk_slider.hpp"
12#include "structure/widget/spk_text_edit.hpp"
13#include "structure/widget/spk_widget.hpp"
14
15#include <array>
16#include <cstdint>
17#include <string>
18
19namespace spk
20{
31 class ColorPicker : public spk::Widget
32 {
33 private:
34 class ColorPreview : public spk::Widget
35 {
36 private:
37 spk::Color _color{};
38 spk::ColorPainter _painter;
39 spk::ColorMesh _mesh;
40 spk::ColorMesh::Builder _meshBuilder;
41 spk::Vector2UInt _minimalSize{};
42
43 void _rebuildMesh();
44
45 protected:
46 void _onGeometryChange() override;
47 void _onPaintEvent(spk::PaintEvent &p_event) override;
48
49 public:
50 ColorPreview(const std::wstring &p_name, spk::Widget *p_parent);
51
52 void setColor(const spk::Color &p_color);
53 const spk::Color &color() const;
54
55 void setMinimalSize(const spk::Vector2UInt &p_minimalSize);
56 const spk::Vector2UInt &minimalSize() const;
57 };
58
59 class ComponentLine
60 {
61 private:
62 spk::TextEdit _textEdit;
63 spk::Slider _slider;
64 spk::Vector2UInt _textEditMinimalSize{};
65 uint32_t _sliderMinimalWidth = 0;
66
67 public:
68 ComponentLine(const std::wstring &p_name, spk::Widget *p_parent);
69
70 spk::TextEdit &textEdit();
71 const spk::TextEdit &textEdit() const;
72
73 spk::Slider &slider();
74 const spk::Slider &slider() const;
75
76 void configureTextEditMinimalSize();
77 const spk::Vector2UInt &textEditMinimalSize() const;
78
79 void setSliderMinimalWidth(uint32_t p_width);
80 uint32_t sliderMinimalWidth() const;
81 };
82
83 spk::HorizontalLayout _mainLayout;
84 spk::GridLayout _componentsLayout;
85
86 spk::Frame _colorFrame;
87 ColorPreview _colorPreview;
88 ComponentLine _redLine;
89 ComponentLine _greenLine;
90 ComponentLine _blueLine;
91 ComponentLine _alphaLine;
92
93 std::array<spk::ObservableValue<float>::Contract, 4> _sliderContracts;
94
95 spk::Color _color{0, 0, 0, 255};
96 spk::Vector2UInt _colorOutputMinimalSize{32, 32};
97 uint32_t _sliderMinimalWidth{120};
98 spk::Vector2UInt _componentTextEditMinimalSize{};
99 bool _isSyncing = false;
100
101 static int _componentValueFromRatio(float p_value);
102 static float _ratioFromComponentValue(int p_value);
103 static bool _parseComponentText(const std::wstring &p_text, int &p_value);
104 static std::wstring _trimmed(const std::wstring &p_text);
105
106 static int _componentValue(const spk::Color &p_color, size_t p_index);
107 void _setComponentValue(size_t p_index, int p_value);
108 ComponentLine &_line(size_t p_index);
109 const ComponentLine &_line(size_t p_index) const;
110
111 void _syncComponentLine(size_t p_index);
112 void _syncAllComponentLines();
113 void _handleSliderChange(size_t p_index);
114 void _ensureComponentTextEditSizes();
115 spk::Vector2UInt _computeComponentTextEditMinimalSize(const spk::TextEdit &p_edit) const;
116 spk::Vector2UInt _colorFrameMinimalSize() const;
117 void _refreshColorFrameSize();
118 void _refreshLayoutSizeHints();
119
120 protected:
124 void _onGeometryChange() override;
129 void _onUpdateEvent(spk::UpdateEvent &p_event) override;
130
131 public:
137 ColorPicker(const std::wstring &p_name, spk::Widget *p_parent);
138
143 void setColor(const spk::Color &p_color);
148 const spk::Color &color() const;
149
154 void setColorOutputMinimalSize(const spk::Vector2UInt &p_minimalSize);
159 const spk::Vector2UInt &colorOutputMinimalSize() const;
160
165 void setComponentSliderMinimalWidth(uint32_t p_width);
170 uint32_t componentSliderMinimalWidth() const;
171 };
172}
Helper used to build ColorMesh instances from primitives.
Definition spk_color_painter.hpp:63
Mesh specialization for colored vertices.
Definition spk_color_painter.hpp:41
Painter that renders ColorMesh instances.
Definition spk_color_painter.hpp:169
const spk::Color & color() const
Returns the current color.
Definition spk_color_picker.cpp:220
ColorPicker(const std::wstring &p_name, spk::Widget *p_parent)
Builds a color picker widget.
Definition spk_color_picker.cpp:154
void setColor(const spk::Color &p_color)
Sets the current color.
Definition spk_color_picker.cpp:209
const spk::Vector2UInt & colorOutputMinimalSize() const
Returns the minimal size of the color preview output.
Definition spk_color_picker.cpp:239
void _onGeometryChange() override
Updates layout and geometry when the widget size changes.
Definition spk_color_picker.cpp:492
void _onUpdateEvent(spk::UpdateEvent &p_event) override
Handles periodic updates for syncing UI state.
Definition spk_color_picker.cpp:500
void setColorOutputMinimalSize(const spk::Vector2UInt &p_minimalSize)
Sets the minimal size of the color preview output.
Definition spk_color_picker.cpp:225
void setComponentSliderMinimalWidth(uint32_t p_width)
Sets the minimal width for component sliders.
Definition spk_color_picker.cpp:244
uint32_t componentSliderMinimalWidth() const
Returns the minimal width for component sliders.
Definition spk_color_picker.cpp:259
Widget frame rendered using a nine-slice background.
Definition spk_frame.hpp:19
Layout arranging elements in a grid by cell position.
Definition spk_grid_layout.hpp:27
Slider widget for selecting a ratio within [0,1].
Definition spk_slider.hpp:25
Editable text field with validation and cursor handling.
Definition spk_text_edit.hpp:29
Base class for interactive UI elements handling focus and events.
Definition spk_widget.hpp:52
virtual void _onGeometryChange()
Notifies derived widgets that the view region changed.
Definition spk_widget.cpp:292
virtual void _onPaintEvent(spk::PaintEvent &p_event)
Paint event hook for derived widgets.
Definition spk_widget.cpp:220
RGBA color utility storing normalized floating components.
Definition spk_color.hpp:18
Event emitted when a drawable region needs repainting.
Definition spk_paint_event.hpp:19
Carries per-frame update timing and input state snapshot.
Definition spk_update_event.hpp:15