Blender  V2.93
transform_mode_timeslide.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 <stdlib.h>
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "DNA_anim_types.h"
29 
30 #include "BLI_math.h"
31 #include "BLI_string.h"
32 
33 #include "BKE_context.h"
34 #include "BKE_nla.h"
35 #include "BKE_unit.h"
36 
37 #include "ED_screen.h"
38 
39 #include "UI_interface.h"
40 #include "UI_view2d.h"
41 
42 #include "BLT_translation.h"
43 
44 #include "transform.h"
45 #include "transform_mode.h"
46 
47 /* -------------------------------------------------------------------- */
51 static void headerTimeSlide(TransInfo *t, const float sval, char str[UI_MAX_DRAW_STR])
52 {
53  char tvec[NUM_STR_REP_LEN * 3];
54 
55  if (hasNumInput(&t->num)) {
56  outputNumInput(&(t->num), tvec, &t->scene->unit);
57  }
58  else {
59  const float *range = t->custom.mode.data;
60  float minx = range[0];
61  float maxx = range[1];
62  float cval = t->values_final[0];
63  float val;
64 
65  val = 2.0f * (cval - sval) / (maxx - minx);
66  CLAMP(val, -1.0f, 1.0f);
67 
68  BLI_snprintf(&tvec[0], NUM_STR_REP_LEN, "%.4f", val);
69  }
70 
71  BLI_snprintf(str, UI_MAX_DRAW_STR, TIP_("TimeSlide: %s"), &tvec[0]);
72 }
73 
74 static void applyTimeSlideValue(TransInfo *t, float sval, float cval)
75 {
76  int i;
77  const float *range = t->custom.mode.data;
78  float minx = range[0];
79  float maxx = range[1];
80 
81  /* set value for drawing black line */
82  if (t->spacetype == SPACE_ACTION) {
83  SpaceAction *saction = (SpaceAction *)t->area->spacedata.first;
84  saction->timeslide = cval;
85  }
86 
87  /* It doesn't matter whether we apply to t->data or
88  * t->data2d, but t->data2d is more convenient. */
90  TransData *td = tc->data;
91  for (i = 0; i < tc->data_len; i++, td++) {
92  /* it is assumed that td->extra is a pointer to the AnimData,
93  * whose active action is where this keyframe comes from
94  * (this is only valid when not in NLA)
95  */
96  AnimData *adt = (t->spacetype != SPACE_NLA) ? td->extra : NULL;
97 
98  /* only apply to data if in range */
99  if ((sval > minx) && (sval < maxx)) {
100  float cvalc = CLAMPIS(cval, minx, maxx);
101  float ival = td->ival;
102  float timefac;
103 
104  /* NLA mapping magic here works as follows:
105  * - "ival" goes from strip time to global time
106  * - calculation is performed into td->val in global time
107  * (since sval and min/max are all in global time)
108  * - "td->val" then gets put back into strip time
109  */
110  if (adt) {
111  /* strip to global */
112  ival = BKE_nla_tweakedit_remap(adt, ival, NLATIME_CONVERT_MAP);
113  }
114 
115  /* left half? */
116  if (ival < sval) {
117  timefac = (sval - ival) / (sval - minx);
118  *(td->val) = cvalc - timefac * (cvalc - minx);
119  }
120  else {
121  timefac = (ival - sval) / (maxx - sval);
122  *(td->val) = cvalc + timefac * (maxx - cvalc);
123  }
124 
125  if (adt) {
126  /* global to strip */
127  *(td->val) = BKE_nla_tweakedit_remap(adt, *(td->val), NLATIME_CONVERT_UNMAP);
128  }
129  }
130  }
131  }
132 }
133 
134 static void applyTimeSlide(TransInfo *t, const int mval[2])
135 {
136  View2D *v2d = (View2D *)t->view;
137  float cval[2], sval[2];
138  const float *range = t->custom.mode.data;
139  float minx = range[0];
140  float maxx = range[1];
141  char str[UI_MAX_DRAW_STR];
142 
143  /* calculate mouse co-ordinates */
144  UI_view2d_region_to_view(v2d, mval[0], mval[1], &cval[0], &cval[1]);
145  UI_view2d_region_to_view(v2d, t->mouse.imval[0], t->mouse.imval[1], &sval[0], &sval[1]);
146 
147  /* t->values_final[0] stores cval[0], which is the current mouse-pointer location (in frames) */
148  /* XXX Need to be able to repeat this. */
149  /* t->values_final[0] = cval[0]; */ /* UNUSED (reset again later). */
150 
151  /* handle numeric-input stuff */
152  t->vec[0] = 2.0f * (cval[0] - sval[0]) / (maxx - minx);
153  applyNumInput(&t->num, &t->vec[0]);
154  t->values_final[0] = (maxx - minx) * t->vec[0] / 2.0f + sval[0];
155 
156  headerTimeSlide(t, sval[0], str);
157  applyTimeSlideValue(t, sval[0], t->values_final[0]);
158 
159  recalcData(t);
160 
161  ED_area_status_text(t->area, str);
162 }
163 
165 {
166  /* this tool is only really available in the Action Editor... */
167  if (t->spacetype == SPACE_ACTION) {
168  SpaceAction *saction = (SpaceAction *)t->area->spacedata.first;
169 
170  /* set flag for drawing stuff */
171  saction->flag |= SACTION_MOVING;
172  }
173  else {
174  t->state = TRANS_CANCEL;
175  }
176 
177  t->mode = TFM_TIME_SLIDE;
178  t->transform = applyTimeSlide;
179 
180  initMouseInputMode(t, &t->mouse, INPUT_NONE);
181 
182  {
183  Scene *scene = t->scene;
184  float *range;
185  t->custom.mode.data = range = MEM_mallocN(sizeof(float[2]), "TimeSlide Min/Max");
186  t->custom.mode.use_free = true;
187 
188  float min = 999999999.0f, max = -999999999.0f;
189  int i;
191  TransData *td = tc->data;
192  for (i = 0; i < tc->data_len; i++, td++) {
193  AnimData *adt = (t->spacetype != SPACE_NLA) ? td->extra : NULL;
194  float val = *(td->val);
195 
196  /* strip/action time to global (mapped) time */
197  if (adt) {
199  }
200 
201  if (min > val) {
202  min = val;
203  }
204  if (max < val) {
205  max = val;
206  }
207  }
208  }
209 
210  if (min == max) {
211  /* just use the current frame ranges */
212  min = (float)PSFRA;
213  max = (float)PEFRA;
214  }
215 
216  range[0] = min;
217  range[1] = max;
218  }
219 
220  /* num-input has max of (n-1) */
221  t->idx_max = 0;
222  t->num.flag = 0;
223  t->num.idx_max = t->idx_max;
224 
225  /* initialize snap like for everything else */
226  t->snap[0] = t->snap[1] = 1.0f;
227 
228  copy_v3_fl(t->num.val_inc, t->snap[0]);
229  t->num.unit_sys = t->scene->unit.system;
230  /* No time unit supporting frames currently... */
231  t->num.unit_type[0] = B_UNIT_NONE;
232 }
typedef float(TangentPoint)[2]
@ NLATIME_CONVERT_MAP
Definition: BKE_nla.h:156
@ NLATIME_CONVERT_UNMAP
Definition: BKE_nla.h:153
float BKE_nla_tweakedit_remap(struct AnimData *adt, float cframe, short mode)
Definition: nla.c:582
@ B_UNIT_NONE
Definition: BKE_unit.h:78
MINLINE void copy_v3_fl(float r[3], float f)
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define CLAMPIS(a, b, c)
#define TIP_(msgid)
@ SACTION_MOVING
#define PSFRA
#define PEFRA
@ SPACE_ACTION
@ SPACE_NLA
void outputNumInput(NumInput *n, char *str, struct UnitSettings *unit_settings)
Definition: numinput.c:102
#define NUM_STR_REP_LEN
Definition: ED_numinput.h:27
bool applyNumInput(NumInput *n, float *vec)
Definition: numinput.c:207
bool hasNumInput(const NumInput *n)
Definition: numinput.c:185
void ED_area_status_text(ScrArea *area, const char *str)
Definition: area.c:815
@ TFM_TIME_SLIDE
Definition: ED_transform.h:66
_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
Read Guarded memory(de)allocation.
Group RGB to Bright Vector Camera CLAMP
#define UI_MAX_DRAW_STR
Definition: UI_interface.h:90
void UI_view2d_region_to_view(const struct View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Scene scene
#define str(s)
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
#define min(a, b)
Definition: sort.c:51
float * val
@ INPUT_NONE
Definition: transform.h:727
void recalcData(TransInfo *t)
@ TRANS_CANCEL
Definition: transform.h:193
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition: transform.h:813
float max
transform modes used by different operators.
static void headerTimeSlide(TransInfo *t, const float sval, char str[UI_MAX_DRAW_STR])
static void applyTimeSlideValue(TransInfo *t, float sval, float cval)
void initTimeSlide(TransInfo *t)
static void applyTimeSlide(TransInfo *t, const int mval[2])