cgv
Loading...
Searching...
No Matches
color_selector.h
1#pragma once
2
3#include <cgv/render/texture.h>
4#include <cgv_g2d/draggable_collection.h>
5#include <cgv_g2d/msdf_text_geometry.h>
6#include <cgv_g2d/shape2d_styles.h>
7#include <cgv_overlay/themed_canvas_overlay.h>
8
9#include "lib_begin.h"
10
11namespace cgv {
12namespace overlay {
13
14class CGV_API color_selector : public themed_canvas_overlay {
15protected:
17 int padding;
18 int size = 280;
19
20 // dependent members
21 cgv::g2d::irect border_rect;
22 cgv::g2d::irect color_rect;
23 cgv::g2d::irect hue_rect;
24 cgv::g2d::irect opacity_rect;
25 cgv::g2d::irect preview_rect;
26
27 cgv::g2d::irect hue_constraint;
28 cgv::g2d::irect opacity_constraint;
29 } layout;
30
31 struct selector_handle : public cgv::g2d::draggable {
32 vec2 val = vec2(0.0f);
33 bool is_rectangular = false;
34
36 size = vec2(10.0f);
37 position_is_center = true;
38 constraint_reference = cgv::g2d::ConstraintReference::kCenter;
39 }
40
41 void update_val() {
42 if(constraint) {
43 const cgv::g2d::irect& c = *constraint;
44 vec2 p = position - static_cast<vec2>(c.position);
45 ivec2 size = c.size;
46 val = p / static_cast<vec2>(size);
47 if(size.x() == 0) val.x() = 0.0f;
48 if(size.y() == 0) val.y() = 0.0f;
49 val = cgv::math::clamp(val, 0.0f, 1.0f);
50 }
51 }
52
53 void update_pos() {
54 if(constraint) {
55 const cgv::g2d::irect& c = *constraint;
56 val = cgv::math::clamp(val, 0.0f, 1.0f);
57 position = static_cast<vec2>(c.position) + val * c.size;
58 }
59 }
60
61 bool contains(const vec2& mp) const override {
62 if(is_rectangular) {
63 return draggable::contains(mp);
64 } else {
65 const auto dist = length(mp - center());
66 return dist <= 0.5f*size.x();
67 }
68 }
69 };
70
71 bool has_opacity = false;
72 bool auto_show = true;
73
74 cgv::g2d::msdf_text_geometry text_geometry;
75
76 cgv::g2d::shape2d_style border_style, color_texture_style, hue_texture_style, opacity_color_style, color_handle_style, hue_handle_style;
77 cgv::g2d::text2d_style text_style;
78 cgv::g2d::grid2d_style opacity_bg_style;
79
80 cgv::g2d::draggable_collection<selector_handle> selector_handles;
81
82 cgv::render::texture color_tex = { "uint8[R,G,B]", cgv::render::TF_LINEAR, cgv::render::TF_LINEAR };
83 cgv::render::texture hue_tex = { "uint8[R,G,B]", cgv::render::TF_LINEAR, cgv::render::TF_LINEAR };
84
85 rgb rgb_color = rgb(0.0f);
86 rgba rgba_color = rgba(0.0f);
87
88 void update_layout(const ivec2& parent_size);
89
90 void init_styles() override;
91 bool create_hue_texture(cgv::render::context& ctx);
92 bool create_color_texture(cgv::render::context& ctx);
93 void update_color();
94 void update_texts();
95
96 void handle_selector_drag(cgv::g2d::DragAction action);
97
98 void set_color(rgba color, bool opacity, bool init = false);
99
100 std::function<void(rgb)> on_change_rgb_callback;
101 std::function<void(rgba)> on_change_rgba_callback;
102
103 void create_gui_impl() override;
104
105public:
106 color_selector();
107 std::string get_type_name() const override { return "color_selector"; }
108
109 void clear(cgv::render::context& ctx) override;
110
111 bool handle_mouse_event(cgv::gui::mouse_event& e, cgv::ivec2 local_mouse_pos) override;
112 void handle_member_change(cgv::data::informed_ptr ptr) override;
113
114 bool init(cgv::render::context& ctx) override;
115 void init_frame(cgv::render::context& ctx) override;
116 void draw_content(cgv::render::context& ctx) override;
117
119 void set_auto_show(bool enabled) { auto_show = enabled; }
120
121 void set_rgb_color(rgb color);
122 void set_rgba_color(rgba color);
123 rgb get_rgb_color() const { return rgb_color; }
124 rgba get_rgba_color() const { return rgba_color; }
125
126 void set_on_change_rgb_callback(std::function<void(rgb)> cb) { on_change_rgb_callback = cb; }
127 void set_on_change_rgba_callback(std::function<void(rgba)> cb) { on_change_rgba_callback = cb; }
128};
129
130typedef cgv::data::ref_ptr<color_selector> color_selector_ptr;
131
132} // namespace overlay
133} // namespace cgv
134
135#include <cgv/config/lib_end.h>
This class provides methods to test if a stored pointer points to addresses of given variables or ins...
reference counted pointer, which can work together with types that are derived from ref_counted,...
Definition ref_ptr.h:160
class to represent all possible mouse events with the EID_MOUSE
Definition mouse_event.h:33
T & y()
return second component
Definition fvec.h:140
T & x()
return first component
Definition fvec.h:136
static cgv::type::uint32_type size()
return number of components
Definition fvec.h:134
std::string get_type_name() const override
overload to return the type name of this object
void set_auto_show(bool enabled)
whether to automatically make the overlay visible if a color is set
base class for all drawables, which is independent of the used rendering API.
Definition context.h:670
the texture class encapsulates all functionality independent of the rendering api.
Definition texture.h:15
this header is dependency free
Definition print.h:11