cgv
Loading...
Searching...
No Matches
color_selector.h
1#pragma once
2
3#include <cgv/render/texture.h>
4#include <cgv_app/themed_canvas_overlay.h>
5#include <cgv_g2d/draggable_collection.h>
6#include <cgv_g2d/msdf_text_geometry.h>
7#include <cgv_g2d/shape2d_styles.h>
8
9#include "lib_begin.h"
10
11namespace cgv {
12namespace app {
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 = CR_CENTER;
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 std::vector<std::string> texts;
75 cgv::g2d::msdf_text_geometry text_geometry;
76
77 cgv::g2d::shape2d_style border_style, color_texture_style, hue_texture_style, opacity_color_style, color_handle_style, hue_handle_style;
78 cgv::g2d::text2d_style text_style;
79 cgv::g2d::grid2d_style opacity_bg_style;
80
81 cgv::g2d::draggable_collection<selector_handle> selector_handles;
82
83 cgv::render::texture color_tex;
85
86 rgb rgb_color = rgb(0.0f);
87 rgba rgba_color = rgba(0.0f);
88
89 void update_layout(const ivec2& parent_size);
90
91 void init_styles() override;
92 void init_textures(cgv::render::context& ctx);
93 void update_color_texture();
94 void update_color();
95 void update_texts();
96
97 void handle_selector_drag(cgv::g2d::DragAction action);
98
99 void set_color(rgba color, bool opacity, bool init = false);
100
101 std::function<void(rgb)> on_change_rgb_callback;
102 std::function<void(rgba)> on_change_rgba_callback;
103
104 void create_gui_impl() override;
105
106public:
108 std::string get_type_name() const override { return "color_selector"; }
109
110 void clear(cgv::render::context& ctx) override;
111
112 bool handle_mouse_event(cgv::gui::mouse_event& e, cgv::ivec2 local_mouse_pos) override;
113 void handle_member_change(const cgv::utils::pointer_test& m) override;
114
115 bool init(cgv::render::context& ctx) override;
116 void init_frame(cgv::render::context& ctx) override;
117 void draw_content(cgv::render::context& ctx) override;
118
120 void set_auto_show(bool enabled) { auto_show = enabled; }
121
122 void set_rgb_color(rgb color);
123 void set_rgba_color(rgba color);
124 rgb get_rgb_color() const { return rgb_color; }
125 rgba get_rgba_color() const { return rgba_color; }
126
127 void set_on_change_rgb_callback(std::function<void(rgb)> cb) { on_change_rgb_callback = cb; }
128 void set_on_change_rgba_callback(std::function<void(rgba)> cb) { on_change_rgba_callback = cb; }
129};
130
131typedef cgv::data::ref_ptr<color_selector> color_selector_ptr;
132
133}
134}
135
136#include <cgv/config/lib_end.h>
std::string get_type_name() const override
overload to return the type name of this object. By default the type interface is queried over get_ty...
void set_auto_show(bool enabled)
whether to automatically make the overlay visible if a color is set
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()
second element
Definition fvec.h:159
T & x()
first element
Definition fvec.h:155
static cgv::type::uint32_type size()
return number of elements
Definition fvec.h:179
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
the texture class encapsulates all functionality independent of the rendering api.
Definition texture.h:15
the cgv namespace
Definition print.h:11