Blender V5.3
interface_template_event.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_string_ref.hh"
10
11#include "BLT_translation.hh"
12
13#include "WM_keymap.hh"
14
16#include "interface_intern.hh"
17
18namespace blender::ui {
19
21 const EnumPropertyItem *item)
22{
23 if (item == nullptr) {
24 return nullptr;
25 }
26
27 for (wmKeyMapItem *kmi = keymap->items.first(); kmi; kmi = kmi->next) {
28 if (kmi->propvalue == item->value) {
29 return kmi;
30 }
31 }
32
33 return nullptr;
34}
35
36static bool keymap_item_can_collapse(const wmKeyMapItem *kmi_a, const wmKeyMapItem *kmi_b)
37{
38 return (kmi_a->shift == kmi_b->shift && kmi_a->ctrl == kmi_b->ctrl && kmi_a->alt == kmi_b->alt &&
39 kmi_a->oskey == kmi_b->oskey && kmi_a->hyper == kmi_b->hyper);
40}
41
43 wmOperator *op,
44 const wmKeyMap *keymap,
45 const EnumPropertyItem *item)
46{
47 const wmKeyMapItem *kmi = keymap_item_from_enum_item(keymap, item);
48 if (kmi == nullptr) {
49 return 0;
50 }
51
52 if ((kmi->val == KM_RELEASE) && ISKEYBOARD(kmi->type)) {
53 /* Assume release events just disable something which was toggled on. */
54 return 1;
55 }
56
57 /* Try to merge some known triplet items to save horizontal space. */
58 const EnumPropertyItem *item_y = (item[1].identifier) ? item + 1 : nullptr;
59 const EnumPropertyItem *item_z = (item_y && item[2].identifier) ? item + 2 : nullptr;
60 const wmKeyMapItem *kmi_y = keymap_item_from_enum_item(keymap, item_y);
61 const wmKeyMapItem *kmi_z = keymap_item_from_enum_item(keymap, item_z);
62
63 if (kmi_y && kmi_z && keymap_item_can_collapse(kmi, kmi_y) &&
64 keymap_item_can_collapse(kmi_y, kmi_z))
65 {
66 const char *xyz_label = nullptr;
67
68 if (STREQ(item->identifier, "AXIS_X") && STREQ(item_y->identifier, "AXIS_Y") &&
69 STREQ(item_z->identifier, "AXIS_Z"))
70 {
71 xyz_label = IFACE_("Axis");
72 }
73 else if (STREQ(item->identifier, "PLANE_X") && STREQ(item_y->identifier, "PLANE_Y") &&
74 STREQ(item_z->identifier, "PLANE_Z"))
75 {
76 xyz_label = IFACE_("Plane");
77 }
78
79 if (STREQ(item->identifier, "PROPORTIONAL_SIZE_UP") &&
80 STREQ(item_y->identifier, "PROPORTIONAL_SIZE_DOWN") &&
81 STREQ(item_z->identifier, "PROPORTIONAL_SIZE"))
82 {
83 xyz_label = IFACE_("Proportional Size");
84 }
85
86 if (xyz_label) {
87 int icon_mod[KM_MOD_NUM] = {0};
88#ifdef WITH_HEADLESS
89 int icon = 0;
90#else
91 int icon = icon_from_keymap_item(kmi, icon_mod);
92#endif
93 for (int j = 0; j < ARRAY_SIZE(icon_mod) && icon_mod[j]; j++) {
94 layout->label("", icon_mod[j]);
95 const float offset = event_icon_offset(icon_mod[j]);
96 if (offset != 0.0f) {
97 layout->separator(offset);
98 }
99 }
100 layout->label("", icon);
101 layout->separator(event_icon_offset(icon));
102
103#ifndef WITH_HEADLESS
104 icon = icon_from_keymap_item(kmi_y, icon_mod);
105#endif
106 layout->label("", icon);
107 layout->separator(event_icon_offset(icon));
108
109 if ((keymap->poll_modal_item == nullptr) ||
110 (keymap->poll_modal_item(op, item_z->value) != false))
111 {
112 /* Z item is included. */
113#ifndef WITH_HEADLESS
114 icon = icon_from_keymap_item(kmi_z, icon_mod);
115#endif
116 layout->label("", icon);
117 layout->separator(event_icon_offset(icon));
118 layout->separator(0.2f);
119 }
120
121 layout->label(xyz_label, ICON_NONE);
122 layout->separator(0.6f);
123 return 3;
124 }
125 }
126
127 /* Try to merge some known paired items to save horizontal space. */
128 if (kmi_y && keymap_item_can_collapse(kmi, kmi_y)) {
129 const char *ab_label = nullptr;
130
131 if (STREQ(item->identifier, "PROPORTIONAL_SIZE_UP") &&
132 STREQ(item_y->identifier, "PROPORTIONAL_SIZE_DOWN"))
133 {
134 ab_label = IFACE_("Proportional Size");
135 }
136
137 if (ab_label) {
138 int icon_mod[KM_MOD_NUM] = {0};
139#ifdef WITH_HEADLESS
140 int icon = 0;
141#else
142 int icon = icon_from_keymap_item(kmi, icon_mod);
143#endif
144 for (int j = 0; j < ARRAY_SIZE(icon_mod) && icon_mod[j]; j++) {
145 layout->label("", icon_mod[j]);
146 const float offset = event_icon_offset(icon_mod[j]);
147 if (offset != 0.0f) {
148 layout->separator(offset);
149 }
150 }
151 layout->label("", icon);
152 layout->separator(event_icon_offset(icon));
153
154#ifndef WITH_HEADLESS
155 icon = icon_from_keymap_item(kmi_y, icon_mod);
156#endif
157 layout->label("", icon);
158 layout->separator(event_icon_offset(icon));
159 layout->separator(0.2f);
160 layout->label(ab_label, ICON_NONE);
161 layout->separator(0.6f);
162 return 2;
163 }
164 }
165
166 /* Single item without merging. */
167 return template_event_from_keymap_item(layout, item->name, kmi, false) ? 1 : 0;
168}
169
171 const StringRefNull text,
172 const wmKeyMapItem *kmi,
173 bool text_fallback)
174{
175 bool ok = false;
176
177 int icon_mod[KM_MOD_NUM];
178#ifdef WITH_HEADLESS
179 int icon = 0;
180#else
181 const int icon = icon_from_keymap_item(kmi, icon_mod);
182#endif
183 if (icon != 0) {
184 for (int j = 0; j < ARRAY_SIZE(icon_mod) && icon_mod[j]; j++) {
185 layout->label("", icon_mod[j]);
186 const float offset = event_icon_offset(icon_mod[j]);
187 if (offset != 0.0f) {
188 layout->separator(offset);
189 }
190 }
191
192 /* Icon and text separately is closer together with aligned layout. */
193
194 layout->label("", icon);
195 if (icon >= ICON_MOUSE_LMB && icon <= ICON_MOUSE_MMB_SCROLL) {
196 /* Negative space after narrow mice icons. */
197 layout->separator(-0.68f);
198 }
199
200 const float offset = event_icon_offset(icon);
201 if (offset != 0.0f) {
202 layout->separator(offset);
203 }
204
205 layout->separator(0.2f);
206 layout->label(CTX_IFACE_(BLT_I18NCONTEXT_ID_WINDOWMANAGER, text.c_str()), ICON_NONE);
207 layout->separator(0.6f);
208 ok = true;
209 }
210 else if (text_fallback) {
211 const char *event_text = WM_key_event_string(kmi->type, true);
212 layout->label(event_text, ICON_NONE);
213 layout->label(CTX_IFACE_(BLT_I18NCONTEXT_ID_WINDOWMANAGER, text.c_str()), ICON_NONE);
214 layout->separator(0.6f);
215 ok = true;
216 }
217 return ok;
218}
219
220} // namespace blender::ui
#define BLT_I18NCONTEXT_ID_WINDOWMANAGER
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define KM_MOD_NUM
Definition WM_types.hh:310
#define STREQ(a, b)
constexpr const char * c_str() const
int icon_from_keymap_item(const wmKeyMapItem *kmi, int r_icon_mod[KM_MOD_NUM])
bool template_event_from_keymap_item(Layout *layout, StringRefNull text, const wmKeyMapItem *kmi, bool text_fallback)
float event_icon_offset(const int icon_id)
static bool keymap_item_can_collapse(const wmKeyMapItem *kmi_a, const wmKeyMapItem *kmi_b)
static const wmKeyMapItem * keymap_item_from_enum_item(const wmKeyMap *keymap, const EnumPropertyItem *item)
int template_status_bar_modal_item(Layout *layout, wmOperator *op, const wmKeyMap *keymap, const EnumPropertyItem *item)
const char * WM_key_event_string(const short type, const bool compact)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
@ KM_RELEASE
Definition WM_types.hh:325
void label(StringRef name, int icon)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
bool(*) poll_modal_item(const struct wmOperator *op, int value)
ListBaseT< wmKeyMapItem > items
#define ISKEYBOARD(event_type)