Blender  V2.93
transform_draw_cursors.c
Go to the documentation of this file.
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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include "BLI_math.h"
25 
26 #include "GPU_immediate.h"
27 #include "GPU_matrix.h"
28 #include "GPU_state.h"
29 
30 #include "BKE_context.h"
31 
32 #include "DNA_screen_types.h"
33 #include "DNA_userdef_types.h"
34 
35 #include "UI_interface.h"
36 #include "UI_resources.h"
37 
38 #include "transform.h"
39 #include "transform_draw_cursors.h" /* Own include. */
40 
42  UP,
46 };
47 
48 #define ARROW_WIDTH (2.0f * U.pixelsize)
49 #define DASH_WIDTH (1.0f)
50 #define DASH_LENGTH (8.0f * DASH_WIDTH * U.pixelsize)
51 
52 static void drawArrow(const uint pos_id, const enum eArrowDirection dir)
53 {
54  int offset = 5.0f * UI_DPI_FAC;
55  int length = (6.0f * UI_DPI_FAC) + (4.0f * U.pixelsize);
56  int size = (3.0f * UI_DPI_FAC) + (2.0f * U.pixelsize);
57 
58  /* To line up the arrow point nicely, one end has to be extended by half its width. But
59  * being on a 45 degree angle, Pythagoras says a movement of sqrt(2)/2 * (line width /2) */
60  float adjust = (M_SQRT2 * ARROW_WIDTH / 4.0f);
61 
62  if (ELEM(dir, LEFT, DOWN)) {
63  offset = -offset;
64  length = -length;
65  size = -size;
66  adjust = -adjust;
67  }
68 
70 
71  if (ELEM(dir, LEFT, RIGHT)) {
72  immVertex2f(pos_id, offset, 0);
73  immVertex2f(pos_id, offset + length, 0);
74  immVertex2f(pos_id, offset + length + adjust, adjust);
75  immVertex2f(pos_id, offset + length - size, -size);
76  immVertex2f(pos_id, offset + length, 0);
77  immVertex2f(pos_id, offset + length - size, size);
78  }
79  else {
80  immVertex2f(pos_id, 0, offset);
81  immVertex2f(pos_id, 0, offset + length);
82  immVertex2f(pos_id, adjust, offset + length + adjust);
83  immVertex2f(pos_id, -size, offset + length - size);
84  immVertex2f(pos_id, 0, offset + length);
85  immVertex2f(pos_id, size, offset + length - size);
86  }
87 
88  immEnd();
89 }
90 
96 {
97  ARegion *region = CTX_wm_region(C);
98  return (region && region->regiontype == RGN_TYPE_WINDOW) ? 1 : 0;
99 }
100 
105 void transform_draw_cursor_draw(bContext *UNUSED(C), int x, int y, void *customdata)
106 {
107  TransInfo *t = (TransInfo *)customdata;
108 
109  if (t->helpline == HLP_NONE) {
110  return;
111  }
112 
113  float cent[2];
114  const float mval[3] = {x, y, 0.0f};
115  float tmval[2] = {
116  (float)t->mval[0],
117  (float)t->mval[1],
118  };
119 
120  projectFloatViewEx(t, t->center_global, cent, V3D_PROJ_TEST_CLIP_ZERO);
121  /* Offset the values for the area region. */
122  const float offset[2] = {
123  t->region->winrct.xmin,
124  t->region->winrct.ymin,
125  };
126 
127  for (int i = 0; i < 2; i++) {
128  cent[i] += offset[i];
129  tmval[i] += offset[i];
130  }
131 
132  float viewport_size[4];
133  GPU_viewport_size_get_f(viewport_size);
134 
135  GPU_line_smooth(true);
137  const uint pos_id = GPU_vertformat_attr_add(
139 
140  /* Dashed lines first. */
141  if (ELEM(t->helpline, HLP_SPRING, HLP_ANGLE)) {
144  immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
145  immUniform1i("colors_len", 0); /* "simple" mode */
147  immUniform1f("dash_width", DASH_LENGTH);
148  immUniform1f("dash_factor", 0.5f);
150  immVertex2fv(pos_id, cent);
151  immVertex2f(pos_id, tmval[0], tmval[1]);
152  immEnd();
154  }
155 
156  /* And now, solid lines. */
157 
160  immUniform2fv("viewportSize", &viewport_size[2]);
161  immUniform1f("lineWidth", ARROW_WIDTH);
162 
163  GPU_matrix_push();
165 
166  switch (t->helpline) {
167  case HLP_SPRING:
168  GPU_matrix_rotate_axis(-RAD2DEGF(atan2f(cent[0] - tmval[0], cent[1] - tmval[1])), 'Z');
169  drawArrow(pos_id, UP);
170  drawArrow(pos_id, DOWN);
171  break;
172  case HLP_HARROW:
173  drawArrow(pos_id, RIGHT);
174  drawArrow(pos_id, LEFT);
175  break;
176  case HLP_VARROW:
177  drawArrow(pos_id, UP);
178  drawArrow(pos_id, DOWN);
179  break;
180  case HLP_CARROW: {
181  /* Draw arrow based on direction defined by custom-points. */
182  const int *data = t->mouse.data;
183  const float angle = -atan2f(data[2] - data[0], data[3] - data[1]);
185  drawArrow(pos_id, UP);
186  drawArrow(pos_id, DOWN);
187  break;
188  }
189  case HLP_ANGLE: {
190  GPU_matrix_push();
191  float angle = atan2f(tmval[1] - cent[1], tmval[0] - cent[0]);
194  drawArrow(pos_id, DOWN);
195  GPU_matrix_pop();
198  drawArrow(pos_id, UP);
199  break;
200  }
201  case HLP_TRACKBALL: {
202  uchar col[3], col2[3];
204  UI_make_axis_color(col, col2, 'X');
205  immUniformColor3ubv(col2);
206  drawArrow(pos_id, RIGHT);
207  drawArrow(pos_id, LEFT);
208  UI_make_axis_color(col, col2, 'Y');
209  immUniformColor3ubv(col2);
210  drawArrow(pos_id, UP);
211  drawArrow(pos_id, DOWN);
212  break;
213  }
214  case HLP_NONE:
215  break;
216  }
217 
218  GPU_matrix_pop();
220  GPU_line_smooth(false);
222 }
typedef float(TangentPoint)[2]
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
#define M_SQRT2
Definition: BLI_math_base.h:47
#define RAD2DEGF(_rad)
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define ELEM(...)
@ RGN_TYPE_WINDOW
@ V3D_PROJ_TEST_CLIP_ZERO
Definition: ED_view3d.h:198
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:142
void GPU_matrix_rotate_axis(float deg, char axis)
Definition: gpu_matrix.cc:290
void GPU_matrix_push(void)
Definition: gpu_matrix.cc:135
void GPU_matrix_translate_3fv(const float vec[3])
Definition: gpu_matrix.cc:220
void GPU_matrix_translate_3f(float x, float y, float z)
Definition: gpu_matrix.cc:204
@ GPU_PRIM_LINES
Definition: GPU_primitive.h:36
@ GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR
Definition: GPU_shader.h:365
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
Definition: GPU_shader.h:223
@ GPU_BLEND_NONE
Definition: GPU_state.h:55
@ GPU_BLEND_ALPHA
Definition: GPU_state.h:57
void GPU_blend(eGPUBlend blend)
Definition: gpu_state.cc:55
void GPU_line_width(float width)
Definition: gpu_state.cc:173
void GPU_line_smooth(bool enable)
Definition: gpu_state.cc:85
void GPU_viewport_size_get_f(float coords[4])
Definition: gpu_state.cc:279
@ 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:39
#define UI_DPI_FAC
Definition: UI_interface.h:309
void UI_make_axis_color(const unsigned char *src_col, unsigned char *dst_col, const char axis)
@ TH_GRID
Definition: UI_resources.h:84
@ TH_VIEW_OVERLAY
Definition: UI_resources.h:343
void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
Definition: resources.c:1350
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
unsigned int U
Definition: btGjkEpa3.h:78
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
uint col
#define sinf(x)
#define cosf(x)
#define atan2f(x, y)
short regiontype
void projectFloatViewEx(TransInfo *t, const float vec[3], float adr[2], const eV3DProjTest flag)
Definition: transform.c:348
@ HLP_HARROW
Definition: transform.h:208
@ HLP_ANGLE
Definition: transform.h:207
@ HLP_SPRING
Definition: transform.h:206
@ HLP_CARROW
Definition: transform.h:210
@ HLP_TRACKBALL
Definition: transform.h:211
@ HLP_NONE
Definition: transform.h:205
@ HLP_VARROW
Definition: transform.h:209
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