Fix T42858: Non uniform gamelogic names on copy
authorJorge Bernal <jbernalmartinez@gmail.com>
Thu, 29 Jan 2015 05:03:19 +0000 (16:03 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 29 Jan 2015 05:43:20 +0000 (16:43 +1100)
Use generic function for consistent behavior

D949 by @lordloki

source/blender/blenkernel/intern/property.c
source/blender/editors/object/object_edit.c
source/blender/editors/space_logic/logic_ops.c
source/blender/editors/space_logic/logic_window.c
source/blender/makesrna/intern/rna_actuator.c
source/blender/makesrna/intern/rna_controller.c
source/blender/makesrna/intern/rna_property.c
source/blender/makesrna/intern/rna_sensor.c

index 819f4af054c57138fa990b7f360401d32e20c5fb..100df5fd121ce626d04952df34771bcae8fab485 100644 (file)
@@ -131,60 +131,6 @@ bProperty *BKE_bproperty_new(int type)
        return prop;
 }
 
-/* used by BKE_bproperty_unique() only */
-static bProperty *bproperty_get(bProperty *first, bProperty *self, const char *name)
-{
-       bProperty *p;
-       for (p = first; p; p = p->next) {
-               if (p != self && STREQ(p->name, name))
-                       return p;
-       }
-       return NULL;
-}
-void BKE_bproperty_unique(bProperty *first, bProperty *prop, int force)
-{
-       bProperty *p;
-
-       /* set the first if its not set */
-       if (first == NULL) {
-               first = prop;
-               while (first->prev) {
-                       first = first->prev;
-               }
-       }
-
-       if (force) {
-               /* change other names to make them unique */
-               while ((p = bproperty_get(first, prop, prop->name))) {
-                       BKE_bproperty_unique(first, p, 0);
-               }
-       }
-       else {
-               /* change our own name until its unique */
-               if (bproperty_get(first, prop, prop->name)) {
-                       /* there is a collision */
-                       char new_name[sizeof(prop->name)];
-                       char base_name[sizeof(prop->name)];
-                       char num[sizeof(prop->name)];
-                       int i = 0;
-
-                       /* strip numbers */
-                       BLI_strncpy(base_name, prop->name, sizeof(base_name));
-                       for (i = strlen(base_name) - 1; (i >= 0 && isdigit(base_name[i])); i--) {
-                               base_name[i] = '\0';
-                       }
-                       i = 0;
-
-                       do { /* ensure we have enough chars for the new number in the name */
-                               const size_t num_len = BLI_snprintf(num, sizeof(num), "%d", i++);
-                               BLI_snprintf(new_name, sizeof(prop->name),
-                                            "%.*s%s", (int)(sizeof(prop->name) - num_len), base_name, num);
-                       } while (bproperty_get(first, prop, new_name));
-
-                       BLI_strncpy(prop->name, new_name, sizeof(prop->name));
-               }
-       }
-}
 
 bProperty *BKE_bproperty_object_get(Object *ob, const char *name)
 {
index 62c27d8922dd0e6b9dea76268df2d9fc0878f440..749a178752a155cb21bb9ad39e841130588ac714 100644 (file)
@@ -42,6 +42,8 @@
 #include "BLI_utildefines.h"
 #include "BLI_ghash.h"
 
+#include "BLF_translation.h"
+
 #include "DNA_armature_types.h"
 #include "DNA_curve_types.h"
 #include "DNA_group_types.h"
@@ -1701,7 +1703,7 @@ static int game_property_new_exec(bContext *C, wmOperator *op)
                BLI_strncpy(prop->name, name, sizeof(prop->name));
        }
 
-       BKE_bproperty_unique(NULL, prop, 0); // make_unique_prop_names(prop->name);
+       BLI_uniquename(&ob->prop, prop, DATA_("Property"), '.', offsetof(bProperty, name), sizeof(prop->name));
 
        WM_event_add_notifier(C, NC_LOGIC, NULL);
        return OPERATOR_FINISHED;
index 62703ba517eccc00938130c9cecd372ad854aef1..2c6280f5670712983ff9487463c64647e509c285 100644 (file)
@@ -39,6 +39,8 @@
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
 
+#include "BLF_translation.h"
+
 #include "BKE_context.h"
 #include "BKE_main.h"
 #include "BKE_sca.h"
@@ -298,7 +300,7 @@ static int sensor_add_exec(bContext *C, wmOperator *op)
                BLI_strncpy(sens->name, sens_name, sizeof(sens->name));
        }
 
-       make_unique_prop_names(C, sens->name);
+       BLI_uniquename(&ob->sensors, sens, DATA_("Sensor"), '.', offsetof(bSensor, name), sizeof(sens->name));
        ob->scaflag |= OB_SHOWSENS;
 
        WM_event_add_notifier(C, NC_LOGIC, NULL);
@@ -405,7 +407,8 @@ static int controller_add_exec(bContext *C, wmOperator *op)
                BLI_strncpy(cont->name, cont_name, sizeof(cont->name));
        }
 
