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.
This commit is contained in:
Campbell Barton
2012-12-09 10:48:18 +00:00
parent a20a0ce7c7
commit 22505c10f8
7 changed files with 60 additions and 17 deletions

View 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")

View 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 */

View 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;

View 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;

View 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)

View 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 */

View 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))
{