Cleanup: replace attrib w/ attr
[blender.git] / source / blender / draw / modes / edit_mesh_mode_text.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  * Copyright 2016, Blender Foundation.
19  * Contributor(s): Blender Institute
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  *
23  */
24
25 /** \file blender/draw/modes/edit_mesh_mode_text.c
26  *  \ingroup draw
27  */
28
29 #include "BLI_math.h"
30 #include "BLI_string.h"
31
32 #include "BKE_editmesh.h"
33 #include "BKE_global.h"
34 #include "BKE_unit.h"
35
36 #include "ED_view3d.h"
37
38 #include "DNA_mesh_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_view3d_types.h"
43
44 #include "UI_resources.h"
45
46 #include "draw_manager_text.h"
47
48 #include "edit_mesh_mode_intern.h" /* own include */
49
50 /* Copied from drawobject.c */
51 void DRW_edit_mesh_mode_text_measure_stats(
52         ARegion *ar, View3D *v3d,
53         Object *ob, const UnitSettings *unit)
54 {
55         /* Do not use ascii when using non-default unit system, some unit chars are utf8 (micro, square, etc.).
56          * See bug #36090.
57          */
58         struct DRWTextStore *dt = DRW_text_cache_ensure();
59         const short txt_flag = DRW_TEXT_CACHE_GLOBALSPACE | (unit->system ? 0 : DRW_TEXT_CACHE_ASCII);
60         Mesh *me = ob->data;
61         BMEditMesh *em = me->edit_btmesh;
62         float v1[3], v2[3], v3[3], vmid[3], fvec[3];
63         char numstr[32]; /* Stores the measurement display text here */
64         size_t numstr_len;
65         const char *conv_float; /* Use a float conversion matching the grid size */
66         uchar col[4] = {0, 0, 0, 255}; /* color of the text to draw */
67         float area; /* area of the face */
68         float grid = unit->system ? unit->scale_length : v3d->grid;
69         const bool do_global = (v3d->flag & V3D_GLOBAL_STATS) != 0;
70         const bool do_moving = (G.moving & G_TRANSFORM_EDIT) != 0;
71         /* when 2 edge-info options are enabled, space apart */
72         const bool do_edge_textpair = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGE_LEN) && (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGE_ANG);
73         const short edge_texpair_sep = (short)(5.0f * U.ui_scale);
74         float clip_planes[4][4];
75         /* allow for displaying shape keys and deform mods */
76         BMIter iter;
77
78         /* make the precision of the display value proportionate to the gridsize */
79
80         if (grid <= 0.01f) conv_float = "%.6g";
81         else if (grid <= 0.1f) conv_float = "%.5g";
82         else if (grid <= 1.0f) conv_float = "%.4g";
83         else if (grid <= 10.0f) conv_float = "%.3g";
84         else conv_float = "%.2g";
85
86         if (v3d->overlay.edit_flag & (V3D_OVERLAY_EDIT_EDGE_LEN | V3D_OVERLAY_EDIT_EDGE_ANG | V3D_OVERLAY_EDIT_INDICES)) {
87                 BoundBox bb;
88                 const rcti rect = {0, ar->winx, 0, ar->winy};
89
90                 ED_view3d_clipping_calc(&bb, clip_planes, ar, em->ob, &rect);
91         }
92
93         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGE_LEN) {
94                 BMEdge *eed;
95
96                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_EDGELEN, col);
97
98                 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
99                         /* draw selected edges, or edges next to selected verts while dragging */
100                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT) ||
101                             (do_moving && (BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) ||
102                                            BM_elem_flag_test(eed->v2, BM_ELEM_SELECT))))
103                         {
104                                 float v1_clip[3], v2_clip[3];
105
106                                 copy_v3_v3(v1, eed->v1->co);
107                                 copy_v3_v3(v2, eed->v2->co);
108
109                                 if (clip_segment_v3_plane_n(v1, v2, clip_planes, 4, v1_clip, v2_clip)) {
110
111                                         mid_v3_v3v3(vmid, v1_clip, v2_clip);
112                                         mul_m4_v3(ob->obmat, vmid);
113
114                                         if (do_global) {
115                                                 mul_mat3_m4_v3(ob->obmat, v1);
116                                                 mul_mat3_m4_v3(ob->obmat, v2);
117                                         }
118
119                                         if (unit->system) {
120                                                 numstr_len = bUnit_AsString2(
121                                                         numstr, sizeof(numstr), len_v3v3(v1, v2) * unit->scale_length, 3,
122                                                         B_UNIT_LENGTH, unit, false);
123                                         }
124                                         else {
125                                                 numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), conv_float, len_v3v3(v1, v2));
126                                         }
127
128                                         DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0,
129                                                            (do_edge_textpair) ? edge_texpair_sep : 0,
130                                                            txt_flag, col);
131                                 }
132                         }
133                 }
134         }
135
136         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGE_ANG) {
137                 const bool is_rad = (unit->system_rotation == USER_UNIT_ROT_RADIANS);
138                 BMEdge *eed;
139
140                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_EDGEANG, col);
141
142                 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
143                         BMLoop *l_a, *l_b;
144                         if (BM_edge_loop_pair(eed, &l_a, &l_b)) {
145                                 /* draw selected edges, or edges next to selected verts while dragging */
146                                 if (BM_elem_flag_test(eed, BM_ELEM_SELECT) ||
147                                     (do_moving &&
148                                      (BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) ||
149                                       BM_elem_flag_test(eed->v2, BM_ELEM_SELECT) ||
150                                       /* special case, this is useful to show when verts connected to
151                                        * this edge via a face are being transformed */
152                                       BM_elem_flag_test(l_a->next->next->v, BM_ELEM_SELECT) ||
153                                       BM_elem_flag_test(l_a->prev->v, BM_ELEM_SELECT)       ||
154                                       BM_elem_flag_test(l_b->next->next->v, BM_ELEM_SELECT) ||
155                                       BM_elem_flag_test(l_b->prev->v, BM_ELEM_SELECT)
156                                       )))
157                                 {
158                                         float v1_clip[3], v2_clip[3];
159
160                                         copy_v3_v3(v1, eed->v1->co);
161                                         copy_v3_v3(v2, eed->v2->co);
162
163                                         if (clip_segment_v3_plane_n(v1, v2, clip_planes, 4, v1_clip, v2_clip)) {
164                                                 float no_a[3], no_b[3];
165                                                 float angle;
166
167                                                 mid_v3_v3v3(vmid, v1_clip, v2_clip);
168                                                 mul_m4_v3(ob->obmat, vmid);
169
170                                                 copy_v3_v3(no_a, l_a->f->no);
171                                                 copy_v3_v3(no_b, l_b->f->no);
172
173                                                 if (do_global) {
174                                                         mul_mat3_m4_v3(ob->imat, no_a);
175                                                         mul_mat3_m4_v3(ob->imat, no_b);
176                                                         normalize_v3(no_a);
177                                                         normalize_v3(no_b);
178                                                 }
179
180                                                 angle = angle_normalized_v3v3(no_a, no_b);
181
182                                                 numstr_len = BLI_snprintf_rlen(
183                                                         numstr, sizeof(numstr), "%.3f%s", (is_rad) ? angle : RAD2DEGF(angle),
184                                                                                           (is_rad) ? "r" : "°");
185
186                                                 DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0,
187                                                                    (do_edge_textpair) ? -edge_texpair_sep : 0,
188                                                                    txt_flag, col);
189                                         }
190                                 }
191                         }
192                 }
193         }
194
195         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_AREA) {
196                 /* would be nice to use BM_face_calc_area, but that is for 2d faces
197                  * so instead add up tessellation triangle areas */
198
199                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEAREA, col);
200
201                 int i, n, numtri;
202                 BMFace *f = NULL;
203                 BM_ITER_MESH_INDEX(f, &iter, em->bm, BM_FACES_OF_MESH, i) {
204                         if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
205                                 n = 0;
206                                 numtri = f->len - 2;
207                                 area = 0;
208                                 zero_v3(vmid);
209                                 BMLoop *(*l)[3] = &em->looptris[poly_to_tri_count(i, BM_elem_index_get(f->l_first))];
210                                 for (int j = 0; j < numtri; j++) {
211                                         copy_v3_v3(v1, l[j][0]->v->co);
212                                         copy_v3_v3(v2, l[j][1]->v->co);
213                                         copy_v3_v3(v3, l[j][2]->v->co);
214
215                                         add_v3_v3(vmid, v1);
216                                         add_v3_v3(vmid, v2);
217                                         add_v3_v3(vmid, v3);
218                                         n += 3;
219
220                                         if (do_global) {
221                                                 mul_mat3_m4_v3(ob->obmat, v1);
222                                                 mul_mat3_m4_v3(ob->obmat, v2);
223                                                 mul_mat3_m4_v3(ob->obmat, v3);
224                                         }
225
226                                         area += area_tri_v3(v1, v2, v3);
227                                 }
228
229                                 mul_v3_fl(vmid, 1.0f / (float)n);
230                                 mul_m4_v3(ob->obmat, vmid);
231
232                                 if (unit->system) {
233                                         numstr_len = bUnit_AsString2(
234                                                 numstr, sizeof(numstr),
235                                                 (double)(area * unit->scale_length * unit->scale_length),
236                                                 3, B_UNIT_AREA, unit, false);
237                                 }
238                                 else {
239                                         numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), conv_float, area);
240                                 }
241
242                                 DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0, 0, txt_flag, col);
243                         }
244                 }
245         }
246
247         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_ANG) {
248                 BMFace *efa;
249                 const bool is_rad = (unit->system_rotation == USER_UNIT_ROT_RADIANS);
250
251                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEANG, col);
252
253                 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
254                         const bool is_face_sel = BM_elem_flag_test_bool(efa, BM_ELEM_SELECT);
255
256                         if (is_face_sel || do_moving) {
257                                 BMIter liter;
258                                 BMLoop *loop;
259                                 bool is_first = true;
260
261                                 BM_ITER_ELEM (loop, &liter, efa, BM_LOOPS_OF_FACE) {
262                                         if (is_face_sel ||
263                                             (do_moving &&
264                                              (BM_elem_flag_test(loop->v, BM_ELEM_SELECT) ||
265                                               BM_elem_flag_test(loop->prev->v, BM_ELEM_SELECT) ||
266                                               BM_elem_flag_test(loop->next->v, BM_ELEM_SELECT))))
267                                         {
268                                                 float v2_local[3];
269
270                                                 /* lazy init center calc */
271                                                 if (is_first) {
272                                                         BM_face_calc_center_bounds(efa, vmid);
273                                                         is_first = false;
274                                                 }
275                                                 copy_v3_v3(v1, loop->prev->v->co);
276                                                 copy_v3_v3(v2, loop->v->co);
277                                                 copy_v3_v3(v3, loop->next->v->co);
278
279                                                 copy_v3_v3(v2_local, v2);
280
281                                                 if (do_global) {
282                                                         mul_mat3_m4_v3(ob->obmat, v1);
283                                                         mul_mat3_m4_v3(ob->obmat, v2);
284                                                         mul_mat3_m4_v3(ob->obmat, v3);
285                                                 }
286
287                                                 float angle = angle_v3v3v3(v1, v2, v3);
288
289                                                 numstr_len = BLI_snprintf_rlen(
290                                                         numstr, sizeof(numstr), "%.3f%s", (is_rad) ? angle : RAD2DEGF(angle),
291                                                                                           (is_rad) ? "r" : "°");
292                                                 interp_v3_v3v3(fvec, vmid, v2_local, 0.8f);
293                                                 mul_m4_v3(ob->obmat, fvec);
294                                                 DRW_text_cache_add(dt, fvec, numstr, numstr_len, 0, 0, txt_flag, col);
295                                         }
296                                 }
297                         }
298                 }
299         }
300
301         /* This option is for mesh ops and addons debugging; only available in UI if Blender starts with --debug */
302         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_INDICES) {
303                 int i;
304
305                 /* For now, reuse an appropriate theme color */
306                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEANG, col);
307
308                 if (em->selectmode & SCE_SELECT_VERTEX) {
309                         BMVert *v;
310
311                         BM_ITER_MESH_INDEX(v, &iter, em->bm, BM_VERTS_OF_MESH, i) {
312                                 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
313                                         float vec[3];
314                                         mul_v3_m4v3(vec, ob->obmat, v->co);
315
316                                         numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), "%d", i);
317                                         DRW_text_cache_add(dt, vec, numstr, numstr_len, 0, 0, txt_flag, col);
318                                 }
319                         }
320                 }
321
322                 if (em->selectmode & SCE_SELECT_EDGE) {
323                         BMEdge *e;
324
325                         BM_ITER_MESH_INDEX(e, &iter, em->bm, BM_EDGES_OF_MESH, i) {
326                                 if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
327                                         float v1_clip[3], v2_clip[3];
328
329                                         copy_v3_v3(v1, e->v1->co);
330                                         copy_v3_v3(v2, e->v2->co);
331
332                                         if (clip_segment_v3_plane_n(v1, v2, clip_planes, 4, v1_clip, v2_clip)) {
333                                                 mid_v3_v3v3(vmid, v1_clip, v2_clip);
334                                                 mul_m4_v3(ob->obmat, vmid);
335
336                                                 numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), "%d", i);
337                                                 DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0, 0, txt_flag, col);
338                                         }
339                                 }
340                         }
341                 }
342
343                 if (em->selectmode & SCE_SELECT_FACE) {
344                         BMFace *f;
345
346                         BM_ITER_MESH_INDEX(f, &iter, em->bm, BM_FACES_OF_MESH, i) {
347                                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
348                                         BM_face_calc_center_median(f, v1);
349                                         mul_m4_v3(ob->obmat, v1);
350
351                                         numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), "%d", i);
352                                         DRW_text_cache_add(dt, v1, numstr, numstr_len, 0, 0, txt_flag, col);
353                                 }
354                         }
355                 }
356         }
357 }