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::apply_origin_setting(mouse_pos, get_viewport_size(), cgv::g2d::OriginSetting::kUpperLeft, overlay_canvas.get_origin_setting());
67 pos -= get_rectangle().position;
68 pos = cgv::g2d::apply_origin_setting(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::apply_origin_setting(mouse_pos, get_viewport_size(), cgv::g2d::OriginSetting::kUpperLeft, overlay_canvas.get_origin_setting());
74 return get_rectangle().contains(test_pos);
75}
76
77bool canvas_overlay::ensure_layout(context& ctx) {
78
79 if(overlay::ensure_layout(ctx) || recreate_layout_requested_) {
80 recreate_layout_requested_ = false;
81 has_damage_ = true;
82
83 frame_buffer_.set_size(get_rectangle().size);
84 frame_buffer_.ensure(ctx);
85
86 content_canvas.set_resolution(ctx, get_rectangle().size);
87 overlay_canvas.set_resolution(ctx, get_viewport_size());
88 return true;
89 }
90 return false;
91}
92
93void canvas_overlay::post_recreate_layout() {
94 recreate_layout_requested_ = true;
95}
96
97void canvas_overlay::clear_damage() {
98 has_damage_ = false;
99}
100
101bool canvas_overlay::is_damaged() const {
102 return has_damage_;
103}
104
105void canvas_overlay::begin_content(context& ctx, bool clear_frame_buffer) {
106
107 frame_buffer_.enable(ctx);
108 if(clear_frame_buffer) {
109 ctx.push_bg_color();
110 ctx.set_bg_color({ 0.0f, 0.0f, 0.0f, 1.0f });
111 ctx.clear_background(true, false);
112 ctx.pop_bg_color();
113 }
114}
115
116void canvas_overlay::end_content(context& ctx, bool keep_damage) {
117
118 frame_buffer_.disable(ctx);
119 has_damage_ = keep_damage;
120}
121
122void canvas_overlay::draw_impl(context& ctx) {
123
125 ctx.disable_depth_test();
126
127 ctx.push_blend_state();
128 const context::BlendState blend_state = {
129 true,
130 BF_SRC_ALPHA,
131 BF_ONE_MINUS_SRC_ALPHA,
132 BF_ZERO,
133 BF_ONE_MINUS_SRC_ALPHA
134 };
135 ctx.set_blend_state(blend_state);
136
137
138 if(has_damage_)
139 draw_content(ctx);
140
141 // draw frame buffer texture to screen
142 ctx.set_blend_func(BF_ONE, BF_SRC_ALPHA);
143
144 overlay_canvas.enable_shader(ctx, "rectangle");
145 overlay_canvas.set_style(ctx, blit_style_);
146 frame_buffer_.enable_attachment(ctx, "color", 0);
147 overlay_canvas.draw_shape(ctx, get_rectangle());
148 frame_buffer_.disable_attachment(ctx, "color");
149 overlay_canvas.disable_current_shader(ctx);
150
151 ctx.pop_blend_state();
153}
154
155}
156}
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...
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:621
virtual void set_blend_func(BlendFunction src_factor, BlendFunction dst_factor)
set the blend function
Definition context.cxx:1733
void pop_bg_color()
pop the top of the current background color from the stack
Definition context.cxx:288
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:1669
void push_blend_state()
push a copy of the current blend state onto the stack saved attributes: blend enablement,...
Definition context.cxx:1716
virtual void disable_depth_test()
disable the depth test
Definition context.cxx:1695
void pop_blend_state()
pop the top of the current culling state from the stack
Definition context.cxx:1720
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:1673
virtual void set_bg_color(vec4 rgba)
set a user defined background color
Definition context.cxx:293
virtual void set_blend_state(BlendState state)
set the complete blend state
Definition context.cxx:1729
void push_bg_color()
push a copy of the current background color onto the stack
Definition context.cxx:284
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 set_size(const ivec2 &size)
Sets the size of the framebuffer renderbuffers.
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:645