Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for zooming from scripting #1560

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions src/object/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ Camera::Camera(const std::string& name) :
m_scroll_goal(),
m_scroll_to_pos(),
m_scrollspeed(),
m_config(std::make_unique<CameraConfig>())
m_config(std::make_unique<CameraConfig>()),
m_scale(1.f),
m_scale_origin(1.f),
m_scale_target(1.f),
m_scale_time_total(0.f),
m_scale_time_remaining(0.f),
m_scale_easing()
{
reload_config();
}
Expand All @@ -162,7 +168,13 @@ Camera::Camera(const ReaderMapping& reader) :
m_scroll_goal(),
m_scroll_to_pos(),
m_scrollspeed(),
m_config(std::make_unique<CameraConfig>())
m_config(std::make_unique<CameraConfig>()),
m_scale(1.f),
m_scale_origin(1.f),
m_scale_target(1.f),
m_scale_time_total(0.f),
m_scale_time_remaining(0.f),
m_scale_easing()
{
std::string modename;

Expand Down Expand Up @@ -232,10 +244,11 @@ Camera::after_editor_set()
}
}

const Vector&
const Vector
Camera::get_translation() const
{
return m_translation;
Vector screen_size = Sizef(m_screen_size).as_vector();
return m_translation + ((screen_size * (m_scale - 1.f)) / 2.f);
}

void
Expand Down Expand Up @@ -305,6 +318,7 @@ Camera::update(float dt_sec)
default:
break;
}
update_scale(dt_sec);
shake();
}

Expand Down Expand Up @@ -594,6 +608,13 @@ Camera::update_scroll_normal(float dt_sec)
}
}

if (m_lookahead_pos.x > RIGHTEND) {
m_lookahead_pos.x = RIGHTEND;
}
if (m_lookahead_pos.x < LEFTEND) {
m_lookahead_pos.x = LEFTEND;
}

// adjust for level ends
if (player_pos.x < LEFTEND) {
m_lookahead_pos.x = LEFTEND;
Expand Down Expand Up @@ -676,6 +697,50 @@ Camera::update_scroll_to(float dt_sec)
m_translation = m_scroll_from + (m_scroll_goal - m_scroll_from) * m_scroll_to_pos;
}

void
Camera::update_scale(float dt_sec)
{
if (m_scale_time_remaining > 0.f)
{
m_scale_time_remaining -= dt_sec;

if (m_scale_time_remaining <= 0.f)
{
m_scale = m_scale_target;
m_scale_time_remaining = 0.f;
}
else
{
float progress = (m_scale_time_total - m_scale_time_remaining)
/ m_scale_time_total;
float true_progress = static_cast<float>(m_scale_easing(
static_cast<double>(progress)));
m_scale = m_scale_origin +
(m_scale_target - m_scale_origin) * true_progress;
}

// Re-center camera when zooming
m_lookahead_pos /= 1.01f;
}

Vector screen_size = Sizef(m_screen_size).as_vector();
m_translation += screen_size * (1.f - m_scale) / 2.f;
}

void
Camera::ease_scale(float scale, float time, easing ease)
{
if (time <= 0.f) {
m_scale = scale;
} else {
m_scale_origin = m_scale;
m_scale_target = scale;
m_scale_time_total = time;
m_scale_time_remaining = time;
m_scale_easing = ease;
}
}

Vector
Camera::get_center() const
{
Expand Down
19 changes: 18 additions & 1 deletion src/object/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Camera final : public GameObject,
void reset(const Vector& tuxpos);

/** return camera position */
const Vector& get_translation() const;
const Vector get_translation() const;
void set_translation(const Vector& translation) { m_translation = translation; }

/** shake camera in a direction 1 time */
Expand All @@ -97,12 +97,22 @@ class Camera final : public GameObject,
Vector get_center() const;

void set_mode(Mode mode_) { m_mode = mode_; }

/** get the exact scale at this exact moment */
float get_current_scale() { return m_scale; }
Semphriss marked this conversation as resolved.
Show resolved Hide resolved

/** get the scale towards which the camera is moving */
float get_target_scale() { return m_scale_target; }
Semphriss marked this conversation as resolved.
Show resolved Hide resolved

/** smoothly slide the scale of the camera towards a new value */
void ease_scale(float scale, float time, easing ease);
/** @} */

private:
void update_scroll_normal(float dt_sec);
void update_scroll_autoscroll(float dt_sec);
void update_scroll_to(float dt_sec);
void update_scale(float dt_sec);
void keep_in_bounds(Vector& vector);
void shake();

Expand Down Expand Up @@ -135,6 +145,13 @@ class Camera final : public GameObject,

std::unique_ptr<CameraConfig> m_config;

float m_scale,
m_scale_origin,
m_scale_target,
m_scale_time_total,
m_scale_time_remaining;
easing m_scale_easing;

private:
Camera(const Camera&) = delete;
Camera& operator=(const Camera&) = delete;
Expand Down
36 changes: 36 additions & 0 deletions src/scripting/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,42 @@ Camera::scroll_to(float x, float y, float scrolltime)
object.scroll_to(Vector(x, y), scrolltime);
}

float
Camera::get_current_scale()
{
SCRIPT_GUARD_DEFAULT;
BIND_SECTOR(::Sector::get());
return object.get_current_scale();
}

float
Camera::get_target_scale()
{
SCRIPT_GUARD_DEFAULT;
BIND_SECTOR(::Sector::get());
return object.get_target_scale();
}

void
Camera::set_scale(float scale)
{
ease_scale(scale, 0, "");
}

void
Camera::scale(float scale, float time)
{
ease_scale(scale, time, "");
}

void
Camera::ease_scale(float scale, float time, std::string ease)
{
SCRIPT_GUARD_VOID;
BIND_SECTOR(::Sector::get());
object.ease_scale(scale, time, getEasingByName(EasingMode_from_string(ease)));
}

} // namespace scripting

/* EOF */
10 changes: 10 additions & 0 deletions src/scripting/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class Camera final
void set_mode(const std::string& mode);
/** Scroll camera to position x,y in scrolltime seconds */
void scroll_to(float x, float y, float scrolltime);
/** Get the curent scale factor of the camera */
float get_current_scale();
/** Get the scale factor the camera is fading towards */
float get_target_scale();
/** Set the scale factor */
void set_scale(float scale);
/** Fade the scale factor over time */
void scale(float scale, float time);
/** Fade the scale factor over time with easing (smooth movement) */
void ease_scale(float scale, float time, std::string ease);
};

} // namespace scripting
Expand Down
1 change: 1 addition & 0 deletions src/scripting/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "scripting/sector.hpp"

#include "math/easing.hpp"
#include "object/ambient_light.hpp"
#include "object/music_object.hpp"
#include "supertux/sector.hpp"
Expand Down
Loading