Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / editors / gizmo_library / gizmo_types / primitive3d_gizmo.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file \ingroup edgizmolib
18  *
19  * \name Primitive Gizmo
20  *
21  * 3D Gizmo
22  *
23  * \brief Gizmo with primitive drawing type (plane, cube, etc.).
24  * Currently only plane primitive supported without own handling, use with operator only.
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_math.h"
30
31 #include "DNA_view3d_types.h"
32
33 #include "BKE_context.h"
34
35
36 #include "GPU_immediate.h"
37 #include "GPU_matrix.h"
38 #include "GPU_select.h"
39 #include "GPU_state.h"
40
41 #include "RNA_access.h"
42 #include "RNA_define.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "ED_gizmo_library.h"
48
49 /* own includes */
50 #include "../gizmo_library_intern.h"
51
52 static float verts_plane[4][3] = {
53         {-1, -1, 0},
54         { 1, -1, 0},
55         { 1,  1, 0},
56         {-1,  1, 0},
57 };
58
59
60 /* -------------------------------------------------------------------- */
61
62 static void gizmo_primitive_draw_geom(
63         const float col_inner[4], const float col_outer[4], const int draw_style)
64 {
65         float (*verts)[3];
66         uint vert_count = 0;
67
68         if (draw_style == ED_GIZMO_PRIMITIVE_STYLE_PLANE) {
69                 verts = verts_plane;
70                 vert_count = ARRAY_SIZE(verts_plane);
71         }
72
73         if (vert_count > 0) {
74                 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
75                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
76                 wm_gizmo_vec_draw(col_inner, verts, vert_count, pos, GPU_PRIM_TRI_FAN);
77                 wm_gizmo_vec_draw(col_outer, verts, vert_count, pos, GPU_PRIM_LINE_LOOP);
78                 immUnbindProgram();
79         }
80 }
81
82 static void gizmo_primitive_draw_intern(
83         wmGizmo *gz, const bool UNUSED(select),
84         const bool highlight)
85 {
86         float color_inner[4], color_outer[4];
87         float matrix_final[4][4];
88         const int draw_style = RNA_enum_get(gz->ptr, "draw_style");
89
90         gizmo_color_get(gz, highlight, color_outer);
91         copy_v4_v4(color_inner, color_outer);
92         color_inner[3] *= 0.5f;
93
94         WM_gizmo_calc_matrix_final(gz, matrix_final);
95
96         GPU_matrix_push();
97         GPU_matrix_mul(matrix_final);
98
99         GPU_blend(true);
100         gizmo_primitive_draw_geom(color_inner, color_outer, draw_style);
101         GPU_blend(false);
102
103         GPU_matrix_pop();
104
105         if (gz->interaction_data) {
106                 GizmoInteraction *inter = gz->interaction_data;
107
108                 copy_v4_fl(color_inner, 0.5f);
109                 copy_v3_fl(color_outer, 0.5f);
110                 color_outer[3] = 0.8f;
111
112                 GPU_matrix_push();
113                 GPU_matrix_mul(inter->init_matrix_final);
114
115                 GPU_blend(true);
116                 gizmo_primitive_draw_geom(color_inner, color_outer, draw_style);
117                 GPU_blend(false);
118
119                 GPU_matrix_pop();
120         }
121 }
122
123 static void gizmo_primitive_draw_select(
124         const bContext *UNUSED(C), wmGizmo *gz,
125         int select_id)
126 {
127         GPU_select_load_id(select_id);
128         gizmo_primitive_draw_intern(gz, true, false);
129 }
130
131 static void gizmo_primitive_draw(const bContext *UNUSED(C), wmGizmo *gz)
132 {
133         gizmo_primitive_draw_intern(
134                 gz, false,
135                 (gz->state & WM_GIZMO_STATE_HIGHLIGHT));
136 }
137
138 static void gizmo_primitive_setup(wmGizmo *gz)
139 {
140         gz->flag |= WM_GIZMO_DRAW_MODAL;
141 }
142
143 static int gizmo_primitive_invoke(
144         bContext *UNUSED(C), wmGizmo *gz, const wmEvent *UNUSED(event))
145 {
146         GizmoInteraction *inter = MEM_callocN(sizeof(GizmoInteraction), __func__);
147
148         WM_gizmo_calc_matrix_final(gz, inter->init_matrix_final);
149
150         gz->interaction_data = inter;
151
152         return OPERATOR_RUNNING_MODAL;
153 }
154
155 /* -------------------------------------------------------------------- */
156 /** \name Primitive Gizmo API
157  *
158  * \{ */
159
160 static void GIZMO_GT_primitive_3d(wmGizmoType *gzt)
161 {
162         /* identifiers */
163         gzt->idname = "GIZMO_GT_primitive_3d";
164
165         /* api callbacks */
166         gzt->draw = gizmo_primitive_draw;
167         gzt->draw_select = gizmo_primitive_draw_select;
168         gzt->setup = gizmo_primitive_setup;
169         gzt->invoke = gizmo_primitive_invoke;
170
171         gzt->struct_size = sizeof(wmGizmo);
172
173         static EnumPropertyItem rna_enum_draw_style[] = {
174                 {ED_GIZMO_PRIMITIVE_STYLE_PLANE, "PLANE", 0, "Plane", ""},
175                 {0, NULL, 0, NULL, NULL},
176         };
177         RNA_def_enum(gzt->srna, "draw_style", rna_enum_draw_style, ED_GIZMO_PRIMITIVE_STYLE_PLANE, "Draw Style", "");
178 }
179
180 void ED_gizmotypes_primitive_3d(void)
181 {
182         WM_gizmotype_append(GIZMO_GT_primitive_3d);
183 }
184
185 /** \} */