cgv
Loading...
Searching...
No Matches
canvas_overlay.cxx
1#include "canvas_overlay.h"
2
3using namespace cgv::render;
4
5namespace cgv {
6namespace app {
7
9
10 blit_style_.use_blending = true;
11 blit_style_.use_texture = true;
12 blit_style_.feather_width = 0.0f;
13}
14
16
17 bool success = true;
18
19 frame_buffer_.add_attachment("color", "uint8[R,G,B,A]");
20 frame_buffer_.set_size(get_rectangle().size);
21 success &= frame_buffer_.ensure(ctx);
22
23 content_canvas.set_apply_gamma(false);
24 success &= content_canvas.init(ctx);
25
26 overlay_canvas.register_shader("rectangle", cgv::g2d::shaders::rectangle);
27 success &= overlay_canvas.init(ctx);
28
29 init_styles();
30
31 return success;
32}
33
35
36 content_canvas.destruct(ctx);
37 overlay_canvas.destruct(ctx);
38 frame_buffer_.destruct(ctx);
39}
40
41void canvas_overlay::on_set(void* member_ptr) {
42
44 update_member(member_ptr);
45 post_damage();
46}
47
49
50 if(is_visible())
51 draw_impl(ctx);
52}
53
54void canvas_overlay::register_shader(const std::string& name, const std::string& filename) {
55
56 content_canvas.register_shader(name, filename);
57}
58
59void canvas_overlay::post_damage(bool redraw) {
60 has_damage_ = true;
61 if(redraw)
63}
64
66 ivec2 pos = cgv::g2d::change_origin(mouse_pos, get_viewport_size(), cgv::g2d::CoordinateOrigin::kUpperLeft, overlay_canvas.get_origin_setting());
67 pos -= get_rectangle().position;
68 pos = cgv::g2d::change_origin(pos, get_rectangle().size, overlay_canvas.get_origin_setting(), content_canvas.get_origin_setting());
69 return pos;
70}
71
72bool canvas_overlay::is_hit(const ivec2& mouse_pos) const {
73 ivec2 test_pos = cgv::g2d::change_origin(mouse_pos, get_viewport_size(), cgv::g2d::CoordinateOrigin::kUpperLeft, overlay_canvas.get_origin_setting());
74 return get_rectangle().contains(test_pos);
75}
76
77bool canvas_overlay::is_hit_local(const ivec2& local_mouse_pos) const {
78 return get_rectangle().contains(local_mouse_pos);
79}
80
81bool canvas_overlay::ensure_layout(context& ctx) {
82
83 if(overlay::ensure_layout(ctx) || recreate_layout_requested_) {
84 recreate_layout_requested_ = false;
85 has_damage_ = true;
86
87 frame_buffer_.set_size(get_rectangle().size);
88 frame_buffer_.ensure(ctx);
89
90 content_canvas.set_resolution(ctx, get_rectangle().size);
91 overlay_canvas.set_resolution(ctx, get_viewport_size());
92 return true;
93 }
94 return false;
95}
96
97void canvas_overlay::post_recreate_layout() {
98 recreate_layout_requested_ = true;
99}
100
101void canvas_overlay::clear_damage() {
102 has_damage_ = false;
103}
104
105bool canvas_overlay::is_damaged() const {
106 return has_damage_;
107}
108
109void canvas_overlay::begin_content(context& ctx, bool clear_frame_buffer) {
110
111 frame_buffer_.enable(ctx);
112 if(clear_frame_buffer) {
113 ctx.push_bg_color();
114 ctx.set_bg_color({ 0.0f, 0.0f, 0.0f, 1.0f });
115 ctx.clear_background(true, false);
116 ctx.pop_bg_color();
117 }
118}
119
120void canvas_overlay::end_content(context& ctx, bool keep_damage) {
121
122 frame_buffer_.disable(ctx);
123 has_damage_ = keep_damage;
124}
125
126void canvas_overlay::draw_impl(context& ctx) {
127
129 ctx.disable_depth_test();
130
131 ctx.push_blend_state();
132 const context::BlendState blend_state = {
133 true,
134 BF_SRC_ALPHA,
135 BF_ONE_MINUS_SRC_ALPHA,
136 BF_ZERO,
137 BF_ONE_MINUS_SRC_ALPHA
138 };
139 ctx.set_blend_state(blend_state);
140
141
142 if(has_damage_)
143 draw_content(ctx);
144
145 // draw frame buffer texture to screen
146 ctx.set_blend_func(BF_ONE, BF_SRC_ALPHA);
147
148 overlay_canvas.enable_shader(ctx, "rectangle");
149 overlay_canvas.set_style(ctx, blit_style_);
150 frame_buffer_.enable_attachment(ctx, "color", 0);
151 overlay_canvas.draw_shape(ctx, get_rectangle());
152 frame_buffer_.disable_attachment(ctx, "color");
153 overlay_canvas.disable_current_shader(ctx);
154
155 ctx.pop_blend_state();
157}
158
159}
160}
canvas_overlay()
creates an overlay in the bottom left corner with zero size using a canvas for 2d drawing
bool is_hit(const ivec2 &mouse_pos) const override
Test if the mouse pointer is hovering over this overlay and returns true if this is the case.
void clear(cgv::render::context &ctx) override
clear all objects living in the context like textures or display lists
ivec2 get_local_mouse_pos(ivec2 mouse_pos) const override
return the mouse position local to the container of this overlay taking the canvas origin into accoun...
bool is_hit_local(const ivec2 &local_mouse_pos) const
See is_hit.
virtual void handle_member_change(const cgv::utils::pointer_test &m) override
implement to handle member changes
void after_finish(cgv::render::context &) override
draw the content of the canvas overlay
virtual void on_set(void *member_ptr) override
default implementation of that calls handle_member_change and afterwards upates the member in the gui...
bool init(cgv::render::context &ctx) override
this method is called after creation or recreation of the context, return whether all necessary funct...
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
ivec2 get_viewport_size() const
return the current viewport size
Definition overlay.h:133
std::string name
store the name as a string
Definition named.h:25
virtual void update_member(void *member_ptr)
call this to update all views and controls of a member
Definition provider.cxx:72
base class for all drawables, which is independent of the used rendering API.
Definition context.h:670
virtual void set_blend_func(BlendFunction src_factor, BlendFunction dst_factor)
set the blend function
Definition context.cxx:1839
void pop_bg_color()
pop the top of the current background color from the stack
Definition context.cxx:379
void push_depth_test_state()
push a copy of the current depth test state onto the stack saved attributes: depth test enablement,...
Definition context.cxx:1775
void push_blend_state()
push a copy of the current blend state onto the stack saved attributes: blend enablement,...
Definition context.cxx:1822
virtual void disable_depth_test()
disable the depth test
Definition context.cxx:1801
void pop_blend_state()
pop the top of the current culling state from the stack
Definition context.cxx:1826
virtual void clear_background(bool color_flag, bool depth_flag, bool stencil_flag=false, bool accum_flag=false)=0
clear the buffer contents of the flagged buffers to the set background colors
void pop_depth_test_state()
pop the top of the current depth test state from the stack
Definition context.cxx:1779
virtual void set_bg_color(vec4 rgba)
set a user defined background color
Definition context.cxx:384
virtual void set_blend_state(BlendState state)
set the complete blend state
Definition context.cxx:1835
void push_bg_color()
push a copy of the current background color onto the stack
Definition context.cxx:375
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
bool ensure(context &ctx)
Ensure the framebuffer is constructed with the specified size.
void set_size(const ivec2 &size)
Set the size of the framebuffer attachments.
namespace for api independent GPU programming
the cgv namespace
Definition print.h:11
Represents a blend state used to configure fragment blending.
Definition context.h:696