diff --git a/paint/sources/functions.h b/paint/sources/functions.h index fefb29a9..e642d357 100644 --- a/paint/sources/functions.h +++ b/paint/sources/functions.h @@ -413,6 +413,7 @@ void tab_stages_apply(stage_t *stage); void tab_stages_rename_object(char *old_name, char *new_name); void tab_stages_rename_layer(char *old_name, char *new_name); void script_set_stage(char *name); +void script_fade_to_stage(char *stage); void tab_stages_prune(); void project_open(); void project_save(bool save_and_quit); diff --git a/paint/sources/minic_api.c b/paint/sources/minic_api.c index ace1e83e..c12b5c51 100644 --- a/paint/sources/minic_api.c +++ b/paint/sources/minic_api.c @@ -29,6 +29,7 @@ void console_log(char *s); bool point_in_aabb(object_t *object, vec4_t point); vec4_t raycast_aabb_mouse(object_t *object); +void script_tween_to(object_t *o, vec4_t to, f32 speed); static const char *minic_read_str(minic_val_t v) { if (v.type == MINIC_T_PTR && v.p != NULL) { @@ -275,6 +276,7 @@ static void minic_set_mat4(minic_val_t *o, mat4_t m) { minic_box(o, m.m, 16); } X(V4, transform_up, transform_up(AP(0))) \ X(V4, raycast_aabb_mouse, raycast_aabb_mouse((object_t *)AP(0))) \ X(I, point_in_aabb, point_in_aabb((object_t *)AP(0), V4(1))) \ + X(VOID, script_tween_to, script_tween_to((object_t *)AP(0), V4(1), AF(2))) \ X(VOID, line_draw_render, line_draw_render(M4(0))) \ X(VOID, line_draw_bounds, line_draw_bounds(M4(0), V4(1))) \ X(VOID, shape_draw_sphere, shape_draw_sphere(M4(0))) \ @@ -383,6 +385,8 @@ project_t *script_get_project() { } void script_set_stage(char *name); +void script_fade_to_stage(char *stage); +char *script_get_stage(); void script_set_tilesheet_anim(object_t *o, char *anim); object_t *script_get_object(char *s) { @@ -1363,6 +1367,8 @@ void minic_register_builtins() { R(script_get_project, "p()"); R(script_get_object, "p(p)"); R(script_set_stage, "v(p)"); + R(script_fade_to_stage, "v(p)"); + R(script_get_stage, "p()"); R(script_set_tilesheet_anim, "v(p,p)"); R(context_set_viewport_shader, "v(p)"); R(context_set_viewport_mode, "v(i)"); diff --git a/paint/sources/util/util_script.c b/paint/sources/util/util_script.c index 68898bb2..eba66c5b 100644 --- a/paint/sources/util/util_script.c +++ b/paint/sources/util/util_script.c @@ -15,6 +15,11 @@ void script_set_stage(char *name) { } } +char *script_get_stage() { + stage_t *s = tab_stages_get_stage(); + return s != NULL ? s->name : NULL; +} + void script_set_tilesheet_anim(object_t *o, char *anim) { mesh_object_t *mo = (mesh_object_t *)o->ext; @@ -60,3 +65,63 @@ void script_set_tilesheet_anim(object_t *o, char *anim) { } } } + +static transform_t *_script_tween_transform = NULL; + +static void script_tween_done(void) { + _script_tween_transform = NULL; +} + +static void script_tween_tick(void) { + _script_tween_transform->dirty = true; +} + +void script_tween_to(object_t *o, vec4_t to, f32 speed) { + if (_script_tween_transform != NULL) { + return; + } + + transform_t *t = o->transform; + _script_tween_transform = t; + f32 duration = vec4_dist(t->loc, to) / speed; + ease_t ease = EASE_LINEAR; + tween_to(GC_ALLOC_INIT(tween_anim_t, + {.target = &t->loc.x, .to = to.x, .duration = duration, .ease = ease, .tick = script_tween_tick, .done = script_tween_done})); + tween_to(GC_ALLOC_INIT(tween_anim_t, {.target = &t->loc.y, .to = to.y, .duration = duration, .ease = ease})); + tween_to(GC_ALLOC_INIT(tween_anim_t, {.target = &t->loc.z, .to = to.z, .duration = duration, .ease = ease})); +} + +static f32 _script_fade_opacity = 0.0f; +static char *_script_fade_stage = NULL; + +static void script_fade_draw(void *_) { + draw_begin(NULL, false, 0); + draw_set_color((u32)(_script_fade_opacity * 255.0f) << 24); + draw_filled_rect(0, 0, iron_window_width(), iron_window_height()); + draw_end(); +} + +static void script_fade_out_done(void *_) { + sys_remove_update(script_fade_draw); + gc_unroot(_script_fade_stage); + _script_fade_stage = NULL; +} + +static void script_fade_in_done(void *_) { + script_set_stage(_script_fade_stage); + tween_to(GC_ALLOC_INIT(tween_anim_t, {.target = &_script_fade_opacity, .to = 0.0f, .duration = 1.0f, .ease = EASE_LINEAR, .done = script_fade_out_done})); +} + +void script_fade_to_stage(char *stage) { + if (_script_fade_stage != NULL) { + return; // Fade in progress + } + _script_fade_stage = string_copy(stage); + gc_root(_script_fade_stage); + + _script_fade_opacity = 0.0f; + sys_notify_on_update(script_fade_draw, NULL); + + // Fade to black, set the stage, then fade back in + tween_to(GC_ALLOC_INIT(tween_anim_t, {.target = &_script_fade_opacity, .to = 1.0f, .duration = 1.0f, .ease = EASE_LINEAR, .done = script_fade_in_done})); +}