cgv
Loading...
Searching...
No Matches
overlay.h
1#pragma once
2
3#include <cgv/data/informed_ptr.h>
4#include <cgv/defines/deprecated.h>
5#include <cgv/gui/event_handler.h>
6#include <cgv/gui/provider.h>
7#include <cgv/render/context.h>
8#include <cgv/render/drawable.h>
9#include <cgv_g2d/trect.h>
10#include <cgv_g2d/utils.h>
11
12#include <cgv/gui/key_event.h>
13#include <cgv/gui/mouse_event.h>
14
15#include "lib_begin.h"
16
17namespace cgv {
18namespace overlay {
19
20enum class Alignment {
21 kFree, // alignment solely controlled by margin
22 kStart, // left for horizontal, bottom for vertical direction
23 kCenter, // center of the reference area for both directions
24 kEnd, // right for horizontal, top for vertical direction
25 kPercentual, // alignment based on offset from lower left corner based on percentual size of reference area
26};
27
28enum class StretchMode {
29 kNone, // size is unchanged
30 kHorizontal, // width is stretched to cover reference area
31 kVertical, // height is stretched to cover reference area
32 kCover, // width and height are stretched to cover reference area
33 kPercentual, // width and height are scaled according to percentual size of reference area
34};
35
36class CGV_API overlay :
37 public cgv::base::node,
39 public cgv::gui::provider,
41{
42public:
44 std::string heading = "";
46 bool create_default_tree_node = true;
48 bool show_heading = false;
50 bool show_layout_options = true;
52 bool allow_alignment = true;
54 bool allow_stretch = true;
56 bool allow_margin = true;
57 };
60
62 virtual bool self_reflect(cgv::reflect::reflection_handler& _rh) { return false; }
63
65 virtual void stream_help(std::ostream& os) {};
66
68 virtual bool handle(cgv::gui::event& e);
69
71 virtual bool handle_key_event(cgv::gui::key_event& e) { return false; };
72
74 virtual bool handle_mouse_event(cgv::gui::mouse_event& e, cgv::ivec2 local_mouse_pos) { return false; };
75
78
80 virtual void on_set(void* member_ptr);
81
83 bool blocks_events() const { return blocks_events_; }
84
86 void blocks_events(bool flag) { blocks_events_ = flag; }
87
89 ivec2 get_viewport_size() const { return last_viewport_size_; }
90
92 virtual ivec2 get_local_mouse_pos(ivec2 mouse_pos) const;
93
95 cgv::g2d::irect get_rectangle() const { return container_; }
96
98 cgv::g2d::irect get_local_rectangle() const { return cgv::g2d::irect(ivec2(0), container_.size); }
99
101 Alignment get_horizontal_alignment() const { return horizontal_alignment_; }
102
104 Alignment get_vertical_alignment() const { return vertical_alignment_; }
105
107 vec2 get_percentual_offset() const { return percentual_offset_; }
108
110 void set_alignment(Alignment horizontal, Alignment vertical, vec2 percentual_offset = vec2(-1.0f));
111
113 StretchMode get_stretch_mode() const { return stretch_; }
114
116 vec2 get_percentual_size() const { return percentual_size_; }
117
119 void set_stretch_mode(StretchMode stretch, vec2 percentual_size = vec2(-1.0f));
120
122 ivec2 get_margin() const { return margin_; }
123
125 void set_margin(const ivec2& margin);
126
128 void set_size(const ivec2& size);
129
131 void set_visibility(bool visible);
132
134 void toggle_visibility();
135
140 bool ensure_viewport(cgv::render::context& ctx);
141
142 bool ensure_layout(cgv::render::context& ctx);
143
149 virtual bool is_hit(const ivec2& mouse_pos) const;
150
152 bool begin_overlay_gui();
154 void end_overlay_gui();
155
157 void create_layout_gui();
158
181 void create_gui();
182
183protected:
185 virtual void on_visibility_change();
186
188 virtual void on_layout_change();
189
191 void update_layout();
192
194 virtual void create_gui_impl() {};
195
196private:
198 ivec2 last_viewport_size_ = ivec2(-1);
200 ivec2 last_size_ = ivec2(-1);
202 ivec2 last_local_mouse_pos_ = ivec2(-1);
204 bool captured_mouse_ = false;
206 cgv::g2d::irect container_;
208 bool blocks_events_ = false;
209
211 Alignment horizontal_alignment_ = Alignment::kStart;
212 Alignment vertical_alignment_ = Alignment::kStart;
213 StretchMode stretch_ = StretchMode::kNone;
214 ivec2 margin_ = ivec2(0);
215 vec2 percentual_offset_ = vec2(0.0f);
216 vec2 percentual_size_ = vec2(1.0f);
217
218 template<typename T>
219 data::ref_ptr<cgv::gui::control<T>> add_layout_member_control(const std::string& label, T& value, const std::string& gui_type = "", const std::string& options = "", const std::string& align = "\n") {
220 data::ref_ptr<cgv::gui::control<T>> cp = add_control(label, value, gui_type, options, align);
221 if(cp)
222 connect_copy(cp->value_change, cgv::signal::rebind(this, &overlay::on_layout_change));
223 return cp;
224 }
225};
226
227typedef cgv::data::ref_ptr<overlay> overlay_ptr;
228
229} // namespace overlay
230} // namespace cgv
231
232#include <cgv/config/lib_end.h>
The node class keeps a pointer to its parent.
Definition node.h:19
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
interface for all classes that want to receive events
class to represent all possible keyboard events with the EID_KEY
Definition key_event.h:23
class to represent all possible mouse events with the EID_MOUSE
Definition mouse_event.h:33
derive from this class to provide a gui to the current viewer
Definition provider.h:64
static cgv::type::uint32_type size()
return number of components
Definition fvec.h:134
void blocks_events(bool flag)
set whether the overlay blocks events
Definition overlay.h:86
virtual void handle_member_change(cgv::data::informed_ptr ptr)
implement to handle member changes
Definition overlay.h:77
virtual void create_gui_impl()
virtual method to implement the derived class gui creation
Definition overlay.h:194
virtual void stream_help(std::ostream &os)
overload to stream help information to the given output stream
Definition overlay.h:65
vec2 get_percentual_offset() const
get the percentual alignment offset (only valid if get_horizontal_alignment() or get_vertical_alignme...
Definition overlay.h:107
vec2 get_percentual_size() const
get the percentual stretch (only valid if get_stretch_mode() returns StretchMode::kPercentual)
Definition overlay.h:116
cgv::g2d::irect get_local_rectangle() const
return the current rectangle area of the overlay in local space, i.e. with position set to zero
Definition overlay.h:98
bool blocks_events() const
return whether this overlay blocks events, i.e. does not pass them to the next event handler
Definition overlay.h:83
Alignment get_vertical_alignment() const
get the vertical alignment
Definition overlay.h:104
ivec2 get_margin() const
return the margin as set in the layout parameters
Definition overlay.h:122
virtual bool handle_mouse_event(cgv::gui::mouse_event &e, cgv::ivec2 local_mouse_pos)
overload this method to handle mouse events; local_mouse_pos is the mouse position in the local coord...
Definition overlay.h:74
cgv::g2d::irect get_rectangle() const
return the current rectangle area (in screen coordinates) of the overlay taking layout into account
Definition overlay.h:95
virtual bool self_reflect(cgv::reflect::reflection_handler &_rh)
overload to reflect members of derived classes
Definition overlay.h:62
StretchMode get_stretch_mode() const
get the stretch
Definition overlay.h:113
gui_options_t gui_options
options for the GUI creation of this overlay (must be set before GUI creation)
Definition overlay.h:59
virtual bool handle_key_event(cgv::gui::key_event &e)
overload this method to handle key events
Definition overlay.h:71
ivec2 get_viewport_size() const
return the current viewport size
Definition overlay.h:89
Alignment get_horizontal_alignment() const
get the horizontal alignment
Definition overlay.h:101
the self reflection handler is passed to the virtual self_reflect() method of cgv::base::base.
base class for all drawables, which is independent of the used rendering API.
Definition context.h:668
base class for all drawables, which is independent of the used rendering API.
Definition drawable.h:15
this header is dependency free
Definition print.h:11