4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2009 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
30 #include "DNA_object_types.h"
31 #include "DNA_sensor_types.h"
32 #include "DNA_controller_types.h"
33 #include "DNA_actuator_types.h"
35 #include "BLI_blenlib.h"
36 #include "BLI_utildefines.h"
38 #include "BKE_context.h"
43 #include "ED_object.h"
44 #include "ED_screen.h"
46 #include "RNA_access.h"
47 #include "RNA_define.h"
48 #include "RNA_enum_types.h"
53 #include "logic_intern.h"
55 /* ************* Generic Operator Helpers ************* */
56 static int edit_sensor_poll(bContext *C)
58 PointerRNA ptr= CTX_data_pointer_get_type(C, "sensor", &RNA_Sensor);
60 if (ptr.data && ((ID*)ptr.id.data)->lib) return 0;
64 static int edit_controller_poll(bContext *C)
66 PointerRNA ptr= CTX_data_pointer_get_type(C, "controller", &RNA_Controller);
68 if (ptr.data && ((ID*)ptr.id.data)->lib) return 0;
72 static int edit_actuator_poll(bContext *C)
74 PointerRNA ptr= CTX_data_pointer_get_type(C, "actuator", &RNA_Actuator);
76 if (ptr.data && ((ID*)ptr.id.data)->lib) return 0;
80 static void edit_sensor_properties(wmOperatorType *ot)
82 RNA_def_string(ot->srna, "sensor", "", 32, "Sensor", "Name of the sensor to edit");
83 RNA_def_string(ot->srna, "object", "", 32, "Object", "Name of the object the sensor belongs to");
86 static int edit_sensor_invoke_properties(bContext *C, wmOperator *op)
88 PointerRNA ptr= CTX_data_pointer_get_type(C, "sensor", &RNA_Sensor);
90 if (RNA_property_is_set(op->ptr, "sensor") && RNA_property_is_set(op->ptr, "object") )
94 bSensor *sens = ptr.data;
95 Object *ob = ptr.id.data;
97 RNA_string_set(op->ptr, "sensor", sens->name);
98 RNA_string_set(op->ptr, "object", ob->id.name+2);
105 static Object *edit_object_property_get(bContext *C, wmOperator *op)
110 RNA_string_get(op->ptr, "object", ob_name);
112 /* if ob_name is valid try to find the object with this name
113 otherwise gets the active object */
114 if (BLI_strnlen(ob_name, 32) > 0)
115 ob = BLI_findstring(&(CTX_data_main(C)->object), ob_name, offsetof(ID, name) + 2);
117 ob= ED_object_active_context(C);
122 static bSensor *edit_sensor_property_get(bContext *C, wmOperator *op, Object **ob)
124 char sensor_name[32];
127 RNA_string_get(op->ptr, "sensor", sensor_name);
129 *ob= edit_object_property_get(C, op);
130 if (!*ob) return NULL;
132 sens = BLI_findstring(&((*ob)->sensors), sensor_name, offsetof(bSensor, name));
136 static void edit_controller_properties(wmOperatorType *ot)
138 RNA_def_string(ot->srna, "controller", "", 32, "Controller", "Name of the controller to edit");
139 RNA_def_string(ot->srna, "object", "", 32, "Object", "Name of the object the controller belongs to");
142 static int edit_controller_invoke_properties(bContext *C, wmOperator *op)
144 PointerRNA ptr= CTX_data_pointer_get_type(C, "controller", &RNA_Controller);
146 if (RNA_property_is_set(op->ptr, "controller") && RNA_property_is_set(op->ptr, "object") )
150 bController *cont = ptr.data;
151 Object *ob = ptr.id.data;
153 RNA_string_set(op->ptr, "controller", cont->name);
154 RNA_string_set(op->ptr, "object", ob->id.name+2);
161 static bController *edit_controller_property_get(bContext *C, wmOperator *op, Object **ob)
163 char controller_name[32];
166 RNA_string_get(op->ptr, "controller", controller_name);
168 *ob= edit_object_property_get(C, op);
169 if (!*ob) return NULL;
171 cont = BLI_findstring(&((*ob)->controllers), controller_name, offsetof(bController, name));
175 static void edit_actuator_properties(wmOperatorType *ot)
177 RNA_def_string(ot->srna, "actuator", "", 32, "Actuator", "Name of the actuator to edit");
178 RNA_def_string(ot->srna, "object", "", 32, "Object", "Name of the object the actuator belongs to");
181 static int edit_actuator_invoke_properties(bContext *C, wmOperator *op)
183 PointerRNA ptr= CTX_data_pointer_get_type(C, "actuator", &RNA_Actuator);
185 if (RNA_property_is_set(op->ptr, "actuator") && RNA_property_is_set(op->ptr, "object") )
189 bActuator *act = ptr.data;
190 Object *ob = ptr.id.data;
192 RNA_string_set(op->ptr, "actuator",act->name);
193 RNA_string_set(op->ptr, "object", ob->id.name+2);
200 static bActuator *edit_actuator_property_get(bContext *C, wmOperator *op, Object **ob)
202 char actuator_name[32];
205 RNA_string_get(op->ptr, "actuator", actuator_name);
207 *ob= edit_object_property_get(C, op);
208 if (!*ob) return NULL;
210 act = BLI_findstring(&((*ob)->actuators), actuator_name, offsetof(bActuator, name));
214 static int logicbricks_move_property_get(wmOperator *op)
216 int type = RNA_enum_get(op->ptr, "direction");
224 /* ************* Add/Remove Sensor Operator ************* */
226 static int sensor_remove_exec(bContext *C, wmOperator *op)
229 bSensor *sens = edit_sensor_property_get(C, op, &ob);
232 return OPERATOR_CANCELLED;
234 BLI_remlink(&(ob->sensors), sens);
237 WM_event_add_notifier(C, NC_LOGIC, NULL);
239 return OPERATOR_FINISHED;
242 static int sensor_remove_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
244 if (edit_sensor_invoke_properties(C, op))
245 return sensor_remove_exec(C, op);
247 return OPERATOR_CANCELLED;
250 static void LOGIC_OT_sensor_remove(wmOperatorType *ot)
252 ot->name= "Remove Sensor";
253 ot->description= "Remove a sensor from the active object";
254 ot->idname= "LOGIC_OT_sensor_remove";
256 ot->invoke= sensor_remove_invoke;
257 ot->exec= sensor_remove_exec;
258 ot->poll= edit_sensor_poll;
261 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
262 edit_sensor_properties(ot);
265 static int sensor_add_exec(bContext *C, wmOperator *op)
271 const char *sens_name;
273 int type= RNA_enum_get(op->ptr, "type");
275 ob= edit_object_property_get(C, op);
277 return OPERATOR_CANCELLED;
279 sens= new_sensor(type);
280 BLI_addtail(&(ob->sensors), sens);
282 /* set the sensor name based on rna type enum */
283 RNA_pointer_create((ID *)ob, &RNA_Sensor, sens, &sens_ptr);
284 prop = RNA_struct_find_property(&sens_ptr, "type");
286 RNA_string_get(op->ptr, "name", name);
287 if(BLI_strnlen(name, 32) < 1){
288 RNA_property_enum_name(C, &sens_ptr, prop, RNA_property_enum_get(&sens_ptr, prop), &sens_name);
289 BLI_strncpy(sens->name, sens_name, sizeof(sens->name));
292 BLI_strncpy(sens->name, name, sizeof(sens->name));
294 make_unique_prop_names(C, sens->name);
295 ob->scaflag |= OB_SHOWSENS;
297 WM_event_add_notifier(C, NC_LOGIC, NULL);
299 return OPERATOR_FINISHED;
302 static void LOGIC_OT_sensor_add(wmOperatorType *ot)
307 ot->name= "Add Sensor";
308 ot->description = "Add a sensor to the active object";
309 ot->idname= "LOGIC_OT_sensor_add";
312 ot->invoke= WM_menu_invoke;
313 ot->exec= sensor_add_exec;
314 ot->poll= ED_operator_object_active_editable;
317 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
320 prop= RNA_def_enum(ot->srna, "type", DummyRNA_NULL_items, SENS_ALWAYS, "Type", "Type of sensor to add");
321 RNA_def_enum_funcs(prop, rna_Sensor_type_itemf);
322 RNA_def_string(ot->srna, "name", "", 32, "Name", "Name of the Sensor to add");
323 RNA_def_string(ot->srna, "object", "", 32, "Object", "Name of the Object to add the Sensor to");
326 /* ************* Add/Remove Controller Operator ************* */
328 static int controller_remove_exec(bContext *C, wmOperator *op)
331 bController *cont = edit_controller_property_get(C, op, &ob);
334 return OPERATOR_CANCELLED;
336 BLI_remlink(&(ob->controllers), cont);
337 unlink_controller(cont);
338 free_controller(cont);
340 WM_event_add_notifier(C, NC_LOGIC, NULL);
342 return OPERATOR_FINISHED;
345 static int controller_remove_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
347 if (edit_controller_invoke_properties(C, op))
348 return controller_remove_exec(C, op);
350 return OPERATOR_CANCELLED;
353 static void LOGIC_OT_controller_remove(wmOperatorType *ot)
355 ot->name= "Remove Controller";
356 ot->description= "Remove a controller from the active object";
357 ot->idname= "LOGIC_OT_controller_remove";
359 ot->invoke= controller_remove_invoke;
360 ot->exec= controller_remove_exec;
361 ot->poll= edit_controller_poll;
364 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
365 edit_controller_properties(ot);
368 static int controller_add_exec(bContext *C, wmOperator *op)
374 const char *cont_name;
377 int type= RNA_enum_get(op->ptr, "type");
379 ob= edit_object_property_get(C, op);
381 return OPERATOR_CANCELLED;
383 cont= new_controller(type);
384 BLI_addtail(&(ob->controllers), cont);
386 /* set the controller name based on rna type enum */
387 RNA_pointer_create((ID *)ob, &RNA_Controller, cont, &cont_ptr);
388 prop = RNA_struct_find_property(&cont_ptr, "type");
390 RNA_string_get(op->ptr, "name", name);
391 if(BLI_strnlen(name, 32) < 1){
392 RNA_property_enum_name(C, &cont_ptr, prop, RNA_property_enum_get(&cont_ptr, prop), &cont_name);
393 BLI_strncpy(cont->name, cont_name, sizeof(cont->name));
396 BLI_strncpy(cont->name, name, sizeof(cont->name));
398 make_unique_prop_names(C, cont->name);
399 /* set the controller state mask from the current object state.
400 A controller is always in a single state, so select the lowest bit set
401 from the object state */
402 for (bit=0; bit<OB_MAX_STATES; bit++) {
403 if (ob->state & (1<<bit))
406 cont->state_mask = (1<<bit);
407 if (cont->state_mask == 0) {
408 /* shouldn't happen, object state is never 0 */
409 cont->state_mask = 1;
412 ob->scaflag |= OB_SHOWCONT;
414 WM_event_add_notifier(C, NC_LOGIC, NULL);
416 return OPERATOR_FINISHED;
419 static void LOGIC_OT_controller_add(wmOperatorType *ot)
422 ot->name= "Add Controller";
423 ot->description = "Add a controller to the active object";
424 ot->idname= "LOGIC_OT_controller_add";
427 ot->invoke= WM_menu_invoke;
428 ot->exec= controller_add_exec;
429 ot->poll= ED_operator_object_active_editable;
432 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
435 RNA_def_enum(ot->srna, "type", controller_type_items, CONT_LOGIC_AND, "Type", "Type of controller to add");
436 RNA_def_string(ot->srna, "name", "", 32, "Name", "Name of the Controller to add");
437 RNA_def_string(ot->srna, "object", "", 32, "Object", "Name of the Object to add the Controller to");
440 /* ************* Add/Remove Actuator Operator ************* */
442 static int actuator_remove_exec(bContext *C, wmOperator *op)
445 bActuator *act = edit_actuator_property_get(C, op, &ob);
448 return OPERATOR_CANCELLED;
450 BLI_remlink(&(ob->actuators), act);
451 unlink_actuator(act);
454 WM_event_add_notifier(C, NC_LOGIC, NULL);
456 return OPERATOR_FINISHED;
459 static int actuator_remove_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
461 if (edit_actuator_invoke_properties(C, op))
462 return actuator_remove_exec(C, op);
464 return OPERATOR_CANCELLED;
467 static void LOGIC_OT_actuator_remove(wmOperatorType *ot)
469 ot->name= "Remove Actuator";
470 ot->description= "Remove a actuator from the active object";
471 ot->idname= "LOGIC_OT_actuator_remove";
473 ot->invoke= actuator_remove_invoke;
474 ot->exec= actuator_remove_exec;
475 ot->poll= edit_actuator_poll;
478 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
479 edit_actuator_properties(ot);
482 static int actuator_add_exec(bContext *C, wmOperator *op)
488 const char *act_name;
490 int type= RNA_enum_get(op->ptr, "type");
492 ob= edit_object_property_get(C, op);
494 return OPERATOR_CANCELLED;
496 act= new_actuator(type);
497 BLI_addtail(&(ob->actuators), act);
499 /* set the actuator name based on rna type enum */
500 RNA_pointer_create((ID *)ob, &RNA_Actuator, act, &act_ptr);
501 prop = RNA_struct_find_property(&act_ptr, "type");
503 RNA_string_get(op->ptr, "name", name);
504 if (BLI_strnlen(name, 32) < 1){
505 RNA_property_enum_name(C, &act_ptr, prop, RNA_property_enum_get(&act_ptr, prop), &act_name);
506 BLI_strncpy(act->name, act_name, sizeof(act->name));
509 BLI_strncpy(act->name, name, sizeof(act->name));
511 make_unique_prop_names(C, act->name);
512 ob->scaflag |= OB_SHOWACT;
514 WM_event_add_notifier(C, NC_LOGIC, NULL);
516 return OPERATOR_FINISHED;
519 static void LOGIC_OT_actuator_add(wmOperatorType *ot)
524 ot->name= "Add Actuator";
525 ot->description = "Add a actuator to the active object";
526 ot->idname= "LOGIC_OT_actuator_add";
529 ot->invoke= WM_menu_invoke;
530 ot->exec= actuator_add_exec;
531 ot->poll= ED_operator_object_active_editable;
534 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
537 prop= RNA_def_enum(ot->srna, "type", DummyRNA_NULL_items, CONT_LOGIC_AND, "Type", "Type of actuator to add");
538 RNA_def_enum_funcs(prop, rna_Actuator_type_itemf);
539 RNA_def_string(ot->srna, "name", "", 32, "Name", "Name of the Actuator to add");
540 RNA_def_string(ot->srna, "object", "", 32, "Object", "Name of the Object to add the Actuator to");
543 /* ************* Move Logic Bricks Operator ************* */
544 static EnumPropertyItem logicbricks_move_direction[] ={
545 {1, "UP", 0, "Move Up", ""},
546 {2, "DOWN", 0, "Move Down", ""},
547 {0, NULL, 0, NULL, NULL}};
550 static int sensor_move_exec(bContext *C, wmOperator *op)
553 bSensor *sens= edit_sensor_property_get(C, op, &ob);
554 int move_up= logicbricks_move_property_get(op);
557 return OPERATOR_CANCELLED;
559 sca_move_sensor(sens, ob, move_up);
561 WM_event_add_notifier(C, NC_LOGIC, NULL);
563 return OPERATOR_FINISHED;
566 static int sensor_move_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
568 if (edit_sensor_invoke_properties(C, op)) {
569 return sensor_move_exec(C, op);
572 return OPERATOR_CANCELLED;
575 static void LOGIC_OT_sensor_move(wmOperatorType *ot)
578 ot->name= "Move Sensor";
579 ot->description = "Move Densor";
580 ot->idname= "LOGIC_OT_sensor_move";
583 ot->invoke= sensor_move_invoke;
584 ot->exec= sensor_move_exec;
585 ot->poll= edit_sensor_poll;
588 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
591 edit_sensor_properties(ot);
592 RNA_def_enum(ot->srna, "direction", logicbricks_move_direction, 1, "Direction", "Move Up or Down");
595 static int controller_move_exec(bContext *C, wmOperator *op)
598 bController *cont= edit_controller_property_get(C, op, &ob);
599 int move_up= logicbricks_move_property_get(op);
602 return OPERATOR_CANCELLED;
604 sca_move_controller(cont, ob, move_up);
606 WM_event_add_notifier(C, NC_LOGIC, NULL);
608 return OPERATOR_FINISHED;
611 static int controller_move_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
613 if (edit_controller_invoke_properties(C, op)) {
614 return controller_move_exec(C, op);
617 return OPERATOR_CANCELLED;
620 static void LOGIC_OT_controller_move(wmOperatorType *ot)
623 ot->name= "Move Controller";
624 ot->description = "Move Controller";
625 ot->idname= "LOGIC_OT_controller_move";
628 ot->invoke= controller_move_invoke;
629 ot->exec= controller_move_exec;
630 ot->poll= edit_controller_poll;
633 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
636 edit_controller_properties(ot);
637 RNA_def_enum(ot->srna, "direction", logicbricks_move_direction, 1, "Direction", "Move Up or Down");
640 static int actuator_move_exec(bContext *C, wmOperator *op)
643 bActuator *act = edit_actuator_property_get(C, op, &ob);
644 int move_up= logicbricks_move_property_get(op);
647 return OPERATOR_CANCELLED;
649 sca_move_actuator(act, ob, move_up);
651 WM_event_add_notifier(C, NC_LOGIC, NULL);
653 return OPERATOR_FINISHED;
656 static int actuator_move_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
658 if (edit_actuator_invoke_properties(C, op)) {
659 return actuator_move_exec(C, op);
662 return OPERATOR_CANCELLED;
665 static void LOGIC_OT_actuator_move(wmOperatorType *ot)
668 ot->name= "Move Actuator";
669 ot->description = "Move Actuator";
670 ot->idname= "LOGIC_OT_actuator_move";
673 ot->invoke= actuator_move_invoke;
674 ot->exec= actuator_move_exec;
675 ot->poll= edit_actuator_poll;
678 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
681 edit_actuator_properties(ot);
682 RNA_def_enum(ot->srna, "direction", logicbricks_move_direction, 1, "Direction", "Move Up or Down");
686 void ED_operatortypes_logic(void)
688 WM_operatortype_append(LOGIC_OT_sensor_remove);
689 WM_operatortype_append(LOGIC_OT_sensor_add);
690 WM_operatortype_append(LOGIC_OT_sensor_move);
691 WM_operatortype_append(LOGIC_OT_controller_remove);
692 WM_operatortype_append(LOGIC_OT_controller_add);
693 WM_operatortype_append(LOGIC_OT_controller_move);
694 WM_operatortype_append(LOGIC_OT_actuator_remove);
695 WM_operatortype_append(LOGIC_OT_actuator_add);
696 WM_operatortype_append(LOGIC_OT_actuator_move);