Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/space_view3d/view3d_gizmo_ruler.c
| Show All 34 Lines | |||||
| #include "BKE_unit.h" | #include "BKE_unit.h" | ||||
| #include "BKE_material.h" | #include "BKE_material.h" | ||||
| #include "DNA_meshdata_types.h" | #include "DNA_meshdata_types.h" | ||||
| #include "DNA_object_types.h" | #include "DNA_object_types.h" | ||||
| #include "DNA_gpencil_types.h" | #include "DNA_gpencil_types.h" | ||||
| #include "DNA_view3d_types.h" | #include "DNA_view3d_types.h" | ||||
| #include "ED_gizmo_library.h" | |||||
| #include "ED_gizmo_utils.h" | #include "ED_gizmo_utils.h" | ||||
| #include "ED_gpencil.h" | #include "ED_gpencil.h" | ||||
| #include "ED_screen.h" | #include "ED_screen.h" | ||||
| #include "ED_transform_snap_object_context.h" | #include "ED_transform_snap_object_context.h" | ||||
| #include "ED_view3d.h" | #include "ED_view3d.h" | ||||
| #include "UI_resources.h" | #include "UI_resources.h" | ||||
| #include "UI_interface.h" | #include "UI_interface.h" | ||||
| Show All 39 Lines | |||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| /* Ruler Info (wmGizmoGroup customdata) */ | /* Ruler Info (wmGizmoGroup customdata) */ | ||||
| enum { | enum { | ||||
| RULER_STATE_NORMAL = 0, | RULER_STATE_NORMAL = 0, | ||||
| RULER_STATE_DRAG, | RULER_STATE_DRAG, | ||||
| }; | }; | ||||
| enum { | |||||
| RULER_SNAP_OK = (1 << 0), | |||||
| }; | |||||
| struct RulerItem; | struct RulerItem; | ||||
| typedef struct RulerInfo { | typedef struct RulerInfo { | ||||
| struct RulerItem *item_active; | struct RulerItem *item_active; | ||||
| int flag; | int flag; | ||||
| int snap_flag; | int snap_elem_type; /* Zero means no snap */ | ||||
| int state; | int state; | ||||
| struct SnapObjectContext *snap_context; | struct SnapObjectContext *snap_context; | ||||
| /* wm state */ | /* wm state */ | ||||
| wmWindow *win; | wmWindow *win; | ||||
| ScrArea *sa; | ScrArea *sa; | ||||
| ARegion *ar; /* re-assigned every modal update */ | ARegion *ar; /* re-assigned every modal update */ | ||||
| ▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | static bool view3d_ruler_item_mousemove(RulerInfo *ruler_info, | ||||
| RulerItem *ruler_item, | RulerItem *ruler_item, | ||||
| const int mval[2], | const int mval[2], | ||||
| const bool do_thickness, | const bool do_thickness, | ||||
| const bool do_snap) | const bool do_snap) | ||||
| { | { | ||||
| const float eps_bias = 0.0002f; | const float eps_bias = 0.0002f; | ||||
| float dist_px = MVAL_MAX_PX_DIST * U.pixelsize; /* snap dist */ | float dist_px = MVAL_MAX_PX_DIST * U.pixelsize; /* snap dist */ | ||||
| ruler_info->snap_flag &= ~RULER_SNAP_OK; | ruler_info->snap_elem_type = 0; | ||||
| if (ruler_item) { | if (ruler_item) { | ||||
| RulerInteraction *inter = ruler_item->gz.interaction_data; | RulerInteraction *inter = ruler_item->gz.interaction_data; | ||||
| float *co = ruler_item->co[inter->co_index]; | float *co = ruler_item->co[inter->co_index]; | ||||
| /* restore the initial depth */ | /* restore the initial depth */ | ||||
| copy_v3_v3(co, inter->drag_start_co); | copy_v3_v3(co, inter->drag_start_co); | ||||
| view3d_ruler_item_project(ruler_info, co, mval); | view3d_ruler_item_project(ruler_info, co, mval); | ||||
| if (do_thickness && inter->co_index != 1) { | if (do_thickness && inter->co_index != 1) { | ||||
| ▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | else if (do_snap) { | ||||
| else if (inter->co_index == 0) { | else if (inter->co_index == 0) { | ||||
| prev_point = ruler_item->co[2]; | prev_point = ruler_item->co[2]; | ||||
| } | } | ||||
| else { | else { | ||||
| prev_point = ruler_item->co[0]; | prev_point = ruler_item->co[0]; | ||||
| } | } | ||||
| } | } | ||||
| if (ED_transform_snap_object_project_view3d( | ruler_info->snap_elem_type = ED_transform_snap_object_project_view3d_ex( | ||||
| ruler_info->snap_context, | ruler_info->snap_context, | ||||
| (SCE_SNAP_MODE_VERTEX | SCE_SNAP_MODE_EDGE | SCE_SNAP_MODE_FACE | | (SCE_SNAP_MODE_VERTEX | SCE_SNAP_MODE_EDGE | SCE_SNAP_MODE_FACE | | ||||
| SCE_SNAP_MODE_EDGE_MIDPOINT | SCE_SNAP_MODE_EDGE_PERPENDICULAR), | SCE_SNAP_MODE_EDGE_MIDPOINT | SCE_SNAP_MODE_EDGE_PERPENDICULAR), | ||||
| &(const struct SnapObjectParams){ | &(const struct SnapObjectParams){ | ||||
| .snap_select = SNAP_ALL, | .snap_select = SNAP_ALL, | ||||
| .use_object_edit_cage = true, | .use_object_edit_cage = true, | ||||
| .use_occlusion_test = true, | .use_occlusion_test = true, | ||||
| }, | }, | ||||
| mval_fl, | mval_fl, | ||||
| prev_point, | prev_point, | ||||
| &dist_px, | &dist_px, | ||||
| co, | co, | ||||
| NULL)) { | NULL, | ||||
| ruler_info->snap_flag |= RULER_SNAP_OK; | NULL, | ||||
| } | NULL, | ||||
| NULL); | |||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| else { | else { | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | static void gizmo_ruler_draw(const bContext *C, wmGizmo *gz) | ||||
| RegionView3D *rv3d = ar->regiondata; | RegionView3D *rv3d = ar->regiondata; | ||||
| const float cap_size = 4.0f; | const float cap_size = 4.0f; | ||||
| const float bg_margin = 4.0f * U.pixelsize; | const float bg_margin = 4.0f * U.pixelsize; | ||||
| const float arc_size = 64.0f * U.pixelsize; | const float arc_size = 64.0f * U.pixelsize; | ||||
| #define ARC_STEPS 24 | #define ARC_STEPS 24 | ||||
| const int arc_steps = ARC_STEPS; | const int arc_steps = ARC_STEPS; | ||||
| const float color_act[4] = {1.0f, 1.0f, 1.0f, 1.0f}; | const float color_act[4] = {1.0f, 1.0f, 1.0f, 1.0f}; | ||||
| const float color_base[4] = {0.0f, 0.0f, 0.0f, 1.0f}; | const float color_base[4] = {0.0f, 0.0f, 0.0f, 1.0f}; | ||||
| uchar color_text[3]; | uchar color_text[4]; | ||||
| uchar color_wire[3]; | uchar color_wire[3]; | ||||
| float color_back[4] = {1.0f, 1.0f, 1.0f, 0.5f}; | float color_back[4] = {1.0f, 1.0f, 1.0f, 0.5f}; | ||||
| /* anti-aliased lines for more consistent appearance */ | /* anti-aliased lines for more consistent appearance */ | ||||
| GPU_line_smooth(true); | GPU_line_smooth(true); | ||||
| GPU_line_width(1.0f); | GPU_line_width(1.0f); | ||||
| BLF_enable(blf_mono_font, BLF_ROTATION); | BLF_enable(blf_mono_font, BLF_ROTATION); | ||||
| BLF_size(blf_mono_font, 14 * U.pixelsize, U.dpi); | BLF_size(blf_mono_font, 14 * U.pixelsize, U.dpi); | ||||
| BLF_rotation(blf_mono_font, 0.0f); | BLF_rotation(blf_mono_font, 0.0f); | ||||
| UI_GetThemeColor3ubv(TH_TEXT, color_text); | UI_GetThemeColor4ubv(TH_TEXT, color_text); | ||||
| UI_GetThemeColor3ubv(TH_WIRE, color_wire); | UI_GetThemeColor3ubv(TH_WIRE, color_wire); | ||||
| /* Avoid white on white text. (TODO Fix by using theme) */ | /* Avoid white on white text. (TODO Fix by using theme) */ | ||||
| if ((int)color_text[0] + (int)color_text[1] + (int)color_text[2] > 127 * 3 * 0.6f) { | if ((int)color_text[0] + (int)color_text[1] + (int)color_text[2] > 127 * 3 * 0.6f) { | ||||
| copy_v3_fl(color_back, 0.0f); | copy_v3_fl(color_back, 0.0f); | ||||
| } | } | ||||
| const bool is_act = (ruler_info->item_active == ruler_item); | const bool is_act = (ruler_info->item_active == ruler_item); | ||||
| ▲ Show 20 Lines • Show All 271 Lines • ▼ Show 20 Lines | #define ARC_STEPS 24 | ||||
| GPU_line_smooth(false); | GPU_line_smooth(false); | ||||
| BLF_disable(blf_mono_font, BLF_ROTATION); | BLF_disable(blf_mono_font, BLF_ROTATION); | ||||
| #undef ARC_STEPS | #undef ARC_STEPS | ||||
| /* draw snap */ | /* draw snap */ | ||||
| if ((ruler_info->snap_flag & RULER_SNAP_OK) && (ruler_info->state == RULER_STATE_DRAG) && | if (ruler_info->snap_elem_type && (ruler_info->state == RULER_STATE_DRAG) && | ||||
| (ruler_item->gz.interaction_data != NULL)) { | (ruler_item->gz.interaction_data != NULL)) { | ||||
| RulerInteraction *inter = ruler_item->gz.interaction_data; | RulerInteraction *inter = ruler_item->gz.interaction_data; | ||||
| /* size from drawSnapping */ | /* size from drawSnapping */ | ||||
| const float size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE); | float co_ss_snap[2]; | ||||
| float co_ss_snap[3]; | |||||
| ED_view3d_project_float_global( | ED_view3d_project_float_global( | ||||
| ar, ruler_item->co[inter->co_index], co_ss_snap, V3D_PROJ_TEST_NOP); | ar, ruler_item->co[inter->co_index], co_ss_snap, V3D_PROJ_TEST_NOP); | ||||
| uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); | ED_gizmo_draw_preset_snap_elem(gz, co_ss_snap, color_text, ruler_info->snap_elem_type); | ||||
| immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); | |||||
| immUniformColor4fv(color_act); | |||||
| imm_draw_circle_wire_2d(pos, co_ss_snap[0], co_ss_snap[1], size * U.pixelsize, 32); | |||||
| immUnbindProgram(); | |||||
| } | } | ||||
| } | } | ||||
| static int gizmo_ruler_test_select(bContext *UNUSED(C), wmGizmo *gz, const int mval[2]) | static int gizmo_ruler_test_select(bContext *UNUSED(C), wmGizmo *gz, const int mval[2]) | ||||
| { | { | ||||
| RulerItem *ruler_item_pick = (RulerItem *)gz; | RulerItem *ruler_item_pick = (RulerItem *)gz; | ||||
| float mval_fl[2] = {UNPACK2(mval)}; | float mval_fl[2] = {UNPACK2(mval)}; | ||||
| int co_index; | int co_index; | ||||
| ▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | |||||
| static void gizmo_ruler_exit(bContext *C, wmGizmo *gz, const bool cancel) | static void gizmo_ruler_exit(bContext *C, wmGizmo *gz, const bool cancel) | ||||
| { | { | ||||
| wmGizmoGroup *gzgroup = gz->parent_gzgroup; | wmGizmoGroup *gzgroup = gz->parent_gzgroup; | ||||
| RulerInfo *ruler_info = gzgroup->customdata; | RulerInfo *ruler_info = gzgroup->customdata; | ||||
| if (!cancel) { | if (!cancel) { | ||||
| if (ruler_info->state == RULER_STATE_DRAG) { | if (ruler_info->state == RULER_STATE_DRAG) { | ||||
| if (ruler_info->snap_flag & RULER_SNAP_OK) { | ruler_info->snap_elem_type = 0; | ||||
| ruler_info->snap_flag &= ~RULER_SNAP_OK; | |||||
| } | |||||
| ruler_state_set(C, ruler_info, RULER_STATE_NORMAL); | ruler_state_set(C, ruler_info, RULER_STATE_NORMAL); | ||||
| } | } | ||||
| /* We could convert only the current gizmo, for now just re-generate. */ | /* We could convert only the current gizmo, for now just re-generate. */ | ||||
| view3d_ruler_to_gpencil(C, gzgroup); | view3d_ruler_to_gpencil(C, gzgroup); | ||||
| } | } | ||||
| if (gz) { | if (gz) { | ||||
| MEM_SAFE_FREE(gz->interaction_data); | MEM_SAFE_FREE(gz->interaction_data); | ||||
| ▲ Show 20 Lines • Show All 189 Lines • Show Last 20 Lines | |||||