Blender V5.3
rna_wm_gizmo_api.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
8
9#include <cstdlib>
10
11#include "RNA_define.hh"
12#include "RNA_enum_types.hh"
13
14#include "WM_api.hh"
15
16#include "rna_internal.hh" /* own include */
17
18#ifdef RNA_RUNTIME
19
20# include "BLT_translation.hh"
21
22# include "BKE_context.hh"
23# include "BKE_idprop.hh"
24# include "BKE_report.hh"
25
26# include "UI_interface.hh"
27
28# include "ED_gizmo_library.hh"
29
30namespace blender {
31
32static void rna_gizmo_draw_preset_box(wmGizmo *gz, const float matrix[16], int select_id)
33{
34 ED_gizmo_draw_preset_box(gz, reinterpret_cast<const float (*)[4]>(matrix), select_id);
35}
36
37static void rna_gizmo_draw_preset_arrow(wmGizmo *gz,
38 const float matrix[16],
39 int axis,
40 int select_id)
41{
42 ED_gizmo_draw_preset_arrow(gz, reinterpret_cast<const float (*)[4]>(matrix), axis, select_id);
43}
44
45static void rna_gizmo_draw_preset_circle(wmGizmo *gz,
46 const float matrix[16],
47 int axis,
48 int select_id)
49{
50 ED_gizmo_draw_preset_circle(gz, reinterpret_cast<const float (*)[4]>(matrix), axis, select_id);
51}
52
53/* -------------------------------------------------------------------- */
56
57static void rna_gizmo_target_set_prop(wmGizmo *gz,
58 ReportList *reports,
59 const char *target_propname,
60 PointerRNA *ptr,
61 const char *propname,
62 int index)
63{
64 const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(gz->type,
65 target_propname);
66 if (gz_prop_type == nullptr) {
67 BKE_reportf(reports,
69 "Gizmo target property '%s.%s' not found",
70 gz->type->idname,
71 target_propname);
72 return;
73 }
74
75 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
76 if (prop == nullptr) {
77 BKE_reportf(reports,
79 "Property '%s.%s' not found",
80 RNA_struct_identifier(ptr->type),
81 propname);
82 return;
83 }
84
85 if (gz_prop_type->data_type != RNA_property_type(prop)) {
86 const int gizmo_type_index = RNA_enum_from_value(rna_enum_property_type_items,
87 gz_prop_type->data_type);
88 const int prop_type_index = RNA_enum_from_value(rna_enum_property_type_items,
89 RNA_property_type(prop));
90 BLI_assert((gizmo_type_index != -1) && (prop_type_index == -1));
91
92 BKE_reportf(reports,
94 "Gizmo target '%s.%s' expects '%s', '%s.%s' is '%s'",
95 gz->type->idname,
96 target_propname,
98 RNA_struct_identifier(ptr->type),
99 propname,
101 return;
102 }
103
104 if (RNA_property_array_check(prop)) {
105 if (index == -1) {
106 const int prop_array_length = RNA_property_array_length(ptr, prop);
107 if (gz_prop_type->array_length != prop_array_length) {
108 BKE_reportf(reports,
109 RPT_ERROR,
110 "Gizmo target property '%s.%s' expects an array of length %d, found %d",
111 gz->type->idname,
112 target_propname,
113 gz_prop_type->array_length,
114 prop_array_length);
115 return;
116 }
117 }
118 }
119 else {
120 if (gz_prop_type->array_length != 1) {
121 BKE_reportf(reports,
122 RPT_ERROR,
123 "Gizmo target property '%s.%s' expects an array of length %d",
124 gz->type->idname,
125 target_propname,
126 gz_prop_type->array_length);
127 return;
128 }
129 }
130
131 if (index >= gz_prop_type->array_length) {
132 BKE_reportf(reports,
133 RPT_ERROR,
134 "Gizmo target property '%s.%s', index %d must be below %d",
135 gz->type->idname,
136 target_propname,
137 index,
138 gz_prop_type->array_length);
139 return;
140 }
141
142 WM_gizmo_target_property_def_rna_ptr(gz, gz_prop_type, ptr, prop, index);
143}
144
145static PointerRNA rna_gizmo_target_set_operator(wmGizmo *gz,
146 ReportList *reports,
147 const char *opname,
148 int part_index)
149{
150 wmOperatorType *ot;
151
152 ot = WM_operatortype_find(opname, false); /* print error next */
153 if (!ot || !ot->srna) {
154 BKE_reportf(reports,
155 RPT_ERROR,
156 "%s '%s'",
157 ot ? RPT_("Operator missing srna") : RPT_("Unknown operator"),
158 opname);
159 return {};
160 }
161
162 /* For the return value to be usable, we need 'PointerRNA.data' to be set. */
163 IDProperty *properties = bke::idprop::create_group("wmGizmoProperties").release();
164
165 return *WM_gizmo_operator_set(gz, part_index, ot, properties);
166}
167
169
170/* -------------------------------------------------------------------- */
173
174static bool rna_gizmo_target_is_valid(wmGizmo *gz,
175 ReportList *reports,
176 const char *target_propname)
177{
178 wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, target_propname);
179 if (gz_prop == nullptr) {
180 BKE_reportf(reports,
181 RPT_ERROR,
182 "Gizmo target property '%s.%s' not found",
183 gz->type->idname,
184 target_propname);
185 return false;
186 }
187 return WM_gizmo_target_property_is_valid(gz_prop);
188}
189
191
192} // namespace blender
193
194#else
195
196namespace blender {
197
199{
200 /* Utility draw functions, since we don't expose new OpenGL drawing wrappers via Python yet.
201 * exactly how these should be exposed isn't totally clear.
202 * However it's probably good to have some high level API's for this anyway.
203 * Just note that this could be re-worked once tests are done.
204 */
205
206 FunctionRNA *func;
207 PropertyRNA *parm;
208
209 /* -------------------------------------------------------------------- */
210 /* Primitive Shapes */
211
212 /* draw_preset_box */
213 func = RNA_def_function(srna, "draw_preset_box", "rna_gizmo_draw_preset_box");
214 RNA_def_function_ui_description(func, "Draw a box");
215 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
218 RNA_def_property_ui_text(parm, "", "The matrix to transform");
219 RNA_def_int(func,
220 "select_id",
221 -1,
222 -1,
223 INT_MAX,
224 "ID to use when gizmo is selectable. Use -1 when not selecting.",
225 "",
226 -1,
227 INT_MAX);
228
229 /* draw_preset_box */
230 func = RNA_def_function(srna, "draw_preset_arrow", "rna_gizmo_draw_preset_arrow");
231 RNA_def_function_ui_description(func, "Draw a box");
232 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
235 RNA_def_property_ui_text(parm, "", "The matrix to transform");
236 RNA_def_enum(func, "axis", rna_enum_object_axis_items, 2, "", "Arrow Orientation");
237 RNA_def_int(func,
238 "select_id",
239 -1,
240 -1,
241 INT_MAX,
242 "ID to use when gizmo is selectable. Use -1 when not selecting.",
243 "",
244 -1,
245 INT_MAX);
246
247 func = RNA_def_function(srna, "draw_preset_circle", "rna_gizmo_draw_preset_circle");
248 RNA_def_function_ui_description(func, "Draw a box");
249 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
252 RNA_def_property_ui_text(parm, "", "The matrix to transform");
253 RNA_def_enum(func, "axis", rna_enum_object_axis_items, 2, "", "Arrow Orientation");
254 RNA_def_int(func,
255 "select_id",
256 -1,
257 -1,
258 INT_MAX,
259 "ID to use when gizmo is selectable. Use -1 when not selecting.",
260 "",
261 -1,
262 INT_MAX);
263
264 /* -------------------------------------------------------------------- */
265 /* Other Shapes */
266
267 /* -------------------------------------------------------------------- */
268 /* Property API */
269
270 /* Define Properties */
271 /* NOTE: 'target_set_handler' is defined in `bpy_rna_gizmo.cc`. */
272 func = RNA_def_function(srna, "target_set_prop", "rna_gizmo_target_set_prop");
275 parm = RNA_def_string(func, "target", nullptr, 0, "", "Target property");
277 /* similar to UILayout.prop */
278 parm = RNA_def_pointer(func, "data", "AnyType", "", "Data from which to take property");
280 parm = RNA_def_string(func, "property", nullptr, 0, "", "Identifier of property in data");
282 RNA_def_int(func, "index", -1, -1, INT_MAX, "", "", -1, INT_MAX); /* RNA_NO_INDEX == -1 */
283
284 func = RNA_def_function(srna, "target_set_operator", "rna_gizmo_target_set_operator");
287 "Operator to run when activating the gizmo "
288 "(overrides property targets)");
289 parm = RNA_def_string(func, "operator", nullptr, 0, "", "Target operator");
291 RNA_def_int(func, "index", 0, 0, 255, "Part index", "", 0, 255);
292
293 /* similar to UILayout.operator */
294 parm = RNA_def_pointer(
295 func, "properties", "OperatorProperties", "", "Operator properties to fill in");
297 RNA_def_function_return(func, parm);
298
299 /* Access Properties */
300 /* NOTE: 'target_get', 'target_set' is defined in `bpy_rna_gizmo.cc`. */
301 func = RNA_def_function(srna, "target_is_valid", "rna_gizmo_target_is_valid");
303 parm = RNA_def_string(func, "property", nullptr, 0, "", "Property identifier");
306 parm = RNA_def_boolean(func, "result", false, "", "");
307 RNA_def_function_return(func, parm);
308}
309
311{
312 /* nothing yet */
313}
314
315} // namespace blender
316
317#endif
#define RPT_(msgid)
#define INT_MAX
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
BMesh Iterators.
void ED_gizmo_draw_preset_circle(const wmGizmo *gz, const float mat[4][4], int axis, int select_id)
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
const wmGizmoPropertyType * WM_gizmotype_target_property_find(const wmGizmoType *gzt, const char *idname)
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
@ FUNC_USE_REPORTS
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void RNA_api_gizmo(StructRNA *srna)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
void ED_gizmo_draw_preset_box(const wmGizmo *gz, const float mat[4][4], int select_id)
int RNA_enum_from_value(const EnumPropertyItem *item, const int value)
bool RNA_property_array_check(PropertyRNA *prop)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
PropertyType RNA_property_type(PropertyRNA *prop)
const EnumPropertyItem rna_enum_property_type_items[]
Definition rna_rna.cc:45
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
wmGizmoProperty * WM_gizmo_target_property_find(wmGizmo *gz, const char *idname)
const EnumPropertyItem rna_enum_object_axis_items[]
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition wm_gizmo.cc:199
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
void ED_gizmo_draw_preset_arrow(const wmGizmo *gz, const float mat[4][4], int axis, int select_id)
bool WM_gizmo_target_property_is_valid(const wmGizmoProperty *gz_prop)
void RNA_api_gizmogroup(StructRNA *srna)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier_c_str, const char *call)
const char * RNA_struct_identifier(const StructRNA *type)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void RNA_def_function_flag(FunctionRNA *func, FunctionFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ PROP_NEVER_NULL
Definition RNA_types.hh:447
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
@ PROP_MATRIX
Definition RNA_types.hh:322
@ PARM_REQUIRED
Definition RNA_types.hh:631
@ PARM_RNAPTR
Definition RNA_types.hh:633
void WM_gizmo_target_property_def_rna_ptr(wmGizmo *gz, const wmGizmoPropertyType *gz_prop_type, PointerRNA *ptr, PropertyRNA *prop, int index)
const int rna_matrix_dimsize_4x4[]