[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43145] trunk/blender: This patch creates an interface for ["Text"] properties in Font objects.

Dalai Felinto dfelinto at gmail.com
Wed Jan 4 22:40:01 CET 2012


Revision: 43145
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43145
Author:   dfelinto
Date:     2012-01-04 21:40:00 +0000 (Wed, 04 Jan 2012)
Log Message:
-----------
This patch creates an interface for ["Text"] properties in Font objects.

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.*

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

Modified: trunk/blender/release/scripts/startup/bl_ui/space_logic.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/space_logic.py	2012-01-04 21:36:06 UTC (rev 43144)
+++ trunk/blender/release/scripts/startup/bl_ui/space_logic.py	2012-01-04 21:40:00 UTC (rev 43145)
@@ -20,6 +20,12 @@
 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 @@
         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="")

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c	2012-01-04 21:36:06 UTC (rev 43144)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c	2012-01-04 21:40:00 UTC (rev 43145)
@@ -12833,6 +12833,23 @@
 				}
 			}
 		}
+		{
+		/* 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! */

Modified: trunk/blender/source/blender/editors/object/object_edit.c
===================================================================
--- trunk/blender/source/blender/editors/object/object_edit.c	2012-01-04 21:36:06 UTC (rev 43144)
+++ trunk/blender/source/blender/editors/object/object_edit.c	2012-01-04 21:40:00 UTC (rev 43145)
@@ -1936,16 +1936,23 @@
 
 /************************ 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 @@
 
 	/* 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)

Modified: trunk/blender/source/blender/makesrna/RNA_enum_types.h
===================================================================
--- trunk/blender/source/blender/makesrna/RNA_enum_types.h	2012-01-04 21:36:06 UTC (rev 43144)
+++ trunk/blender/source/blender/makesrna/RNA_enum_types.h	2012-01-04 21:40:00 UTC (rev 43145)
@@ -104,6 +104,8 @@
 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[];

Modified: trunk/blender/source/blender/makesrna/intern/rna_property.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_property.c	2012-01-04 21:36:06 UTC (rev 43144)
+++ trunk/blender/source/blender/makesrna/intern/rna_property.c	2012-01-04 21:40:00 UTC (rev 43145)
@@ -35,6 +35,15 @@
 
 #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 @@
 	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");

Modified: trunk/blender/source/gameengine/Converter/KX_ConvertProperties.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertProperties.cpp	2012-01-04 21:36:06 UTC (rev 43144)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertProperties.cpp	2012-01-04 21:40:00 UTC (rev 43145)
@@ -48,11 +48,18 @@
 #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 @@
 		//  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;
+	}
+}
+

Modified: trunk/blender/source/gameengine/Ketsji/KX_FontObject.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_FontObject.cpp	2012-01-04 21:36:06 UTC (rev 43144)
+++ trunk/blender/source/gameengine/Ketsji/KX_FontObject.cpp	2012-01-04 21:40:00 UTC (rev 43145)
@@ -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 @@
 	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));
+	}
+

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-blender-cvs mailing list