1 | // Aseprite |
2 | // Copyright (c) 2018 Igara Studio S.A. |
3 | // Copyright (C) 2001-2018 David Capello |
4 | // |
5 | // This program is distributed under the terms of |
6 | // the End-User License Agreement for Aseprite. |
7 | |
8 | #ifndef APP_UI_COLOR_SLIDERS_H_INCLUDED |
9 | #define APP_UI_COLOR_SLIDERS_H_INCLUDED |
10 | #pragma once |
11 | |
12 | #include "app/color.h" |
13 | #include "obs/connection.h" |
14 | #include "obs/signal.h" |
15 | #include "ui/event.h" |
16 | #include "ui/grid.h" |
17 | #include "ui/widget.h" |
18 | |
19 | #include <vector> |
20 | |
21 | namespace ui { |
22 | class Box; |
23 | class Label; |
24 | class Slider; |
25 | class Entry; |
26 | } |
27 | |
28 | namespace app { |
29 | |
30 | class ColorSlidersChangeEvent; |
31 | |
32 | class ColorSliders : public ui::Widget { |
33 | public: |
34 | enum Channel { Red, Green, Blue, |
35 | HsvHue, HsvSaturation, HsvValue, |
36 | HslHue, HslSaturation, HslLightness, |
37 | Gray, Alpha, |
38 | Channels }; |
39 | enum Mode { Absolute, Relative }; |
40 | |
41 | ColorSliders(); |
42 | |
43 | void setColor(const app::Color& color); |
44 | void setColorType(const app::Color::Type type); |
45 | void setColorTypes(const std::vector<app::Color::Type>& types); |
46 | void setMode(Mode mode); |
47 | void resetRelativeSliders(); |
48 | |
49 | int getAbsSliderValue(const Channel i) const; |
50 | int getRelSliderValue(const Channel i) const; |
51 | void syncRelHsvHslSliders(); |
52 | |
53 | // Signals |
54 | obs::signal<void(ColorSlidersChangeEvent&)> ColorChange; |
55 | |
56 | private: |
57 | void onSizeHint(ui::SizeHintEvent& ev) override; |
58 | |
59 | // For derived classes |
60 | void addSlider(const Channel channel, |
61 | const char* labelText, |
62 | const int absMin, const int absMax, |
63 | const int relMin, const int relMax); |
64 | void setAbsSliderValue(const Channel i, int value); |
65 | void setRelSliderValue(const Channel i, int value); |
66 | |
67 | void updateSlidersVisibility(); |
68 | void onSetColor(const app::Color& color); |
69 | app::Color getColorFromSliders(const Channel channel) const; |
70 | void onSliderChange(const Channel i); |
71 | void onEntryChange(const Channel i); |
72 | void onControlChange(const Channel i); |
73 | |
74 | void updateEntryText(const Channel i); |
75 | void updateSlidersBgColor(); |
76 | void updateSliderBgColor(ui::Slider* slider, const app::Color& color); |
77 | |
78 | struct Item { |
79 | bool show = false; |
80 | ui::Label* label = nullptr; |
81 | ui::Box* box = nullptr; |
82 | ui::Slider* absSlider = nullptr; |
83 | ui::Slider* relSlider = nullptr; |
84 | ui::Entry* entry = nullptr; |
85 | }; |
86 | |
87 | std::vector<Item> m_items; |
88 | ui::Grid m_grid; |
89 | Mode m_mode; |
90 | int m_lockSlider; |
91 | int m_lockEntry; |
92 | app::Color m_color; |
93 | obs::scoped_connection m_appConn; |
94 | }; |
95 | |
96 | ////////////////////////////////////////////////////////////////////// |
97 | // Events |
98 | |
99 | class ColorSlidersChangeEvent : public ui::Event { |
100 | public: |
101 | ColorSlidersChangeEvent(ColorSliders::Channel channel, |
102 | ColorSliders::Mode mode, |
103 | const app::Color& color, |
104 | int delta, |
105 | ui::Component* source) |
106 | : Event(source) |
107 | , m_channel(channel) |
108 | , m_mode(mode) |
109 | , m_color(color) |
110 | , m_delta(delta) { |
111 | } |
112 | |
113 | ColorSliders::Channel channel() const { return m_channel; } |
114 | ColorSliders::Mode mode() const { return m_mode; } |
115 | app::Color color() const { return m_color; } |
116 | int delta() const { return m_delta; } |
117 | |
118 | private: |
119 | ColorSliders::Channel m_channel; |
120 | ColorSliders::Mode m_mode; |
121 | app::Color m_color; |
122 | int m_delta; |
123 | }; |
124 | |
125 | } // namespace app |
126 | |
127 | #endif |
128 | |