remove snapping for first point added to the ruler.
[blender.git] / source / blender / editors / space_view3d / view3d_ruler.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/space_view3d/view3d_ruler.c
24  *  \ingroup spview3d
25  */
26
27 /* defines VIEW3D_OT_ruler modal operator */
28
29 #include "DNA_scene_types.h"
30 #include "DNA_gpencil_types.h"
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_math.h"
35 #include "BLI_blenlib.h"
36
37 #include "BKE_context.h"
38 #include "BKE_unit.h"
39 #include "BKE_gpencil.h"
40
41 #include "BIF_gl.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45
46 #include "ED_screen.h"
47 #include "ED_space_api.h"
48
49 #include "BLF_api.h"
50 #include "BIF_glutil.h"
51
52 #include "UI_resources.h"
53 #include "UI_interface.h"
54
55 #include "view3d_intern.h"  /* own include */
56
57
58 /* -------------------------------------------------------------------- */
59 /* Snapping (could be own function) */
60 /* NOTE - this is not very nice use of transform snapping */
61 #include "ED_transform.h"
62
63 static bool ED_view3d_snap_co(bContext *C, float r_co[3], const float co_ss[2], float r_no[3],
64                               bool use_vert, bool use_edge, bool use_face)
65 {
66         float dist_px = 12;  /* snap dist */
67         float r_no_dummy[3];
68         bool ret = false;
69         float *r_no_ptr = r_no ? r_no : r_no_dummy;
70
71         Scene *scene = CTX_data_scene(C);
72         View3D *v3d = CTX_wm_view3d(C);
73         ARegion *ar = CTX_wm_region(C);
74         struct Object *obedit = CTX_data_edit_object(C);
75
76         /* try snap edge, then face if it fails */
77         if (use_vert) {
78                 ret = snapObjectsEx(scene, NULL, v3d, ar, obedit, SCE_SNAP_MODE_VERTEX,
79                                     co_ss, &dist_px, r_co, r_no_ptr, SNAP_ALL);
80         }
81         if (use_edge && (ret == false)) {
82                 ret = snapObjectsEx(scene, NULL, v3d, ar, obedit, SCE_SNAP_MODE_EDGE,
83                                     co_ss, &dist_px, r_co, r_no_ptr, SNAP_ALL);
84         }
85         if (use_face && (ret == false)) {
86                 ret = snapObjectsEx(scene, NULL, v3d, ar, obedit, SCE_SNAP_MODE_FACE,
87                                     co_ss, &dist_px, r_co, r_no_ptr, SNAP_ALL);
88         }
89
90         return ret;
91 }
92
93 static bool ED_view3d_snap_ray(bContext *C, float r_co[3],
94                                const float ray_start[3], const float ray_normal[3])
95 {
96         float dist_px = 12;  /* snap dist */
97         float r_no_dummy[3];
98         float ray_dist = TRANSFORM_DIST_MAX_RAY;
99         bool ret;
100
101         Scene *scene = CTX_data_scene(C);
102         View3D *v3d = CTX_wm_view3d(C);
103         ARegion *ar = CTX_wm_region(C);
104         struct Object *obedit = CTX_data_edit_object(C);
105
106         /* try snap edge, then face if it fails */
107         ret = snapObjectsRayEx(scene, NULL, v3d, ar, obedit, SCE_SNAP_MODE_FACE,
108                                ray_start, ray_normal, &ray_dist,
109                                NULL, &dist_px, r_co, r_no_dummy, SNAP_ALL);
110
111         return ret;
112 }
113 /* done snapping */
114
115
116 /* -------------------------------------------------------------------- */
117 /* Ruler Item (we can have many) */
118 enum {
119         RULERITEM_USE_ANGLE = (1 << 0),  /* use protractor */
120         RULERITEM_USE_RAYCAST = (1 << 1)
121 };
122
123 enum {
124         RULERITEM_DIRECTION_IN = 0,
125         RULERITEM_DIRECTION_OUT
126 };
127
128 #define RULER_PICK_DIST 75.0f
129 #define RULER_PICK_DIST_SQ (RULER_PICK_DIST * RULER_PICK_DIST)
130
131 typedef struct RulerItem {
132         struct RulerItem *next, *prev;
133
134         /* worldspace coords, middle being optional */
135         float co[3][3];
136
137         /* selected coord */
138         char  co_index; /* 0 -> 2*/
139
140         int   flag;
141         int   raycast_dir;  /* RULER_DIRECTION_* */
142 } RulerItem;
143
144 enum {
145         RULER_STATE_NORMAL = 0,
146         RULER_STATE_DRAG
147 };
148
149
150 /* -------------------------------------------------------------------- */
151 /* Ruler Info (one per session) */
152
153 typedef struct RulerInfo {
154         ListBase items;
155         int      item_active;
156         int flag;
157         int snap_flag;
158         int state;
159
160         /* --- */
161         ARegion *ar;
162         void *draw_handle_pixel;
163 } RulerInfo;
164
165 /* -------------------------------------------------------------------- */
166 /* local functions */
167 static RulerItem *ruler_item_add(RulerInfo *ruler_info)
168 {
169         RulerItem *ruler_item = MEM_callocN(sizeof(RulerItem), "RulerItem");
170         BLI_addtail(&ruler_info->items, ruler_item);
171         return ruler_item;
172 }
173
174 static void ruler_item_remove(RulerInfo *ruler_info, RulerItem *ruler_item)
175 {
176         BLI_remlink(&ruler_info->items, ruler_item);
177         MEM_freeN(ruler_item);
178 }
179
180 static RulerItem *ruler_item_active_get(RulerInfo *ruler_info)
181 {
182         return BLI_findlink(&ruler_info->items, ruler_info->item_active);
183 }
184
185 static void ruler_item_active_set(RulerInfo *ruler_info, RulerItem *ruler_item)
186 {
187         ruler_info->item_active = BLI_findindex(&ruler_info->items, ruler_item);
188 }
189
190 static void ruler_item_as_string(RulerItem *ruler_item, UnitSettings *unit,
191                                  char *numstr, size_t numstr_size, int prec)
192 {
193         const int do_split = unit->flag & USER_UNIT_OPT_SPLIT;
194
195         if (ruler_item->flag & RULERITEM_USE_ANGLE) {
196                 const float ruler_angle = angle_v3v3v3(ruler_item->co[0],
197                                                        ruler_item->co[1],
198                                                        ruler_item->co[2]);
199
200                 if (unit->system == USER_UNIT_NONE) {
201                         BLI_snprintf(numstr, numstr_size, "%.*f°", prec, RAD2DEGF(ruler_angle));
202                 }
203                 else {
204                         bUnit_AsString(numstr, numstr_size,
205                                        (double)ruler_angle,
206                                        prec, unit->system, B_UNIT_ROTATION, do_split, false);
207                 }
208         }
209         else {
210                 const float ruler_len = len_v3v3(ruler_item->co[0],
211                                                  ruler_item->co[2]);
212
213                 if (unit->system == USER_UNIT_NONE) {
214                         BLI_snprintf(numstr, numstr_size, "%.*f", prec, ruler_len);
215                 }
216                 else {
217                         bUnit_AsString(numstr, numstr_size,
218                                        (double)(ruler_len * unit->scale_length),
219                                        prec, unit->system, B_UNIT_LENGTH, do_split, false);
220                 }
221         }
222
223 }
224
225 static bool view3d_ruler_pick(RulerInfo *ruler_info, const float mval[2],
226                               RulerItem **r_ruler_item, int *r_co_index)
227 {
228         ARegion *ar = ruler_info->ar;
229         RulerItem *ruler_item;
230
231         float dist_best = RULER_PICK_DIST_SQ;
232         RulerItem *ruler_item_best = NULL;
233         int co_index_best = -1;
234
235         for (ruler_item = ruler_info->items.first; ruler_item; ruler_item = ruler_item->next) {
236                 float co_ss[3][2];
237                 float dist;
238                 int j;
239
240                 /* should these be checked? - ok for now not to */
241                 for (j = 0; j < 3; j++) {
242                         ED_view3d_project_float_global(ar, ruler_item->co[j], co_ss[j], V3D_PROJ_TEST_NOP);
243                 }
244
245                 if (ruler_item->flag & RULERITEM_USE_ANGLE) {
246                         dist = min_ff(dist_squared_to_line_segment_v2(mval, co_ss[0], co_ss[1]),
247                                       dist_squared_to_line_segment_v2(mval, co_ss[1], co_ss[2]));
248                         if (dist < dist_best) {
249                                 dist_best = dist;
250                                 ruler_item_best = ruler_item;
251
252                                 {
253                                         float dist_points[3] = {len_squared_v2v2(co_ss[0], mval),
254                                                                 len_squared_v2v2(co_ss[1], mval),
255                                                                 len_squared_v2v2(co_ss[2], mval)};
256                                         if (min_fff(UNPACK3(dist_points)) < RULER_PICK_DIST_SQ) {
257                                                 co_index_best = min_axis_v3(dist_points);
258                                         }
259                                         else {
260                                                 co_index_best = -1;
261                                         }
262                                 }
263                         }
264                 }
265                 else {
266                         dist = dist_squared_to_line_segment_v2(mval, co_ss[0], co_ss[2]);
267                         if (dist < dist_best) {
268                                 dist_best = dist;
269                                 ruler_item_best = ruler_item;
270
271                                 {
272                                         float dist_points[2] = {len_squared_v2v2(co_ss[0], mval),
273                                                                 len_squared_v2v2(co_ss[2], mval)};
274                                         if (min_ff(UNPACK2(dist_points)) < RULER_PICK_DIST_SQ) {
275                                                 co_index_best = (dist_points[0] < dist_points[1]) ? 0 : 2;
276                                         }
277                                         else {
278                                                 co_index_best = -1;
279                                         }
280                                 }
281                         }
282                 }
283         }
284
285         if (ruler_item_best) {
286                 *r_ruler_item = ruler_item_best;
287                 *r_co_index = co_index_best;
288                 return true;
289         }
290         else {
291                 *r_ruler_item = NULL;
292                 *r_co_index = -1;
293                 return false;
294         }
295 }
296
297 #define RULER_ID "RulerData3D"
298 static bool view3d_ruler_to_gpencil(bContext *C, RulerInfo *ruler_info)
299 {
300         Scene *scene = CTX_data_scene(C);
301         bGPDlayer *gpl;
302         bGPDframe *gpf;
303         bGPDstroke *gps;
304         RulerItem *ruler_item;
305         const char *ruler_name = RULER_ID;
306         bool change = false;
307
308         if (scene->gpd == NULL) {
309                 scene->gpd = gpencil_data_addnew("GPencil");
310         }
311
312         gpl = BLI_findstring(&scene->gpd->layers, ruler_name, offsetof(bGPDlayer, info));
313         if (gpl == NULL) {
314                 gpl = gpencil_layer_addnew(scene->gpd, ruler_name, false);
315                 gpl->thickness = 1;
316                 gpl->flag |= GP_LAYER_HIDE;
317         }
318
319         gpf = gpencil_layer_getframe(gpl, CFRA, true);
320         free_gpencil_strokes(gpf);
321
322         for (ruler_item = ruler_info->items.first; ruler_item; ruler_item = ruler_item->next) {
323                 bGPDspoint *pt;
324                 int j;
325
326                 /* allocate memory for a new stroke */
327                 gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke");
328                 if (ruler_item->flag & RULERITEM_USE_ANGLE) {
329                         gps->totpoints = 3;
330                         pt = gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
331                         for (j = 0; j < 3; j++) {
332                                 copy_v3_v3(&pt->x, ruler_item->co[j]);
333                                 pt->pressure = 1.0f;
334                                 pt++;
335                         }
336                 }
337                 else {
338                         gps->totpoints = 2;
339                         pt = gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
340                         for (j = 0; j < 3; j += 2) {
341                                 copy_v3_v3(&pt->x, ruler_item->co[j]);
342                                 pt->pressure = 1.0f;
343                                 pt++;
344                         }
345                 }
346                 gps->flag = GP_STROKE_3DSPACE;
347                 BLI_addtail(&gpf->strokes, gps);
348                 change = true;
349         }
350
351         return change;
352 }
353
354 static bool view3d_ruler_from_gpencil(bContext *C, RulerInfo *ruler_info)
355 {
356         Scene *scene = CTX_data_scene(C);
357         bool change = false;
358
359         if (scene->gpd) {
360                 bGPDlayer *gpl;
361                 const char *ruler_name = RULER_ID;
362                 gpl = BLI_findstring(&scene->gpd->layers, ruler_name, offsetof(bGPDlayer, info));
363                 if (gpl) {
364                         bGPDframe *gpf;
365                         gpf = gpencil_layer_getframe(gpl, CFRA, false);
366                         if (gpf) {
367                                 bGPDstroke *gps;
368                                 for (gps = gpf->strokes.first; gps; gps = gps->next) {
369                                         bGPDspoint *pt = gps->points;
370                                         int j;
371                                         if (gps->totpoints == 3) {
372                                                 RulerItem *ruler_item = ruler_item_add(ruler_info);
373                                                 for (j = 0; j < 3; j++) {
374                                                         copy_v3_v3(ruler_item->co[j], &pt->x);
375                                                         pt++;
376                                                 }
377                                                 ruler_item->flag |= RULERITEM_USE_ANGLE;
378                                                 change = true;
379                                         }
380                                         else if (gps->totpoints == 2) {
381                                                 RulerItem *ruler_item = ruler_item_add(ruler_info);
382                                                 for (j = 0; j < 3; j += 2) {
383                                                         copy_v3_v3(ruler_item->co[j], &pt->x);
384                                                         pt++;
385                                                 }
386                                                 change = true;
387                                         }
388                                 }
389                         }
390                 }
391         }
392
393         return change;
394 }
395
396 /* -------------------------------------------------------------------- */
397 /* local callbacks */
398
399 static void ruler_info_draw_pixel(const struct bContext *C, ARegion *ar, void *arg)
400 {
401         Scene *scene = CTX_data_scene(C);
402         UnitSettings *unit = &scene->unit;
403         RulerItem *ruler_item;
404         RulerInfo *ruler_info = arg;
405         RegionView3D *rv3d = ruler_info->ar->regiondata;
406 //      ARegion *ar = ruler_info->ar;
407         const float cap_size = 4.0f;
408         const float bg_margin = 4.0f * U.pixelsize;
409         const float bg_radius = 4.0f * U.pixelsize;
410         const float arc_size = 64.0f * U.pixelsize;
411 #define ARC_STEPS 24
412         const int arc_steps = ARC_STEPS;
413         int i;
414         //unsigned int color_act = 0x666600;
415         unsigned int color_act = 0xffffff;
416         unsigned int color_base = 0x0;
417         unsigned char color_back[4] = {0xff, 0xff, 0xff, 0x80};
418         unsigned char color_text[3];
419         unsigned char color_wire[3];
420
421         /* anti-aliased lines for more consistent appearance */
422         glEnable(GL_LINE_SMOOTH);
423
424         BLF_enable(blf_mono_font, BLF_ROTATION);
425         BLF_size(blf_mono_font, 14 * U.pixelsize, U.dpi);
426         BLF_rotation(blf_mono_font, 0.0f);
427
428         UI_GetThemeColor3ubv(TH_TEXT, color_text);
429         UI_GetThemeColor3ubv(TH_WIRE, color_wire);
430
431         for (ruler_item = ruler_info->items.first, i = 0; ruler_item; ruler_item = ruler_item->next, i++) {
432                 const bool is_act = (i == ruler_info->item_active);
433                 float dir_ruler[2];
434                 float co_ss[3][2];
435                 int j;
436
437                 /* should these be checked? - ok for now not to */
438                 for (j = 0; j < 3; j++) {
439                         ED_view3d_project_float_global(ar, ruler_item->co[j], co_ss[j], V3D_PROJ_TEST_NOP);
440                 }
441
442                 glEnable(GL_BLEND);
443
444                 cpack(is_act ? color_act : color_base);
445
446                 if (ruler_item->flag & RULERITEM_USE_ANGLE) {
447                         glBegin(GL_LINE_STRIP);
448                         for (j = 0; j < 3; j++) {
449                                 glVertex2fv(co_ss[j]);
450                         }
451                         glEnd();
452                         cpack(0xaaaaaa);
453                         setlinestyle(3);
454                         glBegin(GL_LINE_STRIP);
455                         for (j = 0; j < 3; j++) {
456                                 glVertex2fv(co_ss[j]);
457                         }
458                         glEnd();
459                         setlinestyle(0);
460
461                         /* arc */
462                         {
463                                 float dir_tmp[3];
464                                 float co_tmp[3];
465                                 float arc_ss_coords[ARC_STEPS + 1][2];
466
467                                 float dir_a[3];
468                                 float dir_b[3];
469                                 float quat[4];
470                                 float axis[3];
471                                 float angle;
472                                 const float px_scale = (ED_view3d_pixel_size(rv3d, ruler_item->co[1]) *
473                                                         min_fff(arc_size,
474                                                                 len_v2v2(co_ss[0], co_ss[1]) / 2.0f,
475                                                                 len_v2v2(co_ss[2], co_ss[1]) / 2.0f));
476
477                                 sub_v3_v3v3(dir_a, ruler_item->co[0], ruler_item->co[1]);
478                                 sub_v3_v3v3(dir_b, ruler_item->co[2], ruler_item->co[1]);
479                                 normalize_v3(dir_a);
480                                 normalize_v3(dir_b);
481
482                                 cross_v3_v3v3(axis, dir_a, dir_b);
483                                 angle = angle_normalized_v3v3(dir_a, dir_b);
484
485                                 axis_angle_to_quat(quat, axis, angle / arc_steps);
486
487                                 copy_v3_v3(dir_tmp, dir_a);
488
489                                 glColor3ubv(color_wire);
490
491                                 for (j = 0; j <= arc_steps; j++) {
492                                         madd_v3_v3v3fl(co_tmp, ruler_item->co[1], dir_tmp, px_scale);
493                                         ED_view3d_project_float_global(ar, co_tmp, arc_ss_coords[j], V3D_PROJ_TEST_NOP);
494                                         mul_qt_v3(quat, dir_tmp);
495                                 }
496
497                                 glEnableClientState(GL_VERTEX_ARRAY);
498                                 glVertexPointer(2, GL_FLOAT, 0, arc_ss_coords);
499                                 glDrawArrays(GL_LINE_STRIP, 0, arc_steps + 1);
500                                 glDisableClientState(GL_VERTEX_ARRAY);
501                         }
502
503                         /* text */
504                         {
505                                 char numstr[256];
506                                 float numstr_size[2];
507                                 float pos[2];
508                                 const int prec = 2;  /* XXX, todo, make optional */
509
510                                 ruler_item_as_string(ruler_item, unit, numstr, sizeof(numstr), prec);
511
512                                 BLF_width_and_height(blf_mono_font, numstr, &numstr_size[0], &numstr_size[1]);
513
514                                 pos[0] = co_ss[1][0] + (cap_size * 2.0f);
515                                 pos[1] = co_ss[1][1] - (numstr_size[1] / 2.0f);
516
517                                 /* draw text (bg) */
518                                 glColor4ubv(color_back);
519                                 uiSetRoundBox(UI_CNR_ALL);
520                                 uiRoundBox(pos[0] - bg_margin,                  pos[1] - bg_margin,
521                                            pos[0] + bg_margin + numstr_size[0], pos[1] + bg_margin + numstr_size[1],
522                                            bg_radius);
523                                 /* draw text */
524                                 glColor3ubv(color_text);
525                                 BLF_position(blf_mono_font, pos[0], pos[1], 0.0f);
526                                 BLF_rotation(blf_mono_font, 0.0f);
527                                 BLF_draw(blf_mono_font, numstr, sizeof(numstr));
528                         }
529
530                         /* capping */
531                         {
532                                 float rot_90_vec_a[2];
533                                 float rot_90_vec_b[2];
534                                 float cap[2];
535
536                                 sub_v2_v2v2(dir_ruler, co_ss[0], co_ss[1]);
537                                 rot_90_vec_a[0] = -dir_ruler[1];
538                                 rot_90_vec_a[1] =  dir_ruler[0];
539                                 normalize_v2(rot_90_vec_a);
540
541                                 sub_v2_v2v2(dir_ruler, co_ss[1], co_ss[2]);
542                                 rot_90_vec_b[0] = -dir_ruler[1];
543                                 rot_90_vec_b[1] =  dir_ruler[0];
544                                 normalize_v2(rot_90_vec_b);
545
546                                 glEnable(GL_BLEND);
547
548                                 glColor3ubv(color_wire);
549
550                                 glBegin(GL_LINES);
551
552                                 madd_v2_v2v2fl(cap, co_ss[0], rot_90_vec_a, cap_size);
553                                 glVertex2fv(cap);
554                                 madd_v2_v2v2fl(cap, co_ss[0], rot_90_vec_a, -cap_size);
555                                 glVertex2fv(cap);
556
557                                 madd_v2_v2v2fl(cap, co_ss[2], rot_90_vec_b, cap_size);
558                                 glVertex2fv(cap);
559                                 madd_v2_v2v2fl(cap, co_ss[2], rot_90_vec_b, -cap_size);
560                                 glVertex2fv(cap);
561
562                                 /* angle vertex */
563                                 glVertex2f(co_ss[1][0] - cap_size, co_ss[1][1] - cap_size);
564                                 glVertex2f(co_ss[1][0] + cap_size, co_ss[1][1] + cap_size);
565                                 glVertex2f(co_ss[1][0] - cap_size, co_ss[1][1] + cap_size);
566                                 glVertex2f(co_ss[1][0] + cap_size, co_ss[1][1] - cap_size);
567                                 glEnd();
568
569                                 glDisable(GL_BLEND);
570                         }
571                 }
572                 else {
573                         glBegin(GL_LINE_STRIP);
574                         for (j = 0; j < 3; j += 2) {
575                                 glVertex2fv(co_ss[j]);
576                         }
577                         glEnd();
578                         cpack(0xaaaaaa);
579                         setlinestyle(3);
580                         glBegin(GL_LINE_STRIP);
581                         for (j = 0; j < 3; j += 2) {
582                                 glVertex2fv(co_ss[j]);
583                         }
584                         glEnd();
585                         setlinestyle(0);
586
587                         sub_v2_v2v2(dir_ruler, co_ss[0], co_ss[2]);
588
589                         /* text */
590                         {
591                                 char numstr[256];
592                                 float numstr_size[2];
593                                 const int prec = 6;  /* XXX, todo, make optional */
594                                 float pos[2];
595
596                                 ruler_item_as_string(ruler_item, unit, numstr, sizeof(numstr), prec);
597
598                                 BLF_width_and_height(blf_mono_font, numstr, &numstr_size[0], &numstr_size[1]);
599
600                                 mid_v2_v2v2(pos, co_ss[0], co_ss[2]);
601
602                                 /* center text */
603                                 pos[0] -= numstr_size[0] / 2.0f;
604                                 pos[1] -= numstr_size[1] / 2.0f;
605
606                                 /* draw text (bg) */
607                                 glColor4ubv(color_back);
608                                 uiSetRoundBox(UI_CNR_ALL);
609                                 uiRoundBox(pos[0] - bg_margin,                  pos[1] - bg_margin,
610                                            pos[0] + bg_margin + numstr_size[0], pos[1] + bg_margin + numstr_size[1],
611                                            bg_radius);
612                                 /* draw text */
613                                 glColor3ubv(color_text);
614                                 BLF_position(blf_mono_font, pos[0], pos[1], 0.0f);
615                                 BLF_draw(blf_mono_font, numstr, sizeof(numstr));
616                         }
617
618                         /* capping */
619                         {
620                                 float rot_90_vec[2] = {-dir_ruler[1], dir_ruler[0]};
621                                 float cap[2];
622
623                                 normalize_v2(rot_90_vec);
624
625                                 glEnable(GL_BLEND);
626                                 glColor3ubv(color_wire);
627
628                                 glBegin(GL_LINES);
629                                 madd_v2_v2v2fl(cap, co_ss[0], rot_90_vec, cap_size);
630                                 glVertex2fv(cap);
631                                 madd_v2_v2v2fl(cap, co_ss[0], rot_90_vec, -cap_size);
632                                 glVertex2fv(cap);
633
634                                 madd_v2_v2v2fl(cap, co_ss[2], rot_90_vec, cap_size);
635                                 glVertex2fv(cap);
636                                 madd_v2_v2v2fl(cap, co_ss[2], rot_90_vec, -cap_size);
637                                 glVertex2fv(cap);
638                                 glEnd();
639
640                                 glDisable(GL_BLEND);
641                         }
642                 }
643         }
644
645         glDisable(GL_LINE_SMOOTH);
646
647         BLF_disable(blf_mono_font, BLF_ROTATION);
648
649 #undef ARC_STEPS
650 }
651
652 /* free, use for both cancel and finish */
653 static void view3d_ruler_end(const struct bContext *UNUSED(C), RulerInfo *ruler_info)
654 {
655         ED_region_draw_cb_exit(ruler_info->ar->type, ruler_info->draw_handle_pixel);
656 }
657
658 static void view3d_ruler_free(RulerInfo *ruler_info)
659 {
660         BLI_freelistN(&ruler_info->items);
661         MEM_freeN(ruler_info);
662 }
663
664 static void view3d_ruler_item_project(RulerInfo *ruler_info, float r_co[3],
665                                       const int xy[2])
666 {
667         ED_view3d_win_to_3d_int(ruler_info->ar, r_co, xy, r_co);
668 }
669
670 /* use for mousemove events */
671 static bool view3d_ruler_item_mousemove(bContext *C, RulerInfo *ruler_info, const int mval[2],
672                                         const bool do_thickness, const bool do_snap)
673 {
674         RulerItem *ruler_item = ruler_item_active_get(ruler_info);
675
676         if (ruler_item) {
677                 float *co = ruler_item->co[ruler_item->co_index];
678                 view3d_ruler_item_project(ruler_info, co, mval);
679                 if (do_thickness && ruler_item->co_index != 1) {
680                         const float mval_fl[2] = {UNPACK2(mval)};
681                         float ray_normal[3];
682                         float ray_start[3];
683                         float *co_other;
684
685                         co_other = ruler_item->co[ruler_item->co_index == 0 ? 2 : 0];
686
687                         if (ED_view3d_snap_co(C, co, mval_fl, ray_normal,
688                                               false, false, true))
689                         {
690                                 negate_v3(ray_normal);
691                                 /* add some bias */
692                                 madd_v3_v3v3fl(ray_start, co, ray_normal, 0.0001f);
693                                 ED_view3d_snap_ray(C, co_other,
694                                                    ray_start, ray_normal);
695                         }
696                 }
697                 else if (do_snap) {
698                         const float mval_fl[2] = {UNPACK2(mval)};
699                         ED_view3d_snap_co(C, co, mval_fl, NULL,
700                                           true, true, true);
701                 }
702                 return true;
703         }
704         else {
705                 return false;
706         }
707 }
708
709 static void view3d_ruler_header_update(ScrArea *sa)
710 {
711         const char *text = "Ctrl+LMB: Add, "
712                            "Del: Remove, "
713                            "Ctrl+Drag: Snap, "
714                            "Shift+Drag: Thickness, "
715                            "Ctrl+C: Copy Value, "
716                            "Enter: Store,  "
717                            "Esc: Cancel";
718
719         ED_area_headerprint(sa, text);
720 }
721
722 /* -------------------------------------------------------------------- */
723 /* Operator callbacks */
724
725 static int view3d_ruler_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
726 {
727         wmWindow *win = CTX_wm_window(C);
728         ScrArea *sa = CTX_wm_area(C);
729         ARegion *ar = CTX_wm_region(C);
730         RulerInfo *ruler_info;
731
732         ruler_info = MEM_callocN(sizeof(RulerInfo), "RulerInfo");
733
734         if (view3d_ruler_from_gpencil(C, ruler_info)) {
735                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, NULL);
736         }
737
738         op->customdata = ruler_info;
739
740         ruler_info->ar = ar;
741         ruler_info->draw_handle_pixel = ED_region_draw_cb_activate(ar->type, ruler_info_draw_pixel,
742                                                                    ruler_info, REGION_DRAW_POST_PIXEL);
743
744         view3d_ruler_header_update(sa);
745
746         WM_cursor_modal(win, BC_CROSSCURSOR);
747         WM_event_add_modal_handler(C, op);
748
749         return OPERATOR_RUNNING_MODAL;
750 }
751
752 static int view3d_ruler_cancel(bContext *C, wmOperator *op)
753 {
754         RulerInfo *ruler_info = op->customdata;
755
756         view3d_ruler_end(C, ruler_info);
757         view3d_ruler_free(ruler_info);
758         op->customdata = NULL;
759
760         return OPERATOR_CANCELLED;
761 }
762
763 static int view3d_ruler_modal(bContext *C, wmOperator *op, const wmEvent *event)
764 {
765         bool do_draw = false;
766         int exit_code = OPERATOR_RUNNING_MODAL;
767         RulerInfo *ruler_info = op->customdata;
768         ARegion *ar = ruler_info->ar;
769         RegionView3D *rv3d = ar->regiondata;
770
771         (void)C;
772
773         switch (event->type) {
774                 case LEFTMOUSE:
775                         if (event->val == KM_RELEASE) {
776                                 if (ruler_info->state == RULER_STATE_DRAG) {
777                                         /* rubber-band angle removal */
778                                         RulerItem *ruler_item = ruler_item_active_get(ruler_info);
779                                         if (ruler_item && (ruler_item->co_index == 1) && (ruler_item->flag & RULERITEM_USE_ANGLE)) {
780                                                 if (!BLI_rcti_isect_pt_v(&ar->winrct, &event->x)) {
781                                                         ruler_item->flag &= ~RULERITEM_USE_ANGLE;
782                                                         do_draw = true;
783                                                 }
784                                         }
785                                         ruler_info->state = RULER_STATE_NORMAL;
786                                 }
787                         }
788                         else {
789                                 if (ruler_info->state == RULER_STATE_NORMAL) {
790
791                                         if (event->ctrl ||
792                                             /* weak - but user friendly */
793                                             (ruler_info->items.first == NULL))
794                                         {
795                                                 /* Create new line */
796                                                 RulerItem *ruler_item;
797                                                 /* check if we want to drag an existing point or add a new one */
798                                                 ruler_info->state = RULER_STATE_DRAG;
799
800                                                 ruler_item = ruler_item_add(ruler_info);
801                                                 ruler_item_active_set(ruler_info, ruler_item);
802
803                                                 negate_v3_v3(ruler_item->co[0], rv3d->ofs);
804                                                 view3d_ruler_item_project(ruler_info, ruler_item->co[0], event->mval);
805
806                                                 copy_v3_v3(ruler_item->co[2], ruler_item->co[0]);
807                                                 ruler_item->co_index = 2;
808
809                                                 do_draw = true;
810                                         }
811                                         else {
812                                                 float mval_fl[2] = {UNPACK2(event->mval)};
813                                                 RulerItem *ruler_item_pick;
814                                                 int co_index;
815
816                                                 /* select and drag */
817                                                 if (view3d_ruler_pick(ruler_info, mval_fl, &ruler_item_pick, &co_index)) {
818                                                         if (co_index == -1) {
819                                                                 if ((ruler_item_pick->flag & RULERITEM_USE_ANGLE) == 0) {
820                                                                         /* Add Center Point */
821                                                                         ruler_item_active_set(ruler_info, ruler_item_pick);
822                                                                         ruler_item_pick->flag |= RULERITEM_USE_ANGLE;
823                                                                         ruler_item_pick->co_index = 1;
824                                                                         ruler_info->state = RULER_STATE_DRAG;
825
826                                                                         /* find the factor */
827                                                                         {
828                                                                                 float co_ss[2][2];
829                                                                                 float fac;
830
831                                                                                 ED_view3d_project_float_global(ar, ruler_item_pick->co[0], co_ss[0], V3D_PROJ_TEST_NOP);
832                                                                                 ED_view3d_project_float_global(ar, ruler_item_pick->co[2], co_ss[1], V3D_PROJ_TEST_NOP);
833
834                                                                                 fac = line_point_factor_v2(mval_fl, co_ss[0], co_ss[1]);
835                                                                                 CLAMP(fac, 0.0f, 1.0f);
836
837                                                                                 interp_v3_v3v3(ruler_item_pick->co[1],
838                                                                                                ruler_item_pick->co[0],
839                                                                                                ruler_item_pick->co[2], fac);
840                                                                         }
841
842                                                                         /* update the new location */
843                                                                         view3d_ruler_item_mousemove(C, ruler_info, event->mval,
844                                                                                                     event->shift != 0, event->ctrl != 0);
845                                                                         do_draw = true;
846                                                                 }
847                                                         }
848                                                         else {
849                                                                 ruler_item_active_set(ruler_info, ruler_item_pick);
850                                                                 ruler_item_pick->co_index = co_index;
851                                                                 ruler_info->state = RULER_STATE_DRAG;
852                                                                 do_draw = true;
853                                                         }
854                                                 }
855                                                 else {
856                                                         exit_code = OPERATOR_PASS_THROUGH;
857                                                 }
858
859                                         }
860                                 }
861                         }
862                         break;
863                 case CKEY:
864                 {
865                         if (event->ctrl) {
866                                 RulerItem *ruler_item = ruler_item_active_get(ruler_info);
867                                 if (ruler_item) {
868                                         const int prec = 8;
869                                         char numstr[256];
870                                         Scene *scene = CTX_data_scene(C);
871                                         UnitSettings *unit = &scene->unit;
872
873                                         ruler_item_as_string(ruler_item, unit, numstr, sizeof(numstr), prec);
874                                         WM_clipboard_text_set((void *) numstr, false);
875                                 }
876                         }
877                 }
878                 case RIGHTCTRLKEY:
879                 case LEFTCTRLKEY:
880                 {
881                         WM_event_add_mousemove(C);
882                         break;
883                 }
884                 case MOUSEMOVE:
885                 {
886                         if (ruler_info->state == RULER_STATE_DRAG) {
887                                 if (view3d_ruler_item_mousemove(C, ruler_info, event->mval,
888                                                                 event->shift != 0, event->ctrl != 0))
889                                 {
890                                         do_draw = true;
891                                 }
892                         }
893                         break;
894                 }
895                 case ESCKEY:
896                 {
897                         do_draw = true;
898                         exit_code = OPERATOR_CANCELLED;
899                         break;
900                 }
901                 case RETKEY:
902                 {
903                         view3d_ruler_to_gpencil(C, ruler_info);
904                         do_draw = true;
905                         exit_code = OPERATOR_FINISHED;
906                         break;
907                 }
908                 case DELKEY:
909                 {
910                         if (event->val == KM_PRESS) {
911                                 if (ruler_info->state == RULER_STATE_NORMAL) {
912                                         RulerItem *ruler_item = ruler_item_active_get(ruler_info);
913                                         if (ruler_item) {
914                                                 ruler_item_remove(ruler_info, ruler_item);
915                                                 ruler_info->item_active = -1;
916                                                 do_draw = true;
917                                         }
918                                 }
919                         }
920                         break;
921                 }
922                 default:
923                         exit_code = OPERATOR_PASS_THROUGH;
924                         break;
925
926         }
927
928         if (do_draw) {
929                 ScrArea *sa = CTX_wm_area(C);
930
931                 view3d_ruler_header_update(sa);
932
933                 /* all 3d views draw rulers */
934                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, NULL);
935         }
936
937         if (ELEM(exit_code, OPERATOR_FINISHED, OPERATOR_CANCELLED)) {
938                 wmWindow *win = CTX_wm_window(C);
939                 ScrArea *sa = CTX_wm_area(C);
940
941                 WM_cursor_restore(win);
942
943                 view3d_ruler_end(C, ruler_info);
944                 view3d_ruler_free(ruler_info);
945                 op->customdata = NULL;
946
947                 ED_area_headerprint(sa, NULL);
948         }
949
950         return exit_code;
951 }
952
953 void VIEW3D_OT_ruler(wmOperatorType *ot)
954 {
955         /* identifiers */
956         ot->name = "Ruler/Protractor";
957         ot->description = "Interactive ruler";
958         ot->idname = "VIEW3D_OT_ruler";
959
960         /* api callbacks */
961         ot->invoke = view3d_ruler_invoke;
962         ot->cancel = view3d_ruler_cancel;
963         ot->modal = view3d_ruler_modal;
964         ot->poll = ED_operator_view3d_active;
965
966         /* flags */
967         ot->flag = 0;
968 }