Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/space_view3d/view3d_ruler.c
| Show All 20 Lines | |||||
| */ | */ | ||||
| /** \file blender/editors/space_view3d/view3d_ruler.c | /** \file blender/editors/space_view3d/view3d_ruler.c | ||||
| * \ingroup spview3d | * \ingroup spview3d | ||||
| */ | */ | ||||
| /* defines VIEW3D_OT_ruler modal operator */ | /* defines VIEW3D_OT_ruler modal operator */ | ||||
| #include "DNA_meshdata_types.h" | |||||
| #include "DNA_scene_types.h" | #include "DNA_scene_types.h" | ||||
| #include "DNA_object_types.h" | #include "DNA_object_types.h" | ||||
| #include "DNA_gpencil_types.h" | #include "DNA_gpencil_types.h" | ||||
| #include "DNA_brush_types.h" | |||||
| #include "MEM_guardedalloc.h" | #include "MEM_guardedalloc.h" | ||||
| #include "BLI_math.h" | #include "BLI_math.h" | ||||
| #include "BLI_blenlib.h" | #include "BLI_blenlib.h" | ||||
| #include "BLT_translation.h" | #include "BLT_translation.h" | ||||
| #include "BKE_context.h" | #include "BKE_context.h" | ||||
| #include "BKE_gpencil.h" | #include "BKE_gpencil.h" | ||||
| #include "BKE_main.h" | #include "BKE_main.h" | ||||
| #include "BKE_material.h" | |||||
| #include "BKE_unit.h" | #include "BKE_unit.h" | ||||
| #include "BIF_gl.h" | #include "BIF_gl.h" | ||||
| #include "GPU_immediate.h" | #include "GPU_immediate.h" | ||||
| #include "GPU_immediate_util.h" | #include "GPU_immediate_util.h" | ||||
| #include "WM_api.h" | #include "WM_api.h" | ||||
| #include "WM_types.h" | #include "WM_types.h" | ||||
| #include "ED_gpencil.h" | |||||
| #include "ED_screen.h" | #include "ED_screen.h" | ||||
| #include "ED_view3d.h" | #include "ED_view3d.h" | ||||
| #include "ED_transform_snap_object_context.h" | #include "ED_transform_snap_object_context.h" | ||||
| #include "ED_space_api.h" | #include "ED_space_api.h" | ||||
| #include "BLF_api.h" | #include "BLF_api.h" | ||||
| #include "BIF_glutil.h" | #include "BIF_glutil.h" | ||||
| ▲ Show 20 Lines • Show All 233 Lines • ▼ Show 20 Lines | static void ruler_state_set(bContext *C, RulerInfo *ruler_info, int state) | ||||
| ruler_info->state = state; | ruler_info->state = state; | ||||
| } | } | ||||
| #define RULER_ID "RulerData3D" | #define RULER_ID "RulerData3D" | ||||
| static bool view3d_ruler_to_gpencil(bContext *C, RulerInfo *ruler_info) | static bool view3d_ruler_to_gpencil(bContext *C, RulerInfo *ruler_info) | ||||
| { | { | ||||
| Main *bmain = CTX_data_main(C); | Main *bmain = CTX_data_main(C); | ||||
| Scene *scene = CTX_data_scene(C); | Scene *scene = CTX_data_scene(C); | ||||
| Object *ob = CTX_data_active_object(C); | |||||
| bGPDlayer *gpl; | bGPDlayer *gpl; | ||||
| bGPDframe *gpf; | bGPDframe *gpf; | ||||
| bGPDstroke *gps; | bGPDstroke *gps; | ||||
| bGPDpalette *palette; | |||||
| bGPDpalettecolor *palcolor; | |||||
| RulerItem *ruler_item; | RulerItem *ruler_item; | ||||
| const char *ruler_name = RULER_ID; | const char *ruler_name = RULER_ID; | ||||
| bool changed = false; | bool changed = false; | ||||
| if (scene->gpd == NULL) { | /* FIXME: This needs to be reviewed. Should it keep being done like this? */ | ||||
| scene->gpd = BKE_gpencil_data_addnew(bmain, "GPencil"); | float cur[3] = { 0 }; | ||||
| } | Object *gp_ob = ED_add_gpencil_object(C, scene, cur); | ||||
| bGPdata *gpd = gp_ob->data; | |||||
| gpl = BLI_findstring(&scene->gpd->layers, ruler_name, offsetof(bGPDlayer, info)); | gpl = BLI_findstring(&gpd->layers, ruler_name, offsetof(bGPDlayer, info)); | ||||
| if (gpl == NULL) { | if (gpl == NULL) { | ||||
| gpl = BKE_gpencil_layer_addnew(scene->gpd, ruler_name, false); | gpl = BKE_gpencil_layer_addnew(gpd, ruler_name, false); | ||||
| gpl->thickness = 1; | gpl->thickness = 1; | ||||
| gpl->flag |= GP_LAYER_HIDE; | gpl->flag |= GP_LAYER_HIDE; | ||||
| } | } | ||||
| /* try to get active palette or create a new one */ | /* try to get active color or create a new one */ | ||||
| palette = BKE_gpencil_palette_getactive(scene->gpd); | Material *mat = BKE_gpencil_material_ensure(bmain, ob); | ||||
| if (palette == NULL) { | |||||
| palette = BKE_gpencil_palette_addnew(scene->gpd, DATA_("GP_Palette"), true); | |||||
| } | |||||
| /* try to get color with the ruler name or create a new one */ | |||||
| palcolor = BKE_gpencil_palettecolor_getbyname(palette, (char *)ruler_name); | |||||
| if (palcolor == NULL) { | |||||
| palcolor = BKE_gpencil_palettecolor_addnew(palette, (char *)ruler_name, true); | |||||
| } | |||||
| gpf = BKE_gpencil_layer_getframe(gpl, CFRA, true); | gpf = BKE_gpencil_layer_getframe(gpl, CFRA, true); | ||||
| BKE_gpencil_free_strokes(gpf); | BKE_gpencil_free_strokes(gpf); | ||||
| for (ruler_item = ruler_info->items.first; ruler_item; ruler_item = ruler_item->next) { | for (ruler_item = ruler_info->items.first; ruler_item; ruler_item = ruler_item->next) { | ||||
| bGPDspoint *pt; | bGPDspoint *pt; | ||||
| int j; | int j; | ||||
| /* allocate memory for a new stroke */ | /* allocate memory for a new stroke */ | ||||
| gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke"); | gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke"); | ||||
| if (ruler_item->flag & RULERITEM_USE_ANGLE) { | if (ruler_item->flag & RULERITEM_USE_ANGLE) { | ||||
| gps->totpoints = 3; | gps->totpoints = 3; | ||||
| pt = gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); | pt = gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); | ||||
| gps->dvert = MEM_callocN(sizeof(MDeformVert) * gps->totpoints, "gp_stroke_weights"); | |||||
| for (j = 0; j < 3; j++) { | for (j = 0; j < 3; j++) { | ||||
| copy_v3_v3(&pt->x, ruler_item->co[j]); | copy_v3_v3(&pt->x, ruler_item->co[j]); | ||||
| pt->pressure = 1.0f; | pt->pressure = 1.0f; | ||||
| pt->strength = 1.0f; | pt->strength = 1.0f; | ||||
| pt++; | pt++; | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| gps->totpoints = 2; | gps->totpoints = 2; | ||||
| pt = gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); | pt = gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); | ||||
| gps->dvert = MEM_callocN(sizeof(MDeformVert) * gps->totpoints, "gp_stroke_weights"); | |||||
| for (j = 0; j < 3; j += 2) { | for (j = 0; j < 3; j += 2) { | ||||
| copy_v3_v3(&pt->x, ruler_item->co[j]); | copy_v3_v3(&pt->x, ruler_item->co[j]); | ||||
| pt->pressure = 1.0f; | pt->pressure = 1.0f; | ||||
| pt->strength = 1.0f; | pt->strength = 1.0f; | ||||
| pt++; | pt++; | ||||
| } | } | ||||
| } | } | ||||
| gps->flag = GP_STROKE_3DSPACE; | gps->flag = GP_STROKE_3DSPACE; | ||||
| gps->thickness = 3; | gps->thickness = 3; | ||||
| /* assign color to stroke */ | /* assign color to stroke */ | ||||
| BLI_strncpy(gps->colorname, palcolor->info, sizeof(gps->colorname)); | gps->mat_nr = BKE_object_material_slot_find_index(ob, mat) - 1; | ||||
| gps->palcolor = palcolor; | |||||
| BLI_addtail(&gpf->strokes, gps); | BLI_addtail(&gpf->strokes, gps); | ||||
| changed = true; | changed = true; | ||||
| } | } | ||||
| return changed; | return changed; | ||||
| } | } | ||||
| static bool view3d_ruler_from_gpencil(bContext *C, RulerInfo *ruler_info) | static bool view3d_ruler_from_gpencil(bContext *C, RulerInfo *ruler_info) | ||||
| ▲ Show 20 Lines • Show All 743 Lines • Show Last 20 Lines | |||||