Blender  V3.3
interface_region_hud.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2008 Blender Foundation. All rights reserved. */
3 
10 #include <cstring>
11 
12 #include "MEM_guardedalloc.h"
13 
14 #include "DNA_screen_types.h"
15 #include "DNA_userdef_types.h"
16 
17 #include "BLI_listbase.h"
18 #include "BLI_rect.h"
19 #include "BLI_string.h"
20 #include "BLI_utildefines.h"
21 
22 #include "BKE_context.h"
23 #include "BKE_screen.h"
24 
25 #include "WM_api.h"
26 #include "WM_types.h"
27 
28 #include "RNA_access.h"
29 
30 #include "UI_interface.h"
31 #include "UI_view2d.h"
32 
33 #include "BLT_translation.h"
34 
35 #include "ED_screen.h"
36 #include "ED_undo.h"
37 
38 #include "GPU_framebuffer.h"
39 #include "interface_intern.h"
40 
41 /* -------------------------------------------------------------------- */
45 struct HudRegionData {
46  short regionid;
47 };
48 
49 static bool last_redo_poll(const bContext *C, short region_type)
50 {
52  if (op == nullptr) {
53  return false;
54  }
55 
56  bool success = false;
57  {
58  /* Make sure that we are using the same region type as the original
59  * operator call. Otherwise we would be polling the operator with the
60  * wrong context.
61  */
63  ARegion *region_op = (region_type != -1) ? BKE_area_find_region_type(area, region_type) :
64  nullptr;
65  ARegion *region_prev = CTX_wm_region(C);
66  CTX_wm_region_set((bContext *)C, region_op);
67 
68  if (WM_operator_repeat_check(C, op) && WM_operator_check_ui_empty(op->type) == false) {
69  success = WM_operator_poll((bContext *)C, op->type);
70  }
71  CTX_wm_region_set((bContext *)C, region_prev);
72  }
73  return success;
74 }
75 
76 static void hud_region_hide(ARegion *region)
77 {
78  region->flag |= RGN_FLAG_HIDDEN;
79  /* Avoids setting 'AREA_FLAG_REGION_SIZE_UPDATE'
80  * since other regions don't depend on this. */
81  BLI_rcti_init(&region->winrct, 0, 0, 0, 0);
82 }
83 
86 /* -------------------------------------------------------------------- */
91 {
94  if (region != nullptr) {
95  HudRegionData *hrd = static_cast<HudRegionData *>(region->regiondata);
96  if (hrd != nullptr) {
97  return last_redo_poll(C, hrd->regionid);
98  }
99  }
100  return false;
101 }
102 
104 {
106  BLI_strncpy(panel->drawname, WM_operatortype_name(op->type, op->ptr), sizeof(panel->drawname));
107 }
108 
109 static void hud_panel_operator_redo_draw(const bContext *C, Panel *panel)
110 {
112  if (op == nullptr) {
113  return;
114  }
115  if (!WM_operator_check_ui_enabled(C, op->type->name)) {
116  uiLayoutSetEnabled(panel->layout, false);
117  }
118  uiLayout *col = uiLayoutColumn(panel->layout, false);
120 }
121 
122 static void hud_panels_register(ARegionType *art, int space_type, int region_type)
123 {
124  PanelType *pt = MEM_cnew<PanelType>(__func__);
125  strcpy(pt->idname, "OPERATOR_PT_redo");
126  strcpy(pt->label, N_("Redo"));
131  pt->space_type = space_type;
132  pt->region_type = region_type;
134  BLI_addtail(&art->paneltypes, pt);
135 }
136 
139 /* -------------------------------------------------------------------- */
143 static void hud_region_init(wmWindowManager *wm, ARegion *region)
144 {
145  ED_region_panels_init(wm, region);
146 
147  /* Reset zoom from panels init because we don't want zoom allowed for redo panel. */
148  region->v2d.maxzoom = 1.0f;
149  region->v2d.minzoom = 1.0f;
150 
152  region->flag |= RGN_FLAG_TEMP_REGIONDATA;
153 }
154 
155 static void hud_region_free(ARegion *region)
156 {
157  MEM_SAFE_FREE(region->regiondata);
158 }
159 
160 static void hud_region_layout(const bContext *C, ARegion *region)
161 {
162  HudRegionData *hrd = static_cast<HudRegionData *>(region->regiondata);
163  if (hrd == nullptr || !last_redo_poll(C, hrd->regionid)) {
164  ED_region_tag_redraw(region);
165  hud_region_hide(region);
166  return;
167  }
168 
170  const int size_y = region->sizey;
171 
172  ED_region_panels_layout(C, region);
173 
174  if (region->panels.first &&
175  ((area->flag & AREA_FLAG_REGION_SIZE_UPDATE) || (region->sizey != size_y))) {
176  int winx_new = UI_DPI_FAC * (region->sizex + 0.5f);
177  int winy_new = UI_DPI_FAC * (region->sizey + 0.5f);
178  View2D *v2d = &region->v2d;
179 
180  if (region->flag & RGN_FLAG_SIZE_CLAMP_X) {
181  CLAMP_MAX(winx_new, region->winx);
182  }
183  if (region->flag & RGN_FLAG_SIZE_CLAMP_Y) {
184  CLAMP_MAX(winy_new, region->winy);
185  }
186 
187  region->winx = winx_new;
188  region->winy = winy_new;
189 
190  region->winrct.xmax = (region->winrct.xmin + region->winx) - 1;
191  region->winrct.ymax = (region->winrct.ymin + region->winy) - 1;
192 
193  UI_view2d_region_reinit(v2d, V2D_COMMONVIEW_LIST, region->winx, region->winy);
194 
195  /* Weak, but needed to avoid glitches, especially with hi-dpi
196  * (where resizing the view glitches often).
197  * Fortunately this only happens occasionally. */
198  ED_region_panels_layout(C, region);
199  }
200 
201  /* restore view matrix */
203 }
204 
205 static void hud_region_draw(const bContext *C, ARegion *region)
206 {
207  UI_view2d_view_ortho(&region->v2d);
209  GPU_clear_color(0.0f, 0.0f, 0.0f, 0.0f);
210 
211  if ((region->flag & RGN_FLAG_HIDDEN) == 0) {
212  rcti reset_rect = {};
213  reset_rect.xmax = region->winx;
214  reset_rect.ymax = region->winy;
215  ui_draw_menu_back(nullptr, nullptr, &reset_rect);
216  ED_region_panels_draw(C, region);
217  }
218 }
219 
221 {
222  ARegionType *art = MEM_cnew<ARegionType>(__func__);
223  art->regionid = RGN_TYPE_HUD;
225  art->layout = hud_region_layout;
226  art->draw = hud_region_draw;
227  art->init = hud_region_init;
228  art->free = hud_region_free;
229 
230  /* We need to indicate a preferred size to avoid false `RGN_FLAG_TOO_SMALL`
231  * the first time the region is created. */
232  art->prefsizex = AREAMINX;
233  art->prefsizey = HEADERY;
234 
235  hud_panels_register(art, space_type, art->regionid);
236 
237  art->lock = 1; /* can become flag, see BKE_spacedata_draw_locks */
238  return art;
239 }
240 
242 {
243  ARegion *region = MEM_cnew<ARegion>(__func__);
245  if (region_win) {
246  BLI_insertlinkbefore(&area->regionbase, region_win, region);
247  }
248  else {
249  BLI_addtail(&area->regionbase, region);
250  }
251  region->regiontype = RGN_TYPE_HUD;
252  region->alignment = RGN_ALIGN_FLOAT;
253  region->overlap = true;
254  region->flag |= RGN_FLAG_DYNAMIC_SIZE;
255 
256  if (region_win) {
257  float x, y;
258 
259  UI_view2d_scroller_size_get(&region_win->v2d, true, &x, &y);
260  region->runtime.offset_x = x;
261  region->runtime.offset_y = y;
262  }
263 
264  return region;
265 }
266 
268 {
269  LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
270  bScreen *screen = WM_window_get_active_screen(win);
271  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
272  if (area != area_keep) {
273  LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
274  if (region->regiontype == RGN_TYPE_HUD) {
275  if ((region->flag & RGN_FLAG_HIDDEN) == 0) {
276  hud_region_hide(region);
277  ED_region_tag_redraw(region);
279  }
280  }
281  }
282  }
283  }
284  }
285 }
286 
288 {
291 
293  if (art == nullptr) {
294  return;
295  }
296 
298 
299  if (region && (region->flag & RGN_FLAG_HIDDEN_BY_USER)) {
300  /* The region is intentionally hidden by the user, don't show it. */
301  hud_region_hide(region);
302  return;
303  }
304 
305  bool init = false;
306  const bool was_hidden = region == nullptr || region->visible == false;
307  ARegion *region_op = CTX_wm_region(C);
308  BLI_assert((region_op == nullptr) || (region_op->regiontype != RGN_TYPE_HUD));
309  if (!last_redo_poll(C, region_op ? region_op->regiontype : -1)) {
310  if (region) {
311  ED_region_tag_redraw(region);
312  hud_region_hide(region);
313  }
314  return;
315  }
316 
317  if (region == nullptr) {
318  init = true;
319  region = hud_region_add(area);
320  region->type = art;
321  }
322 
323  /* Let 'ED_area_update_region_sizes' do the work of placing the region.
324  * Otherwise we could set the 'region->winrct' & 'region->winx/winy' here. */
325  if (init) {
327  }
328  else {
329  if (region->flag & RGN_FLAG_HIDDEN) {
330  /* Also forces recalculating HUD size in hud_region_layout(). */
332  }
333  region->flag &= ~RGN_FLAG_HIDDEN;
334  }
335 
336  {
337  HudRegionData *hrd = static_cast<HudRegionData *>(region->regiondata);
338  if (hrd == nullptr) {
339  hrd = MEM_cnew<HudRegionData>(__func__);
340  region->regiondata = hrd;
341  }
342  if (region_op) {
343  hrd->regionid = region_op->regiontype;
344  }
345  else {
346  hrd->regionid = -1;
347  }
348  }
349 
350  if (init) {
351  /* This is needed or 'winrct' will be invalid. */
352  wmWindow *win = CTX_wm_window(C);
354  }
355 
356  ED_region_floating_init(region);
357  ED_region_tag_redraw(region);
358 
359  /* Reset zoom level (not well supported). */
360  rctf reset_rect = {};
361  reset_rect.xmax = region->winx;
362  reset_rect.ymax = region->winy;
363  region->v2d.cur = region->v2d.tot = reset_rect;
364  region->v2d.minzoom = 1.0f;
365  region->v2d.maxzoom = 1.0f;
366 
367  region->visible = !(region->flag & RGN_FLAG_HIDDEN);
368 
369  /* We shouldn't need to do this every time :S */
370  /* XXX, this is evil! - it also makes the menu show on first draw. :( */
371  if (region->visible) {
372  ARegion *region_prev = CTX_wm_region(C);
373  CTX_wm_region_set((bContext *)C, region);
374  hud_region_layout(C, region);
375  if (was_hidden) {
376  region->winx = region->v2d.winx;
377  region->winy = region->v2d.winy;
378  region->v2d.cur = region->v2d.tot = reset_rect;
379  }
380  CTX_wm_region_set((bContext *)C, region_prev);
381  }
382 
383  region->visible = !((region->flag & RGN_FLAG_HIDDEN) || (region->flag & RGN_FLAG_TOO_SMALL));
384 }
385 
struct ScrArea * CTX_wm_area(const bContext *C)
Definition: context.c:738
void CTX_wm_region_set(bContext *C, struct ARegion *region)
Definition: context.c:1009
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:713
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:723
struct ARegion * BKE_area_find_region_type(const struct ScrArea *area, int type)
@ PANEL_TYPE_DEFAULT_CLOSED
Definition: BKE_screen.h:279
struct ARegionType * BKE_regiontype_from_id(const struct SpaceType *st, int regionid)
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:340
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition: rct.c:417
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define CLAMP_MAX(a, c)
#define UNUSED(x)
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
#define HEADERY
@ AREA_FLAG_REGION_SIZE_UPDATE
@ RGN_FLAG_DYNAMIC_SIZE
@ RGN_FLAG_SIZE_CLAMP_X
@ RGN_FLAG_HIDDEN
@ RGN_FLAG_SIZE_CLAMP_Y
@ RGN_FLAG_TOO_SMALL
@ RGN_FLAG_TEMP_REGIONDATA
@ RGN_FLAG_HIDDEN_BY_USER
#define AREAMINX
@ RGN_TYPE_WINDOW
@ RGN_TYPE_HUD
@ RGN_ALIGN_FLOAT
void ED_area_tag_redraw(ScrArea *area)
Definition: area.c:729
@ ED_KEYMAP_UI
Definition: ED_screen.h:691
@ ED_KEYMAP_VIEW2D
Definition: ED_screen.h:694
void ED_area_update_region_sizes(struct wmWindowManager *wm, struct wmWindow *win, struct ScrArea *area)
Definition: area.c:1862
void ED_region_panels_draw(const struct bContext *C, struct ARegion *region)
void ED_region_floating_init(struct ARegion *region)
Definition: area.c:2057
void ED_region_panels_init(struct wmWindowManager *wm, struct ARegion *region)
Definition: area.c:3153
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:655
void ED_region_panels_layout(const struct bContext *C, struct ARegion *region)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define C
Definition: RandGen.cpp:25
void uiLayoutSetEnabled(uiLayout *layout, bool enabled)
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiTemplateOperatorRedoProperties(uiLayout *layout, const struct bContext *C)
#define UI_DPI_FAC
Definition: UI_interface.h:305
void UI_region_handlers_add(struct ListBase *handlers)
void UI_view2d_region_reinit(struct View2D *v2d, short type, int winx, int winy)
Definition: view2d.cc:217
void UI_view2d_view_restore(const struct bContext *C)
void UI_view2d_view_ortho(const struct View2D *v2d)
@ V2D_COMMONVIEW_LIST
Definition: UI_view2d.h:38
void UI_view2d_scroller_size_get(const struct View2D *v2d, bool mapped, float *r_x, float *r_y)
uint col
void GPU_clear_color(float red, float green, float blue, float alpha)
void ui_draw_menu_back(struct uiStyle *style, uiBlock *block, rcti *rect)
static void hud_region_draw(const bContext *C, ARegion *region)
static void hud_panels_register(ARegionType *art, int space_type, int region_type)
static ARegion * hud_region_add(ScrArea *area)
static void hud_region_layout(const bContext *C, ARegion *region)
static void hud_region_free(ARegion *region)
static bool hud_panel_operator_redo_poll(const bContext *C, PanelType *UNUSED(pt))
static void hud_region_hide(ARegion *region)
static void hud_region_init(wmWindowManager *wm, ARegion *region)
void ED_area_type_hud_clear(wmWindowManager *wm, ScrArea *area_keep)
ARegionType * ED_area_type_hud(int space_type)
static void hud_panel_operator_redo_draw_header(const bContext *C, Panel *panel)
static bool last_redo_poll(const bContext *C, short region_type)
static void hud_panel_operator_redo_draw(const bContext *C, Panel *panel)
void ED_area_type_hud_ensure(bContext *C, ScrArea *area)
static void area(int d1, int d2, int e1, int e2, float weights[2])
void(* draw)(const struct bContext *C, struct ARegion *region)
Definition: BKE_screen.h:151
short lock
Definition: BKE_screen.h:211
int keymapflag
Definition: BKE_screen.h:208
void(* free)(struct ARegion *)
Definition: BKE_screen.h:169
void(* layout)(const struct bContext *C, struct ARegion *region)
Definition: BKE_screen.h:161
void(* init)(struct wmWindowManager *wm, struct ARegion *region)
Definition: BKE_screen.h:147
ListBase paneltypes
Definition: BKE_screen.h:198
ARegion_Runtime runtime
void * regiondata
ListBase panels
ListBase handlers
short alignment
short regiontype
struct ARegionType * type
void * first
Definition: DNA_listBase.h:31
void(* draw)(const struct bContext *C, struct Panel *panel)
Definition: BKE_screen.h:248
bool(* poll)(const struct bContext *C, struct PanelType *pt)
Definition: BKE_screen.h:242
void(* draw_header)(const struct bContext *C, struct Panel *panel)
Definition: BKE_screen.h:244
short region_type
Definition: BKE_screen.h:234
char idname[BKE_ST_MAXNAME]
Definition: BKE_screen.h:223
short space_type
Definition: BKE_screen.h:233
char translation_context[BKE_ST_MAXNAME]
Definition: BKE_screen.h:226
char label[BKE_ST_MAXNAME]
Definition: BKE_screen.h:224
struct uiLayout * layout
char drawname[64]
short winx
float minzoom
short winy
float maxzoom
ListBase areabase
float xmax
Definition: DNA_vec_types.h:69
float ymax
Definition: DNA_vec_types.h:70
int ymin
Definition: DNA_vec_types.h:64
int ymax
Definition: DNA_vec_types.h:64
int xmin
Definition: DNA_vec_types.h:63
int xmax
Definition: DNA_vec_types.h:63
const char * name
Definition: WM_types.h:888
struct wmOperatorType * type
struct PointerRNA * ptr
#define N_(msgid)
bool WM_operator_poll(bContext *C, wmOperatorType *ot)
bool WM_operator_check_ui_empty(wmOperatorType *ot)
bool WM_operator_repeat_check(const bContext *UNUSED(C), wmOperator *op)
const char * WM_operatortype_name(struct wmOperatorType *ot, struct PointerRNA *properties)
bool WM_operator_check_ui_enabled(const bContext *C, const char *idname)
wmOperator * WM_operator_last_redo(const bContext *C)
void wmOrtho2_region_pixelspace(const ARegion *region)
Definition: wm_subwindow.c:103
bScreen * WM_window_get_active_screen(const wmWindow *win)
Definition: wm_window.c:2300