Blender V5.3
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.hh"
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
33namespace blender {
34
35/************************ add/del boid rule operators *********************/
37{
38 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", RNA_ParticleSettings);
39 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
40 eBoidRuleType type = eBoidRuleType(RNA_enum_get(op->ptr, "type"));
41
42 BoidRule *rule;
44
45 if (!part || part->phystype != PART_PHYS_BOIDS) {
46 return OPERATOR_CANCELLED;
47 }
48
50
51 for (BoidRule &rule : state->rules) {
52 rule.flag &= ~BOIDRULE_CURRENT;
53 }
54
55 rule = boid_new_rule(type);
56 rule->flag |= BOIDRULE_CURRENT;
57
58 BLI_addtail(&state->rules, rule);
59
61
62 return OPERATOR_FINISHED;
63}
64
66{
67 /* identifiers */
68 ot->name = "Add Boid Rule";
69 ot->description = "Add a boid rule to the current boid state";
70 ot->idname = "BOID_OT_rule_add";
71
72 /* API callbacks. */
74 ot->exec = rule_add_exec;
75
76 /* flags */
78
79 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_boidrule_type_items, 0, "Type", "");
80}
82{
83 Main *bmain = CTX_data_main(C);
84 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", RNA_ParticleSettings);
85 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
86 BoidRule *rule;
88
89 if (!part || part->phystype != PART_PHYS_BOIDS) {
90 return OPERATOR_CANCELLED;
91 }
92
94
95 for (BoidRule &rule : state->rules) {
96 if (rule.flag & BOIDRULE_CURRENT) {
97 BLI_remlink(&state->rules, &rule);
98 MEM_delete(&rule);
99 break;
100 }
101 }
102 rule = state->rules.first();
103
104 if (rule) {
105 rule->flag |= BOIDRULE_CURRENT;
106 }
107
110
111 return OPERATOR_FINISHED;
112}
113
115{
116 /* identifiers */
117 ot->name = "Remove Boid Rule";
118 ot->idname = "BOID_OT_rule_del";
119 ot->description = "Delete current boid rule";
120
121 /* API callbacks. */
122 ot->exec = rule_del_exec;
123
124 /* flags */
126}
127
128/************************ move up/down boid rule operators *********************/
130{
131 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", RNA_ParticleSettings);
132 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
134
135 if (!part || part->phystype != PART_PHYS_BOIDS) {
136 return OPERATOR_CANCELLED;
137 }
138
140 for (BoidRule &rule : state->rules) {
141 if (rule.flag & BOIDRULE_CURRENT && rule.prev) {
142 BLI_remlink(&state->rules, &rule);
143 BLI_insertlinkbefore(&state->rules, rule.prev, &rule);
144
146 break;
147 }
148 }
149
150 return OPERATOR_FINISHED;
151}
152
154{
155 ot->name = "Move Up Boid Rule";
156 ot->description = "Move boid rule up in the list";
157 ot->idname = "BOID_OT_rule_move_up";
158
160
161 /* flags */
163}
164
166{
167 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", RNA_ParticleSettings);
168 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
170
171 if (!part || part->phystype != PART_PHYS_BOIDS) {
172 return OPERATOR_CANCELLED;
173 }
174
176 for (BoidRule &rule : state->rules) {
177 if (rule.flag & BOIDRULE_CURRENT && rule.next) {
178 BLI_remlink(&state->rules, &rule);
179 BLI_insertlinkafter(&state->rules, rule.next, &rule);
180
182 break;
183 }
184 }
185
186 return OPERATOR_FINISHED;
187}
188
190{
191 ot->name = "Move Down Boid Rule";
192 ot->description = "Move boid rule down in the list";
193 ot->idname = "BOID_OT_rule_move_down";
194
196
197 /* flags */
199}
200
201/************************ add/del boid state operators *********************/
203{
204 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", RNA_ParticleSettings);
205 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
207
208 if (!part || part->phystype != PART_PHYS_BOIDS) {
209 return OPERATOR_CANCELLED;
210 }
211
212 for (BoidState &state : part->boids->states) {
214 }
215
216 state = boid_new_state(part->boids);
217 state->flag |= BOIDSTATE_CURRENT;
218
219 BLI_addtail(&part->boids->states, state);
220
221 return OPERATOR_FINISHED;
222}
223
225{
226 /* identifiers */
227 ot->name = "Add Boid State";
228 ot->description = "Add a boid state to the particle system";
229 ot->idname = "BOID_OT_state_add";
230
231 /* API callbacks. */
232 ot->exec = state_add_exec;
233
234 /* flags */
236}
238{
239 Main *bmain = CTX_data_main(C);
240 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", RNA_ParticleSettings);
241 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
243
244 if (!part || part->phystype != PART_PHYS_BOIDS) {
245 return OPERATOR_CANCELLED;
246 }
247
248 for (BoidState &state : part->boids->states) {
249 if (state.flag & BOIDSTATE_CURRENT) {
250 BLI_remlink(&part->boids->states, &state);
251 MEM_delete(&state);
252 break;
253 }
254 }
255
256 /* there must be at least one state */
257 if (!part->boids->states.first_) {
258 state = boid_new_state(part->boids);
259 BLI_addtail(&part->boids->states, state);
260 }
261 else {
262 state = part->boids->states.first();
263 }
264
265 state->flag |= BOIDSTATE_CURRENT;
266
269
270 return OPERATOR_FINISHED;
271}
272
274{
275 /* identifiers */
276 ot->name = "Remove Boid State";
277 ot->idname = "BOID_OT_state_del";
278 ot->description = "Delete current boid state";
279
280 /* API callbacks. */
281 ot->exec = state_del_exec;
282
283 /* flags */
285}
286
287/************************ move up/down boid state operators *********************/
289{
290 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", RNA_ParticleSettings);
291 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
292 BoidSettings *boids;
293
294 if (!part || part->phystype != PART_PHYS_BOIDS) {
295 return OPERATOR_CANCELLED;
296 }
297
298 boids = part->boids;
299
300 for (BoidState &state : boids->states) {
301 if (state.flag & BOIDSTATE_CURRENT && state.prev) {
302 BLI_remlink(&boids->states, &state);
303 BLI_insertlinkbefore(&boids->states, state.prev, &state);
304 break;
305 }
306 }
307
308 return OPERATOR_FINISHED;
309}
310
312{
313 ot->name = "Move Up Boid State";
314 ot->description = "Move boid state up in the list";
315 ot->idname = "BOID_OT_state_move_up";
316
318
319 /* flags */
321}
322
324{
325 PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", RNA_ParticleSettings);
326 ParticleSettings *part = static_cast<ParticleSettings *>(ptr.data);
327 BoidSettings *boids;
328
329 if (!part || part->phystype != PART_PHYS_BOIDS) {
330 return OPERATOR_CANCELLED;
331 }
332
333 boids = part->boids;
334
335 for (BoidState &state : boids->states) {
336 if (state.flag & BOIDSTATE_CURRENT && state.next) {
337 BLI_remlink(&boids->states, &state);
338 BLI_insertlinkafter(&boids->states, state.next, &state);
340 break;
341 }
342 }
343
344 return OPERATOR_FINISHED;
345}
346
348{
349 ot->name = "Move Down Boid State";
350 ot->description = "Move boid state down in the list";
351 ot->idname = "BOID_OT_state_move_down";
352
354
355 /* flags */
357}
358
359} // namespace blender
#define BOIDSTATE_CURRENT
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
const int state
BMesh Iterators.
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void BOID_OT_state_move_up(wmOperatorType *ot)
static wmOperatorStatus rule_move_down_exec(bContext *C, wmOperator *)
static wmOperatorStatus state_move_down_exec(bContext *C, wmOperator *)
static wmOperatorStatus rule_add_exec(bContext *C, wmOperator *op)
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 BOID_OT_rule_add(wmOperatorType *ot)
void BOID_OT_state_del(wmOperatorType *ot)
static wmOperatorStatus rule_move_up_exec(bContext *C, wmOperator *)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
struct BoidRule * boid_new_rule(eBoidRuleType type)
Definition boids.cc:1586
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
void BOID_OT_rule_move_down(wmOperatorType *ot)
static wmOperatorStatus rule_del_exec(bContext *C, wmOperator *)
void BOID_OT_state_add(wmOperatorType *ot)
static wmOperatorStatus state_move_up_exec(bContext *C, wmOperator *)
const EnumPropertyItem rna_enum_boidrule_type_items[]
Definition rna_boid.cc:24
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
struct BoidState * boid_new_state(struct BoidSettings *boids)
Definition boids.cc:1663
void BOID_OT_rule_move_up(wmOperatorType *ot)
@ ID_RECALC_PSYS_RESET
Definition DNA_ID.h:1083
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static wmOperatorStatus state_del_exec(bContext *C, wmOperator *)
static wmOperatorStatus state_add_exec(bContext *C, wmOperator *)
void DEG_relations_tag_update(Main *bmain)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
struct BoidState * boid_get_current_state(struct BoidSettings *boids)
Definition boids.cc:1733
void BOID_OT_rule_del(wmOperatorType *ot)
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
void BOID_OT_state_move_down(wmOperatorType *ot)
struct BoidRule * next
eBoidRule_Flag flag
struct BoidRule * prev
ListBaseT< BoidState > states
struct BoidSettings * boids
eParticlePhysicsType phystype
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141