Blender  V3.3
particle_boids.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2009 Janne Karhu. All rights reserved. */
3 
8 #include <stdlib.h>
9 
10 #include "MEM_guardedalloc.h"
11 
12 #include "DNA_particle_types.h"
13 
14 #include "BLI_listbase.h"
15 #include "BLI_utildefines.h"
16 
17 #include "BKE_boids.h"
18 #include "BKE_context.h"
19 #include "BKE_main.h"
20 
21 #include "DEG_depsgraph.h"
22 #include "DEG_depsgraph_build.h"
23 
24 #include "RNA_access.h"
25 #include "RNA_define.h"
26 #include "RNA_enum_types.h"
27 #include "RNA_prototypes.h"
28 
29 #include "WM_api.h"
30 #include "WM_types.h"
31 
32 #include "physics_intern.h"
33 
34 /************************ add/del boid rule operators *********************/
36 {
37  PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
38  ParticleSettings *part = ptr.data;
39  int type = RNA_enum_get(op->ptr, "type");
40 
41  BoidRule *rule;
43 
44  if (!part || part->phystype != PART_PHYS_BOIDS) {
45  return OPERATOR_CANCELLED;
46  }
47 
49 
50  for (rule = state->rules.first; rule; rule = rule->next) {
51  rule->flag &= ~BOIDRULE_CURRENT;
52  }
53 
54  rule = boid_new_rule(type);
55  rule->flag |= BOIDRULE_CURRENT;
56 
57  BLI_addtail(&state->rules, rule);
58 
60 
61  return OPERATOR_FINISHED;
62 }
63 
65 {
66  /* identifiers */
67  ot->name = "Add Boid Rule";
68  ot->description = "Add a boid rule to the current boid state";
69  ot->idname = "BOID_OT_rule_add";
70 
71  /* api callbacks */
74 
75  /* flags */
77 
78  ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_boidrule_type_items, 0, "Type", "");
79 }
81 {
82  Main *bmain = CTX_data_main(C);
83  PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
84  ParticleSettings *part = ptr.data;
85  BoidRule *rule;
87 
88  if (!part || part->phystype != PART_PHYS_BOIDS) {
89  return OPERATOR_CANCELLED;
90  }
91 
93 
94  for (rule = state->rules.first; rule; rule = rule->next) {
95  if (rule->flag & BOIDRULE_CURRENT) {
96  BLI_remlink(&state->rules, rule);
97  MEM_freeN(rule);
98  break;
99  }
100  }
101  rule = state->rules.first;
102 
103  if (rule) {
104  rule->flag |= BOIDRULE_CURRENT;
105  }
106 
109 
110  return OPERATOR_FINISHED;
111 }
112 
114 {
115  /* identifiers */
116  ot->name = "Remove Boid Rule";
117  ot->idname = "BOID_OT_rule_del";
118  ot->description = "Delete current boid rule";
119 
120  /* api callbacks */
121  ot->exec = rule_del_exec;
122 
123  /* flags */
125 }
126 
127 /************************ move up/down boid rule operators *********************/
129 {
130  PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
131  ParticleSettings *part = ptr.data;
132  BoidRule *rule;
133  BoidState *state;
134 
135  if (!part || part->phystype != PART_PHYS_BOIDS) {
136  return OPERATOR_CANCELLED;
137  }
138 
140  for (rule = state->rules.first; rule; rule = rule->next) {
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 = ptr.data;
169  BoidRule *rule;
170  BoidState *state;
171 
172  if (!part || part->phystype != PART_PHYS_BOIDS) {
173  return OPERATOR_CANCELLED;
174  }
175 
177  for (rule = state->rules.first; rule; rule = rule->next) {
178  if (rule->flag & BOIDRULE_CURRENT && rule->next) {
179  BLI_remlink(&state->rules, rule);
180  BLI_insertlinkafter(&state->rules, rule->next, rule);
181 
183  break;
184  }
185  }
186 
187  return OPERATOR_FINISHED;
188 }
189 
191 {
192  ot->name = "Move Down Boid Rule";
193  ot->description = "Move boid rule down in the list";
194  ot->idname = "BOID_OT_rule_move_down";
195 
197 
198  /* flags */
200 }
201 
202 /************************ add/del boid state operators *********************/
204 {
205  PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
206  ParticleSettings *part = ptr.data;
207  BoidState *state;
208 
209  if (!part || part->phystype != PART_PHYS_BOIDS) {
210  return OPERATOR_CANCELLED;
211  }
212 
213  for (state = part->boids->states.first; state; state = state->next) {
214  state->flag &= ~BOIDSTATE_CURRENT;
215  }
216 
217  state = boid_new_state(part->boids);
218  state->flag |= BOIDSTATE_CURRENT;
219 
220  BLI_addtail(&part->boids->states, state);
221 
222  return OPERATOR_FINISHED;
223 }
224 
226 {
227  /* identifiers */
228  ot->name = "Add Boid State";
229  ot->description = "Add a boid state to the particle system";
230  ot->idname = "BOID_OT_state_add";
231 
232  /* api callbacks */
234 
235  /* flags */
237 }
239 {
240  Main *bmain = CTX_data_main(C);
241  PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
242  ParticleSettings *part = ptr.data;
243  BoidState *state;
244 
245  if (!part || part->phystype != PART_PHYS_BOIDS) {
246  return OPERATOR_CANCELLED;
247  }
248 
249  for (state = part->boids->states.first; state; state = state->next) {
250  if (state->flag & BOIDSTATE_CURRENT) {
251  BLI_remlink(&part->boids->states, state);
252  MEM_freeN(state);
253  break;
254  }
255  }
256 
257  /* there must be at least one state */
258  if (!part->boids->states.first) {
259  state = boid_new_state(part->boids);
260  BLI_addtail(&part->boids->states, state);
261  }
262  else {
263  state = part->boids->states.first;
264  }
265 
266  state->flag |= BOIDSTATE_CURRENT;
267 
270 
271  return OPERATOR_FINISHED;
272 }
273 
275 {
276  /* identifiers */
277  ot->name = "Remove Boid State";
278  ot->idname = "BOID_OT_state_del";
279  ot->description = "Delete current boid state";
280 
281  /* api callbacks */
283 
284  /* flags */
286 }
287 
288 /************************ move up/down boid state operators *********************/
290 {
291  PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
292  ParticleSettings *part = ptr.data;
293  BoidSettings *boids;
294  BoidState *state;
295 
296  if (!part || part->phystype != PART_PHYS_BOIDS) {
297  return OPERATOR_CANCELLED;
298  }
299 
300  boids = part->boids;
301 
302  for (state = boids->states.first; state; state = state->next) {
303  if (state->flag & BOIDSTATE_CURRENT && state->prev) {
304  BLI_remlink(&boids->states, state);
305  BLI_insertlinkbefore(&boids->states, state->prev, state);
306  break;
307  }
308  }
309 
310  return OPERATOR_FINISHED;
311 }
312 
314 {
315  ot->name = "Move Up Boid State";
316  ot->description = "Move boid state up in the list";
317  ot->idname = "BOID_OT_state_move_up";
318 
320 
321  /* flags */
323 }
324 
326 {
327  PointerRNA ptr = CTX_data_pointer_get_type(C, "particle_settings", &RNA_ParticleSettings);
328  ParticleSettings *part = ptr.data;
329  BoidSettings *boids;
330  BoidState *state;
331 
332  if (!part || part->phystype != PART_PHYS_BOIDS) {
333  return OPERATOR_CANCELLED;
334  }
335 
336  boids = part->boids;
337 
338  for (state = boids->states.first; state; state = state->next) {
339  if (state->flag & BOIDSTATE_CURRENT && state->next) {
340  BLI_remlink(&boids->states, state);
341  BLI_insertlinkafter(&boids->states, state->next, state);
343  break;
344  }
345  }
346 
347  return OPERATOR_FINISHED;
348 }
349 
351 {
352  ot->name = "Move Down Boid State";
353  ot->description = "Move boid state down in the list";
354  ot->idname = "BOID_OT_state_move_down";
355 
357 
358  /* flags */
360 }
struct BoidState * boid_get_current_state(struct BoidSettings *boids)
Definition: boids.c:1710
struct BoidRule * boid_new_rule(int type)
Definition: boids.c:1574
struct BoidState * boid_new_state(struct BoidSettings *boids)
Definition: boids.c:1640
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
Definition: context.c:473
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:301
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:340
#define UNUSED(x)
void DEG_id_tag_update(struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
@ ID_RECALC_PSYS_RESET
Definition: DNA_ID.h:800
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:791
#define BOIDSTATE_CURRENT
#define BOIDRULE_CURRENT
#define PART_PHYS_BOIDS
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
const int state
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void BOID_OT_state_add(wmOperatorType *ot)
static int state_add_exec(bContext *C, wmOperator *UNUSED(op))
static int state_move_up_exec(bContext *C, wmOperator *UNUSED(op))
static int state_del_exec(bContext *C, wmOperator *UNUSED(op))
static int state_move_down_exec(bContext *C, wmOperator *UNUSED(op))
void BOID_OT_state_move_down(wmOperatorType *ot)
static int rule_move_up_exec(bContext *C, wmOperator *UNUSED(op))
static int rule_del_exec(bContext *C, wmOperator *UNUSED(op))
static int rule_move_down_exec(bContext *C, wmOperator *UNUSED(op))
static int rule_add_exec(bContext *C, wmOperator *op)
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)
Definition: rna_access.c:5004
const EnumPropertyItem rna_enum_boidrule_type_items[]
Definition: rna_boid.c:27
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3783
struct BoidRule * next
struct BoidRule * prev
struct ListBase states
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
struct BoidSettings * boids
void * data
Definition: RNA_types.h:38
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:919
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
PropertyRNA * prop
Definition: WM_types.h:981
struct PointerRNA * ptr
PointerRNA * ptr
Definition: wm_files.c:3480
wmOperatorType * ot
Definition: wm_files.c:3479
int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))