-       make_unique_prop_names(C, cont->name);
+       BLI_uniquename(&ob->controllers, cont, DATA_("Controller"), '.', offsetof(bController, name), sizeof(cont->name));
+
        /* set the controller state mask from the current object state.
         * A controller is always in a single state, so select the lowest bit set
         * from the object state */
@@ -523,7 +526,7 @@ static int actuator_add_exec(bContext *C, wmOperator *op)
                BLI_strncpy(act->name, act_name, sizeof(act->name));
        }
 
-       make_unique_prop_names(C, act->name);
+       BLI_uniquename(&ob->actuators, act, DATA_("Actuator"), '.', offsetof(bActuator, name), sizeof(act->name));
        ob->scaflag |= OB_SHOWACT;
        
        WM_event_add_notifier(C, NC_LOGIC, NULL);
index 9e87fe02da13389e1d39bc487e46e08d7fc320c7..37c634672dff773bdad2f037d157017aecc30772 100644 (file)
@@ -48,6 +48,7 @@
 
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
+#include "BLI_path_util.h"
 
 #include "BKE_action.h"
 #include "BKE_context.h"
 /* proto */
 static ID **get_selected_and_linked_obs(bContext *C, short *count, short scavisflag);
 
-static int vergname(const void *v1, const void *v2)
-{
-       const char * const *x1 = v1, * const *x2 = v2;
-       return BLI_natstrcmp(*x1, *x2);
-}
-
-void make_unique_prop_names(bContext *C, char *str)
-{
-       Object *ob;
-       bProperty *prop;
-       bSensor *sens;
-       bController *cont;
-       bActuator *act;
-       ID **idar;
-       short a, obcount, propcount=0, nr;
-       const char **names;
-       
-       /* this function is called by a Button, and gives the current
-        * stringpointer as an argument, this is the one that can change
-        */
-       
-       idar= get_selected_and_linked_obs(C, &obcount, BUTS_SENS_SEL|BUTS_SENS_ACT|BUTS_ACT_SEL|BUTS_ACT_ACT|BUTS_CONT_SEL|BUTS_CONT_ACT);
-       
-       /* for each object, make properties and sca names unique */
-       
-       /* count total names */
-       for (a=0; a<obcount; a++) {
-               ob= (Object *)idar[a];
-               propcount+= BLI_listbase_count(&ob->prop);
-               propcount+= BLI_listbase_count(&ob->sensors);
-               propcount+= BLI_listbase_count(&ob->controllers);
-               propcount+= BLI_listbase_count(&ob->actuators);
-       }
-       if (propcount==0) {
-               if (idar) MEM_freeN(idar);
-               return;
-       }
-       
-       /* make names array for sorting */
-       names= MEM_callocN(propcount*sizeof(void *), "names");
-       
-       /* count total names */
-       nr= 0;
-       for (a=0; a<obcount; a++) {
-               ob= (Object *)idar[a];
-               prop= ob->prop.first;
-               while (prop) {
-                       names[nr++] = prop->name;
-                       prop= prop->next;
-               }
-               sens= ob->sensors.first;
-               while (sens) {
-                       names[nr++] = sens->name;
-                       sens= sens->next;
-               }
-               cont= ob->controllers.first;
-               while (cont) {
-                       names[nr++] = cont->name;
-                       cont= cont->next;
-               }
-               act= ob->actuators.first;
-               while (act) {
-                       names[nr++] = act->name;
-                       act= act->next;
-               }
-       }
-       
-       qsort(names, propcount, sizeof(void *), vergname);
-       
-       /* now we check for double names, and change them */
-       
-       for (nr=0; nr<propcount; nr++) {
-               if (names[nr] != str && STREQ(names[nr], str)) {
-                       BLI_newname(str, +1);
-               }
-       }
-       
-       MEM_freeN(idar);
-       MEM_freeN(names);
-}
-
 static void do_logic_buts(bContext *C, void *UNUSED(arg), int event)
 {
        Main *bmain= CTX_data_main(C);
@@ -206,7 +126,7 @@ static void do_logic_buts(bContext *C, void *UNUSED(arg), int event)
                                ob->scaflag &= ~OB_ADDSENS;
                                sens= new_sensor(SENS_ALWAYS);
                                BLI_addtail(&(ob->sensors), sens);
-                               make_unique_prop_names(C, sens->name);
+                               BLI_uniquename(&ob->sensors, sens, DATA_("Sensor"), '.', offsetof(bSensor, name), sizeof(sens->name));
                                ob->scaflag |= OB_SHOWSENS;
                        }
                }
