cgv
Loading...
Searching...
No Matches
overlay.cxx
1#include "overlay.h"
2
3namespace cgv {
4namespace app {
5
7 update_member(&(cgv::render::drawable::active));
9}
10
15
17 if(!cgv::render::drawable::active)
18 return false;
19
20 switch(e.get_kind()) {
22 return handle_key_event(dynamic_cast<cgv::gui::key_event&>(e));
24 {
25 cgv::gui::mouse_event& me = dynamic_cast<cgv::gui::mouse_event&>(e);
26 ivec2 local_mouse_pos = get_local_mouse_pos({ me.get_x(), me.get_y() });
27
28 cgv::g2d::irect local_rect = get_local_rectangle();
29 bool mouse_is_over_overlay = local_rect.contains(local_mouse_pos);
30
32
33 if(action == cgv::gui::MA_MOVE && local_mouse_pos != last_local_mouse_pos_) {
34 bool mouse_was_over_overlay = local_rect.contains(last_local_mouse_pos_);
35
36 last_local_mouse_pos_ = local_mouse_pos;
37
38 if(!mouse_was_over_overlay && mouse_is_over_overlay)
40 else if(mouse_was_over_overlay && !mouse_is_over_overlay)
42
43 bool handled = handle_mouse_event(me, local_mouse_pos);
44 me.set_action(action);
45 }
46
47 switch(action) {
49 captured_mouse_ = mouse_is_over_overlay;
50 break;
52 captured_mouse_ = false;
53 break;
55 return mouse_is_over_overlay;
56 default:
57 break;
58 }
59
60 if((mouse_is_over_overlay || captured_mouse_) && handle_mouse_event(me, local_mouse_pos))
61 return true;
62
63 if(captured_mouse_ && blocks_events_)
64 return true;
65 }
66 default:
67 break;
68 }
69
70 return false;
71}
72
73void overlay::on_set(void* member_ptr) {
75 update_member(member_ptr);
77}
78
80 return cgv::g2d::apply_origin_setting(mouse_pos, last_viewport_size_, cgv::g2d::OriginSetting::kUpperLeft, cgv::g2d::OriginSetting::kLowerLeft) - container_.position;
81}
82
83void overlay::set_alignment(AlignmentOption horizontal, AlignmentOption vertical, vec2 percentual_offset) {
84 horizontal_alignment_ = horizontal;
85 vertical_alignment_ = vertical;
86
87 for(int i = 0; i < 2; ++i) {
88 if(percentual_offset[i] != -1.0f)
89 percentual_offset_[i] = percentual_offset[i];
90 }
91
93}
94
95void overlay::set_stretch(StretchOption stretch, vec2 percentual_size) {
96 stretch_ = stretch;
97
98 for(int i = 0; i < 2; ++i) {
99 if(percentual_size[i] != -1.0f)
100 percentual_size_[i] = percentual_size[i];
101 }
102
104}
105
106void overlay::set_margin(const ivec2& margin) {
107 margin_ = margin;
109}
110
111void overlay::set_size(const ivec2& size) {
112 container_.size = size;
114}
115
116void overlay::set_visibility(bool visible) {
117 if(visible)
118 show();
119 else
120 hide();
122}
123
127
129 ivec2 viewport_size(ctx.get_width(), ctx.get_height());
130 if(last_viewport_size_ != viewport_size) {
131 last_viewport_size_ = viewport_size;
133 return true;
134 }
135 return false;
136}
137
138bool overlay::ensure_layout(cgv::render::context& ctx) {
139 bool ret = ensure_viewport(ctx);
140
141 if(last_size_ != container_.size) {
142 last_size_ = container_.size;
143 ret = true;
144 }
145
146 return ret;
147}
148
149bool overlay::is_hit(const ivec2& mouse_pos) const {
150 ivec2 test_pos = cgv::g2d::apply_origin_setting(mouse_pos, last_viewport_size_, cgv::g2d::OriginSetting::kUpperLeft, cgv::g2d::OriginSetting::kLowerLeft);
151 return container_.contains(test_pos);
152};
153
155 bool node_is_open = begin_tree_node_void(gui_options.heading != "" ? gui_options.heading : name, &name, -1, false, "level=2;options='w=160';align=''");
156
157 add_layout_member_control("Show", cgv::render::drawable::active, "toggle", "w=40");
158
159 if(node_is_open) {
160 align("\a");
161 return true;
162 }
163
164 return false;
165}
166
168 align("\b");
170}
171
174 return;
175
177 return;
178
179 if(begin_tree_node("Layout", gui_options, false)) {
180 align("\a");
181
183 add_decorator("Alignment", "heading", "level=4", "%y-=8\n");
184
185 add_decorator("Horizontal", "heading", "level=4;font_style=regular;w=94", " ");
186 add_decorator("Vertical", "heading", "level=4;font_style=regular;w=94");
187
188 add_layout_member_control("", horizontal_alignment_, "dropdown", "enums='Free,Left,Center,Right';w=94", " ");
189 add_layout_member_control("", vertical_alignment_, "dropdown", "enums='Free=0,Top=3,Center=2,Bottom=1';w=94");
190 }
191
193 add_layout_member_control("Stretch", stretch_, "dropdown", "enums='None,Horizontal,Vertical,Both'");
194
196 add_layout_member_control("Margin", margin_[0], "value", "w=94;min=-10000;max=10000;step=1;", " ");
197 add_layout_member_control("", margin_[1], "value", "w=94;min=-10000;max=10000;step=1;");
198 }
199
200 align("\b");
202 }
203}
204
207 if(begin_overlay_gui()) {
211 }
212 } else {
215 }
216}
217
219 container_.position = margin_;
220 ivec2 max_size = last_viewport_size_ - 2 * margin_;
221
222 switch(stretch_) {
223 case SO_HORIZONTAL:
224 container_.w() = max_size.x();
225 break;
226 case SO_VERTICAL:
227 container_.h() = max_size.y();
228 break;
229 case SO_BOTH:
230 container_.size = max_size;
231 break;
232 case SO_PERCENTUAL:
233 container_.size = percentual_size_ * max_size;
234 break;
235 default:
236 break;
237 }
238
239 const auto compute_aligned_position = [this, &max_size](AlignmentOption alignment, int axis) {
240 switch(alignment) {
241 case AO_CENTER: return (last_viewport_size_[axis] - container_.size[axis]) / 2;
242 case AO_END: return last_viewport_size_[axis] - container_.size[axis] - margin_[axis];
243 case AO_PERCENTUAL: return static_cast<int32_t>(margin_[axis] + percentual_offset_[axis] * max_size[axis]);
244 case AO_START:
245 case AO_FREE:
246 default: return margin_[axis];
247 }
248 };
249
250 container_.x() = compute_aligned_position(horizontal_alignment_, 0);
251 container_.y() = compute_aligned_position(vertical_alignment_, 1);
252}
253
254}
255}
void create_layout_gui()
provides a default gui implementation for private overlay layout members
Definition overlay.cxx:172
void end_overlay_gui()
ends the tree node of the overlay gui
Definition overlay.cxx:167
void create_gui()
Creates a tree node containing the overlay layout options and the gui as specified by the derived ove...
Definition overlay.cxx:205
virtual void handle_member_change(const cgv::utils::pointer_test &m)
implement to handle member changes
Definition overlay.h:121
void set_visibility(bool visible)
set the visibility of the overlay
Definition overlay.cxx:116
void set_stretch(StretchOption stretch, vec2 percentual_size=vec2(-1.0f))
set the stretch option
Definition overlay.cxx:95
void update_layout()
update the layout of the overlay container
Definition overlay.cxx:218
bool ensure_viewport(cgv::render::context &ctx)
Check whether the viewport size has changed since the last call to this method.
Definition overlay.cxx:128
void set_alignment(AlignmentOption horizontal, AlignmentOption vertical, vec2 percentual_offset=vec2(-1.0f))
set the alignment options
Definition overlay.cxx:83
bool begin_overlay_gui()
begins a tree node if create default tree node is set in the gui options; automatically creates the l...
Definition overlay.cxx:154
virtual void on_visibility_change()
called when the overlay visibility is changed through the default gui
Definition overlay.cxx:6
virtual void on_set(void *member_ptr)
default implementation of that calls handle_member_change and afterwards updates the member in the gu...
Definition overlay.cxx:73
void toggle_visibility()
toggle the visibility of the overlay
Definition overlay.cxx:124
virtual bool handle_key_event(cgv::gui::key_event &e)
overload this method to handle key events
Definition overlay.h:115
void set_margin(const ivec2 &margin)
set the overlay margin
Definition overlay.cxx:106
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
virtual ivec2 get_local_mouse_pos(ivec2 mouse_pos) const
return the mouse position local to the container of this overlay
Definition overlay.cxx:79
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
virtual bool is_hit(const ivec2 &mouse_pos) const
Test if the mouse pointer is hovering over this overlay and returns true if this is the case.
Definition overlay.cxx:149
virtual bool handle(cgv::gui::event &e)
handle incomming events; calls handle_key_events or handle_mouse_events depending on the event type; ...
Definition overlay.cxx:16
virtual void on_layout_change()
called when the overlay layout parameters are changed through the default gui
Definition overlay.cxx:11
void set_size(const ivec2 &size)
set the default size of the overlay before stretch gets applied
Definition overlay.cxx:111
virtual void create_gui_impl()
virtual method to implement the derived class gui creation
Definition overlay.h:84
std::string name
store the name as a string
Definition named.h:25
unsigned get_kind() const
return, what kind of event this is, typically a value from the EventId enum
Definition event.cxx:202
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
short get_y() const
current mouse y position (origin is top-left of window)
MouseAction get_action() const
return the mouse action
void set_action(MouseAction _action)
return the mouse action
short get_x() const
current mouse x position (origin is top-left of window)
cgv::base::base_ptr add_decorator(const std::string &label, const std::string &decorator_type, const std::string &options="", const std::string &align="\n")
add a newly created decorator to the group
Definition provider.cxx:168
bool begin_tree_node_void(const std::string &label, const void *value_ptr, int index=-1, bool initial_visibility=false, const std::string &options="", gui_group_ptr ggp=gui_group_ptr())
void version of the templated functions
Definition provider.cxx:207
void align(const std::string &_align)
send pure alignment information
Definition provider.cxx:36
bool begin_tree_node(const std::string &label, const T &value, bool initial_visibility=false, const std::string &options="", gui_group_ptr ggp=gui_group_ptr())
Begin a sub tree of a tree structured gui.
Definition provider.h:212
virtual void update_member(void *member_ptr)
call this to update all views and controls of a member
Definition provider.cxx:72
void end_tree_node(const T &value)
template specialization that allows to specify value reference plus node_instance by using the result...
Definition provider.h:222
T & y()
second element
Definition fvec.h:160
T & x()
first element
Definition fvec.h:156
base class for all drawables, which is independent of the used rendering API.
Definition context.h:626
virtual unsigned int get_width() const =0
return the width of the window
virtual unsigned int get_height() const =0
return the height of the window
void hide()
hide the drawable
Definition drawable.cxx:21
void show()
show the drawable
Definition drawable.cxx:26
void post_redraw()
posts a redraw event to the current context if one is available
Definition drawable.cxx:43
bool is_visible() const
check whether the drawable is visible
Definition drawable.cxx:31
MouseAction
different actions that a mouse can perform
Definition mouse_event.h:12
@ MA_PRESS
mouse button pressed
Definition mouse_event.h:13
@ MA_LEAVE
mouse leave window action
Definition mouse_event.h:19
@ MA_MOVE
mouse pointer moved
Definition mouse_event.h:16
@ MA_ENTER
mouse enter window action
Definition mouse_event.h:18
@ MA_WHEEL
mouse wheel moved
Definition mouse_event.h:15
@ MA_RELEASE
mouse button released
Definition mouse_event.h:14
@ EID_KEY
id for key event
Definition event.h:17
@ EID_MOUSE
id for mouse event
Definition event.h:18
the cgv namespace
Definition print.h:11
bool create_default_tree_node
if true the overlay GUI will be placed inside a collapsible tree node
Definition overlay.h:90
bool allow_stretch
whether to show the stretch options (show_layout_options must be enabled)
Definition overlay.h:98
bool allow_alignment
whether to show the alignment options (show_layout_options must be enabled)
Definition overlay.h:96
bool allow_margin
whether to show the margin options (show_layout_options must be enabled)
Definition overlay.h:100
bool show_layout_options
whether to show the layout options
Definition overlay.h:94