Blender V5.3
wm_operator_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 <cmath>
12
13#include "BLI_array.hh"
14#include "BLI_string.hh"
15
16#include "BKE_context.hh"
17#include "BKE_global.hh"
18#include "BKE_layer.hh"
19
20#include "RNA_access.hh"
21#include "RNA_define.hh"
22
23#include "WM_api.hh" /* Own include. */
24#include "WM_types.hh"
25
26#include "MEM_guardedalloc.h"
27
28#include "ED_object.hh"
29#include "ED_screen.hh"
30
31namespace blender {
32
33/* -------------------------------------------------------------------- */
36
38 const wmEvent *event)
39{
40 if (event->val != KM_PRESS) {
41 if (retval & OPERATOR_PASS_THROUGH) {
42 /* Operators that use this function should either finish or cancel,
43 * otherwise non-press events will be passed through to other key-map items. */
44 BLI_assert((retval & ~OPERATOR_PASS_THROUGH) != 0);
45 if (retval & (OPERATOR_FINISHED | OPERATOR_CANCELLED)) {
46 retval &= ~OPERATOR_PASS_THROUGH;
47 }
48 }
49 }
50 return retval;
51}
52
54
55/* -------------------------------------------------------------------- */
63
65 struct {
66 float mval[2];
67 float prop_value;
69 struct {
70 float prop_value;
71 bool is_snap;
72 bool is_precise;
74 float range[2];
75
76 struct {
80};
81
83 ValueInteraction *inter,
84 const wmEvent *event,
85 const float value_final,
86 const float range[2])
87{
88
89 inter->context_vars.area = CTX_wm_area(C);
90 inter->context_vars.region = CTX_wm_region(C);
91
92 inter->init.mval[0] = event->mval[0];
93 inter->init.mval[1] = event->mval[1];
94 inter->init.prop_value = value_final;
95 inter->prev.prop_value = value_final;
96 inter->range[0] = range[0];
97 inter->range[1] = range[1];
98}
99
101 bContext *C, ValueInteraction *inter, const wmEvent *event, PointerRNA *ptr, PropertyRNA *prop)
102{
103 float range[2];
104 float step, precision;
105 RNA_property_float_ui_range(ptr, prop, &range[0], &range[1], &step, &precision);
106 const float value_final = RNA_property_float_get(ptr, prop);
107 interactive_value_init(C, inter, event, value_final, range);
108}
109
111{
112 ED_area_status_text(inter->context_vars.area, nullptr);
113}
114
116 const wmEvent *event,
117 float *r_value_final)
118{
119 const int mval_axis = 0;
120
121 const float value_scale = 4.0f; /* Could be option. */
122 const float value_range = inter->range[1] - inter->range[0];
123 const int mval_curr = event->mval[mval_axis];
124 const int mval_init = inter->init.mval[mval_axis];
125 float value_delta = (inter->init.prop_value +
126 ((float(mval_curr - mval_init) / inter->context_vars.region->winx) *
127 value_range)) *
128 value_scale;
129 if (event->modifier & KM_CTRL) {
130 const double snap = 0.1;
131 value_delta = roundf(double(value_delta) / snap) * snap;
132 }
133 if (event->modifier & KM_SHIFT) {
134 value_delta *= 0.1f;
135 }
136 const float value_final = inter->init.prop_value + value_delta;
137
138 const bool changed = value_final != inter->prev.prop_value;
139 if (changed) {
140 /* Set the property for the operator and call its modal function. */
141 char str[64];
142 SNPRINTF(str, "%.4f", value_final);
144 }
145
146 inter->prev.prop_value = value_final;
147 inter->prev.is_snap = (event->modifier & KM_CTRL) != 0;
148 inter->prev.is_precise = (event->modifier & KM_SHIFT) != 0;
149
150 *r_value_final = value_final;
151 return changed;
152}
153
155
156/* -------------------------------------------------------------------- */
161
173
174/* Internal callback to free. */
176{
178 if (cd) {
180 MEM_delete(cd);
181 }
182
183 G.moving &= ~G_TRANSFORM_EDIT;
184}
185
187{
189 for (std::unique_ptr<ed::object::XFormObjectData> &xod : cd->objects_xform) {
192 }
193}
194
196{
198}
199
201{
202 if (RNA_property_is_set(op->ptr, op->type->prop)) {
203 return WM_operator_call_notest(C, op);
204 }
205
206 const Main *bmain = CTX_data_main(C);
207 const Scene *scene = CTX_data_scene(C);
208 ViewLayer *view_layer = CTX_data_view_layer(C);
210 *bmain, scene, view_layer, CTX_wm_view3d(C));
211 if (objects.is_empty()) {
212 return OPERATOR_CANCELLED;
213 }
214
215 ObCustomData_ForEditMode *cd = MEM_new<ObCustomData_ForEditMode>(__func__);
217 cd->wait_for_input = RNA_boolean_get(op->ptr, "wait_for_input");
218 cd->is_active = !cd->wait_for_input;
219 cd->is_first = true;
220
221 if (cd->wait_for_input == false) {
222 interactive_value_init_from_property(C, &cd->inter, event, op->ptr, op->type->prop);
223 }
224
225 cd->objects_xform.reinitialize(objects.size());
226 for (const int i : objects.index_range()) {
227 Object *obedit = objects[i];
229 }
230
231 op->customdata = cd;
232
234 G.moving |= G_TRANSFORM_EDIT;
235
237}
238
240{
242
243 /* Special case, check if we release the event that activated this operator. */
244 if ((event->type == cd->launch_event) && (event->val == KM_RELEASE)) {
245 if (cd->wait_for_input == false) {
247 return OPERATOR_FINISHED;
248 }
249 }
250
251 switch (event->type) {
252 case MOUSEMOVE:
253 case EVT_LEFTCTRLKEY:
254 case EVT_RIGHTCTRLKEY:
255 case EVT_LEFTSHIFTKEY:
256 case EVT_RIGHTSHIFTKEY: {
257 float value_final;
258 if (cd->is_active && interactive_value_update(&cd->inter, event, &value_final)) {
260
261 RNA_property_float_set(op->ptr, op->type->prop, value_final);
262 if (cd->is_first == false) {
264 }
265
266 wm->op_undo_depth++;
267 const wmOperatorStatus retval = op->type->exec(C, op);
268 OPERATOR_RETVAL_CHECK(retval);
269 wm->op_undo_depth--;
270
271 cd->is_first = false;
272
273 if ((retval & OPERATOR_FINISHED) == 0) {
275 return OPERATOR_CANCELLED;
276 }
277 }
278 break;
279 }
280 case EVT_RETKEY:
281 case EVT_PADENTER:
282 case LEFTMOUSE: {
283 if (cd->wait_for_input) {
284 if (event->val == KM_PRESS) {
285 if (cd->is_active == false) {
286 cd->is_active = true;
287 interactive_value_init_from_property(C, &cd->inter, event, op->ptr, op->type->prop);
288 }
289 }
290 else if (event->val == KM_RELEASE) {
291 if (cd->is_active == true) {
293 return OPERATOR_FINISHED;
294 }
295 }
296 }
297 else {
298 if (event->val == KM_RELEASE) {
300 return OPERATOR_FINISHED;
301 }
302 }
303 break;
304 }
305 case EVT_ESCKEY:
306 case RIGHTMOUSE: {
307 if (event->val == KM_PRESS) {
308 if (cd->is_active == true) {
310 }
312 return OPERATOR_CANCELLED;
313 }
314 break;
315 }
316 default: {
317 break;
318 }
319 }
321}
322
324{
325 PropertyRNA *prop;
326
327 BLI_assert(ot->modal == nullptr);
328 BLI_assert(ot->invoke == nullptr);
329 BLI_assert(ot->cancel == nullptr);
330 BLI_assert(ot->prop != nullptr);
331
335
336 prop = RNA_def_boolean(ot->srna, "wait_for_input", true, "Wait for Input", "");
338}
339
341
342} // namespace blender
#define SNPRINTF(dst, format,...)
#define OPERATOR_RETVAL_CHECK(ret)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
int64_t size() const
bool is_empty() const
IndexRange index_range() const
#define roundf(x)
#define str(s)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
void data_xform_restore(XFormObjectData &xod)
std::unique_ptr< XFormObjectData > data_xform_create_from_edit_mode(ID *id)
void data_xform_tag_update(XFormObjectData &xod)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
wmOperatorStatus WM_operator_flag_only_pass_through_on_press(wmOperatorStatus retval, const wmEvent *event)
static wmOperatorStatus op_generic_value_modal(bContext *C, wmOperator *op, const wmEvent *event)
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
static bool interactive_value_update(ValueInteraction *inter, const wmEvent *event, float *r_value_final)
static void op_generic_value_cancel(bContext *, wmOperator *op)
wmOperatorStatus WM_operator_call_notest(bContext *C, wmOperator *op)
static void interactive_value_exit(ValueInteraction *inter)
ViewLayer * CTX_data_view_layer(const bContext *C)
static void interactive_value_init_from_property(bContext *C, ValueInteraction *inter, const wmEvent *event, PointerRNA *ptr, PropertyRNA *prop)
Global G
Definition blender.cc:57
static void op_generic_value_exit(wmOperator *op)
wmWindowManager * CTX_wm_manager(const bContext *C)
void ED_area_status_text(ScrArea *area, const char *str)
Definition area.cc:975
static void op_generic_value_restore(wmOperator *op)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Main * CTX_data_main(const bContext *C)
static wmOperatorStatus op_generic_value_invoke(bContext *C, wmOperator *op, const wmEvent *event)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
@ G_TRANSFORM_EDIT
@ KM_RELEASE
Definition WM_types.hh:325
Scene * CTX_data_scene(const bContext *C)
void WM_operator_type_modal_from_exec_for_object_edit_coords(wmOperatorType *ot)
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
int WM_userdef_event_type_from_keymap_type(int kmitype)
ARegion * CTX_wm_region(const bContext *C)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
View3D * CTX_wm_view3d(const bContext *C)
static void interactive_value_init(bContext *C, ValueInteraction *inter, const wmEvent *event, const float value_final, const float range[2])
vector snap(vector a, vector b)
Definition node_math.h:147
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Array< std::unique_ptr< ed::object::XFormObjectData > > objects_xform
struct blender::ValueInteraction::@374175025043227376010141067344066117240370362266 init
struct blender::ValueInteraction::@002176324267031152061356325266040213007124017151 prev
struct blender::ValueInteraction::@010075370330154005075264377370312313324032006104 context_vars
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct wmOperatorType * type