This patch creates an interface for ["Text"] properties in Font objects.
authorDalai Felinto <dfelinto@gmail.com>
Wed, 4 Jan 2012 21:40:00 +0000 (21:40 +0000)
committerDalai Felinto <dfelinto@gmail.com>
Wed, 4 Jan 2012 21:40:00 +0000 (21:40 +0000)
Interface:
http://www.pasteall.org/pic/show.php?id=23785

Simple test file:
http://www.pasteall.org/blend/10616
(I'll commit this to the text suite later)

Code Explanation:
---------------
(1) it adds a toggle to add/remove a "Text" gameproperty.
 - internally this property is just another game property (so we can find it within the game.properties lookup).
 - the property itself has no 'value', the interface shows the content of ob.data.body instead (why? because gameproperties are per object, while the text is per data).

(2) at BGE converter time it sets the current value of the object.data.body to the ["Text"] property.

(3) if you change object.text (bge text property) it automatically convert ["Text"] to a CStringValue.

*** that means if the original property was a CIntegerValue, it will be converted to CStringValue forever ***

* the only to do I can think of is to add a warning at doversion time if user has ["Text"] property for a Font object *
* when that happens we print a warning in console/popup.*

release/scripts/startup/bl_ui/space_logic.py
source/blender/blenloader/intern/readfile.c
source/blender/editors/object/object_edit.c
source/blender/makesrna/RNA_enum_types.h
source/blender/makesrna/intern/rna_property.c
source/gameengine/Converter/KX_ConvertProperties.cpp
source/gameengine/Ketsji/KX_FontObject.cpp

index 1fc58475ace5773f42e2738c0333abcd94a7acc0..79a3f72ff4866a5ddcf5b61b50da511cbb975dcd 100644 (file)
 import bpy
 from bpy.types import Header, Menu, Panel
 
+def get_id_by_name(properties, name):
+    """returns ID"""
+    for i, prop in enumerate(properties):
+            if prop.name == name:
+                return i
+    return -1
 
 class LOGIC_PT_properties(Panel):
     bl_space_type = 'LOGIC_EDITOR'
@@ -37,10 +43,29 @@ class LOGIC_PT_properties(Panel):
         ob = context.active_object
         game = ob.game
 
+        if ob.type == 'FONT':
+            prop = game.properties.get("Text")
+            if prop:
+                layout.operator("object.game_property_remove", text="Text Game Property", icon='X').index = get_id_by_name(game.properties, "Text")
+                row = layout.row()
+                sub=row.row()
+                sub.enabled=0
+                sub.prop(prop, "name", text="")
+                row.prop(prop, "type", text="")
+                # get the property from the body, not the game property
+                row.prop(ob.data, "body", text="")
+            else:
+                props=layout.operator("object.game_property_new", text="Text Game Property", icon='ZOOMIN')
+                props.name='Text'
+                props.type='STRING'
+
         layout.operator("object.game_property_new", text="Add Game Property", icon='ZOOMIN')
 
         for i, prop in enumerate(game.properties):
 
+            if ob.type == 'FONT' and prop.name == "Text":
+                continue
+
             box = layout.box()
             row = box.row()
             row.prop(prop, "name", text="")
index 59a34a706eef327dee0d954721a12544996d81cb..6a15ec139c889fd14e09ce606a9259fa07ac1a13 100644 (file)
@@ -12833,6 +12833,23 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                }
                        }
                }
