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 switch(e.get_kind()) {
19 return handle_key_event(dynamic_cast<cgv::gui::key_event&>(e));
21 {
22 cgv::gui::mouse_event& me = dynamic_cast<cgv::gui::mouse_event&>(e);
23 ivec2 local_mouse_pos = get_local_mouse_pos({ me.get_x(), me.get_y() });
24
25 cgv::g2d::irect local_rect = get_local_rectangle();
26 bool mouse_is_over_overlay = local_rect.contains(local_mouse_pos);
27
29
30 if(action == cgv::gui::MA_MOVE && local_mouse_pos != last_local_mouse_pos_) {
31 bool mouse_was_over_overlay = local_rect.contains(last_local_mouse_pos_);
32
33 last_local_mouse_pos_ = local_mouse_pos;
34
35 if(!mouse_was_over_overlay && mouse_is_over_overlay)
37 else if(mouse_was_over_overlay && !mouse_is_over_overlay)
39
40 bool handled = handle_mouse_event(me, local_mouse_pos);
41 me.set_action(action);
42 }
43
44 switch(action) {
46 captured_mouse_ = mouse_is_over_overlay;
47 break;
49 captured_mouse_ = false;
50 break;
52 return mouse_is_over_overlay;
53 default:
54 break;
55 }
56
57 if((mouse_is_over_overlay || captured_mouse_) && handle_mouse_event(me, local_mouse_pos))
58 return true;
59
60 if(captured_mouse_ && blocks_events_)
61 return true;
62 }
63 default:
64 break;
65 }
66
67 return false;
68}
69
70void overlay::on_set(void* member_ptr) {
72 update_member(member_ptr);
74}
75
77 return cgv::g2d::apply_origin_setting(mouse_pos, last_viewport_size_, cgv::g2d::OriginSetting::kUpperLeft, cgv::g2d::OriginSetting::kLowerLeft) - container_.position;
78}
79
80void overlay::set_alignment(AlignmentOption horizontal, AlignmentOption vertical, vec2 percentual_offset) {
81 horizontal_alignment_ = horizontal;
82 vertical_alignment_ = vertical;
83
84 for(int i = 0; i < 2; ++i) {
85 if(percentual_offset[i] != -1.0f)
86 percentual_offset_[i] = percentual_offset[i];
87 }
88
90}
91
92void overlay::set_stretch(StretchOption stretch, vec2 percentual_size) {
93 stretch_ = stretch;
94
95 for(int i = 0; i < 2; ++i) {
96 if(percentual_size[i] != -1.0f)
97 percentual_size_[i] = percentual_size[i];
98 }
99
101}
102
103void overlay::set_margin(const ivec2& margin) {
104 margin_ = margin;
106}
107
108void overlay::set_size(const ivec2& size) {
109 container_.size = size;
111}
112
113void overlay::set_visibility(bool visible) {
114 if(visible)
115 show();
116 else
117 hide();
119}
120
124
126 ivec2 viewport_size(ctx.get_width(), ctx.get_height());
127 if(last_viewport_size_ != viewport_size) {
128 last_viewport_size_ = viewport_size;
130 return true;
131 }
132 return false;
133}
134
135bool overlay::ensure_layout(cgv::render::context& ctx) {
136 bool ret = ensure_viewport(ctx);
137
138 if(last_size_ != container_.size) {
139 last_size_ = container_.size;
140 ret = true;
141 }
142
143 return ret;
144}
145
146bool overlay::is_hit(const ivec2& mouse_pos) const {
147 ivec2 test_pos = cgv::g2d::apply_origin_setting(mouse_pos, last_viewport_size_, cgv::g2d::OriginSetting::kUpperLeft, cgv::g2d::OriginSetting::kLowerLeft);
148 return container_.contains(test_pos);
149};
150
152 bool node_is_open = begin_tree_node_void(gui_options.heading != "" ? gui_options.heading : name, &name, -1, false, "level=2;options='w=160';align=''");
153
154 add_layout_member_control("Show", cgv::render::drawable::active, "toggle", "w=40");
155
156 if(node_is_open) {
157 align("\a");
158 return true;
159 }
160
161 return false;
162}
163
165 align("\b");
167}
168
171 return;
172
174 return;
175
176 if(begin_tree_node("Layout", gui_options, false)) {
177 align("\a");
178
180 add_decorator("Alignment", "heading", "level=4", "%y-=8\n");
181
182 add_decorator("Horizontal", "heading", "level=4;font_style=regular;w=94", " ");
183 add_decorator("Vertical", "heading", "level=4;font_style=regular;w=94");
184
185 add_layout_member_control("", horizontal_alignment_, "dropdown", "enums='Free,Left,Center,Right';w=94", " ");
186 add_layout_member_control("", vertical_alignment_, "dropdown", "enums='Free=0,Top=3,Center=2,Bottom=1';w=94");
187 }
188
190 add_layout_member_control("Stretch", stretch_, "dropdown", "enums='None,Horizontal,Vertical,Both'");
191
193 add_layout_member_control("Margin", margin_[0], "value", "w=94;min=-10000;max=10000;step=1;", " ");
194 add_layout_member_control("", margin_[1], "value", "w=94;min=-10000;max=10000;step=1;");
195 }
196
197 align("\b");
199 }
200}
201
204 if(begin_overlay_gui()) {
208 }
209 } else {
212 }
213}
214
216 container_.position = margin_;
217 ivec2 max_size = last_viewport_size_ - 2 * margin_;
218
219 switch(stretch_) {
220 case SO_HORIZONTAL:
221 container_.w() = max_size.x();
222 break;
223 case SO_VERTICAL:
224 container_.h() = max_size.y();
225 break;
226 case SO_BOTH:
227 container_.size = max_size;
228 break;
229 case SO_PERCENTUAL:
230 container_.size = percentual_size_ * max_size;
231 break;
232 default:
233 break;
234 }
235
236 const auto compute_aligned_position = [this, &max_size](AlignmentOption alignment, int axis) {
237 switch(alignment) {
238 case AO_CENTER: return (last_viewport_size_[axis] - container_.size[axis]) / 2;
239 case AO_END: return last_viewport_size_[axis] - container_.size[axis] - margin_[axis];
240 case AO_PERCENTUAL: return static_cast<int32_t>(margin_[axis] + percentual_offset_[axis] * max_size[axis]);
241 case AO_START:
242 case AO_FREE:
243 default: return margin_[axis];
244 }
245 };
246
247 container_.x() = compute_aligned_position(horizontal_alignment_, 0);
248 container_.y() = compute_aligned_position(vertical_alignment_, 1);
249}
250
251}
252}
void create_layout_gui()
provides a default gui implementation for private overlay layout members
Definition overlay.cxx:169
void end_overlay_gui()
ends the tree node of the overlay gui
Definition overlay.cxx:164
void create_gui()
Creates a tree node containing the overlay layout options and the gui as specified by the derived ove...
Definition overlay.cxx:202
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:113
void set_stretch(StretchOption stretch, vec2 percentual_size=vec2(-1.0f))
set the stretch option
Definition overlay.cxx:92
void update_layout()
update the layout of the overlay container
Definition overlay.cxx:215
bool ensure_viewport(cgv::render::context &ctx)
Check whether the viewport size has changed since the last call to this method.
Definition overlay.cxx:125
void set_alignment(AlignmentOption horizontal, AlignmentOption vertical, vec2 percentual_offset=vec2(-1.0f))
set the alignment options
Definition overlay.cxx:80
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:151
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:70
void toggle_visibility()
toggle the visibility of the overlay
Definition overlay.cxx:121
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:103
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:76
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:146
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:108
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:159
T & x()
first element
Definition fvec.h:155
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
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