Adding the cubic-bezier curve to edje transitions

Summary: Adding an option to use a cubic-bezier curve in edje transitions.

Reviewers: Sachiel, cedric, raster

Reviewed By: raster

CC: raster

Differential Revision: https://phab.enlightenment.org/D319
This commit is contained in:
Otavio Pontes 2013-12-02 14:59:43 +09:00 committed by Carsten Haitzler (Rasterman)
parent 0b605c5daa
commit afd8a238d1
12 changed files with 238 additions and 24 deletions

View File

@ -421,6 +421,7 @@
; program transition (st_collections_group_programs_program_transition)
"LINEAR"
"SINUSOIDAL"
"CUBIC_BEZIER"
"ACCELERATE"
"DECELERATE"
) t) "\\>")

View File

@ -1282,6 +1282,8 @@ _edje_program_copy(Edje_Program *ep, Edje_Program *ep2)
ep->tween.time = ep2->tween.time;
ep->tween.v1 = ep2->tween.v1;
ep->tween.v2 = ep2->tween.v2;
ep->tween.v3 = ep2->tween.v3;
ep->tween.v4 = ep2->tween.v4;
ep->sample_name = STRDUP(ep2->sample_name);
ep->tone_name = STRDUP(ep2->tone_name);
ep->duration = ep2->duration;
@ -9265,6 +9267,7 @@ st_collections_group_programs_program_transition(void)
// long/full names
"LINEAR", EDJE_TWEEN_MODE_LINEAR,
"SINUSOIDAL", EDJE_TWEEN_MODE_SINUSOIDAL,
"CUBIC_BEZIER", EDJE_TWEEN_MODE_CUBIC_BEZIER,
"ACCELERATE", EDJE_TWEEN_MODE_ACCELERATE,
"DECELERATE", EDJE_TWEEN_MODE_DECELERATE,
"ACCELERATE_FACTOR", EDJE_TWEEN_MODE_ACCELERATE_FACTOR,
@ -9327,6 +9330,22 @@ st_collections_group_programs_program_transition(void)
current_program->tween.v1 = FROM_DOUBLE(parse_float_range(2, 0.0, 999999999.0));
current_program->tween.v2 = FROM_DOUBLE(parse_float_range(3, 0.0, 999999999.0));
}
else if ((current_program->tween.mode == EDJE_TWEEN_MODE_CUBIC_BEZIER))
{
if ((get_arg_count() == 7) && (!strcmp(parse_str(4), "CURRENT")))
current_program->tween.mode |= EDJE_TWEEN_MODE_OPT_FROM_CURRENT;
else if (get_arg_count() != 6)
{
ERR("parse error %s:%i. "
"Need 3rd, 4th, 5th and 6th parameters to set x1, y1, x2 and y2",
file_in, line - 1);
exit(-1);
}
current_program->tween.v1 = FROM_DOUBLE(parse_float_range(2, 0.0, 999999999.0));
current_program->tween.v2 = FROM_DOUBLE(parse_float_range(3, 0.0, 999999999.0));
current_program->tween.v3 = FROM_DOUBLE(parse_float_range(4, 0.0, 999999999.0));
current_program->tween.v4 = FROM_DOUBLE(parse_float_range(5, 0.0, 999999999.0));
}
}
/**

View File

@ -1062,6 +1062,7 @@ _transition_name_get(Edje_Tween_Mode mode)
case EDJE_TWEEN_MODE_ACCELERATE: return "ACCELERATE";
case EDJE_TWEEN_MODE_DECELERATE: return "DECELERATE";
case EDJE_TWEEN_MODE_SINUSOIDAL: return "SINUSOIDAL";
case EDJE_TWEEN_MODE_CUBIC_BEZIER: return "CUBIC_BEZIER";
default:
ERR("Unknown transition mode %d", mode);
return "???";

View File

@ -1899,7 +1899,8 @@ enum _Ecore_Pos_Map /* Position mappings */
ECORE_POS_MAP_SINUSOIDAL_FACTOR, /**< Start slow, speed up then slow down at end, v1 being a power factor, 0.0 being linear, 1.0 being normal sinusoidal, 2.0 being much more pronounced sinusoidal (squared), 3.0 being cubed, etc. */
ECORE_POS_MAP_DIVISOR_INTERP, /**< Start at gradient * v1, interpolated via power of v2 curve */
ECORE_POS_MAP_BOUNCE, /**< Start at 0.0 then "drop" like a ball bouncing to the ground at 1.0, and bounce v2 times, with decay factor of v1 */
ECORE_POS_MAP_SPRING /**< Start at 0.0 then "wobble" like a spring rest position 1.0, and wobble v2 times, with decay factor of v1 */
ECORE_POS_MAP_SPRING, /**< Start at 0.0 then "wobble" like a spring rest position 1.0, and wobble v2 times, with decay factor of v1 */
ECORE_POS_MAP_CUBIC_BEZIER /**< Follow the cubic-bezier curve calculated with the points (x1, y1), (x2, y2) */
};
typedef enum _Ecore_Pos_Map Ecore_Pos_Map;
@ -2010,6 +2011,71 @@ EAPI double ecore_animator_frametime_get(void);
* @since 1.1.0
*/
EAPI double ecore_animator_pos_map(double pos, Ecore_Pos_Map map, double v1, double v2);
/**
* @brief Maps an input position from 0.0 to 1.0 along a timeline to a
* position in a different curve.
*
* @param pos The input position to map
* @param map The mapping to use
* @param v_size The size of the v array.
* @param v An array with the double parameters to be used by the mapping.
* NULL if not used.
* @return The mapped value
*
* Takes an input position (0.0 to 1.0) and maps to a new position (normally
* between 0.0 and 1.0, but it may go above/below 0.0 or 1.0 to show that it
* has "overshot" the mark) using some interpolation (mapping) algorithm.
*
* This function useful to create non-linear animations. It offers a variety
* of possible animation curves to be used:
* @li ECORE_POS_MAP_LINEAR - Linear, returns @p pos
* @li ECORE_POS_MAP_ACCELERATE - Start slow then speed up
* @li ECORE_POS_MAP_DECELERATE - Start fast then slow down
* @li ECORE_POS_MAP_SINUSOIDAL - Start slow, speed up then slow down at end
* @li ECORE_POS_MAP_ACCELERATE_FACTOR - Start slow then speed up, v[0] being a
* power factor, 0.0 being linear, 1.0 being ECORE_POS_MAP_ACCELERATE, 2.0
* being much more pronounced accelerate (squared), 3.0 being cubed, etc.
* @li ECORE_POS_MAP_DECELERATE_FACTOR - Start fast then slow down, v[0] being a
* power factor, 0.0 being linear, 1.0 being ECORE_POS_MAP_DECELERATE, 2.0
* being much more pronounced decelerate (squared), 3.0 being cubed, etc.
* @li ECORE_POS_MAP_SINUSOIDAL_FACTOR - Start slow, speed up then slow down
* at end, v[0] being a power factor, 0.0 being linear, 1.0 being
* ECORE_POS_MAP_SINUSOIDAL, 2.0 being much more pronounced sinusoidal
* (squared), 3.0 being cubed, etc.
* @li ECORE_POS_MAP_DIVISOR_INTERP - Start at gradient * v[0], interpolated via
* power of v2 curve
* @li ECORE_POS_MAP_BOUNCE - Start at 0.0 then "drop" like a ball bouncing to
* the ground at 1.0, and bounce v2 times, with decay factor of v[0]
* @li ECORE_POS_MAP_SPRING - Start at 0.0 then "wobble" like a spring rest
* position 1.0, and wobble v2 times, with decay factor of v[0]
* @li ECORE_POS_MAP_CUBIC_BEZIER - Use an interpolated cubic-bezier curve
* ajusted with parameters from v[0] to v[3].
* @note When not listed v has no effect.
*
* @image html ecore-pos-map.png
* @image latex ecore-pos-map.eps width=\textwidth
*
* One way to use this would be:
* @code
* double pos; // input position in a timeline from 0.0 to 1.0
* double out; // output position after mapping
* int x1, y1, x2, y2; // x1 & y1 are start position, x2 & y2 are end position
* int x, y; // x & y are the calculated position
* double v[2] = {1.8, 7};
*
* out = ecore_animator_pos_map(pos, ECORE_POS_MAP_BOUNCE, 2, v);
* x = (x1 * out) + (x2 * (1.0 - out));
* y = (y1 * out) + (y2 * (1.0 - out));
* move_my_object_to(myobject, x, y);
* @endcode
* This will make an animation that bounces 7 each times diminishing by a
* factor of 1.8.
*
* @see _Ecore_Pos_Map
*/
EAPI double ecore_animator_pos_map_n(double pos, Ecore_Pos_Map map, int v_size, double v[]);
/**
* @brief Set the source of animator ticks for the mainloop
*

View File

@ -313,6 +313,72 @@ _pos_map_spring(double pos,
return _pos_map_sin((M_PI / 2.0) + (p2 * len)) * decay;
}
static double
_cubic_bezier_a (double a1, double a2)
{
return 1.0 - 3.0 * a2 + 3.0 * a1;
}
static double
_cubic_bezier_b (double a1, double a2)
{
return 3.0 * a2 - 6.0 * a1;
}
static double
_cubic_bezier_c(double a1)
{
return 3.0 * a1;
}
static double
_cubic_bezier_calc(double t,
double a1,
double a2)
{
return ((_cubic_bezier_a(a1, a2) * t +
_cubic_bezier_b(a1, a2)) * t +
_cubic_bezier_c(a1)) * t;
}
static double
_cubic_bezier_slope_get(double t,
double a1,
double a2)
{
return 3.0 * _cubic_bezier_a(a1, a2) * t * t +
2.0 * _cubic_bezier_b(a1, a2) * t +
_cubic_bezier_c(a1);
}
static double
_cubic_bezier_t_get(double a,
double x1,
double x2)
{
double guess_t = a;
for (int i = 0; i < 4; ++i)
{
double current_slope = _cubic_bezier_slope_get(a, x1, x2);
if (current_slope == 0.0)
return guess_t;
double current_x = _cubic_bezier_calc(guess_t, x1, x2) - a;
guess_t -= current_x / current_slope;
}
return guess_t;
}
static double
_pos_map_cubic_bezier(double pos,
double x1,
double y1,
double x2,
double y2)
{
if (x1 == y1 && x2 == y2) return pos;
return _cubic_bezier_calc(_cubic_bezier_t_get(pos, x1, x2), y1, y2);
}
#define DBL_TO(Fp) eina_f32p32_double_to(Fp)
#define DBL_FROM(D) eina_f32p32_double_from(D)
#define INT_FROM(I) eina_f32p32_int_from(I)
@ -323,11 +389,13 @@ _pos_map_spring(double pos,
#define MUL(A, B) eina_f32p32_mul(A, B)
EAPI double
ecore_animator_pos_map(double pos,
Ecore_Pos_Map map,
double v1,
double v2)
ecore_animator_pos_map_n(double pos,
Ecore_Pos_Map map,
int v_size,
double v[])
{
double v0 = 0, v1 = 0, v2 = 0, v3 = 0;
/* purely functional - locking not required */
if (pos >= 1.0) return 1.0;
else if (pos <= 0.0)
@ -353,30 +421,47 @@ ecore_animator_pos_map(double pos,
return pos;
case ECORE_POS_MAP_ACCELERATE_FACTOR:
pos = _pos_map_accel_factor(pos, v1);
if (v_size > 0) v0 = v[0];
pos = _pos_map_accel_factor(pos, v0);
return pos;
case ECORE_POS_MAP_DECELERATE_FACTOR:
pos = 1.0 - _pos_map_accel_factor(1.0 - pos, v1);
if (v_size > 0) v0 = v[0];
pos = 1.0 - _pos_map_accel_factor(1.0 - pos, v0);
return pos;
case ECORE_POS_MAP_SINUSOIDAL_FACTOR:
if (pos < 0.5) pos = _pos_map_accel_factor(pos * 2.0, v1) / 2.0;
else pos = 1.0 - (_pos_map_accel_factor((1.0 - pos) * 2.0, v1) / 2.0);
if (v_size > 0) v0 = v[0];
if (pos < 0.5) pos = _pos_map_accel_factor(pos * 2.0, v0) / 2.0;
else pos = 1.0 - (_pos_map_accel_factor((1.0 - pos) * 2.0, v0) / 2.0);
return pos;
case ECORE_POS_MAP_DIVISOR_INTERP:
pos = _pos_map_pow(pos, v1, (int)v2);
if (v_size > 0) v0 = v[0];
if (v_size > 1) v1 = v[1];
pos = _pos_map_pow(pos, v0, (int)v1);
return pos;
case ECORE_POS_MAP_BOUNCE:
pos = _pos_map_spring(pos, (int)v2, v1);
if (v_size > 0) v0 = v[0];
if (v_size > 1) v1 = v[1];
pos = _pos_map_spring(pos, (int)v1, v0);
if (pos < 0.0) pos = -pos;
pos = 1.0 - pos;
return pos;
case ECORE_POS_MAP_SPRING:
pos = 1.0 - _pos_map_spring(pos, (int)v2, v1);
if (v_size > 0) v0 = v[0];
if (v_size > 1) v1 = v[1];
pos = 1.0 - _pos_map_spring(pos, (int)v1, v0);
return pos;
case ECORE_POS_MAP_CUBIC_BEZIER:
if (v_size > 0) v0 = v[0];
if (v_size > 1) v1 = v[1];
if (v_size > 2) v2 = v[2];
if (v_size > 3) v3 = v[3];
pos = _pos_map_cubic_bezier(pos, v0, v1, v2, v3);
return pos;
default:
@ -386,6 +471,19 @@ ecore_animator_pos_map(double pos,
return pos;
}
EAPI double
ecore_animator_pos_map(double pos,
Ecore_Pos_Map map,
double v1,
double v2)
{
double v[2];
v[0] = v1;
v[1] = v2;
return ecore_animator_pos_map_n(pos, map, 2, v);
}
EAPI void *
ecore_animator_del(Ecore_Animator *obj)
{

View File

@ -1680,7 +1680,8 @@ typedef enum _Edje_Tween_Mode
EDJE_TWEEN_MODE_DIVISOR_INTERP = 8,
EDJE_TWEEN_MODE_BOUNCE = 9,
EDJE_TWEEN_MODE_SPRING = 10,
EDJE_TWEEN_MODE_LAST = 11,
EDJE_TWEEN_MODE_CUBIC_BEZIER = 11,
EDJE_TWEEN_MODE_LAST = 12,
EDJE_TWEEN_MODE_MASK = 0xff,
EDJE_TWEEN_MODE_OPT_FROM_CURRENT = (1 << 31)
} Edje_Tween_Mode;

View File

@ -26,10 +26,11 @@ static void _edje_part_recalc_single(Edje *ed, Edje_Real_Part *ep,
EINA_COW_WRITE_END(_edje_calc_params_map_cow, Calc->map, Write);
void
_edje_part_pos_set(Edje *ed, Edje_Real_Part *ep, int mode, FLOAT_T pos, FLOAT_T v1, FLOAT_T v2)
_edje_part_pos_set(Edje *ed, Edje_Real_Part *ep, int mode, FLOAT_T pos, FLOAT_T v1, FLOAT_T v2, FLOAT_T v3, FLOAT_T v4)
{
FLOAT_T fp_pos;
FLOAT_T npos;
double v[4];
pos = CLAMP(pos, ZERO, FROM_INT(1));
@ -119,6 +120,16 @@ _edje_part_pos_set(Edje *ed, Edje_Real_Part *ep, int mode, FLOAT_T pos, FLOAT_T
ECORE_POS_MAP_SPRING,
TO_DOUBLE(v1), TO_DOUBLE(v2)));
break;
case EDJE_TWEEN_MODE_CUBIC_BEZIER:
v[0] = TO_DOUBLE(v1);
v[1] = TO_DOUBLE(v2);
v[2] = TO_DOUBLE(v3);
v[3] = TO_DOUBLE(v4);
npos = FROM_DOUBLE(ecore_animator_pos_map_n(TO_DOUBLE(pos),
ECORE_POS_MAP_CUBIC_BEZIER,
4, v));
break;
default:
npos = fp_pos;
break;

View File

@ -460,6 +460,8 @@ _edje_edd_init(void)
EET_DATA_DESCRIPTOR_ADD_BASIC(_edje_edd_edje_program, Edje_Program, "tween.time", tween.time, EDJE_T_FLOAT);
EET_DATA_DESCRIPTOR_ADD_BASIC(_edje_edd_edje_program, Edje_Program, "v1", tween.v1, EDJE_T_FLOAT);
EET_DATA_DESCRIPTOR_ADD_BASIC(_edje_edd_edje_program, Edje_Program, "v2", tween.v2, EDJE_T_FLOAT);
EET_DATA_DESCRIPTOR_ADD_BASIC(_edje_edd_edje_program, Edje_Program, "v3", tween.v3, EDJE_T_FLOAT);
EET_DATA_DESCRIPTOR_ADD_BASIC(_edje_edd_edje_program, Edje_Program, "v4", tween.v4, EDJE_T_FLOAT);
EET_DATA_DESCRIPTOR_ADD_LIST(_edje_edd_edje_program, Edje_Program, "targets", targets, _edje_edd_edje_program_target);
EET_DATA_DESCRIPTOR_ADD_LIST(_edje_edd_edje_program, Edje_Program, "after", after, _edje_edd_edje_program_after);
EET_DATA_DESCRIPTOR_ADD_BASIC(_edje_edd_edje_program, Edje_Program, "api.name", api.name, EET_T_STRING);

View File

@ -955,7 +955,8 @@ _edje_embryo_fn_set_state(Embryo_Program *ep, Embryo_Cell *params)
{
if (rp->program) _edje_program_end(ed, rp->program);
_edje_part_description_apply(ed, rp, state, value, NULL, 0.0);
_edje_part_pos_set(ed, rp, EDJE_TWEEN_MODE_LINEAR, ZERO, ZERO, ZERO);
_edje_part_pos_set(ed, rp, EDJE_TWEEN_MODE_LINEAR, ZERO, ZERO, ZERO,
ZERO, ZERO);
_edje_recalc(ed);
}
return 0;
@ -1037,7 +1038,8 @@ _edje_embryo_fn_set_tween_state(Embryo_Program *ep, Embryo_Cell *params)
{
if (rp->program) _edje_program_end(ed, rp->program);
_edje_part_description_apply(ed, rp, state1, value1, state2, value2);
_edje_part_pos_set(ed, rp, EDJE_TWEEN_MODE_LINEAR, FROM_DOUBLE(tween), ZERO, ZERO);
_edje_part_pos_set(ed, rp, EDJE_TWEEN_MODE_LINEAR, FROM_DOUBLE(tween),
ZERO, ZERO, ZERO, ZERO);
_edje_recalc(ed);
}
return 0;

View File

@ -3761,7 +3761,8 @@ _edje_lua_part_set_state(lua_State *L)
_edje_part_description_apply(obj->ed, obj->rp,
luaL_checkstring(L, -2), luaL_checknumber(L, -1),
NULL, 0.0);
_edje_part_pos_set(obj->ed, obj->rp, EDJE_TWEEN_MODE_LINEAR, ZERO, ZERO, ZERO);
_edje_part_pos_set(obj->ed, obj->rp, EDJE_TWEEN_MODE_LINEAR, ZERO,
ZERO, ZERO, ZERO, ZERO);
_edje_recalc(obj->ed);
return 0;
}
@ -3780,7 +3781,7 @@ _edje_lua_part_set_tween_state(lua_State *L)
luaL_checkstring(L, -4), luaL_checknumber(L, -3),
luaL_checkstring(L, -2), luaL_checknumber(L, -1));
_edje_part_pos_set(obj->ed, obj->rp, EDJE_TWEEN_MODE_LINEAR,
FROM_DOUBLE(luaL_checknumber(L, -5)), ZERO, ZERO);
FROM_DOUBLE(luaL_checknumber(L, -5)), ZERO, ZERO, ZERO, ZERO);
_edje_recalc(obj->ed);
return 0;
}