+               {
+               /* Warn the user if he is using ["Text"] properties for Font objects */
+                       Object *ob;
+                       bProperty *prop;
+
+                       for (ob= main->object.first; ob; ob= ob->id.next) {
+                               if (ob->type == OB_FONT) {
+                                       for (prop=ob->prop.first;prop;prop=prop->next) {
+                                               if (strcmp(prop->name, "Text") == 0) {
+                                                       BKE_reportf(fd->reports, RPT_WARNING, "Game property name conflict in object: \"%s\".\nText objects reserve the [\"Text\"] game property to change their content through Logic Bricks.\n", ob->id.name+2);
+                                                       if(G.background==0) printf("Game property conflict in object: \"%s\". Texts reserve the [\"Text\"] game property to change their content through Logic Bricks.\n", ob->id.name+2);
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
+               }
        }
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
index 658f4acf940beb482cf012ea102d46bedb9a7a7f..6e1a4ba6cf2d7157f60d87ce5e5bf06acdca39a7 100644 (file)
@@ -1936,16 +1936,23 @@ void ED_object_toggle_modes(bContext *C, int mode)
 
 /************************ Game Properties ***********************/
 
-static int game_property_new(bContext *C, wmOperator *UNUSED(op))
+static int game_property_new(bContext *C, wmOperator *op)
 {
        Object *ob= CTX_data_active_object(C);
        bProperty *prop;
+       char name[32];
+       int type= RNA_enum_get(op->ptr, "type");
 
        if(!ob)
                return OPERATOR_CANCELLED;
 
-       prop= new_property(PROP_FLOAT);
+       prop= new_property(type);
        BLI_addtail(&ob->prop, prop);
+
+       RNA_string_get(op->ptr, "name", name);
+       if(BLI_strnlen(name, 32) > 0)
+               BLI_strncpy(prop->name, name, sizeof(prop->name));
+
        unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
 
        WM_event_add_notifier(C, NC_LOGIC, NULL);
@@ -1966,6 +1973,9 @@ void OBJECT_OT_game_property_new(wmOperatorType *ot)
 
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+
+       RNA_def_enum(ot->srna, "type", gameproperty_type_items, 2, "Type", "Type of game property to add");
+       RNA_def_string(ot->srna, "name", "", 32, "Name", "Name of the game property to add");
 }
 
 static int game_property_remove(bContext *C, wmOperator *op)
index 91e5d06fe5ea84684ee7d95729125d5e07fba14d..72917600d05223a4064fda038d48b424ba203963 100644 (file)
@@ -104,6 +104,8 @@ extern EnumPropertyItem property_type_items[];
 extern EnumPropertyItem property_subtype_items[];
 extern EnumPropertyItem property_unit_items[];
 
+extern EnumPropertyItem gameproperty_type_items[];
+
 extern EnumPropertyItem viewport_shade_items[];
 
 extern EnumPropertyItem nodetree_type_items[];
index e8ef61b6d74c0ac1a5e0625a370e546cab1d27fb..dfdc175d18be4c0a2b23496594967765e8ec9af6 100644 (file)
 
 #include "WM_types.h"
 
+EnumPropertyItem gameproperty_type_items[] ={
+       {GPROP_BOOL, "BOOL", 0, "Boolean", "Boolean Property"},
+       {GPROP_INT, "INT", 0, "Integer", "Integer Property"},
+       {GPROP_FLOAT, "FLOAT", 0, "Float", "Floating-Point Property"},
+       {GPROP_STRING, "STRING", 0, "String", "String Property"},
+       {GPROP_TIME, "TIMER", 0, "Timer", "Timer Property"},
+       {0, NULL, 0, NULL, NULL}};
+
+
 #ifdef RNA_RUNTIME
 
 #include "BKE_property.h"
@@ -98,14 +107,6 @@ void RNA_def_gameproperty(BlenderRNA *brna)
        StructRNA *srna;
        PropertyRNA *prop;
 
-       static EnumPropertyItem gameproperty_type_items[] ={
-               {GPROP_BOOL, "BOOL", 0, "Boolean", "Boolean Property"},
-               {GPROP_INT, "INT", 0, "Integer", "Integer Property"},
-               {GPROP_FLOAT, "FLOAT", 0, "Float", "Floating-Point Property"},
-               {GPROP_STRING, "STRING", 0, "String", "String Property"},
-               {GPROP_TIME, "TIMER", 0, "Timer", "Timer Property"},
-               {0, NULL, 0, NULL, NULL}};
-
        /* Base Struct for GameProperty */
        srna= RNA_def_struct(brna, "GameProperty", NULL);
        RNA_def_struct_ui_text(srna , "Game Property", "Game engine user defined object property");
index 7a574276eb406b1c99c8496168a87ec839d1bbcf..03d9be0d909725c9cc4709730b036b13846e1a24 100644 (file)
 #include "SCA_TimeEventManager.h"
 #include "SCA_IScene.h"
 
+#include "KX_FontObject.h"
+#include "DNA_curve_types.h"
+
 /* This little block needed for linking to Blender... */
 #ifdef WIN32
 #include "BLI_winstuff.h"
 #endif
 
+
+/* prototype */
+void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer);
+
 void BL_ConvertProperties(Object* object,KX_GameObject* gameobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
 {
        
@@ -155,4 +162,91 @@ void BL_ConvertProperties(Object* object,KX_GameObject* gameobj,SCA_TimeEventMan
                //  reserve name for object state
                scene->AddDebugProperty(gameobj,STR_String("__state__"));
        }
+
+       /* Font Objects need to 'copy' the Font Object data body to ["Text"] */
+       if (object->type == OB_FONT)
+       {
+               BL_ConvertTextProperty(object, (KX_FontObject *)gameobj, timemgr, scene, isInActiveLayer);
+       }
 }
+
+void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
+{
+       CValue* tprop = fontobj->GetProperty("Text");
+       if(!tprop) return;
+
+       Curve *curve = static_cast<Curve *>(object->data);
+       STR_String str = curve->str;
+
+       bProperty* prop = (bProperty*)object->prop.first;
+       CValue* propval;        
+
+       while(prop)
+       {
+               if (strcmp(prop->name, "Text")!=0) {
+                       prop = prop->next;
+                       continue;
+               }
+       
+               propval = NULL;
+
+               switch(prop->type) {
+                       case GPROP_BOOL:
+                       {
+                               int value = atoi(str);
+                               propval = new CBoolValue((bool)(value != 0));
+                               tprop->SetValue(propval);
+                               break;
+                       }
+                       case GPROP_INT:
+                       {
+                               int value = atoi(str);
+                               propval = new CIntValue(value);
+                               tprop->SetValue(propval);
+                               break;
+                       }
+                       case GPROP_FLOAT:
+                       {
+                               float floatprop = atof(str);
+                               propval = new CFloatValue(floatprop);
+                               tprop->SetValue(propval);
+                               break;
+                       }
+                       case GPROP_STRING:
+                       {
+                               propval = new CStringValue(str, "");
+                               tprop->SetValue(propval);
+                               break;
+                       }
+                       case GPROP_TIME:
+                       {
+                               float floatprop = atof(str);
+
+                               CValue* timeval = new CFloatValue(floatprop);
+                               // set a subproperty called 'timer' so that 
+                               // we can register the replica of this property 
+                               // at the time a game object is replicated (AddObjectActuator triggers this)
+                               CValue *bval = new CBoolValue(true);
+                               timeval->SetProperty("timer",bval);
+                               bval->Release();
+                               if (isInActiveLayer)
+                               {
+                                       timemgr->AddTimeProperty(timeval);
+                               }
+                               
+                               propval = timeval;
+                               tprop->SetValue(timeval);
+                       }
+                       default:
+                       {
+                               // todo make an assert etc.
+                       }
+               }
+               
+               if (propval)
+                       propval->Release();
+
+               prop = prop->next;
+       }
+}
+
index 5a4d9065605a73dea2bd15dae3572d4193091570..c20015945aa2b703229e77fa36f964a34cb0a5f0 100644 (file)
@@ -34,6 +34,7 @@
 #include "KX_Scene.h"
 #include "KX_PythonInit.h"
 #include "BLI_math.h"
+#include "StringValue.h"
 
 extern "C" {
 #include "BLF_api.h"
@@ -224,7 +225,18 @@ int KX_FontObject::pyattr_set_text(void *self_v, const KX_PYATTRIBUTE_DEF *attrd
        if(!PyUnicode_Check(value))
                return PY_SET_ATTR_FAIL;
        char* chars = _PyUnicode_AsString(value);
-       self->m_text = split_string(STR_String(chars));
+
+       /* Allow for some logic brick control */
+       CValue* tprop = self->GetProperty("Text");
+       if(tprop) {
+               CValue *newstringprop = new CStringValue(STR_String(chars), "Text");
+               self->SetProperty("Text", newstringprop);
+               newstringprop->Release();
+       }
+       else {
+               self->m_text = split_string(STR_String(chars));
+       }
+
        return PY_SET_ATTR_SUCCESS;
 }