Blender V5.3
transform_mode_timeslide.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 <algorithm>
10#include <cstdlib>
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_math_vector_c.hh"
15#include "BLI_string_utf8.hh"
16
17#include "BKE_nla.hh"
18#include "BKE_scene.hh"
19#include "BKE_unit.hh"
20
21#include "ED_screen.hh"
22
23#include "UI_view2d.hh"
24
25#include "BLT_translation.hh"
26
27#include "UI_interface_types.hh"
28
29#include "transform.hh"
30#include "transform_convert.hh"
31
32#include "transform_mode.hh"
33
34namespace blender::ed::transform {
35
36/* -------------------------------------------------------------------- */
39
40static void headerTimeSlide(TransInfo *t, const float sval, char str[UI_MAX_DRAW_STR])
41{
42 char tvec[NUM_STR_REP_LEN * 3];
43
44 if (hasNumInput(&t->num)) {
45 outputNumInput(&(t->num), tvec, t->scene->unit);
46 }
47 else {
48 const float *range = static_cast<const float *>(t->custom.mode.data);
49 float minx = range[0];
50 float maxx = range[1];
51 float cval = t->values_final[0];
52 float val;
53
54 val = 2.0f * (cval - sval) / (maxx - minx);
55 CLAMP(val, -1.0f, 1.0f);
56
57 BLI_snprintf_utf8(&tvec[0], NUM_STR_REP_LEN, "%.4f", val);
58 }
59
60 BLI_snprintf_utf8(str, UI_MAX_DRAW_STR, IFACE_("TimeSlide: %s"), &tvec[0]);
61}
62
63static void applyTimeSlideValue(TransInfo *t, float sval, float cval)
64{
65 int i;
66 const float *range = static_cast<const float *>(t->custom.mode.data);
67 float minx = range[0];
68 float maxx = range[1];
69
70 /* Set value for drawing black line. */
71 if (t->spacetype == SPACE_ACTION) {
72 SpaceAction *saction = t->area->spacedata.first_as<SpaceAction>();
73 saction->timeslide = cval;
74 }
75
76 /* It doesn't matter whether we apply to t->data or
77 * t->data2d, but t->data2d is more convenient. */
79 TransData *td = tc->data;
80 for (i = 0; i < tc->data_len; i++, td++) {
81 /* It is assumed that td->extra is a pointer to the AnimData,
82 * whose active action is where this keyframe comes from
83 * (this is only valid when not in NLA). */
84 AnimData *adt = static_cast<AnimData *>((t->spacetype != SPACE_NLA) ? td->extra : nullptr);
85
86 /* Only apply to data if in range. */
87 if ((sval > minx) && (sval < maxx)) {
88 float cvalc = std::clamp(cval, minx, maxx);
89 float timefac;
90 float *dst;
91 float ival;
92
93 if (td->val) {
94 dst = td->val;
95 ival = td->ival;
96 }
97 else {
98 dst = &td->loc[0];
99 ival = td->iloc[0];
100 }
101
102 /* NLA mapping magic here works as follows:
103 * - `ival` goes from strip time to global time.
104 * - Calculation is performed into `td->val` in global time
105 * (since `sval` and min/max are all in global time).
106 * - `td->val` then gets put back into strip time.
107 */
108 if (adt) {
109 /* Strip to global. */
111 }
112
113 /* Left half? */
114 if (ival < sval) {
115 timefac = (sval - ival) / (sval - minx);
116 *dst = cvalc - timefac * (cvalc - minx);
117 }
118 else {
119 timefac = (ival - sval) / (maxx - sval);
120 *dst = cvalc + timefac * (maxx - cvalc);
121 }
122
123 if (adt) {
124 /* Global to strip. */
126 }
127 }
128 }
129 }
130}
131
133{
134 View2D *v2d = static_cast<View2D *>(t->view);
135 float cval[2], sval[2];
136 const float *range = static_cast<const float *>(t->custom.mode.data);
137 float minx = range[0];
138 float maxx = range[1];
139 char str[UI_MAX_DRAW_STR];
140
141 /* Calculate mouse co-ordinates. */
142 ui::view2d_region_to_view(v2d, t->mval[0], t->mval[1], &cval[0], &cval[1]);
143 ui::view2d_region_to_view(v2d, t->mouse.imval[0], t->mouse.imval[1], &sval[0], &sval[1]);
144
145 /* `t->values_final[0]` stores `cval[0]`,
146 * which is the current mouse-pointer location (in frames). */
147 /* XXX Need to be able to repeat this. */
148 // t->values_final[0] = cval[0]; /* UNUSED (reset again later). */
149
150 /* Handle numeric-input stuff. */
151 t->vec[0] = 2.0f * (cval[0] - sval[0]) / (maxx - minx);
152 applyNumInput(&t->num, &t->vec[0]);
153 t->values_final[0] = (maxx - minx) * t->vec[0] / 2.0f + sval[0];
154
155 headerTimeSlide(t, sval[0], str);
156 applyTimeSlideValue(t, sval[0], t->values_final[0]);
157
158 recalc_data(t);
159
161}
162
163static void initTimeSlide(TransInfo *t, wmOperator * /*op*/)
164{
165 /* This tool is only really available in the Action Editor. */
166 if (t->spacetype == SPACE_ACTION) {
167 SpaceAction *saction = t->area->spacedata.first_as<SpaceAction>();
168
169 /* Set flag for drawing stuff. */
170 saction->flag |= SACTION_MOVING;
171 }
172 else {
173 t->state = TRANS_CANCEL;
174 }
175
176 t->mode = TFM_TIME_SLIDE;
177
179
180 {
181 Scene *scene = t->scene;
182 float *range;
183 t->custom.mode.data = range = MEM_new_array_uninitialized<float>(2, "TimeSlide Min/Max");
184 t->custom.mode.use_free = true;
185
186 float min = 999999999.0f, max = -999999999.0f;
187 int i;
189 TransData *td = tc->data;
190 for (i = 0; i < tc->data_len; i++, td++) {
191 AnimData *adt = static_cast<AnimData *>((t->spacetype != SPACE_NLA) ? td->extra : nullptr);
192 float val = *(td->val);
193
194 /* Strip/action time to global (mapped) time. */
195 if (adt) {
197 }
198
199 min = std::min(min, val);
200 max = std::max(max, val);
201 }
202 }
203
204 if (min == max) {
205 /* Just use the current frame ranges. */
206 min = float(scene->playback_start());
207 max = float(scene->playback_end());
208 }
209
210 range[0] = min;
211 range[1] = max;
212 }
213
214 /* Numeric-input has max of (n-1). */
215 t->idx_max = 0;
216 t->num.flag = 0;
217 t->num.idx_max = t->idx_max;
218
219 /* Initialize snap like for everything else. */
220 t->increment[0] = 1.0f;
221 t->increment_precision = 1.0f;
222
223 copy_v3_fl(t->num.val_inc, t->increment[0]);
224 t->num.unit_sys = t->scene->unit.system;
225 /* No time unit supporting frames currently. */
226 t->num.unit_type[0] = B_UNIT_NONE;
227}
228
230
232 /*flags*/ T_NULL_ONE,
233 /*init_fn*/ initTimeSlide,
234 /*transform_fn*/ applyTimeSlide,
235 /*transform_matrix_fn*/ nullptr,
236 /*handle_event_fn*/ nullptr,
237 /*snap_distance_fn*/ nullptr,
238 /*snap_apply_fn*/ nullptr,
239 /*draw_fn*/ nullptr,
240 /*status_fn*/ nullptr,
241};
242
243} // namespace blender::ed::transform
#define IFACE_(msgid)
#define NUM_STR_REP_LEN
Read Blender memory allocation and freeing.
#define UI_MAX_DRAW_STR
#define CLAMP(a, b, c)
btScalar max
#define str(s)
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
void recalc_data(TransInfo *t)
static void applyTimeSlideValue(TransInfo *t, float sval, float cval)
static void headerTimeSlide(TransInfo *t, const float sval, char str[UI_MAX_DRAW_STR])
static void initTimeSlide(TransInfo *t, wmOperator *)
static void applyTimeSlide(TransInfo *t)
void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1651
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
float BKE_nla_tweakedit_remap(AnimData *adt, float cframe, eNlaTime_ConvertModes mode)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ NLATIME_CONVERT_UNMAP
Definition BKE_nla.hh:560
@ NLATIME_CONVERT_MAP
Definition BKE_nla.hh:563
bool hasNumInput(const NumInput *n)
Definition numinput.cc:188
void ED_area_status_text(ScrArea *area, const char *str)
Definition area.cc:975
bool applyNumInput(NumInput *n, float *vec)
Definition numinput.cc:207
MINLINE void copy_v3_fl(float r[3], float f)
void outputNumInput(NumInput *n, char *str, const UnitSettings &unit_settings)
Definition numinput.cc:104
@ B_UNIT_NONE
Definition BKE_unit.hh:165
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
int unit_type[NUM_MAX_ELEMENTS]
float val_inc[NUM_MAX_ELEMENTS]
struct UnitSettings unit
ListBaseT< SpaceLink > spacedata
TransCustomDataContainer custom
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:48
conversion and adaptation of different datablocks to a common struct.
transform modes used by different operators.