Blender V5.0
transform_draw_cursors.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "GPU_immediate.hh"
10#include "GPU_matrix.hh"
11#include "GPU_state.hh"
12
13#include "BLI_math_rotation.h"
14
15#include "BKE_context.hh"
16
17#include "DNA_screen_types.h"
18#include "DNA_userdef_types.h"
19
20#include "UI_resources.hh"
21
22#include "transform.hh"
23#include "transform_draw_cursors.hh" /* Own include. */
24
25namespace blender::ed::transform {
26
33
34#define ARROW_WIDTH (2.0f * U.pixelsize)
35#define DASH_WIDTH (1.0f)
36#define DASH_LENGTH (8.0f * DASH_WIDTH * U.pixelsize)
37
38static void drawArrow(const uint pos_id, const enum eArrowDirection dir)
39{
40 int offset = 5.0f * UI_SCALE_FAC;
41 int length = (6.0f * UI_SCALE_FAC) + (4.0f * U.pixelsize);
42 int size = (3.0f * UI_SCALE_FAC) + (2.0f * U.pixelsize);
43
44 /* To line up the arrow point nicely, one end has to be extended by half its width. But
45 * being on a 45 degree angle, Pythagoras says a movement of `sqrt(2) / 2 * (line width / 2)`. */
46 float adjust = (M_SQRT2 * ARROW_WIDTH / 4.0f);
47
48 if (ELEM(dir, LEFT, DOWN)) {
49 offset = -offset;
50 length = -length;
51 size = -size;
52 adjust = -adjust;
53 }
54
56
57 if (ELEM(dir, LEFT, RIGHT)) {
58 immVertex2f(pos_id, offset, 0);
59 immVertex2f(pos_id, offset + length, 0);
60 immVertex2f(pos_id, offset + length + adjust, adjust);
61 immVertex2f(pos_id, offset + length - size, -size);
62 immVertex2f(pos_id, offset + length, 0);
63 immVertex2f(pos_id, offset + length - size, size);
64 }
65 else {
66 immVertex2f(pos_id, 0, offset);
67 immVertex2f(pos_id, 0, offset + length);
68 immVertex2f(pos_id, adjust, offset + length + adjust);
69 immVertex2f(pos_id, -size, offset + length - size);
70 immVertex2f(pos_id, 0, offset + length);
71 immVertex2f(pos_id, size, offset + length - size);
72 }
73
74 immEnd();
75}
76
78{
79 ARegion *region = CTX_wm_region(C);
80 return (region && ELEM(region->regiontype, RGN_TYPE_WINDOW, RGN_TYPE_PREVIEW)) ? true : false;
81}
82
84 const blender::int2 &xy,
85 const blender::float2 & /*tilt*/,
86 void *customdata)
87{
88 TransInfo *t = (TransInfo *)customdata;
89
90 if (t->helpline == HLP_NONE) {
91 return;
92 }
93
94 /* Offset the values for the area region. */
95 const float2 offset = {
98 };
99
100 float2 cent;
101 float2 tmval = t->mval;
102
104
105 cent += offset;
106 tmval += offset;
107
108 float viewport_size[4];
109 GPU_viewport_size_get_f(viewport_size);
110
111 Scene *scene = CTX_data_scene(C);
112 View3D *v3d = CTX_wm_view3d(C);
113
114 float fg_color[4];
115 float bg_color[4];
116 if (v3d && scene) {
117 /* Use overlay colors for 3D Viewport. */
118 ED_view3d_text_colors_get(scene, v3d, fg_color, bg_color);
119 }
120 else {
121 /* Otherwise editor foreground and background colors. */
123 UI_GetThemeColor3fv(TH_BACK, bg_color);
124 }
125 fg_color[3] = 1.0f;
126 bg_color[3] = 0.5f;
127
128 GPU_line_smooth(true);
130 const uint pos_id = GPU_vertformat_attr_add(
131 immVertexFormat(), "pos", blender::gpu::VertAttrType::SFLOAT_32_32);
132
133 /* Dashed lines first. */
137 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
138 immUniform1i("colors_len", 0); /* "simple" mode. */
139 immUniform1f("dash_width", DASH_LENGTH);
140 immUniform1f("udash_factor", 0.5f);
141
142 /* Draw in background color first. */
143 immUniformColor4fv(bg_color);
145 immVertex2fv(pos_id, cent);
146 immVertex2f(pos_id, tmval[0], tmval[1]);
147 immEnd();
148
149 /* Then foreground over top, shifted slightly. */
150 immUniformColor4fv(fg_color);
152 immVertex2f(pos_id, cent[0] - U.pixelsize, cent[1] + U.pixelsize);
153 immVertex2f(pos_id, tmval[0] - U.pixelsize, tmval[1] + U.pixelsize);
154 immEnd();
155
157 }
158
159 /* And now, solid lines. */
160
162 immUniform2fv("viewportSize", &viewport_size[2]);
163
164 /* First pass is background color and wider lines. */
165 immUniformColor4fv(bg_color);
166 immUniform1f("lineWidth", ARROW_WIDTH * 2.0f);
167
169 GPU_matrix_translate_3f(float(xy.x), float(xy.y), 0.0f);
170
171 switch (t->helpline) {
172 case HLP_SPRING:
173 GPU_matrix_rotate_axis(-RAD2DEGF(atan2f(cent[0] - tmval[0], cent[1] - tmval[1])), 'Z');
174 drawArrow(pos_id, UP);
175 drawArrow(pos_id, DOWN);
176 immUniformColor4fv(fg_color);
177 immUniform1f("lineWidth", ARROW_WIDTH);
178 drawArrow(pos_id, UP);
179 drawArrow(pos_id, DOWN);
180 break;
181 case HLP_HARROW:
182 drawArrow(pos_id, RIGHT);
183 drawArrow(pos_id, LEFT);
184 immUniform1f("lineWidth", ARROW_WIDTH);
185 immUniformColor4fv(fg_color);
186 drawArrow(pos_id, RIGHT);
187 drawArrow(pos_id, LEFT);
188 break;
189 case HLP_VARROW:
190 drawArrow(pos_id, UP);
191 drawArrow(pos_id, DOWN);
192 immUniform1f("lineWidth", ARROW_WIDTH);
193 immUniformColor4fv(fg_color);
194 drawArrow(pos_id, UP);
195 drawArrow(pos_id, DOWN);
196 break;
197 case HLP_CARROW: {
198 /* Draw arrow based on direction defined by custom-points. */
199 const int *data = static_cast<const int *>(t->mouse.data);
200 const float angle = -atan2f(data[2] - data[0], data[3] - data[1]);
202 drawArrow(pos_id, UP);
203 drawArrow(pos_id, DOWN);
204 immUniform1f("lineWidth", ARROW_WIDTH);
205 immUniformColor4fv(fg_color);
206 drawArrow(pos_id, UP);
207 drawArrow(pos_id, DOWN);
208 break;
209 }
210 case HLP_ANGLE: {
212 float angle = atan2f(tmval[1] - cent[1], tmval[0] - cent[0]);
215
216 immUniform1f("lineWidth", ARROW_WIDTH * 2.0f);
217 drawArrow(pos_id, DOWN);
218 immUniformColor4fv(fg_color);
219 immUniform1f("lineWidth", ARROW_WIDTH);
220 drawArrow(pos_id, DOWN);
221
225 immUniformColor4fv(bg_color);
226 immUniform1f("lineWidth", ARROW_WIDTH * 2.0f);
227 drawArrow(pos_id, UP);
228 immUniformColor4fv(fg_color);
229 immUniform1f("lineWidth", ARROW_WIDTH);
230 drawArrow(pos_id, UP);
231 break;
232 }
233 case HLP_TRACKBALL: {
234 immUniformColor4fv(bg_color);
235 GPU_matrix_translate_3f(U.pixelsize, -U.pixelsize, 0.0f);
236 drawArrow(pos_id, RIGHT);
237 drawArrow(pos_id, LEFT);
238 drawArrow(pos_id, UP);
239 drawArrow(pos_id, DOWN);
240 GPU_matrix_translate_3f(-U.pixelsize, U.pixelsize, 0.0f);
241
242 immUniform1f("lineWidth", ARROW_WIDTH);
243 uchar col[3], col2[3];
245 UI_make_axis_color(col, 'X', col2);
247 drawArrow(pos_id, RIGHT);
248 drawArrow(pos_id, LEFT);
249 UI_make_axis_color(col, 'Y', col2);
251 drawArrow(pos_id, UP);
252 drawArrow(pos_id, DOWN);
253 break;
254 }
255 case HLP_ERROR:
256 case HLP_ERROR_DASH:
257 case HLP_NONE:
258 break;
259 }
260
263 GPU_line_smooth(false);
265}
266
267} // namespace blender::ed::transform
Scene * CTX_data_scene(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
#define M_SQRT2
#define RAD2DEGF(_rad)
unsigned char uchar
unsigned int uint
#define ELEM(...)
@ RGN_TYPE_WINDOW
@ RGN_TYPE_PREVIEW
#define UI_SCALE_FAC
@ V3D_PROJ_TEST_CLIP_ZERO
Definition ED_view3d.hh:284
void ED_view3d_text_colors_get(const Scene *scene, const View3D *v3d, float r_text_color[4], float r_shadow_color[4])
void immEnd()
void immUniform2fv(const char *name, const float data[2])
void immUnbindProgram()
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void immUniform2f(const char *name, float x, float y)
void immVertex2f(uint attr_id, float x, float y)
void immVertex2fv(uint attr_id, const float data[2])
void immUniformColor3ubv(const unsigned char rgb[3])
void immUniform1i(const char *name, int x)
void immUniform1f(const char *name, float x)
GPUVertFormat * immVertexFormat()
void immUniformColor4fv(const float rgba[4])
void immBegin(GPUPrimType, uint vertex_len)
void GPU_matrix_rotate_axis(float deg, char axis)
void GPU_matrix_push()
void GPU_matrix_translate_3f(float x, float y, float z)
void GPU_matrix_pop()
@ GPU_PRIM_LINES
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
void GPU_line_width(float width)
Definition gpu_state.cc:166
void GPU_line_smooth(bool enable)
Definition gpu_state.cc:78
@ GPU_BLEND_NONE
Definition GPU_state.hh:85
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:87
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:42
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:273
uint GPU_vertformat_attr_add(GPUVertFormat *format, blender::StringRef name, blender::gpu::VertAttrType type)
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
#define C
Definition RandGen.cpp:29
void UI_GetThemeColor3fv(int colorid, float col[3])
@ TH_GRID
@ TH_BACK
@ TH_TEXT_HI
void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
void UI_make_axis_color(const unsigned char col[3], char axis, unsigned char r_col[3])
#define U
BMesh const char void * data
return true
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
nullptr float
uint col
float length(VecOp< float, D >) RET
static void drawArrow(const uint pos_id, const enum eArrowDirection dir)
void transform_draw_cursor_draw(bContext *C, const blender::int2 &xy, const blender::float2 &, void *customdata)
void projectFloatViewEx(TransInfo *t, const float vec[3], float adr[2], const eV3DProjTest flag)
bool transform_draw_cursor_poll(bContext *C)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
#define sinf
#define cosf
#define atan2f
int ymin
int xmin
#define DASH_WIDTH
#define ARROW_WIDTH
#define DASH_LENGTH
int xy[2]
Definition wm_draw.cc:178