Blender  V3.3
transform_draw_cursors.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
8 #include "BLI_math.h"
9 
10 #include "GPU_immediate.h"
11 #include "GPU_matrix.h"
12 #include "GPU_state.h"
13 
14 #include "BKE_context.h"
15 
16 #include "DNA_screen_types.h"
17 #include "DNA_userdef_types.h"
18 
19 #include "UI_interface.h"
20 #include "UI_resources.h"
21 
22 #include "transform.h"
23 #include "transform_draw_cursors.h" /* Own include. */
24 
26  UP,
30 };
31 
32 #define ARROW_WIDTH (2.0f * U.pixelsize)
33 #define DASH_WIDTH (1.0f)
34 #define DASH_LENGTH (8.0f * DASH_WIDTH * U.pixelsize)
35 
36 static void drawArrow(const uint pos_id, const enum eArrowDirection dir)
37 {
38  int offset = 5.0f * UI_DPI_FAC;
39  int length = (6.0f * UI_DPI_FAC) + (4.0f * U.pixelsize);
40  int size = (3.0f * UI_DPI_FAC) + (2.0f * U.pixelsize);
41 
42  /* To line up the arrow point nicely, one end has to be extended by half its width. But
43  * being on a 45 degree angle, Pythagoras says a movement of sqrt(2)/2 * (line width /2) */
44  float adjust = (M_SQRT2 * ARROW_WIDTH / 4.0f);
45 
46  if (ELEM(dir, LEFT, DOWN)) {
47  offset = -offset;
48  length = -length;
49  size = -size;
50  adjust = -adjust;
51  }
52 
54 
55  if (ELEM(dir, LEFT, RIGHT)) {
56  immVertex2f(pos_id, offset, 0);
57  immVertex2f(pos_id, offset + length, 0);
58  immVertex2f(pos_id, offset + length + adjust, adjust);
59  immVertex2f(pos_id, offset + length - size, -size);
60  immVertex2f(pos_id, offset + length, 0);
61  immVertex2f(pos_id, offset + length - size, size);
62  }
63  else {
64  immVertex2f(pos_id, 0, offset);
65  immVertex2f(pos_id, 0, offset + length);
66  immVertex2f(pos_id, adjust, offset + length + adjust);
67  immVertex2f(pos_id, -size, offset + length - size);
68  immVertex2f(pos_id, 0, offset + length);
69  immVertex2f(pos_id, size, offset + length - size);
70  }
71 
72  immEnd();
73 }
74 
76 {
77  ARegion *region = CTX_wm_region(C);
78  return (region && ELEM(region->regiontype, RGN_TYPE_WINDOW, RGN_TYPE_PREVIEW)) ? 1 : 0;
79 }
80 
81 void transform_draw_cursor_draw(bContext *UNUSED(C), int x, int y, void *customdata)
82 {
83  TransInfo *t = (TransInfo *)customdata;
84 
85  if (t->helpline == HLP_NONE) {
86  return;
87  }
88 
89  float cent[2];
90  const float mval[3] = {x, y, 0.0f};
91  float tmval[2] = {
92  (float)t->mval[0],
93  (float)t->mval[1],
94  };
95 
96  projectFloatViewEx(t, t->center_global, cent, V3D_PROJ_TEST_CLIP_ZERO);
97  /* Offset the values for the area region. */
98  const float offset[2] = {
99  t->region->winrct.xmin,
100  t->region->winrct.ymin,
101  };
102 
103  for (int i = 0; i < 2; i++) {
104  cent[i] += offset[i];
105  tmval[i] += offset[i];
106  }
107 
108  float viewport_size[4];
109  GPU_viewport_size_get_f(viewport_size);
110 
111  GPU_line_smooth(true);
113  const uint pos_id = GPU_vertformat_attr_add(
115 
116  /* Dashed lines first. */
117  if (ELEM(t->helpline, HLP_SPRING, HLP_ANGLE)) {
120  immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
121  immUniform1i("colors_len", 0); /* "simple" mode */
123  immUniform1f("dash_width", DASH_LENGTH);
124  immUniform1f("dash_factor", 0.5f);
126  immVertex2fv(pos_id, cent);
127  immVertex2f(pos_id, tmval[0], tmval[1]);
128  immEnd();
130  }
131 
132  /* And now, solid lines. */
133 
136  immUniform2fv("viewportSize", &viewport_size[2]);
137  immUniform1f("lineWidth", ARROW_WIDTH);
138 
139  GPU_matrix_push();
141 
142  switch (t->helpline) {
143  case HLP_SPRING:
144  GPU_matrix_rotate_axis(-RAD2DEGF(atan2f(cent[0] - tmval[0], cent[1] - tmval[1])), 'Z');
145  drawArrow(pos_id, UP);
146  drawArrow(pos_id, DOWN);
147  break;
148  case HLP_HARROW:
149  drawArrow(pos_id, RIGHT);
150  drawArrow(pos_id, LEFT);
151  break;
152  case HLP_VARROW:
153  drawArrow(pos_id, UP);
154  drawArrow(pos_id, DOWN);
155  break;
156  case HLP_CARROW: {
157  /* Draw arrow based on direction defined by custom-points. */
158  const int *data = t->mouse.data;
159  const float angle = -atan2f(data[2] - data[0], data[3] - data[1]);
161  drawArrow(pos_id, UP);
162  drawArrow(pos_id, DOWN);
163  break;
164  }
165  case HLP_ANGLE: {
166  GPU_matrix_push();
167  float angle = atan2f(tmval[1] - cent[1], tmval[0] - cent[0]);
170  drawArrow(pos_id, DOWN);
171  GPU_matrix_pop();
174  drawArrow(pos_id, UP);
175  break;
176  }
177  case HLP_TRACKBALL: {
178  uchar col[3], col2[3];
180  UI_make_axis_color(col, col2, 'X');
181  immUniformColor3ubv(col2);
182  drawArrow(pos_id, RIGHT);
183  drawArrow(pos_id, LEFT);
184  UI_make_axis_color(col, col2, 'Y');
185  immUniformColor3ubv(col2);
186  drawArrow(pos_id, UP);
187  drawArrow(pos_id, DOWN);
188  break;
189  }
190  case HLP_NONE:
191  break;
192  }
193 
194  GPU_matrix_pop();
196  GPU_line_smooth(false);
198 }
typedef float(TangentPoint)[2]
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
#define M_SQRT2
Definition: BLI_math_base.h:29
#define RAD2DEGF(_rad)
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNUSED(x)
#define ELEM(...)
@ RGN_TYPE_WINDOW
@ RGN_TYPE_PREVIEW
@ V3D_PROJ_TEST_CLIP_ZERO
Definition: ED_view3d.h:239
void immUniform2fv(const char *name, const float data[2])
void immUniform2f(const char *name, float x, float y)
void immUnbindProgram(void)
void immVertex2f(uint attr_id, float x, float y)
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
void immVertex2fv(uint attr_id, const float data[2])
void immUniformColor3ubv(const unsigned char rgb[3])
void immUniform1i(const char *name, int x)
void immUniformThemeColor3(int color_id)
void immUniform1f(const char *name, float x)
GPUVertFormat * immVertexFormat(void)
void immBegin(GPUPrimType, uint vertex_len)
void immEnd(void)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
void GPU_matrix_pop(void)
Definition: gpu_matrix.cc:126
void GPU_matrix_rotate_axis(float deg, char axis)
Definition: gpu_matrix.cc:274
void GPU_matrix_push(void)
Definition: gpu_matrix.cc:119
void GPU_matrix_translate_3fv(const float vec[3])
Definition: gpu_matrix.cc:204
void GPU_matrix_translate_3f(float x, float y, float z)
Definition: gpu_matrix.cc:188
@ GPU_PRIM_LINES
Definition: GPU_primitive.h:20
@ GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR
Definition: GPU_shader.h:349
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
Definition: GPU_shader.h:253
@ GPU_BLEND_NONE
Definition: GPU_state.h:60
@ GPU_BLEND_ALPHA
Definition: GPU_state.h:62
void GPU_blend(eGPUBlend blend)
Definition: gpu_state.cc:39
void GPU_line_width(float width)
Definition: gpu_state.cc:158
void GPU_line_smooth(bool enable)
Definition: gpu_state.cc:75
void GPU_viewport_size_get_f(float coords[4])
Definition: gpu_state.cc:259
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
#define C
Definition: RandGen.cpp:25
#define UI_DPI_FAC
Definition: UI_interface.h:305
void UI_make_axis_color(const unsigned char src_col[3], unsigned char dst_col[3], char axis)
Definition: resources.c:1462
@ TH_GRID
Definition: UI_resources.h:68
@ TH_VIEW_OVERLAY
Definition: UI_resources.h:327
void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
Definition: resources.c:1323
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
unsigned int U
Definition: btGjkEpa3.h:78
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
#define sinf(x)
Definition: cuda/compat.h:102
#define cosf(x)
Definition: cuda/compat.h:101
uint col
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
#define atan2f(x, y)
Definition: metal/compat.h:227
T length(const vec_base< T, Size > &a)
short regiontype
void projectFloatViewEx(TransInfo *t, const float vec[3], float adr[2], const eV3DProjTest flag)
Definition: transform.c:338
bool transform_draw_cursor_poll(bContext *C)
static void drawArrow(const uint pos_id, const enum eArrowDirection dir)
#define DASH_WIDTH
#define ARROW_WIDTH
void transform_draw_cursor_draw(bContext *UNUSED(C), int x, int y, void *customdata)
#define DASH_LENGTH