@@ -248,7 +168,7 @@ static void do_logic_buts(bContext *C, void *UNUSED(arg), int event)
                        if (ob->scaflag & OB_ADDCONT) {
                                ob->scaflag &= ~OB_ADDCONT;
                                cont= new_controller(CONT_LOGIC_AND);
-                               make_unique_prop_names(C, cont->name);
+                               BLI_uniquename(&ob->controllers, cont, DATA_("Controller"), '.', offsetof(bController, name), sizeof(cont->name));
                                ob->scaflag |= OB_SHOWCONT;
                                BLI_addtail(&(ob->controllers), cont);
                                /* set the controller state mask from the current object state.
@@ -324,7 +244,7 @@ static void do_logic_buts(bContext *C, void *UNUSED(arg), int event)
                        if (ob->scaflag & OB_ADDACT) {
                                ob->scaflag &= ~OB_ADDACT;
                                act= new_actuator(ACT_OBJECT);
-                               make_unique_prop_names(C, act->name);
+                               BLI_uniquename(&ob->actuators, act, DATA_("Actuator"), '.', offsetof(bActuator, name), sizeof(act->name));
                                BLI_addtail(&(ob->actuators), act);
                                ob->scaflag |= OB_SHOWACT;
                        }
index 9c7d66d00c71f8a7716aa98088913faac3ded482..691a7432275a6500ee20f3737332ce0719f43111 100644 (file)
@@ -120,14 +120,10 @@ static StructRNA *rna_Actuator_refine(struct PointerRNA *ptr)
 
 static void rna_Actuator_name_set(PointerRNA *ptr, const char *value)
 {
-       bActuator *act = (bActuator *)ptr->data;
-
+       Object *ob = ptr->id.data;
+       bActuator *act = ptr->data;
        BLI_strncpy_utf8(act->name, value, sizeof(act->name));
-
-       if (ptr->id.data) {
-               Object *ob = (Object *)ptr->id.data;
-               BLI_uniquename(&ob->actuators, act, DATA_("Actuator"), '.', offsetof(bActuator, name), sizeof(act->name));
-       }
+       BLI_uniquename(&ob->actuators, act, DATA_("Actuator"), '.', offsetof(bActuator, name), sizeof(act->name));
 }
 
 static void rna_Actuator_type_set(struct PointerRNA *ptr, int value)
index 8b5074eaf0d9eb8a60d4d0034dbdbc3b0c781fb3..ba0214d36ec01915c8c5257a007df908cbe77038 100644 (file)
@@ -85,15 +85,10 @@ static StructRNA *rna_Controller_refine(struct PointerRNA *ptr)
 
 static void rna_Constroller_name_set(PointerRNA *ptr, const char *value)
 {
-       bController *cont = (bController *)ptr->data;
-
+       Object *ob = ptr->id.data;
+       bController *cont = ptr->data;
        BLI_strncpy_utf8(cont->name, value, sizeof(cont->name));
-
-       if (ptr->id.data) {
-               Object *ob = (Object *)ptr->id.data;
-               BLI_uniquename(&ob->controllers, cont, DATA_("Controller"), '.', offsetof(bController, name),
-                              sizeof(cont->name));
-       }
+       BLI_uniquename(&ob->controllers, cont, DATA_("Controller"), '.', offsetof(bController, name), sizeof(cont->name));
 }
 
 static void rna_Controller_type_set(struct PointerRNA *ptr, int value)
index c6b8e89c282c0358d6bc8a1296126a62deb7f3a4..a2c19e55056513933ffbba5c0a68bb01e0b2ef32 100644 (file)
 #include <stdlib.h>
 
 #include "DNA_property_types.h"
+#include "DNA_object_types.h"
+
+#include "BLI_path_util.h"
+
+#include "BLF_translation.h"
 
 #include "RNA_define.h"
 #include "RNA_enum_types.h"
@@ -97,9 +102,11 @@ static void rna_GameProperty_type_set(PointerRNA *ptr, int value)
 
 static void rna_GameProperty_name_set(PointerRNA *ptr, const char *value)
 {
-       bProperty *prop = (bProperty *)(ptr->data);
+       Object *ob = ptr->id.data;
+       bProperty *prop = ptr->data;
        BLI_strncpy_utf8(prop->name, value, sizeof(prop->name));
-       BKE_bproperty_unique(NULL, prop, 1);
+
+       BLI_uniquename(&ob->prop, prop, DATA_("Property"), '.', offsetof(bProperty, name), sizeof(prop->name));
 }
 
 
index 28b7b5fbcd8f421513ee0b41946ad38e648d9813..5d7bb6d2d94b5223b8b6133ab1ce5115d0c407b2 100644 (file)
@@ -107,14 +107,10 @@ static StructRNA *rna_Sensor_refine(struct PointerRNA *ptr)
 
 static void rna_Sensor_name_set(PointerRNA *ptr, const char *value)
 {
-       bSensor *sens = (bSensor *)ptr->data;
-
+       Object *ob = ptr->id.data;
+       bSensor *sens = ptr->data;
        BLI_strncpy_utf8(sens->name, value, sizeof(sens->name));
-
-       if (ptr->id.data) {
-               Object *ob = (Object *)ptr->id.data;
-               BLI_uniquename(&ob->sensors, sens, DATA_("Sensor"), '.', offsetof(bSensor, name), sizeof(sens->name));
-       }
+       BLI_uniquename(&ob->sensors, sens, DATA_("Sensor"), '.', offsetof(bSensor, name), sizeof(sens->name));
 }
 
 static void rna_Sensor_type_set(struct PointerRNA *ptr, int value)