fix [#33442] Units
authorCampbell Barton <ideasman42@gmail.com>
Sun, 9 Dec 2012 10:48:18 +0000 (10:48 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 9 Dec 2012 10:48:18 +0000 (10:48 +0000)
adding meshes were scaling the user input values so the distance on the button didnt relate to the scale of the object added.

Now use an invoke function that scales unset default values.

release/scripts/startup/bl_ui/space_info.py
source/blender/editors/include/ED_view3d.h
source/blender/editors/mesh/editmesh_add.c
source/blender/editors/object/object_add.c
source/blender/editors/space_view3d/view3d_draw.c
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_operators.c

index 662d5526a37828f37287beb5a61bdb416ad992ee..8df117e27a0806d8b03f4085e5c24ae041db4dc8 100644 (file)
@@ -193,7 +193,7 @@ class INFO_MT_mesh_add(Menu):
     def draw(self, context):
         layout = self.layout
 
-        layout.operator_context = 'EXEC_REGION_WIN'
+        layout.operator_context = 'INVOKE_REGION_WIN'
         layout.operator("mesh.primitive_plane_add", icon='MESH_PLANE', text="Plane")
         layout.operator("mesh.primitive_cube_add", icon='MESH_CUBE', text="Cube")
         layout.operator("mesh.primitive_circle_add", icon='MESH_CIRCLE', text="Circle")
index d5c9b9ef01a2f9cb3661d50a4aba32eeb9704871..b4679bd1c69a5c433534324d4c7b42974f38259b 100644 (file)
@@ -295,6 +295,8 @@ void ED_view3D_background_image_clear(struct View3D *v3d);
 
 #define VIEW3D_MARGIN 1.4f
 float ED_view3d_offset_distance(float mat[4][4], float ofs[3]);
+
+float ED_scene_grid_scale(struct Scene *scene, const char **grid_unit);
 float ED_view3d_grid_scale(struct Scene *scene, struct View3D *v3d, const char **grid_unit);
 
 /* view matrix properties utilities */
index eed72935b3c6bfae4f92d8a7f5dec8b91b4a135e..4a425c83d86563da6783e90dad95b020718747ad 100644 (file)
@@ -204,7 +204,7 @@ static int add_primitive_circle_exec(bContext *C, wmOperator *op)
 
        if (!EDBM_op_call_and_selectf(em, op, "verts.out",
                                      "create_circle segments=%i diameter=%f cap_ends=%b cap_tris=%b matrix=%m4",
-                                     RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius") * dia,
+                                     RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius"),
                                      cap_end, cap_tri, mat))
        {
                return OPERATOR_CANCELLED;
@@ -225,6 +225,7 @@ void MESH_OT_primitive_circle_add(wmOperatorType *ot)
        ot->idname = "MESH_OT_primitive_circle_add";
 
        /* api callbacks */
+       ot->invoke = WM_operator_view3d_distance_invoke;
        ot->exec = add_primitive_circle_exec;
        ot->poll = ED_operator_scene_editable;
 
@@ -260,10 +261,10 @@ static int add_primitive_cylinder_exec(bContext *C, wmOperator *op)
                em, op, "verts.out",
                "create_cone segments=%i diameter1=%f diameter2=%f cap_ends=%b cap_tris=%b depth=%f matrix=%m4",
                RNA_int_get(op->ptr, "vertices"),
-               RNA_float_get(op->ptr, "radius") * dia,
-               RNA_float_get(op->ptr, "radius") * dia,
+               RNA_float_get(op->ptr, "radius"),
+               RNA_float_get(op->ptr, "radius"),
                cap_end, cap_tri,
-               RNA_float_get(op->ptr, "depth") * dia, mat))
+               RNA_float_get(op->ptr, "depth"), mat))
        {
                return OPERATOR_CANCELLED;
        }
@@ -283,6 +284,7 @@ void MESH_OT_primitive_cylinder_add(wmOperatorType *ot)
        ot->idname = "MESH_OT_primitive_cylinder_add";
 
        /* api callbacks */
+       ot->invoke = WM_operator_view3d_distance_invoke;
        ot->exec = add_primitive_cylinder_exec;
        ot->poll = ED_operator_scene_editable;
 
