cgv
Loading...
Searching...
No Matches
canvas_overlay.cxx
1#include "canvas_overlay.h"
2
3using namespace cgv::render;
4
5namespace cgv {
6namespace overlay {
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}
48{
49 draw_in_finish_frame = enable;
50}
51
53 if (is_visible() && draw_in_finish_frame)
54 draw_impl(ctx);
55}
56
58 if(is_visible() && !draw_in_finish_frame)
59 draw_impl(ctx);
60}
61
62void canvas_overlay::register_shader(const std::string& name, const std::string& filename) {
63
64 content_canvas.register_shader(name, filename);
65}
66
67void canvas_overlay::post_damage(bool redraw) {
68 has_damage_ = true;
69 if(redraw)
71}
72
74 ivec2 pos = cgv::g2d::change_origin(mouse_pos, get_viewport_size(), cgv::g2d::CoordinateOrigin::kUpperLeft, overlay_canvas.get_origin_setting());
75 pos -= get_rectangle().position;
76 pos = cgv::g2d::change_origin(pos, get_rectangle().size, overlay_canvas.get_origin_setting(), content_canvas.get_origin_setting());
77 return pos;
78}
79
80bool canvas_overlay::is_hit(const ivec2& mouse_pos) const {
81 ivec2 test_pos = cgv::g2d::change_origin(mouse_pos, get_viewport_size(), cgv::g2d::CoordinateOrigin::kUpperLeft, overlay_canvas.get_origin_setting());
82 return get_rectangle().contains(test_pos);
83}
84
85bool canvas_overlay::is_hit_local(const ivec2& local_mouse_pos) const {
86 return get_rectangle().contains(local_mouse_pos);
87}
88
89bool canvas_overlay::ensure_layout(context& ctx) {
90
91 if(overlay::ensure_layout(ctx) || recreate_layout_requested_) {
92 recreate_layout_requested_ = false;
93 has_damage_ = true;
94
95 frame_buffer_.set_size(get_rectangle().size);
96 frame_buffer_.ensure(ctx);
97
98 content_canvas.set_resolution(get_rectangle().size);
99 overlay_canvas.set_resolution(get_viewport_size());
100 return true;
101 }
102 return false;
103}
104
105void canvas_overlay::post_recreate_layout() {
106 recreate_layout_requested_ = true;
107}
108
109void canvas_overlay::clear_damage() {
110 has_damage_ = false;
111}
112
113bool canvas_overlay::is_damaged() const {
114 return has_damage_;
115}
116
117void canvas_overlay::begin_content(context& ctx, bool clear_frame_buffer) {
118
119 frame_buffer_.enable(ctx);
120 if(clear_frame_buffer) {
121 ctx.push_bg_color();
122 ctx.set_bg_color({ 0.0f, 0.0f, 0.0f, 1.0f });
123 ctx.clear_background(true, false);
124 ctx.pop_bg_color();
125 }
126}
127
128void canvas_overlay::end_content(context& ctx, bool keep_damage) {
129
130 frame_buffer_.disable(ctx);
131 has_damage_ = keep_damage;
132}
133
134void canvas_overlay::draw_impl(context& ctx) {
135
137 ctx.disable_depth_test();
138
139 ctx.push_blend_state();
140 const context::BlendState blend_state = {
141 true,
142 BF_SRC_ALPHA,
143 BF_ONE_MINUS_SRC_ALPHA,
144 BF_ZERO,
145 BF_ONE_MINUS_SRC_ALPHA
146 };
147 ctx.set_blend_state(blend_state);
148
149
150 if(has_damage_)
151 draw_content(ctx);
152
153 // draw frame buffer texture to screen
154 ctx.set_blend_func(BF_ONE, BF_SRC_ALPHA);
155
156 overlay_canvas.enable_shader(ctx, "rectangle");
157 overlay_canvas.set_style(ctx, blit_style_);
158 frame_buffer_.enable_attachment(ctx, "color", 0);
159 overlay_canvas.draw_shape(ctx, get_rectangle());
160 frame_buffer_.disable_attachment(ctx, "color");
161 overlay_canvas.disable_current_shader(ctx);
162
163 ctx.pop_blend_state();
165}
166
167} // namespace overlay
168} // namespace cgv
std::string name
store the name as a string
Definition named.h:25
This class provides methods to test if a stored pointer points to addresses of given variables or ins...
virtual void update_member(void *member_ptr)
call this to update all views and controls of a member
Definition provider.cxx:72
bool init(cgv::render::context &ctx) override
this method is called after creation or recreation of the context, return whether all necessary funct...
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...
virtual void handle_member_change(cgv::data::informed_ptr ptr) override
implement to handle member changes
void finish_frame(cgv::render::context &) override
draw the content of the canvas overlay either in finish_frame
bool is_hit_local(const ivec2 &local_mouse_pos) const
See is_hit.
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.
virtual void on_set(void *member_ptr) override
default implementation of that calls handle_member_change and afterwards upates the member in the gui...
void after_finish(cgv::render::context &) override
or in after_finish, what is the default
virtual void draw_content(cgv::render::context &ctx)=0
draw_content is implemented by decendent classes
void set_draw_in_finish_frame(bool enable=true)
set the drawing of the content to be in finish_frame (enable = true) or after_finish (enable = false)
void clear(cgv::render::context &ctx) override
clear all objects living in the context like textures or display lists
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
ivec2 get_viewport_size() const
return the current viewport size
Definition overlay.h:89
base class for all drawables, which is independent of the used rendering API.
Definition context.h:668
virtual void set_blend_func(BlendFunction src_factor, BlendFunction dst_factor)
set the blend function
Definition context.cxx:1877
void pop_bg_color()
pop the top of the current background color from the stack
Definition context.cxx:385
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:1813
void push_blend_state()
push a copy of the current blend state onto the stack saved attributes: blend enablement,...
Definition context.cxx:1860
virtual void disable_depth_test()
disable the depth test
Definition context.cxx:1839
void pop_blend_state()
pop the top of the current culling state from the stack
Definition context.cxx:1864
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:1817
virtual void set_bg_color(vec4 rgba)
set a user defined background color
Definition context.cxx:390
virtual void set_blend_state(BlendState state)
set the complete blend state
Definition context.cxx:1873
void push_bg_color()
push a copy of the current background color onto the stack
Definition context.cxx:381
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
this header is dependency free
Definition print.h:11
Represents a blend state used to configure fragment blending.
Definition context.h:694