Blender  V2.93
particle_boids.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Janne Karhu.
17  * All rights reserved.
18  */
19 
24 #include <stdlib.h>
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "DNA_particle_types.h"
29 
30 #include "BLI_listbase.h"
31 #include "BLI_utildefines.h"
32 
33 #include "BKE_boids.h"
34 #include "BKE_context.h"
35 #include "BKE_main.h"
36 
37 #include "DEG_depsgraph.h"
38 #include "DEG_depsgraph_build.h"
39 
40 #include "RNA_access.h"
41 #include "RNA_define.h"
42 #include "RNA_enum_types.h"
43 
44 #include "WM_api.h"
45 #include "WM_types.h"
46 
47 #include "physics_intern.h"
48 
49 /************************ add/del boid rule operators *********************/
51 {
53  ParticleSettings *part = ptr.data;
54  int type = RNA_enum_get(op->ptr, "type");
55 
56  BoidRule *rule;
58 
59  if (!part || part->phystype != PART_PHYS_BOIDS) {
60  return OPERATOR_CANCELLED;
61  }
62 
64 
65  for (rule = state->rules.first; rule; rule = rule->next) {
66  rule->flag &= ~BOIDRULE_CURRENT;
67  }
68 
69  rule = boid_new_rule(type);
70  rule->flag |= BOIDRULE_CURRENT;
71 
72  BLI_addtail(&state->rules, rule);
73 
75 
76  return OPERATOR_FINISHED;
77 }
78 
80 {
81  /* identifiers */
82  ot->name = "Add Boid Rule";
83  ot->description = "Add a boid rule to the current boid state";
84  ot->idname = "BOID_OT_rule_add";
85 
86  /* api callbacks */
89 
90  /* flags */
92 
93  ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_boidrule_type_items, 0, "Type", "");
94 }
96 {
97  Main *bmain = CTX_data_main(C);
99  ParticleSettings *part = ptr.data;
100  BoidRule *rule;
101  BoidState *state;
102 
103  if (!part || part->phystype != PART_PHYS_BOIDS) {
104  return OPERATOR_CANCELLED;
105  }
106 
108 
109  for (rule = state->rules.first; rule; rule = rule->next) {
110  if (rule->flag & BOIDRULE_CURRENT) {
111  BLI_remlink(&state->rules, rule);
112  MEM_freeN(rule);
113  break;
114  }
115  }
116  rule = state->rules.first;
117 
118  if (rule) {
119  rule->flag |= BOIDRULE_CURRENT;
120  }
121 
124 
125  return OPERATOR_FINISHED;
126 }
127 
129 {
130  /* identifiers */
131  ot->name = "Remove Boid Rule";
132  ot->idname = "BOID_OT_rule_del";
133  ot->description = "Delete current boid rule";
134 
135  /* api callbacks */
136  ot->exec = rule_del_exec;
137 
138  /* flags */
140 }
141 
142 /************************ move up/down boid rule operators *********************/
144 {
146  ParticleSettings *part = ptr.data;
147  BoidRule *rule;
148  BoidState *state;
149 
150  if (!part || part->phystype != PART_PHYS_BOIDS) {
151  return OPERATOR_CANCELLED;
152  }
153 
155  for (rule = state->rules.first; rule; rule = rule->next) {
156  if (rule->flag & BOIDRULE_CURRENT && rule->prev) {
157  BLI_remlink(&state->rules, rule);
158  BLI_insertlinkbefore(&state->rules, rule->prev, rule);
159 
161  break;
162  }
163  }
164 
165  return OPERATOR_FINISHED;
166 }
167 
169 {
170  ot->name = "Move Up Boid Rule";
171  ot->description = "Move boid rule up in the list";
172  ot->idname = "BOID_OT_rule_move_up";
173 
175 
176  /* flags */
178 }
179 
181 {
183  ParticleSettings *part = ptr.data;
184  BoidRule *rule;
185  BoidState *state;
186 
187  if (!part || part->phystype != PART_PHYS_BOIDS) {
188  return OPERATOR_CANCELLED;
189  }
190 
192  for (rule = state->rules.first; rule; rule = rule->next) {
193  if (rule->flag & BOIDRULE_CURRENT && rule->next) {
194  BLI_remlink(&state->rules, rule);
195  BLI_insertlinkafter(&state->rules, rule->next, rule);
196 
198  break;
199  }
200  }
201 
202  return OPERATOR_FINISHED;
203 }
204 
206 {
207  ot->name = "Move Down Boid Rule";
208  ot->description = "Move boid rule down in the list";
209  ot->idname = "BOID_OT_rule_move_down";
210 
212 
213  /* flags */
215 }
216 
217 /************************ add/del boid state operators *********************/
219 {
221  ParticleSettings *part = ptr.data;
222  BoidState *state;
223 
224  if (!part || part->phystype != PART_PHYS_BOIDS) {
225  return OPERATOR_CANCELLED;
226  }
227 
228  for (state = part->boids->states.first; state; state = state->next) {
229  state->flag &= ~BOIDSTATE_CURRENT;
230  }
231 
232  state = boid_new_state(part->boids);
233  state->flag |= BOIDSTATE_CURRENT;
234 
235  BLI_addtail(&part->boids->states, state);
236 
237  return OPERATOR_FINISHED;
238 }
239 
241 {
242  /* identifiers */
243  ot->name = "Add Boid State";
244  ot->description = "Add a boid state to the particle system";
245  ot->idname = "BOID_OT_state_add";
246 
247  /* api callbacks */
249 
250  /* flags */
252 }
254 {
255  Main *bmain = CTX_data_main(C);
257  ParticleSettings *part = ptr.data;
258  BoidState *state;
259 
260  if (!part || part->phystype != PART_PHYS_BOIDS) {
261  return OPERATOR_CANCELLED;
262  }
263 
264  for (state = part->boids->states.first; state; state = state->next) {
265  if (state->flag & BOIDSTATE_CURRENT) {
266  BLI_remlink(&part->boids->states, state);
267  MEM_freeN(state);
268  break;
269  }
270  }
271 
272  /* there must be at least one state */
273  if (!part->boids->states.first) {
274  state = boid_new_state(part->boids);
275  BLI_addtail(&part->boids->states, state);
276  }
277  else {
278  state = part->boids->states.first;
279  }
280 
281  state->flag |= BOIDSTATE_CURRENT;
282 
285 
286  return OPERATOR_FINISHED;
287 }
288 
290 {
291  /* identifiers */
292  ot->name = "Remove Boid State";
293  ot->idname = "BOID_OT_state_del";
294  ot->description = "Delete current boid state";
295 
296  /* api callbacks */
298 
299  /* flags */
301 }
302 
303 /************************ move up/down boid state operators *********************/
305 {
307  ParticleSettings *part = ptr.data;
308  BoidSettings *boids;
309  BoidState *state;
310 
311  if (!part || part->phystype != PART_PHYS_BOIDS) {
312  return OPERATOR_CANCELLED;
313  }
314 
315  boids = part->boids;
316 
317  for (state = boids->states.first; state; state = state->next) {
318  if (state->flag & BOIDSTATE_CURRENT && state->prev) {
319  BLI_remlink(&boids->states, state);
320  BLI_insertlinkbefore(&boids->states, state->prev, state);
321  break;
322  }
323  }
324 
325  return OPERATOR_FINISHED;
326 }
327 
329 {
330  ot->name = "Move Up Boid State";
331  ot->description = "Move boid state up in the list";
332  ot->idname = "BOID_OT_state_move_up";
333 
335 
336  /* flags */
338 }
339 
341 {
343  ParticleSettings *part = ptr.data;
344  BoidSettings *boids;
345  BoidState *state;
346 
347  if (!part || part->phystype != PART_PHYS_BOIDS) {
348  return OPERATOR_CANCELLED;
349  }
350 
351  boids = part->boids;
352 
353  for (state = boids->states.first; state; state = state->next) {
354  if (state->flag & BOIDSTATE_CURRENT && state->next) {
355  BLI_remlink(&boids->states, state);
356  BLI_insertlinkafter(&boids->states, state->next, state);
358  break;
359  }
360  }
361 
362  return OPERATOR_FINISHED;
363 }
364 
366 {
367  ot->name = "Move Down Boid State";
368  ot->description = "Move boid state down in the list";
369  ot->idname = "BOID_OT_state_move_down";
370 
372 
373  /* flags */
375 }
struct BoidState * boid_get_current_state(struct BoidSettings *boids)
Definition: boids.c:1739
struct BoidRule * boid_new_rule(int type)
Definition: boids.c:1603
struct BoidState * boid_new_state(struct BoidSettings *boids)
Definition: boids.c:1669
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
Definition: context.c:456
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:352
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:395
#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:620
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
#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.
StructRNA RNA_ParticleSettings
#define C
Definition: RandGen.cpp:39
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
static ulong state[N]
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:6402
const EnumPropertyItem rna_enum_boidrule_type_items[]
Definition: rna_boid.c:43
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:3771
struct BoidRule * next
struct BoidRule * prev
struct ListBase states
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
struct BoidSettings * boids
void * data
Definition: RNA_types.h:52
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
PropertyRNA * prop
Definition: WM_types.h:814
struct PointerRNA * ptr
PointerRNA * ptr
Definition: wm_files.c:3157
wmOperatorType * ot
Definition: wm_files.c:3156
int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
Definition: wm_operators.c:982