@@ -319,8 +321,8 @@ static int add_primitive_cone_exec(bContext *C, wmOperator *op)
        if (!EDBM_op_call_and_selectf(
                em, op, "verts.out",
                "create_cone segments=%i diameter1=%f diameter2=%f cap_ends=%b cap_tris=%b depth=%f matrix=%m4",
-               RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius1") * dia,
-               RNA_float_get(op->ptr, "radius2") * dia, cap_end, cap_tri, RNA_float_get(op->ptr, "depth") * dia, mat))
+               RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius1"),
+               RNA_float_get(op->ptr, "radius2"), cap_end, cap_tri, RNA_float_get(op->ptr, "depth"), mat))
        {
                return OPERATOR_CANCELLED;
        }
@@ -340,6 +342,7 @@ void MESH_OT_primitive_cone_add(wmOperatorType *ot)
        ot->idname = "MESH_OT_primitive_cone_add";
 
        /* api callbacks */
+       ot->invoke = WM_operator_view3d_distance_invoke;
        ot->exec = add_primitive_cone_exec;
        ot->poll = ED_operator_scene_editable;
 
@@ -376,7 +379,7 @@ static int add_primitive_grid_exec(bContext *C, wmOperator *op)
                                      "create_grid x_segments=%i y_segments=%i size=%f matrix=%m4",
                                      RNA_int_get(op->ptr, "x_subdivisions"),
                                      RNA_int_get(op->ptr, "y_subdivisions"),
-                                     RNA_float_get(op->ptr, "size") * dia, mat))
+                                     RNA_float_get(op->ptr, "size"), mat))
        {
                return OPERATOR_CANCELLED;
        }
@@ -396,6 +399,7 @@ void MESH_OT_primitive_grid_add(wmOperatorType *ot)
        ot->idname = "MESH_OT_primitive_grid_add";
 
        /* api callbacks */
+       ot->invoke = WM_operator_view3d_distance_invoke;
        ot->exec = add_primitive_grid_exec;
        ot->poll = ED_operator_scene_editable;
 
@@ -473,7 +477,7 @@ static int add_primitive_uvsphere_exec(bContext *C, wmOperator *op)
        if (!EDBM_op_call_and_selectf(em, op, "verts.out",
                                      "create_uvsphere u_segments=%i v_segments=%i diameter=%f matrix=%m4",
                                      RNA_int_get(op->ptr, "segments"), RNA_int_get(op->ptr, "ring_count"),
-                                     RNA_float_get(op->ptr, "size") * dia, mat))
+                                     RNA_float_get(op->ptr, "size"), mat))
        {
                return OPERATOR_CANCELLED;
        }
@@ -493,6 +497,7 @@ void MESH_OT_primitive_uv_sphere_add(wmOperatorType *ot)
        ot->idname = "MESH_OT_primitive_uv_sphere_add";
 
        /* api callbacks */
+       ot->invoke = WM_operator_view3d_distance_invoke;
        ot->exec = add_primitive_uvsphere_exec;
        ot->poll = ED_operator_scene_editable;
 
@@ -525,7 +530,7 @@ static int add_primitive_icosphere_exec(bContext *C, wmOperator *op)
                em, op, "verts.out",
                "create_icosphere subdivisions=%i diameter=%f matrix=%m4",
                RNA_int_get(op->ptr, "subdivisions"),
-               RNA_float_get(op->ptr, "size") * dia, mat))
+               RNA_float_get(op->ptr, "size"), mat))
        {
                return OPERATOR_CANCELLED;
        }
@@ -545,6 +550,7 @@ void MESH_OT_primitive_ico_sphere_add(wmOperatorType *ot)
        ot->idname = "MESH_OT_primitive_ico_sphere_add";
 
        /* api callbacks */
+       ot->invoke = WM_operator_view3d_distance_invoke;
        ot->exec = add_primitive_icosphere_exec;
        ot->poll = ED_operator_scene_editable;
 
