Cleanup: replace attrib w/ attr
[blender.git] / source / blender / draw / modes / edit_text_mode.c
index 7d6aa5b560cfa5a3162321d9d2321548d4151a07..ca533a00737272dd54b3e8b12d63aa01ae2f4651 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016, Blender Foundation.
+ * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
+ * Copyright 2016, Blender Foundation.
  * Contributor(s): Blender Institute
  *
+ * ***** END GPL LICENSE BLOCK *****
+ *
  */
 
 /** \file blender/draw/modes/edit_text_mode.c
 
 #include "BIF_glutil.h"
 
+#include "BKE_font.h"
+
 /* If builtin shaders are needed */
 #include "GPU_shader.h"
-#include "GPU_batch.h"
 
 #include "draw_common.h"
 
-#include "draw_mode_engines.h"
-
-/* If needed, contains all global/Theme colors
- * Add needed theme colors / values to DRW_globals_update() and update UBO
- * Not needed for constant color. */
-extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
-extern struct GlobalsUboStorage ts; /* draw_common.c */
-
 /* *********** LISTS *********** */
 /* All lists are per viewport specific datas.
  * They are all free when viewport changes engines
@@ -188,11 +184,85 @@ static void EDIT_TEXT_cache_init(void *vedata)
                psl->text_box_pass = DRW_pass_create(
                        "Font Text Boxes",
                        DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH);
-               stl->g_data->box_shgrp = shgroup_dynlines_dashed_uniform_color(psl->text_box_pass, ts.colorWire);
-               stl->g_data->box_active_shgrp = shgroup_dynlines_dashed_uniform_color(psl->text_box_pass, ts.colorActive);
+               stl->g_data->box_shgrp = shgroup_dynlines_dashed_uniform_color(psl->text_box_pass, G_draw.block.colorWire);
+               stl->g_data->box_active_shgrp = shgroup_dynlines_dashed_uniform_color(psl->text_box_pass, G_draw.block.colorActive);
+       }
+}
+
+/* Use 2D quad corners to create a matrix that set
+ * a [-1..1] quad at the right position. */
+static void v2_quad_corners_to_mat4(float corners[4][2], float r_mat[4][4])
+{
+       unit_m4(r_mat);
+       sub_v2_v2v2(r_mat[0], corners[1], corners[0]);
+       sub_v2_v2v2(r_mat[1], corners[3], corners[0]);
+       mul_v2_fl(r_mat[0], 0.5f);
+       mul_v2_fl(r_mat[1], 0.5f);
+       copy_v2_v2(r_mat[3], corners[0]);
+       add_v2_v2(r_mat[3], r_mat[0]);
+       add_v2_v2(r_mat[3], r_mat[1]);
+}
+
+static void edit_text_cache_populate_select(void *vedata, Object *ob)
+{
+       EDIT_TEXT_StorageList *stl = ((EDIT_TEXT_Data *)vedata)->stl;
+       const Curve *cu = ob->data;
+       EditFont *ef = cu->editfont;
+       float final_mat[4][4], box[4][2];
+       struct GPUBatch *geom = DRW_cache_quad_get();
+
+       for (int i = 0; i < ef->selboxes_len; i++) {
+               EditFontSelBox *sb = &ef->selboxes[i];
+
+               float selboxw;
+               if (i + 1 != ef->selboxes_len) {
+                       if (ef->selboxes[i + 1].y == sb->y) {
+                               selboxw = ef->selboxes[i + 1].x - sb->x;
+                       }
+                       else {
+                               selboxw = sb->w;
+                       }
+               }
+               else {
+                       selboxw = sb->w;
+               }
+               /* NOTE: v2_quad_corners_to_mat4 don't need the 3rd corner. */
+               if (sb->rot == 0.0f) {
+                       copy_v2_fl2(box[0], sb->x, sb->y);
+                       copy_v2_fl2(box[1], sb->x + selboxw, sb->y);
+                       copy_v2_fl2(box[3], sb->x, sb->y + sb->h);
+               }
+               else {
+                       float mat[2][2];
+                       angle_to_mat2(mat, sb->rot);
+                       copy_v2_fl2(box[0], sb->x, sb->y);
+                       mul_v2_v2fl(box[1], mat[0], selboxw);
+                       add_v2_v2(box[1], &sb->x);
+                       mul_v2_v2fl(box[3], mat[1], sb->h);
+                       add_v2_v2(box[3], &sb->x);
+               }
+               v2_quad_corners_to_mat4(box, final_mat);
+               mul_m4_m4m4(final_mat, ob->obmat, final_mat);
+
+               DRW_shgroup_call_add(stl->g_data->overlay_select_shgrp, geom, final_mat);
        }
 }
 
+static void edit_text_cache_populate_cursor(void *vedata, Object *ob)
+{
+       EDIT_TEXT_StorageList *stl = ((EDIT_TEXT_Data *)vedata)->stl;
+       const Curve *cu = ob->data;
+       EditFont *edit_font = cu->editfont;
+       float (*cursor)[2] = edit_font->textcurs;
+       float mat[4][4];
+
+       v2_quad_corners_to_mat4(cursor, mat);
+       mul_m4_m4m4(mat, ob->obmat, mat);
+
+       struct GPUBatch *geom = DRW_cache_quad_get();
+       DRW_shgroup_call_add(stl->g_data->overlay_cursor_shgrp, geom, mat);
+}
+
 static void edit_text_cache_populate_boxes(void *vedata, Object *ob)
 {
        EDIT_TEXT_StorageList *stl = ((EDIT_TEXT_Data *)vedata)->stl;
@@ -263,7 +333,8 @@ static void EDIT_TEXT_cache_populate(void *vedata, Object *ob)
                        /* Get geometry cache */
                        struct GPUBatch *geom;
 
-                       if (cu->flag & CU_FAST) {
+                       bool has_surface = (cu->flag & (CU_FRONT | CU_BACK)) || cu->ext1 != 0.0f || cu->ext2 != 0.0f;
+                       if ((cu->flag & CU_FAST) || !has_surface) {
                                geom = DRW_cache_text_edge_wire_get(ob);
                                if (geom) {
                                        DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
@@ -273,16 +344,8 @@ static void EDIT_TEXT_cache_populate(void *vedata, Object *ob)
                                /* object mode draws */
                        }
 
-                       geom = DRW_cache_text_select_overlay_get(ob);
-                       if (geom) {
-                               DRW_shgroup_call_add(stl->g_data->overlay_select_shgrp, geom, ob->obmat);
-                       }
-
-                       geom = DRW_cache_text_cursor_overlay_get(ob);
-                       if (geom) {
-                               DRW_shgroup_call_add(stl->g_data->overlay_cursor_shgrp, geom, ob->obmat);
-                       }
-
+                       edit_text_cache_populate_select(vedata, ob);
+                       edit_text_cache_populate_cursor(vedata, ob);
                        edit_text_cache_populate_boxes(vedata, ob);
                }
        }