cgv
Loading...
Searching...
No Matches
overlay.h
1#pragma once
2
3#include <cgv/defines/deprecated.h>
4#include <cgv/gui/event_handler.h>
5#include <cgv/gui/provider.h>
6#include <cgv/render/context.h>
7#include <cgv/render/drawable.h>
8#include <cgv/utils/pointer_test.h>
9#include <cgv_g2d/trect.h>
10#include <cgv_g2d/utils2d.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 app {
19
20class CGV_API overlay :
21 public cgv::base::node,
23 public cgv::gui::provider,
25{
26public:
27 enum AlignmentOption {
28 AO_FREE, // alignment solely controlled by margin
29 AO_START, // left for horizontal, bottom for vertical direction
30 AO_CENTER, // center of the viewport for both directions
31 AO_END, // right for horizontal, top for vertical direction
32 AO_PERCENTUAL, // alignment based on offset from lower left corner based on percentual size of viewport
33 };
34
35 enum StretchOption {
36 SO_NONE, // size is unchanged
37 SO_HORIZONTAL, // width is stretched to cover viewport
38 SO_VERTICAL, // height is stretched to cover viewport
39 SO_BOTH, // width and height are stretched to cover viewport
40 SO_PERCENTUAL, // width and height are scaled according to percentual size of viewport
41 };
42
43private:
45 ivec2 last_viewport_size_ = ivec2(-1);
47 ivec2 last_size_ = ivec2(-1);
49 ivec2 last_local_mouse_pos_ = ivec2(-1);
51 bool captured_mouse_ = false;
53 cgv::g2d::irect container_;
55 bool blocks_events_ = false;
56
58 AlignmentOption horizontal_alignment_ = AlignmentOption::AO_START;
59 AlignmentOption vertical_alignment_ = AlignmentOption::AO_START;
60 StretchOption stretch_ = SO_NONE;
61 ivec2 margin_ = ivec2(0);
62 vec2 percentual_offset_ = vec2(0.0f);
63 vec2 percentual_size_ = vec2(1.0f);
64
65 template<typename T>
66 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") {
67 data::ref_ptr<cgv::gui::control<T>> cp = add_control(label, value, gui_type, options, align);
68 if(cp)
69 connect_copy(cp->value_change, cgv::signal::rebind(this, &overlay::on_layout_change));
70 return cp;
71 }
72
73protected:
75 virtual void on_visibility_change();
76
78 virtual void on_layout_change();
79
81 void update_layout();
82
84 virtual void create_gui_impl() {};
85
86public:
88 std::string heading = "";
90 bool create_default_tree_node = true;
92 bool show_heading = false;
94 bool show_layout_options = true;
96 bool allow_alignment = true;
98 bool allow_stretch = true;
100 bool allow_margin = true;
101 };
104
106 virtual bool self_reflect(cgv::reflect::reflection_handler& _rh) { return false; }
107
109 virtual void stream_help(std::ostream& os) {};
110
112 virtual bool handle(cgv::gui::event& e);
113
115 virtual bool handle_key_event(cgv::gui::key_event& e) { return false; };
116
118 virtual bool handle_mouse_event(cgv::gui::mouse_event& e, cgv::ivec2 local_mouse_pos) { return false; };
119
122
124 virtual void on_set(void* member_ptr);
125
127 bool blocks_events() const { return blocks_events_; }
128
130 void blocks_events(bool flag) { blocks_events_ = flag; }
131
133 ivec2 get_viewport_size() const { return last_viewport_size_; }
134
136 virtual ivec2 get_local_mouse_pos(ivec2 mouse_pos) const;
137
139 cgv::g2d::irect get_rectangle() const { return container_; }
140
142 cgv::g2d::irect get_local_rectangle() const { return cgv::g2d::irect(ivec2(0), container_.size); }
143
145 AlignmentOption get_horizontal_alignment() const { return horizontal_alignment_; }
146
148 AlignmentOption get_vertical_alignment() const { return vertical_alignment_; }
149
151 vec2 get_percentual_offset() const { return percentual_offset_; }
152
154 void set_alignment(AlignmentOption horizontal, AlignmentOption vertical, vec2 percentual_offset = vec2(-1.0f));
155
157 StretchOption get_stretch() const { return stretch_; }
158
160 vec2 get_percentual_size() const { return percentual_size_; }
161
163 void set_stretch(StretchOption stretch, vec2 percentual_size = vec2(-1.0f));
164
166 ivec2 get_margin() const { return margin_; }
167
169 void set_margin(const ivec2& margin);
170
172 void set_size(const ivec2& size);
173
175 void set_visibility(bool visible);
176
178 void toggle_visibility();
179
184 bool ensure_viewport(cgv::render::context& ctx);
185
186 bool ensure_layout(cgv::render::context& ctx);
187
193 virtual bool is_hit(const ivec2& mouse_pos) const;
194
196 bool begin_overlay_gui();
198 void end_overlay_gui();
199
201 void create_layout_gui();
202
225 void create_gui();
226};
227
228typedef cgv::data::ref_ptr<overlay> overlay_ptr;
229
230}
231}
232
233#include <cgv/config/lib_end.h>
virtual void stream_help(std::ostream &os)
overload to stream help information to the given output stream
Definition overlay.h:109
cgv::g2d::irect get_rectangle() const
return the current rectangle area (in screen coordinates) of the overlay taking layout into account
Definition overlay.h:139
virtual void handle_member_change(const cgv::utils::pointer_test &m)
implement to handle member changes
Definition overlay.h:121
void blocks_events(bool flag)
set whether the overlay blocks events
Definition overlay.h:130
vec2 get_percentual_offset() const
get the percentual alignment offset (only valid if get_horizontal_alignment() or get_vertical_alignme...
Definition overlay.h:151
vec2 get_percentual_size() const
get the percentual stretch (only valid if get_stretch() returns StretchOption::SO_PERCENTUAL)
Definition overlay.h:160
virtual bool self_reflect(cgv::reflect::reflection_handler &_rh)
overload to reflect members of derived classes
Definition overlay.h:106
ivec2 get_margin() const
return the margin as set in the layout parameters
Definition overlay.h:166
bool blocks_events() const
return whether this overlay blocks events, i.e. does not pass them to the next event handler
Definition overlay.h:127
virtual bool handle_key_event(cgv::gui::key_event &e)
overload this method to handle key events
Definition overlay.h:115
AlignmentOption get_horizontal_alignment() const
get the horizontal alignment
Definition overlay.h:145
ivec2 get_viewport_size() const
return the current viewport size
Definition overlay.h:133
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:142
gui_options_t gui_options
options for the GUI creation of this overlay (must be set before GUI creation)
Definition overlay.h:103
AlignmentOption get_vertical_alignment() const
get the vertical alignment
Definition overlay.h:148
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:118
StretchOption get_stretch() const
get the stretch
Definition overlay.h:157
virtual void create_gui_impl()
virtual method to implement the derived class gui creation
Definition overlay.h:84
The node class keeps a pointer to its parent.
Definition node.h:19
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 elements
Definition fvec.h:179
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:621
base class for all drawables, which is independent of the used rendering API.
Definition drawable.h:15
the cgv namespace
Definition print.h:11