Blender V2.61 - r43446

rna_controller.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Blender Foundation (2008).
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00028 #include <stdlib.h>
00029 
00030 #include "WM_types.h"
00031 #include "RNA_define.h"
00032 
00033 #include "rna_internal.h"
00034 #include "DNA_object_types.h"
00035 #include "DNA_controller_types.h"
00036 
00037 EnumPropertyItem controller_type_items[] ={
00038     {CONT_LOGIC_AND, "LOGIC_AND", 0, "And", "Logic And"},
00039     {CONT_LOGIC_OR, "LOGIC_OR", 0, "Or", "Logic Or"},
00040     {CONT_LOGIC_NAND, "LOGIC_NAND", 0, "Nand", "Logic Nand"},
00041     {CONT_LOGIC_NOR, "LOGIC_NOR", 0, "Nor", "Logic Nor"},
00042     {CONT_LOGIC_XOR, "LOGIC_XOR", 0, "Xor", "Logic Xor"},
00043     {CONT_LOGIC_XNOR, "LOGIC_XNOR", 0, "Xnor", "Logic Xnor"},
00044     {CONT_EXPRESSION, "EXPRESSION", 0, "Expression", ""},
00045     {CONT_PYTHON, "PYTHON", 0, "Python", ""},
00046     {0, NULL, 0, NULL, NULL}};
00047 
00048 #ifdef RNA_RUNTIME
00049 
00050 #include "BKE_sca.h"
00051 
00052 static struct StructRNA* rna_Controller_refine(struct PointerRNA *ptr)
00053 {
00054     bController *controller= (bController*)ptr->data;
00055 
00056     switch(controller->type) {
00057     case CONT_LOGIC_AND:
00058         return &RNA_AndController;
00059     case CONT_LOGIC_OR:
00060         return &RNA_OrController;
00061     case CONT_LOGIC_NAND:
00062         return &RNA_NandController;
00063     case CONT_LOGIC_NOR:
00064         return &RNA_NorController;
00065     case CONT_LOGIC_XOR:
00066         return &RNA_XorController;
00067     case CONT_LOGIC_XNOR:
00068         return &RNA_XnorController;
00069     case CONT_EXPRESSION:
00070         return &RNA_ExpressionController;
00071     case CONT_PYTHON:
00072         return &RNA_PythonController;
00073     default:
00074         return &RNA_Controller;
00075     }
00076 }
00077 
00078 void rna_Constroller_name_set(PointerRNA *ptr, const char *value)
00079 {
00080     bController *cont= (bController *)ptr->data;
00081 
00082     BLI_strncpy_utf8(cont->name, value, sizeof(cont->name));
00083 
00084     if (ptr->id.data) {
00085         Object *ob= (Object *)ptr->id.data;
00086         BLI_uniquename(&ob->controllers, cont, "Controller", '.', offsetof(bController, name), sizeof(cont->name));
00087     }
00088 }
00089 
00090 static void rna_Controller_type_set(struct PointerRNA *ptr, int value)
00091 {
00092     bController *cont= (bController *)ptr->data;
00093     if (value != cont->type)
00094     {
00095         cont->type = value;
00096         init_controller(cont);
00097     }
00098 }
00099 
00100 static void rna_Controller_mode_set(struct PointerRNA *ptr, int value)
00101 {
00102     bController *cont= (bController *)ptr->data;
00103     bPythonCont *pycon= (bPythonCont *)cont->data;
00104 
00105     // if mode changed and previous mode were Script
00106     if (value != pycon->mode && pycon->mode == CONT_PY_SCRIPT)
00107     {
00108         // clear script to avoid it to get linked with the controller
00109         pycon->text = NULL;
00110     }
00111     pycon->mode = value;
00112 }
00113 
00114 static int rna_Controller_state_number_get(struct PointerRNA *ptr)
00115 {
00116     bController *cont= (bController *)ptr->data;
00117     int bit;
00118 
00119     for (bit=0; bit<32; bit++) {
00120         if (cont->state_mask & (1<<bit))
00121             return bit+1;
00122     }
00123     return 0;
00124 }
00125 
00126 static void rna_Controller_state_number_set(struct PointerRNA *ptr, const int value)
00127 {
00128     bController *cont= (bController *)ptr->data;
00129     if (value < 1 || value > OB_MAX_STATES)
00130         return;
00131 
00132     cont->state_mask = (1 << (value - 1));
00133 }
00134 
00135 #if 0 /* editable is set to false, comment for now. */
00136 static void rna_Controller_state_get(PointerRNA *ptr, int *values)
00137 {
00138     bController *cont= (bController *)ptr->data;
00139     int i;
00140 
00141     memset(values, 0, sizeof(int)*OB_MAX_STATES);
00142     for(i=0; i<OB_MAX_STATES; i++)
00143         values[i] = (cont->state_mask & (1<<i));
00144 }
00145 
00146 static void rna_Controller_state_set(PointerRNA *ptr, const int *values)
00147 {
00148     bController *cont= (bController *)ptr->data;
00149     int i, tot= 0;
00150 
00151     /* ensure we always have some state selected */
00152     for(i=0; i<OB_MAX_STATES; i++)
00153         if(values[i])
00154             tot++;
00155     
00156     if(tot==0)
00157         return;
00158 
00159     /* only works for one state at once */
00160     if(tot>1)
00161         return;
00162 
00163     for(i=0; i<OB_MAX_STATES; i++) {
00164         if(values[i]) cont->state_mask |= (1<<i);
00165         else cont->state_mask &= ~(1<<i);
00166     }
00167 }
00168 #endif
00169 
00170 #else
00171 
00172 void RNA_def_controller(BlenderRNA *brna)
00173 {
00174     StructRNA *srna;
00175     PropertyRNA *prop;
00176 
00177     static EnumPropertyItem python_controller_modes[] ={
00178         {CONT_PY_SCRIPT, "SCRIPT", 0, "Script", ""},
00179         {CONT_PY_MODULE, "MODULE", 0, "Module", ""},
00180         {0, NULL, 0, NULL, NULL}};
00181 
00182     /* Controller */
00183     srna= RNA_def_struct(brna, "Controller", NULL);
00184     RNA_def_struct_sdna(srna, "bController");
00185     RNA_def_struct_refine_func(srna, "rna_Controller_refine");
00186     RNA_def_struct_ui_text(srna, "Controller", "Game engine logic brick to process events, connecting sensors to actuators");
00187 
00188     RNA_api_controller(srna);
00189 
00190     prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
00191     RNA_def_property_ui_text(prop, "Name", "");
00192     RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Constroller_name_set");
00193     RNA_def_struct_name_property(srna, prop);
00194     RNA_def_property_update(prop, NC_LOGIC, NULL);
00195 
00196     prop= RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
00197     RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
00198     RNA_def_property_enum_funcs(prop, NULL, "rna_Controller_type_set", NULL);
00199     RNA_def_property_enum_items(prop, controller_type_items);
00200     RNA_def_property_ui_text(prop, "Type", "");
00201     RNA_def_property_update(prop, NC_LOGIC, NULL);
00202 
00203     prop= RNA_def_property(srna, "show_expanded", PROP_BOOLEAN, PROP_NONE);
00204     RNA_def_property_boolean_sdna(prop, NULL, "flag", CONT_SHOW);
00205     RNA_def_property_ui_text(prop, "Expanded", "Set controller expanded in the user interface");
00206     RNA_def_property_ui_icon(prop, ICON_TRIA_RIGHT, 1); 
00207     RNA_def_property_update(prop, NC_LOGIC, NULL);
00208 
00209     prop= RNA_def_property(srna, "use_priority", PROP_BOOLEAN, PROP_NONE);
00210     RNA_def_property_boolean_sdna(prop, NULL, "flag", CONT_PRIO);
00211     RNA_def_property_ui_text(prop, "Priority", "Mark controller for execution before all non-marked controllers (good for startup scripts)");
00212     RNA_def_property_ui_icon(prop, ICON_BOOKMARKS, 1);
00213     RNA_def_property_update(prop, NC_LOGIC, NULL);
00214 
00215     /* State */
00216     
00217     // array of OB_MAX_STATES
00218     //prop= RNA_def_property(srna, "states", PROP_BOOLEAN, PROP_LAYER_MEMBER);
00219     //RNA_def_property_array(prop, OB_MAX_STATES);
00220     //RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00221     //RNA_def_property_ui_text(prop, "", "Set Controller state index (1 to 30)");
00222     //RNA_def_property_boolean_funcs(prop, "rna_Controller_state_get", "rna_Controller_state_set");
00223     //RNA_def_property_update(prop, NC_LOGIC, NULL);
00224 
00225     // number of the state
00226     prop= RNA_def_property(srna, "states", PROP_INT, PROP_UNSIGNED);
00227     RNA_def_property_int_sdna(prop, NULL, "state_mask");
00228     RNA_def_property_range(prop, 1, OB_MAX_STATES);
00229     RNA_def_property_ui_text(prop, "", "Set Controller state index (1 to 30)");
00230     RNA_def_property_int_funcs(prop, "rna_Controller_state_number_get", "rna_Controller_state_number_set", NULL);
00231     RNA_def_property_update(prop, NC_LOGIC, NULL);
00232 
00233     /* Expression Controller */
00234     srna= RNA_def_struct(brna, "ExpressionController", "Controller");
00235     RNA_def_struct_sdna_from(srna, "bExpressionCont", "data");
00236     RNA_def_struct_ui_text(srna, "Expression Controller", "Controller passing on events based on the evaluation of an expression");
00237 
00238     prop= RNA_def_property(srna, "expression", PROP_STRING, PROP_NONE);
00239     RNA_def_property_string_sdna(prop, NULL, "str");
00240     RNA_def_property_ui_text(prop, "Expression", "");
00241     RNA_def_property_update(prop, NC_LOGIC, NULL);
00242 
00243     /* Python Controller */
00244     srna= RNA_def_struct(brna, "PythonController", "Controller" );
00245     RNA_def_struct_sdna_from(srna, "bPythonCont", "data");
00246     RNA_def_struct_ui_text(srna, "Python Controller", "Controller executing a python script");
00247 
00248     prop= RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
00249     RNA_def_property_enum_items(prop, python_controller_modes);
00250     RNA_def_property_enum_funcs(prop, NULL, "rna_Controller_mode_set", NULL);
00251     RNA_def_property_ui_text(prop, "Execution Method", "Python script type (textblock or module - faster)");
00252     RNA_def_property_update(prop, NC_LOGIC, NULL);
00253 
00254     prop= RNA_def_property(srna, "text", PROP_POINTER, PROP_NONE);
00255     RNA_def_property_struct_type(prop, "Text");
00256     RNA_def_property_flag(prop, PROP_EDITABLE);
00257     RNA_def_property_ui_text(prop, "Text", "Text datablock with the python script");
00258     RNA_def_property_update(prop, NC_LOGIC, NULL);
00259 
00260     prop= RNA_def_property(srna, "module", PROP_STRING, PROP_NONE);
00261     RNA_def_property_ui_text(prop, "Module", "Module name and function to run, e.g. \"someModule.main\" (internal texts and external python files can be used)");
00262     RNA_def_property_update(prop, NC_LOGIC, NULL);
00263 
00264     prop= RNA_def_property(srna, "use_debug", PROP_BOOLEAN, PROP_NONE);
00265     RNA_def_property_boolean_sdna(prop, NULL, "flag", CONT_PY_DEBUG);
00266     RNA_def_property_ui_text(prop, "D", "Continuously reload the module from disk for editing external modules without restarting");
00267     RNA_def_property_update(prop, NC_LOGIC, NULL);
00268 
00269     /* Other Controllers */
00270     srna= RNA_def_struct(brna, "AndController", "Controller");
00271     RNA_def_struct_ui_text(srna, "And Controller", "Controller passing on events based on a logical AND operation");
00272     
00273     srna= RNA_def_struct(brna, "OrController", "Controller");
00274     RNA_def_struct_ui_text(srna, "Or Controller", "Controller passing on events based on a logical OR operation");
00275     
00276     srna= RNA_def_struct(brna, "NorController", "Controller");
00277     RNA_def_struct_ui_text(srna, "Nor Controller", "Controller passing on events based on a logical NOR operation");
00278     
00279     srna= RNA_def_struct(brna, "NandController", "Controller");
00280     RNA_def_struct_ui_text(srna, "Nand Controller", "Controller passing on events based on a logical NAND operation");
00281     
00282     srna= RNA_def_struct(brna, "XorController", "Controller");
00283     RNA_def_struct_ui_text(srna, "Xor Controller", "Controller passing on events based on a logical XOR operation");
00284     
00285     srna= RNA_def_struct(brna, "XnorController", "Controller");
00286     RNA_def_struct_ui_text(srna, "Xnor Controller", "Controller passing on events based on a logical XNOR operation");
00287 }
00288 
00289 #endif
00290