From 22ea65727c03bd87be9f40008b8cdbbbdeaa927d Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Sun, 3 Oct 2021 17:54:35 +0800 Subject: [PATCH] Add camera tool --- src/game/bootloader.cpp | 27 +++++++++++++++++++++ src/game/context.hpp | 2 ++ src/game/states/forage.cpp | 49 +++++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/game/bootloader.cpp b/src/game/bootloader.cpp index c204975..346e0d3 100644 --- a/src/game/bootloader.cpp +++ b/src/game/bootloader.cpp @@ -673,6 +673,23 @@ void setup_scenes(game::context* ctx) ctx->splash_billboard->set_translation({0.0f, 0.0f, 0.0f}); ctx->splash_billboard->update_tweens(); + // Create camera flash billboard + + material* flash_material = new material(); + flash_material->set_shader_program(ctx->resource_manager->load("ui-element-untextured.glsl")); + auto flash_tint = flash_material->add_property("tint"); + flash_tint->set_value(float4{1, 1, 1, 1}); + //flash_tint->set_tween_interpolator(ease::out_quad); + + flash_material->set_flags(MATERIAL_FLAG_TRANSLUCENT); + flash_material->update_tweens(); + + ctx->camera_flash_billboard = new scene::billboard(); + ctx->camera_flash_billboard->set_material(flash_material); + ctx->camera_flash_billboard->set_scale({(float)viewport_dimensions[0] * 0.5f, (float)viewport_dimensions[1] * 0.5f, 1.0f}); + ctx->camera_flash_billboard->set_translation({0.0f, 0.0f, 0.0f}); + ctx->camera_flash_billboard->update_tweens(); + // Create depth debug billboard /* material* depth_debug_material = new material(); @@ -755,6 +772,16 @@ void setup_animation(game::context* ctx) ctx->ui_scene->add_object(ctx->radial_transition_outer->get_billboard()); ctx->animator->add_animation(ctx->radial_transition_outer->get_animation()); + // Create camera flash animation + ctx->camera_flash_animation = new animation(); + { + ctx->camera_flash_animation->set_interpolator(ease::out_sine); + const float duration = 0.5f; + animation_channel* channel = ctx->camera_flash_animation->add_channel(0); + channel->insert_keyframe({0.0f, 1.0f}); + channel->insert_keyframe({duration, 0.0f}); + } + // Set material pass tweens ctx->common_final_pass->set_time_tween(ctx->time_tween); ctx->surface_sky_pass->set_time_tween(ctx->time_tween); diff --git a/src/game/context.hpp b/src/game/context.hpp index ed4b0d6..ff01952 100644 --- a/src/game/context.hpp +++ b/src/game/context.hpp @@ -188,6 +188,7 @@ struct context scene::collection* ui_scene; scene::camera* ui_camera; scene::billboard* splash_billboard; + scene::billboard* camera_flash_billboard; // Surface scene scene::collection* surface_scene; @@ -211,6 +212,7 @@ struct context screen_transition* radial_transition_outer; animation* equip_tool_animation; animation* unequip_tool_animation; + animation* camera_flash_animation; // Controls input::event_router* input_event_router; diff --git a/src/game/states/forage.cpp b/src/game/states/forage.cpp index e446f00..3fc1cba 100644 --- a/src/game/states/forage.cpp +++ b/src/game/states/forage.cpp @@ -34,6 +34,8 @@ #include "entity/components/constraints/three-dof.hpp" #include "entity/components/constraint-stack.hpp" #include "application.hpp" +#include "utility/timestamp.hpp" +#include "animation/animator.hpp" namespace game { namespace state { @@ -258,8 +260,53 @@ void setup_tools(game::context* ctx) ctx->entity_registry->assign(tool_eid, tool); } + if (!ctx->entities.count("camera_tool")) + { + entity::id tool_eid = ctx->entity_registry->create(); + ctx->entities["camera_tool"] = tool_eid; + + entity::component::tool tool; + tool.activated = [ctx]() + { + if (!ctx->camera_flash_animation->is_stopped()) + return; + + std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png"; + ctx->app->save_frame(path); + + material_property* tint = static_cast*>(ctx->camera_flash_billboard->get_material()->get_property("tint")); + tint->set_value({1.0f, 1.0f, 1.0f, 1.0f}); + ctx->camera_flash_billboard->get_material()->update_tweens(); + ctx->ui_scene->add_object(ctx->camera_flash_billboard); + + ctx->camera_flash_animation->set_end_callback + ( + [ctx]() + { + ctx->ui_scene->remove_object(ctx->camera_flash_billboard); + } + ); + + ctx->camera_flash_animation->set_frame_callback + ( + [ctx, tint](int channel, const float& opacity) + { + + tint->set_value({1.0f, 1.0f, 1.0f, opacity}); + } + ); + + ctx->animator->remove_animation(ctx->camera_flash_animation); + ctx->animator->add_animation(ctx->camera_flash_animation); + ctx->camera_flash_animation->rewind(); + ctx->camera_flash_animation->play(); + }; + + ctx->entity_registry->assign(tool_eid, tool); + } + // Set active tool - ctx->entities["active_tool"] = ctx->entities["time_tool"]; + ctx->entities["active_tool"] = ctx->entities["camera_tool"]; } void setup_controls(game::context* ctx)