View File

@ -648,6 +648,8 @@ struct _Edje_Program /* a conditional program to be run */
FLOAT_T time; /* time to graduate between current and new state */
FLOAT_T v1; /* other value for drag actions */
FLOAT_T v2; /* other value for drag actions */
FLOAT_T v3; /* other value for drag actions */
FLOAT_T v4; /* other value for drag actions */
} tween;
Eina_List *targets; /* list of target parts to apply the state to */
@ -1941,7 +1943,7 @@ EAPI extern Eina_Mempool *_emp_EXTERNAL;
EAPI extern Eina_Mempool *_emp_SPACER;
EAPI extern Eina_Mempool *_emp_part;
void _edje_part_pos_set(Edje *ed, Edje_Real_Part *ep, int mode, FLOAT_T pos, FLOAT_T v1, FLOAT_T v2);
void _edje_part_pos_set(Edje *ed, Edje_Real_Part *ep, int mode, FLOAT_T pos, FLOAT_T v1, FLOAT_T v2, FLOAT_T v3, FLOAT_T v4);
Edje_Part_Description_Common *_edje_part_description_find(Edje *ed,
Edje_Real_Part *rp,
const char *name, double val);

View File

@ -343,7 +343,9 @@ _edje_program_run_iterate(Edje_Running_Program *runp, double tim)
if (rp) _edje_part_pos_set(ed, rp,
runp->program->tween.mode, t,
runp->program->tween.v1,
runp->program->tween.v2);
runp->program->tween.v2,
runp->program->tween.v3,
runp->program->tween.v4);
}
}
if (t >= FROM_INT(1))
@ -365,7 +367,9 @@ _edje_program_run_iterate(Edje_Running_Program *runp, double tim)
_edje_part_pos_set(ed, rp,
runp->program->tween.mode, ZERO,
runp->program->tween.v1,
runp->program->tween.v2);
runp->program->tween.v2,
runp->program->tween.v3,
runp->program->tween.v4);
rp->program = NULL;
}
}
@ -442,7 +446,9 @@ _edje_program_end(Edje *ed, Edje_Running_Program *runp)
_edje_part_pos_set(ed, rp,
runp->program->tween.mode, ZERO,
runp->program->tween.v1,
runp->program->tween.v2);
runp->program->tween.v2,
runp->program->tween.v3,
runp->program->tween.v4);
if (rp->current)
{
@ -605,7 +611,9 @@ low_mem_current:
pr->value);
_edje_part_pos_set(ed, rp, pr->tween.mode, ZERO,
pr->tween.v1,
pr->tween.v2);
pr->tween.v2,
pr->tween.v3,
pr->tween.v4);
rp->program = runp;
}
}
@ -644,7 +652,9 @@ low_mem_current:
0.0);
_edje_part_pos_set(ed, rp, pr->tween.mode, ZERO,
pr->tween.v1,
pr->tween.v2);
pr->tween.v2,
pr->tween.v3,
pr->tween.v4);
}
}
}