Blender V5.3
interface_button_sections.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include "BLI_listbase.hh"
14#include "BLI_rect.hh"
15#include "BLI_span.hh"
16#include "BLI_vector.hh"
17
18#include "BKE_screen.hh"
19
20#include "DNA_screen_types.h"
21
22#include "GPU_immediate.hh"
23#include "GPU_state.hh"
24
25#include "interface_intern.hh"
26
27namespace blender::ui {
28
38static Vector<rcti> button_section_bounds_calc(const ARegion *region, const bool add_padding)
39{
40 Vector<rcti> section_bounds;
41
42 const auto finish_section_fn = [&](const rcti cur_section_bounds) {
43 if (!section_bounds.is_empty() &&
44 std::abs(section_bounds.last().xmax - cur_section_bounds.xmin) <
46 {
47 section_bounds.last().xmax = cur_section_bounds.xmax;
48 }
49 else {
50 section_bounds.append(cur_section_bounds);
51 }
52
53 rcti &last_bounds = section_bounds.last();
54 /* Extend to region edge if close enough. */
55 if (last_bounds.xmin <= UI_BUTTON_SECTION_MERGE_DISTANCE) {
56 last_bounds.xmin = 0;
57 }
58 if (last_bounds.xmax >= (region->winx - UI_BUTTON_SECTION_MERGE_DISTANCE)) {
59 last_bounds.xmax = region->winx;
60 }
61 };
62
63 {
64 bool has_section_content = false;
65 rcti cur_section_bounds;
66 BLI_rcti_init_minmax(&cur_section_bounds);
67
68 /* A bit annoying, but this function is called for both drawing and event handling. When
69 * drawing, we need to exclude inactive blocks since they mess with the result. However, this
70 * active state is only useful during drawing and must be ignored for handling (at which point
71 * #Block::active is false for all blocks). */
72 const bool is_drawing = region->runtime->do_draw & RGN_DRAWING;
73 for (Block &block : region->runtime->uiblocks) {
74 if (is_drawing && !block.active) {
75 continue;
76 }
77
78 for (const Button &but : block.buttons()) {
79 if (but.type == ButtonType::SeprSpacer) {
80 /* Start a new section. */
81 if (has_section_content) {
82 finish_section_fn(cur_section_bounds);
83
84 /* Reset for next section. */
85 BLI_rcti_init_minmax(&cur_section_bounds);
86 has_section_content = false;
87 }
88 continue;
89 }
90
91 rcti but_pixelrect;
92 button_to_pixelrect(&but_pixelrect, region, &block, &but);
93 BLI_rcti_do_minmax_rcti(&cur_section_bounds, &but_pixelrect);
94 has_section_content = true;
95 }
96 }
97
98 /* Finish last section in case the last button is not a spacer. */
99 if (has_section_content) {
100 finish_section_fn(cur_section_bounds);
101 }
102 }
103
104 if (add_padding) {
105 const uiStyle *style = style_get_dpi();
106 const int pad_x = style->buttonspacex;
107 /* Making this based on the header size since this feature is typically used in headers, and
108 * this way we are more likely to pad the bounds all the way to the region edge. */
109 const int pad_y = ceil((HEADER_PADDING_Y * UI_SCALE_FAC) / 2.0f);
110
111 for (rcti &bounds : section_bounds) {
112 BLI_rcti_pad(&bounds, pad_x, pad_y);
113 /* Clamp, important for the rounded-corners to draw correct. */
114 CLAMP_MIN(bounds.xmin, 0);
115 CLAMP_MAX(bounds.xmax, region->winx);
116 CLAMP_MIN(bounds.ymin, 0);
117 CLAMP_MAX(bounds.ymax, region->winy);
118 }
119 }
120
121 return section_bounds;
122}
123
124static void draw_button_sections_background(const ARegion *region,
125 const Span<rcti> section_bounds,
126 const ThemeColorID colorid,
127 const ButtonSectionsAlign align,
128 const float corner_radius)
129{
130 float bg_color[4];
131 theme::get_color_4fv(colorid, bg_color);
132
133 for (const rcti &bounds : section_bounds) {
134 int roundbox_corners = [align]() -> int {
135 switch (align) {
141 return CNR_ALL;
142 }
143 return CNR_ALL;
144 }();
145
146 /* No rounded corners at the region edge. */
147 if (bounds.xmin == 0) {
148 roundbox_corners &= ~(CNR_TOP_LEFT | CNR_BOTTOM_LEFT);
149 }
150 if (bounds.xmax >= region->winx) {
151 roundbox_corners &= ~(CNR_TOP_RIGHT | CNR_BOTTOM_RIGHT);
152 }
153
154 rctf bounds_float;
155 BLI_rctf_rcti_copy(&bounds_float, &bounds);
157 /* Make space for the separator line. */
158 if (align == ButtonSectionsAlign::Top) {
160 }
161 else if (align == ButtonSectionsAlign::Bottom) {
163 }
164 }
165 draw_roundbox_corner_set(roundbox_corners);
166 draw_roundbox_4fv(&bounds_float, true, corner_radius, bg_color);
167 }
168}
169
171 const Span<rcti> section_bounds,
172 const ThemeColorID colorid,
173 const ButtonSectionsAlign align,
174 const float corner_radius)
175{
176 const int separator_line_width = region->regiontype == RGN_TYPE_ASSET_SHELF_HEADER ?
178 0;
179 float bg_color[4];
180 theme::get_color_4fv(colorid, bg_color);
181
183
184 /* Separator line. */
186 GPUVertFormat *format = immVertexFormat();
187 const uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
189 immUniformColor4fv(bg_color);
190
191 if (align == ButtonSectionsAlign::Top) {
192 immRectf(pos, 0, region->winy - separator_line_width, region->winx, region->winy);
193 }
194 else if (align == ButtonSectionsAlign::Bottom) {
195 immRectf(pos, 0, 0, region->winx, separator_line_width);
196 }
197 else {
199 }
201 }
202
203 int prev_xmax = 0;
204 for (const rcti &bounds : section_bounds) {
205 if (prev_xmax != 0) {
206 const rcti rounded_corner_rect = {
207 prev_xmax, bounds.xmin, separator_line_width, region->winy - separator_line_width};
208
212 draw_rounded_corners_inverted(rounded_corner_rect, corner_radius, bg_color);
213 }
214
215 prev_xmax = bounds.xmax;
216 }
217
219}
220
222 const int /*ThemeColorID*/ colorid,
223 const ButtonSectionsAlign align)
224{
225 const float aspect = BLI_rctf_size_x(&region->v2d.cur) /
226 (BLI_rcti_size_x(&region->v2d.mask) + 1);
227 const float corner_radius = 4.0f * UI_SCALE_FAC / aspect;
228
229 const Vector<rcti> section_bounds = button_section_bounds_calc(region, true);
230
232 region, section_bounds, ThemeColorID(colorid), align, corner_radius);
233 if (align != ButtonSectionsAlign::None) {
235 section_bounds,
236 ThemeColorID(colorid),
237 align,
238 /* Slightly bigger corner radius, looks better. */
239 corner_radius + 1);
240 }
241}
242
243bool region_button_sections_is_inside_x(const ARegion *region, const int mval_x)
244{
245 const Vector<rcti> section_bounds = button_section_bounds_calc(region, true);
246
247 for (const rcti &bounds : section_bounds) {
248 if (BLI_rcti_isect_x(&bounds, mval_x)) {
249 return true;
250 }
251 }
252 return false;
253}
254
255} // namespace blender::ui
#define HEADER_PADDING_Y
#define UI_SCALE_FAC
#define UI_BUTTON_SECTION_SEPERATOR_LINE_WITH
#define UI_BUTTON_SECTION_MERGE_DISTANCE
#define CLAMP_MAX(a, c)
#define BLI_assert_unreachable()
#define CLAMP_MIN(a, b)
void append(const T &value)
const T & last(const int64_t n=0) const
#define ceil
void get_color_4fv(int colorid, float col[4])
bool region_button_sections_is_inside_x(const ARegion *region, const int mval_x)
static Vector< rcti > button_section_bounds_calc(const ARegion *region, const bool add_padding)
static void draw_button_sections_background(const ARegion *region, const Span< rcti > section_bounds, const ThemeColorID colorid, const ButtonSectionsAlign align, const float corner_radius)
static void draw_button_sections_alignment_separator(const ARegion *region, const Span< rcti > section_bounds, const ThemeColorID colorid, const ButtonSectionsAlign align, const float corner_radius)
const uiStyle * style_get_dpi()
void draw_roundbox_corner_set(int type)
void button_to_pixelrect(rcti *rect, const ARegion *region, const Block *block, const Button *but)
void draw_rounded_corners_inverted(const rcti &rect, const float rad, const float4 color)
void region_button_sections_draw(const ARegion *region, int colorid, ButtonSectionsAlign align)
void draw_roundbox_4fv(const rctf *rect, bool filled, float rad, const float col[4])
bool BLI_rcti_isect_x(const rcti *rect, int x)
Definition rct.cc:39
unsigned int uint
Definition BKE_text.h:55
@ RGN_TYPE_ASSET_SHELF_HEADER
GPUVertFormat * immVertexFormat()
void BLI_rcti_init_minmax(struct rcti *rect)
Definition rct.cc:488
void immUniformColor4fv(const float rgba[4])
void immUnbindProgram()
void BLI_rctf_rcti_copy(struct rctf *dst, const struct rcti *src)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void BLI_rcti_pad(struct rcti *rect, int pad_x, int pad_y)
Definition rct.cc:643
void immRectf(uint pos, float x1, float y1, float x2, float y2)
void BLI_rcti_do_minmax_rcti(struct rcti *rect, const struct rcti *other)
bke::ARegionRuntime * runtime
eRegion_Type regiontype
ListBaseT< ui::Block > uiblocks
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
short buttonspacex