Blender V5.3
interface_region_hud.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "DNA_screen_types.h"
16#include "DNA_userdef_types.h"
17
18#include "BLI_listbase.hh"
19#include "BLI_rect.hh"
20#include "BLI_string_utf8.hh"
21#include "BLI_utildefines.hh"
22
23#include "BKE_context.hh"
24#include "BKE_screen.hh"
25
26#include "WM_api.hh"
27#include "WM_types.hh"
28
30#include "UI_view2d.hh"
31
32#include "BLT_translation.hh"
33
34#include "ED_screen.hh"
35
36#include "GPU_framebuffer.hh"
37#include "interface_intern.hh"
38
39namespace blender::ui {
40
41/* -------------------------------------------------------------------- */
44
48static int area_calc_region_type_index(const ScrArea *area, const ARegion *region)
49{
50 const int region_type = region->regiontype;
51 int index = 0;
52 for (const ARegion &region_iter : area->regionbase) {
53 if (region_iter.regiontype != region_type) {
54 continue;
55 }
56 if (&region_iter == region) {
57 return index;
58 }
59 index += 1;
60 }
61
62 /* Bad input as the `region` was not found in the `area`,
63 * -1 causes the first to be returned. */
65 return -1;
66}
67
72 const short region_type,
73 int index_hint)
74{
75 ARegion *region_match_type = nullptr;
76 /* Any negative values can return the first match. */
77 index_hint = std::max(0, index_hint);
78 int index = 0;
79 for (ARegion &region : area->regionbase) {
80 if (region.regiontype != region_type) {
81 continue;
82 }
83 if (index == index_hint) {
84 region_match_type = &region;
85 break;
86 }
87 if (region_match_type == nullptr) {
88 region_match_type = &region;
89 }
90 index += 1;
91 }
92 return region_match_type;
93}
94
107
108static bool last_redo_poll(const bContext *C, short region_type, int region_index_hint)
109{
111 if (op == nullptr) {
112 return false;
113 }
114
115 bool success = false;
116 {
117 /* Make sure that we are using the same region type as the original
118 * operator call. Otherwise we would be polling the operator with the
119 * wrong context.
120 */
121 ScrArea *area = CTX_wm_area(C);
122 ARegion *region_op = (region_type != -1) ? area_find_region_by_type_and_index_hint(
123 area, region_type, region_index_hint) :
124 nullptr;
125 ARegion *region_prev = CTX_wm_region(C);
126 CTX_wm_region_set(const_cast<bContext *>(C), region_op);
127
128 if (WM_operator_repeat_check(C, op) && WM_operator_ui_poll(op->type, op->ptr)) {
129 success = WM_operator_poll(const_cast<bContext *>(C), op->type);
130 }
131 CTX_wm_region_set(const_cast<bContext *>(C), region_prev);
132 }
133 return success;
134}
135
136static void hud_region_hide(ARegion *region)
137{
138 region->flag |= RGN_FLAG_HIDDEN;
139 /* Avoids setting 'AREA_FLAG_REGION_SIZE_UPDATE'
140 * since other regions don't depend on this. */
141 BLI_rcti_init(&region->winrct, 0, 0, 0, 0);
142}
143
145
146/* -------------------------------------------------------------------- */
149
151{
152 ScrArea *area = CTX_wm_area(C);
154 if (region != nullptr) {
155 HudRegionData *hrd = static_cast<HudRegionData *>(region->regiondata);
156 if (hrd != nullptr) {
157 return last_redo_poll(C, hrd->regionid, hrd->region_index_hint);
158 }
159 }
160 return false;
161}
162
164{
166 const std::string opname = WM_operatortype_name(op->type, op->ptr);
167 panel_drawname_set(panel, opname);
168}
169
170static void hud_panel_operator_redo_draw(const bContext *C, Panel *panel)
171{
173 if (op == nullptr) {
174 return;
175 }
177 panel->layout->enabled_set(false);
178 }
179 Layout &col = panel->layout->column(false);
180 /* Redo HUD is a kind of popup, use persistent layout panel states for the redo operator. */
182 op->type->idname);
184}
185
186static void hud_panels_register(ARegionType *art, int space_type, int region_type)
187{
188 PanelType *pt = MEM_new_zeroed<PanelType>(__func__);
189 STRNCPY_UTF8(pt->idname, "OPERATOR_PT_redo");
190 STRNCPY_UTF8(pt->label, N_("Redo"));
195 pt->space_type = space_type;
196 pt->region_type = region_type;
198 BLI_addtail(&art->paneltypes, pt);
199}
200
202
203/* -------------------------------------------------------------------- */
206
208{
209 ED_region_panels_init(wm, region);
210
211 /* Reset zoom from panels init because we don't want zoom allowed for redo panel. */
212 region->v2d.maxzoom = 1.0f;
213 region->v2d.minzoom = 1.0f;
214
217}
218
219static void hud_region_free(ARegion *region)
220{
221 if (region->regiondata) {
222 MEM_delete(static_cast<HudRegionData *>(region->regiondata));
223 region->regiondata = nullptr;
224 }
225}
226
227static void hud_region_layout(const bContext *C, ARegion *region)
228{
229 HudRegionData *hrd = static_cast<HudRegionData *>(region->regiondata);
230 if (hrd == nullptr || !last_redo_poll(C, hrd->regionid, hrd->region_index_hint)) {
231 ED_region_tag_redraw(region);
232 hud_region_hide(region);
233 return;
234 }
235
236 ScrArea *area = CTX_wm_area(C);
237 const int size_y = region->sizey;
238
239 ED_region_panels_layout(C, region);
240
241 if (region->panels.first() &&
242 ((area->flag & AREA_FLAG_REGION_SIZE_UPDATE) || (region->sizey != size_y)))
243 {
244 int winx_new = UI_SCALE_FAC * (region->sizex + 0.5f);
245 int winy_new = UI_SCALE_FAC * (region->sizey + 0.5f);
246 View2D *v2d = &region->v2d;
247
248 if (region->flag & RGN_FLAG_SIZE_CLAMP_X) {
249 CLAMP_MAX(winx_new, region->winx);
250 }
251 if (region->flag & RGN_FLAG_SIZE_CLAMP_Y) {
252 CLAMP_MAX(winy_new, region->winy);
253 }
254
255 region->winx = winx_new;
256 region->winy = winy_new;
257
258 region->winrct.xmax = (region->winrct.xmin + region->winx) - 1;
259 region->winrct.ymax = (region->winrct.ymin + region->winy) - 1;
260
261 view2d_region_reinit(v2d, V2D_COMMONVIEW_LIST, region->winx, region->winy);
262
263 /* Weak, but needed to avoid glitches, especially with hi-dpi
264 * (where resizing the view glitches often).
265 * Fortunately this only happens occasionally. */
266 ED_region_panels_layout(C, region);
267 }
268
269 /* restore view matrix */
271}
272
273static void hud_region_draw(const bContext *C, ARegion *region)
274{
275 view2d_view_ortho(&region->v2d);
277 GPU_clear_color(0.0f, 0.0f, 0.0f, 0.0f);
278
279 if ((region->flag & RGN_FLAG_HIDDEN) == 0) {
280 rcti reset_rect = {};
281 reset_rect.xmax = region->winx;
282 reset_rect.ymax = region->winy;
283 draw_menu_back(nullptr, nullptr, &reset_rect);
284 ED_region_panels_draw(C, region);
285 }
286}
287
289{
290 ARegion *region = params->region;
291 const wmNotifier *wmn = params->notifier;
292
293 switch (wmn->category) {
294 case NC_WM:
295 if (wmn->data == ND_HISTORY) {
296 ED_region_tag_redraw(region);
297 }
298 break;
299 }
300}
301
303{
305 art->regionid = RGN_TYPE_HUD;
309 art->draw = hud_region_draw;
310 art->init = hud_region_init;
311 art->free = hud_region_free;
312
313 /* We need to indicate a preferred size to avoid false `RGN_FLAG_TOO_SMALL`
314 * the first time the region is created. */
315 art->prefsizex = AREAMINX;
316 art->prefsizey = HEADERY;
317
318 hud_panels_register(art, space_type, art->regionid);
319
321 return art;
322}
323
325{
326 ARegion *region = BKE_area_region_new();
328 if (region_win) {
329 BLI_insertlinkbefore(&area->regionbase, region_win, region);
330 }
331 else {
332 BLI_addtail(&area->regionbase, region);
333 }
334 region->regiontype = RGN_TYPE_HUD;
335 region->alignment = RGN_ALIGN_FLOAT;
336 region->overlap = true;
337 region->flag |= RGN_FLAG_DYNAMIC_SIZE;
338
339 return region;
340}
341
343{
344 for (wmWindow &win : wm->windows) {
345 bScreen *screen = WM_window_get_active_screen(&win);
346 for (ScrArea &area : screen->areabase) {
347 if (&area != area_keep) {
348 for (ARegion &region : area.regionbase) {
349 if (region.regiontype == RGN_TYPE_HUD) {
350 if ((region.flag & RGN_FLAG_HIDDEN) == 0) {
351 hud_region_hide(&region);
352 ED_region_tag_redraw(&region);
353 ED_area_tag_redraw(&area);
354 }
355 }
356 }
357 }
358 }
359 }
360}
361
363{
366
368 if (art == nullptr) {
369 return;
370 }
371
373
374 if (region && (region->flag & RGN_FLAG_HIDDEN_BY_USER)) {
375 /* The region is intentionally hidden by the user, don't show it. */
376 hud_region_hide(region);
377 return;
378 }
379
380 bool init = false;
381 const bool was_hidden = region == nullptr || region->runtime->visible == false;
382 ARegion *region_op = CTX_wm_region(C);
383 BLI_assert((region_op == nullptr) || (region_op->regiontype != RGN_TYPE_HUD));
384 const int region_index_hint = region_op ? area_calc_region_type_index(area, region_op) : -1;
385 if (!last_redo_poll(C, region_op ? region_op->regiontype : -1, region_index_hint)) {
386 if (region) {
387 ED_region_tag_redraw(region);
388 hud_region_hide(region);
389 }
390 return;
391 }
392
393 if (region == nullptr) {
394 init = true;
395 region = hud_region_add(area);
396 region->runtime->type = art;
397 }
398
399 /* Let 'ED_area_update_region_sizes' do the work of placing the region.
400 * Otherwise we could set the 'region->winrct' & 'region->winx/winy' here. */
401 if (init) {
402 ED_area_tag_region_size_update(area, region);
403 }
404 else {
405 if (region->flag & RGN_FLAG_HIDDEN) {
406 /* Also forces recalculating HUD size in hud_region_layout(). */
407 ED_area_tag_region_size_update(area, region);
408 }
409 region->flag &= ~RGN_FLAG_HIDDEN;
410 }
411
412 {
413 HudRegionData *hrd = static_cast<HudRegionData *>(region->regiondata);
414 if (hrd == nullptr) {
415 hrd = MEM_new_zeroed<HudRegionData>(__func__);
416 region->regiondata = hrd;
417 }
418 if (region_op) {
419 hrd->regionid = region_op->regiontype;
420 hrd->region_index_hint = region_index_hint;
421 }
422 else {
423 hrd->regionid = -1;
424 hrd->region_index_hint = -1;
425 }
426 }
427
428 if (init) {
429 /* This is needed or 'winrct' will be invalid. */
430 wmWindow *win = CTX_wm_window(C);
432 }
433
435 ED_region_tag_redraw(region);
436
437 /* We need to update/initialize the runtime offsets. */
439 if (region_win) {
440 float x, y;
441
442 view2d_scroller_size_get(&region_win->v2d, true, &x, &y);
443 region->runtime->offset_x = x;
444 region->runtime->offset_y = y;
445 }
446
447 /* Reset zoom level (not well supported). */
448 rctf reset_rect = {};
449 reset_rect.xmax = region->winx;
450 reset_rect.ymax = region->winy;
451 region->v2d.cur = region->v2d.tot = reset_rect;
452 region->v2d.minzoom = 1.0f;
453 region->v2d.maxzoom = 1.0f;
454
455 region->runtime->visible = !(region->flag & RGN_FLAG_HIDDEN);
456
457 /* We shouldn't need to do this every time :S */
458 /* XXX, this is evil! - it also makes the menu show on first draw. :( */
459 if (region->runtime->visible) {
460 ARegion *region_prev = CTX_wm_region(C);
461 CTX_wm_region_set(C, region);
462 hud_region_layout(C, region);
463 if (was_hidden) {
464 region->winx = region->v2d.winx;
465 region->winy = region->v2d.winy;
466 region->v2d.cur = region->v2d.tot = reset_rect;
467 }
468 CTX_wm_region_set(C, region_prev);
469 }
470
471 region->runtime->visible = !((region->flag & RGN_FLAG_HIDDEN) ||
472 (region->flag & RGN_FLAG_TOO_SMALL));
473}
474
476{
477 BLI_assert(hud_region->regiontype == RGN_TYPE_HUD);
478 HudRegionData *hrd = static_cast<HudRegionData *>(hud_region->regiondata);
479
480 if (hrd->regionid == -1) {
481 return nullptr;
482 }
483
485}
486
488
489} // namespace blender::ui
#define STRNCPY_UTF8(dst, src)
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
#define HEADERY
#define AREAMINX
#define UI_SCALE_FAC
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_WM
Definition WM_types.hh:387
#define ND_HISTORY
Definition WM_types.hh:431
#define CLAMP_MAX(a, c)
#define BLI_assert_unreachable()
void panel_drawname_set(Panel *panel, StringRef name)
void ED_area_type_hud_clear(wmWindowManager *wm, ScrArea *area_keep)
static ARegion * hud_region_add(ScrArea *area)
void view2d_view_ortho(const View2D *v2d)
Definition view2d.cc:1080
void view2d_view_restore(const bContext *C)
Definition view2d.cc:1147
void view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
Definition view2d.cc:223
static void hud_region_listener(const wmRegionListenerParams *params)
ARegionType * ED_area_type_hud(int space_type)
static int area_calc_region_type_index(const ScrArea *area, const ARegion *region)
static void hud_panels_register(ARegionType *art, int space_type, int region_type)
void draw_menu_back(uiStyle *style, Block *block, const rcti *rect)
static void hud_panel_operator_redo_draw(const bContext *C, Panel *panel)
void view2d_scroller_size_get(const View2D *v2d, bool mapped, float *r_x, float *r_y)
Definition view2d.cc:1868
static bool hud_panel_operator_redo_poll(const bContext *C, PanelType *)
ARegion * ED_area_type_hud_redo_region_find(const ScrArea *area, const ARegion *hud_region)
static ARegion * area_find_region_by_type_and_index_hint(const ScrArea *area, const short region_type, int index_hint)
ListBaseT< LayoutPanelState > & popup_persistent_layout_panel_states(StringRef idname)
void template_operator_redo_properties(Layout *layout, const bContext *C)
static void hud_region_hide(ARegion *region)
static void hud_region_init(wmWindowManager *wm, ARegion *region)
static bool last_redo_poll(const bContext *C, short region_type, int region_index_hint)
static void hud_panel_operator_redo_draw_header(const bContext *C, Panel *panel)
static void hud_region_draw(const bContext *C, ARegion *region)
static void hud_region_free(ARegion *region)
@ V2D_COMMONVIEW_LIST
Definition UI_view2d.hh:60
static void hud_region_layout(const bContext *C, ARegion *region)
void ED_area_type_hud_ensure(bContext *C, ScrArea *area)
void region_handlers_add(ListBaseT< wmEventHandler > *handlers)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
void wmOrtho2_region_pixelspace(const ARegion *region)
void ED_area_update_region_sizes(wmWindowManager *wm, wmWindow *win, ScrArea *area)
Definition area.cc:2290
std::string WM_operatortype_name(wmOperatorType *ot, PointerRNA *properties)
void GPU_clear_color(float red, float green, float blue, float alpha)
@ REGION_DRAW_LOCK_ALL
wmWindow * CTX_wm_window(const bContext *C)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
wmWindowManager * CTX_wm_manager(const bContext *C)
ARegionType * BKE_regiontype_from_id(const SpaceType *st, int regionid)
Definition screen.cc:364
void ED_area_tag_region_size_update(ScrArea *area, ARegion *changed_region)
Definition area.cc:820
void ED_region_floating_init(ARegion *region)
Definition area.cc:2544
ARegion * BKE_area_region_new()
Definition screen.cc:522
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
@ ED_KEYMAP_UI
Definition ED_screen.hh:852
@ ED_KEYMAP_VIEW2D
Definition ED_screen.hh:855
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:1024
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
@ PANEL_TYPE_DEFAULT_CLOSED
wmOperator * WM_operator_last_redo(const bContext *C)
bool WM_operator_repeat_check(const bContext *, wmOperator *op)
void CTX_wm_region_set(bContext *C, ARegion *region)
bool WM_operator_ui_poll(wmOperatorType *ot, PointerRNA *ptr)
void ED_region_panels_layout(const bContext *C, ARegion *region)
Definition area.cc:3835
@ RGN_FLAG_HIDDEN_BY_USER
@ RGN_FLAG_TEMP_REGIONDATA
bool WM_operator_check_ui_enabled(const bContext *C, const char *idname)
bool WM_operator_poll(bContext *C, wmOperatorType *ot)
bScreen * WM_window_get_active_screen(const wmWindow *win)
void ED_region_panels_draw(const bContext *C, ARegion *region)
Definition area.cc:3916
ARegion * CTX_wm_region(const bContext *C)
void ED_region_panels_init(wmWindowManager *wm, ARegion *region)
Definition area.cc:4006
@ AREA_FLAG_REGION_SIZE_UPDATE
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784
void(*) free(ARegion *)
void(*) draw(const bContext *C, ARegion *region)
void(*) listener(const wmRegionListenerParams *params)
void(*) layout(const bContext *C, ARegion *region)
ListBaseT< PanelType > paneltypes
void(*) init(wmWindowManager *wm, ARegion *region)
ListBaseT< Panel > panels
bke::ARegionRuntime * runtime
eRegion_Alignment alignment
eRegion_Type regiontype
char translation_context[BKE_ST_MAXNAME]
void(*) draw(const bContext *C, Panel *panel)
bool(*) poll(const bContext *C, PanelType *pt)
void(*) draw_header(const bContext *C, Panel *panel)
char idname[BKE_ST_MAXNAME]
char label[BKE_ST_MAXNAME]
ListBaseT< LayoutPanelState > * layout_panel_states_storage
ui::Layout * layout
struct Panel_Runtime * runtime
ListBaseT< ARegion > regionbase
struct SpaceType * type
ListBaseT< struct ScrArea > areabase
ListBaseT< wmEventHandler > handlers
struct ARegionType * type
void enabled_set(bool enabled)
Layout & column(bool align)
unsigned int category
Definition WM_types.hh:371
unsigned int data
Definition WM_types.hh:371
struct wmOperatorType * type
#define N_(msgid)