Blender V5.0
area_utils.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
10
11#include <limits>
12
13#include "BKE_screen.hh"
14
15#include "BLI_rect.h"
16#include "BLI_utildefines.h"
17
18#include "WM_message.hh"
19
20#include "ED_screen.hh"
21
22#include "UI_interface.hh"
23
24/* -------------------------------------------------------------------- */
27
29{
30 wmMsgBus *mbus = params->message_bus;
31 ARegion *region = params->region;
32
33 wmMsgSubscribeValue msg_sub_value_region_tag_redraw{};
34 msg_sub_value_region_tag_redraw.owner = region;
35 msg_sub_value_region_tag_redraw.user_data = region;
36 msg_sub_value_region_tag_redraw.notify = ED_region_do_msg_notify_tag_redraw;
37 WM_msg_subscribe_rna_anon_prop(mbus, WorkSpace, tools, &msg_sub_value_region_tag_redraw);
38}
39
40int ED_region_generic_tools_region_snap_size(const ARegion *region, int size, int axis)
41{
42 if (axis == 0) {
43 /* Using Y axis avoids slight feedback loop when adjusting X. */
44 const float aspect = BLI_rctf_size_y(&region->v2d.cur) /
45 (BLI_rcti_size_y(&region->v2d.mask) + 1);
46 const float column = UI_TOOLBAR_COLUMN / aspect;
47 const float margin = UI_TOOLBAR_MARGIN / aspect;
48 const float snap_units[] = {
49 column + margin,
50 (2.0f * column) + margin,
51 (2.7f * column) + margin,
52 };
53 int best_diff = std::numeric_limits<int>::max();
54 int best_size = size;
55 /* Only snap if less than last snap unit. */
56 if (size <= snap_units[ARRAY_SIZE(snap_units) - 1]) {
57 for (uint i = 0; i < ARRAY_SIZE(snap_units); i += 1) {
58 const int test_size = snap_units[i];
59 const int test_diff = abs(test_size - size);
60 if (test_diff < best_diff) {
61 best_size = test_size;
62 best_diff = test_diff;
63 }
64 }
65 }
66 return best_size;
67 }
68 return size;
69}
70
71int ED_region_generic_panel_region_snap_size(const ARegion *region, int size, int axis)
72{
73 if (axis == 0) {
74 if (!UI_panel_category_is_visible(region)) {
75 return size;
76 }
77
78 /* Using Y axis avoids slight feedback loop when adjusting X. */
79 const float aspect = BLI_rctf_size_y(&region->v2d.cur) /
80 (BLI_rcti_size_y(&region->v2d.mask) + 1);
81 return int(UI_PANEL_CATEGORY_MIN_WIDTH / aspect);
82 }
83 return size;
84}
85
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.h:198
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.h:206
unsigned int uint
#define ARRAY_SIZE(arr)
void ED_region_do_msg_notify_tag_redraw(bContext *C, wmMsgSubscribeKey *msg_key, wmMsgSubscribeValue *msg_val)
Definition area.cc:361
#define UI_TOOLBAR_MARGIN
#define UI_PANEL_CATEGORY_MIN_WIDTH
bool UI_panel_category_is_visible(const ARegion *region)
#define UI_TOOLBAR_COLUMN
int ED_region_generic_tools_region_snap_size(const ARegion *region, int size, int axis)
Definition area_utils.cc:40
void ED_region_generic_tools_region_message_subscribe(const wmRegionMessageSubscribeParams *params)
Definition area_utils.cc:28
int ED_region_generic_panel_region_snap_size(const ARegion *region, int size, int axis)
Definition area_utils.cc:71
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define abs
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
i
Definition text_draw.cc:230
#define WM_msg_subscribe_rna_anon_prop(mbus, type_, prop_, value)