Blender V5.0
particle_boids.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Janne Karhu. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_particle_types.h"
14
15#include "BLI_listbase.h"
16
17#include "BKE_boids.h"
18#include "BKE_context.hh"
19
20#include "DEG_depsgraph.hh"
22
23#include "RNA_access.hh"
24#include "RNA_define.hh"
25#include "RNA_enum_types.hh"
26#include "RNA_prototypes.hh"
27
28#include "WM_api.hh"
29#include "WM_types.hh"
30
31#include "physics_intern.hh"
32
33/************************ add/del boid rule operators *********************/
35{
36 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
37 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
38 int type = RNA_enum_get(op->ptr, "type");
39
40 BoidRule *rule;
42
43 if (!part || part->phystype != PART_PHYS_BOIDS) {
44 return OPERATOR_CANCELLED;
45 }
46
48
49 LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
50 rule->flag &= ~BOIDRULE_CURRENT;
51 }
52
53 rule = boid_new_rule(type);
54 rule->flag |= BOIDRULE_CURRENT;
55
56 BLI_addtail(&state->rules, rule);
57
59
60 return OPERATOR_FINISHED;
61}
62
64{
65 /* identifiers */
66 ot->name = "Add Boid Rule";
67 ot->description = "Add a boid rule to the current boid state";
68 ot->idname = "BOID_OT_rule_add";
69
70 /* API callbacks. */
71 ot->invoke = WM_menu_invoke;
72 ot->exec = rule_add_exec;
73
74 /* flags */
76
77 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_boidrule_type_items, 0, "Type", "");
78}
80{
81 Main *bmain = CTX_data_main(C);
82 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
83 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
84 BoidRule *rule;
86
87 if (!part || part->phystype != PART_PHYS_BOIDS) {
88 return OPERATOR_CANCELLED;
89 }
90
92
93 LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
94 if (rule->flag & BOIDRULE_CURRENT) {
95 BLI_remlink(&state->rules, rule);
96 MEM_freeN(rule);
97 break;
98 }
99 }
100 rule = static_cast<BoidRule *>(state->rules.first);
101
102 if (rule) {
103 rule->flag |= BOIDRULE_CURRENT;
104 }
105
108
109 return OPERATOR_FINISHED;
110}
111
113{
114 /* identifiers */
115 ot->name = "Remove Boid Rule";
116 ot->idname = "BOID_OT_rule_del";
117 ot->description = "Delete current boid rule";
118
119 /* API callbacks. */
120 ot->exec = rule_del_exec;
121
122 /* flags */
124}
125
126/************************ move up/down boid rule operators *********************/
128{
129 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
130 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
132
133 if (!part || part->phystype != PART_PHYS_BOIDS) {
134 return OPERATOR_CANCELLED;
135 }
136
138 LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
139 if (rule->flag & BOIDRULE_CURRENT && rule->prev) {
140 BLI_remlink(&state->rules, rule);
141 BLI_insertlinkbefore(&state->rules, rule->prev, rule);
142
144 break;
145 }
146 }
147
148 return OPERATOR_FINISHED;
149}
150
152{
153 ot->name = "Move Up Boid Rule";
154 ot->description = "Move boid rule up in the list";
155 ot->idname = "BOID_OT_rule_move_up";
156
157 ot->exec = rule_move_up_exec;
158
159 /* flags */
161}
162
164{
165 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
166 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
168
169 if (!part || part->phystype != PART_PHYS_BOIDS) {
170 return OPERATOR_CANCELLED;
171 }
172
174 LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
175 if (rule->flag & BOIDRULE_CURRENT && rule->next) {
176 BLI_remlink(&state->rules, rule);
177 BLI_insertlinkafter(&state->rules, rule->next, rule);
178
180 break;
181 }
182 }
183
184 return OPERATOR_FINISHED;
185}
186
188{
189 ot->name = "Move Down Boid Rule";
190 ot->description = "Move boid rule down in the list";
191 ot->idname = "BOID_OT_rule_move_down";
192
193 ot->exec = rule_move_down_exec;
194
195 /* flags */
197}
198
199/************************ add/del boid state operators *********************/
201{
202 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
203 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
205
206 if (!part || part->phystype != PART_PHYS_BOIDS) {
207 return OPERATOR_CANCELLED;
208 }
209
211 state->flag &= ~BOIDSTATE_CURRENT;
212 }
213
214 state = boid_new_state(part->boids);
215 state->flag |= BOIDSTATE_CURRENT;
216
217 BLI_addtail(&part->boids->states, state);
218
219 return OPERATOR_FINISHED;
220}
221
223{
224 /* identifiers */
225 ot->name = "Add Boid State";
226 ot->description = "Add a boid state to the particle system";
227 ot->idname = "BOID_OT_state_add";
228
229 /* API callbacks. */
230 ot->exec = state_add_exec;
231
232 /* flags */
234}
236{
237 Main *bmain = CTX_data_main(C);
238 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
239 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
241
242 if (!part || part->phystype != PART_PHYS_BOIDS) {
243 return OPERATOR_CANCELLED;
244 }
245
247 if (state->flag & BOIDSTATE_CURRENT) {
248 BLI_remlink(&part->boids->states, state);
250 break;
251 }
252 }
253
254 /* there must be at least one state */
255 if (!part->boids->states.first) {
256 state = boid_new_state(part->boids);
257 BLI_addtail(&part->boids->states, state);
258 }
259 else {
260 state = static_cast<BoidState *>(part->boids->states.first);
261 }
262
263 state->flag |= BOIDSTATE_CURRENT;
264
267
268 return OPERATOR_FINISHED;
269}
270
272{
273 /* identifiers */
274 ot->name = "Remove Boid State";
275 ot->idname = "BOID_OT_state_del";
276 ot->description = "Delete current boid state";
277
278 /* API callbacks. */
279 ot->exec = state_del_exec;
280
281 /* flags */
283}
284
285/************************ move up/down boid state operators *********************/
287{
288 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
289 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
290 BoidSettings *boids;
291
292 if (!part || part->phystype != PART_PHYS_BOIDS) {
293 return OPERATOR_CANCELLED;
294 }
295
296 boids = part->boids;
297
298 LISTBASE_FOREACH (BoidState *, state, &boids->states) {
299 if (state->flag & BOIDSTATE_CURRENT && state->prev) {
300 BLI_remlink(&boids->states, state);
301 BLI_insertlinkbefore(&boids->states, state->prev, state);
302 break;
303 }
304 }
305
306 return OPERATOR_FINISHED;
307}
308
310{
311 ot->name = "Move Up Boid State";
312 ot->description = "Move boid state up in the list";
313 ot->idname = "BOID_OT_state_move_up";
314
315 ot->exec = state_move_up_exec;
316
317 /* flags */
319}
320
322{
323 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
324 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
325 BoidSettings *boids;
326
327 if (!part || part->phystype != PART_PHYS_BOIDS) {
328 return OPERATOR_CANCELLED;
329 }
330
331 boids = part->boids;
332
333 LISTBASE_FOREACH (BoidState *, state, &boids->states) {
334 if (state->flag & BOIDSTATE_CURRENT && state->next) {
335 BLI_remlink(&boids->states, state);
336 BLI_insertlinkafter(&boids->states, state->next, state);
338 break;
339 }
340 }
341
342 return OPERATOR_FINISHED;
343}
344
346{
347 ot->name = "Move Down Boid State";
348 ot->description = "Move boid state down in the list";
349 ot->idname = "BOID_OT_state_move_down";
350
351 ot->exec = state_move_down_exec;
352
353 /* flags */
355}
struct BoidState * boid_get_current_state(struct BoidSettings *boids)
Definition boids.cc:1734
struct BoidRule * boid_new_rule(int type)
Definition boids.cc:1585
struct BoidState * boid_new_state(struct BoidSettings *boids)
Definition boids.cc:1664
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
Main * CTX_data_main(const bContext *C)
#define LISTBASE_FOREACH(type, var, list)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:332
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:131
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:371
void DEG_id_tag_update(ID *id, unsigned int flags)
void DEG_relations_tag_update(Main *bmain)
@ ID_RECALC_PSYS_RESET
Definition DNA_ID.h:1083
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
#define BOIDSTATE_CURRENT
@ BOIDRULE_CURRENT
@ PART_PHYS_BOIDS
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
static ulong state[N]
void BOID_OT_state_add(wmOperatorType *ot)
static wmOperatorStatus state_add_exec(bContext *C, wmOperator *)
void BOID_OT_state_move_down(wmOperatorType *ot)
static wmOperatorStatus rule_move_down_exec(bContext *C, wmOperator *)
static wmOperatorStatus rule_add_exec(bContext *C, wmOperator *op)
static wmOperatorStatus state_move_up_exec(bContext *C, wmOperator *)
static wmOperatorStatus state_del_exec(bContext *C, wmOperator *)
static wmOperatorStatus state_move_down_exec(bContext *C, wmOperator *)
static wmOperatorStatus rule_del_exec(bContext *C, wmOperator *)
static wmOperatorStatus rule_move_up_exec(bContext *C, wmOperator *)
void BOID_OT_rule_add(wmOperatorType *ot)
void BOID_OT_rule_move_down(wmOperatorType *ot)
void BOID_OT_rule_move_up(wmOperatorType *ot)
void BOID_OT_state_del(wmOperatorType *ot)
void BOID_OT_state_move_up(wmOperatorType *ot)
void BOID_OT_rule_del(wmOperatorType *ot)
int RNA_enum_get(PointerRNA *ptr, const char *name)
const EnumPropertyItem rna_enum_boidrule_type_items[]
Definition rna_boid.cc:22
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
struct ListBase states
void * first
struct BoidSettings * boids
struct PointerRNA * ptr
PointerRNA * ptr
Definition wm_files.cc:4238
wmOperatorType * ot
Definition wm_files.cc:4237
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)