index ad1e2816c10097f762f19b3c0922527faf8d5632..7c4a547debc7fc707a037882e7e2d2d08d774c49 100644 (file)
@@ -210,8 +210,8 @@ float ED_object_new_primitive_matrix(bContext *C, Object *obedit,
        invert_m3_m3(imat, mat);
        mul_m3_v3(imat, primmat[3]);
 
-       if (v3d) {
-               float dia = ED_view3d_grid_scale(scene, v3d, NULL);
+       {
+               const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
 
                if (apply_diameter) {
                        primmat[0][0] *= dia;
index 6c23635da90da30e39cea3c911b91847a9633568..51261f4c341c7aae3509afea0b89497899cbeda7 100644 (file)
@@ -451,10 +451,9 @@ static void drawgrid(UnitSettings *unit, ARegion *ar, View3D *v3d, const char **
 }
 #undef GRID_MIN_PX
 
-float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
+/** could move this elsewhere, but tied into #ED_view3d_grid_scale */
+float ED_scene_grid_scale(Scene *scene, const char **grid_unit)
 {
-       float grid_scale = v3d->grid;
-
        /* apply units */
        if (scene->unit.system) {
                void *usys;
@@ -466,11 +465,16 @@ float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
                        int i = bUnit_GetBaseUnit(usys);
                        if (grid_unit)
                                *grid_unit = bUnit_GetNameDisplay(usys, i);
-                       grid_scale = (grid_scale * (float)bUnit_GetScaler(usys, i)) / scene->unit.scale_length;
+                       return (float)bUnit_GetScaler(usys, i) / scene->unit.scale_length;
                }
        }
 
-       return grid_scale;
+       return 1.0f;
+}
+
+float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
+{
+       return v3d->grid * ED_scene_grid_scale(scene, grid_unit);
 }
 
 static void drawfloor(Scene *scene, View3D *v3d, const char **grid_unit)
index 1037542759fa344e0585aaff2d9b9355ce5649be..8d885bf6d6f27ca54e9774a9c92dd19be380eb45 100644 (file)
@@ -171,6 +171,7 @@ void                WM_event_timer_sleep(struct wmWindowManager *wm, struct wmWindow *win, str
 
                /* operator api, default callbacks */
                        /* invoke callback, uses enum property named "type" */
+int                    WM_operator_view3d_distance_invoke(struct bContext *C, struct wmOperator *op, struct wmEvent *event);
 int                    WM_menu_invoke                  (struct bContext *C, struct wmOperator *op, struct wmEvent *event);
 int                    WM_enum_search_invoke(struct bContext *C, struct wmOperator *op, struct wmEvent *event);
                        /* invoke callback, confirm menu + exec */
index c555f771a48d406a503e4e945bc7398d6061c041..8a0701b1063f523df96c474a9b16d49c2be64153 100644 (file)
@@ -87,6 +87,7 @@
 #include "ED_screen.h"
 #include "ED_util.h"
 #include "ED_object.h"
+#include "ED_view3d.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
@@ -689,6 +690,35 @@ void WM_operator_properties_free(PointerRNA *ptr)
 
 /* ************ default op callbacks, exported *********** */
 
+int WM_operator_view3d_distance_invoke(struct bContext *C, struct wmOperator *op, struct wmEvent *UNUSED(event))
+{
+       Scene *scene = CTX_data_scene(C);
+       View3D *v3d = CTX_wm_view3d(C);
+
+       const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
+
+       /* always run, so the values are initialized,
+        * otherwise we may get differ behavior when (dia != 1.0) */
+       RNA_STRUCT_BEGIN(op->ptr, prop)
+       {
+               if (RNA_property_type(prop) == PROP_FLOAT) {
+                       PropertySubType pstype = RNA_property_subtype(prop);
+                       if (pstype == PROP_DISTANCE) {
+                               /* we don't support arrays yet */
+                               BLI_assert(RNA_property_array_check(prop) == FALSE);
+                               /* initialize */
+                               if (!RNA_property_is_set_ex(op->ptr, prop, FALSE)) {
+                                       const float value = RNA_property_float_get_default(op->ptr, prop) * dia;
+                                       RNA_property_float_set(op->ptr, prop, value);
+                               }
+                       }
+               }
+       }
+       RNA_STRUCT_END;
+
+       return op->type->exec(C, op);
+}
+
 /* invoke callback, uses enum property named "type" */
 int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 {