Skip to content

Commit

Permalink
Added fading gradients through Squirrel (#1508)
Browse files Browse the repository at this point in the history
* Added fading gradients through Squirrel

* Fixed missing initializers for fading gradient properties

* I forgot one of the two constructors (Gradient fading Travis warnings)
  • Loading branch information
Semphriss committed Aug 16, 2020
1 parent 620de47 commit c7518a7
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 3 deletions.
56 changes: 53 additions & 3 deletions src/object/gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ Gradient::Gradient() :
m_gradient_bottom(),
m_gradient_direction(),
m_blend(),
m_target(DrawingTarget::COLORMAP)
m_target(DrawingTarget::COLORMAP),
m_start_gradient_top(),
m_start_gradient_bottom(),
m_fade_gradient_top(),
m_fade_gradient_bottom(),
m_fade_total_time(),
m_fade_time()
{
}

Expand All @@ -45,7 +51,13 @@ Gradient::Gradient(const ReaderMapping& reader) :
m_gradient_bottom(),
m_gradient_direction(),
m_blend(),
m_target(DrawingTarget::COLORMAP)
m_target(DrawingTarget::COLORMAP),
m_start_gradient_top(),
m_start_gradient_bottom(),
m_fade_gradient_top(),
m_fade_gradient_bottom(),
m_fade_total_time(),
m_fade_time()
{
m_layer = reader_get_layer (reader, LAYER_BACKGROUND0);
std::vector<float> bkgd_top_color, bkgd_bottom_color;
Expand Down Expand Up @@ -150,8 +162,35 @@ Gradient::~Gradient()
}

void
Gradient::update(float)
Gradient::update(float delta)
{
if (m_fade_time <= 0) return;

m_fade_time -= delta;
if (m_fade_time <= 0)
{
m_fade_time = 0;

m_gradient_top = m_fade_gradient_top;
m_gradient_bottom = m_fade_gradient_bottom;

return;
}

float progress = m_fade_time / m_fade_total_time;

m_gradient_top = Color(
m_fade_gradient_top.red + (m_start_gradient_top.red - m_fade_gradient_top.red) * progress,
m_fade_gradient_top.green + (m_start_gradient_top.green - m_fade_gradient_top.green) * progress,
m_fade_gradient_top.blue + (m_start_gradient_top.blue - m_fade_gradient_top.blue) * progress,
m_fade_gradient_top.alpha + (m_start_gradient_top.alpha - m_fade_gradient_top.alpha) * progress
);
m_gradient_bottom = Color(
m_fade_gradient_bottom.red + (m_start_gradient_bottom.red - m_fade_gradient_bottom.red) * progress,
m_fade_gradient_bottom.green + (m_start_gradient_bottom.green - m_fade_gradient_bottom.green) * progress,
m_fade_gradient_bottom.blue + (m_start_gradient_bottom.blue - m_fade_gradient_bottom.blue) * progress,
m_fade_gradient_bottom.alpha + (m_start_gradient_bottom.alpha - m_fade_gradient_bottom.alpha) * progress
);
}

void
Expand All @@ -177,6 +216,17 @@ Gradient::set_gradient(Color top, Color bottom)
}
}

void
Gradient::fade_gradient(Color top, Color bottom, float time)
{
m_start_gradient_top = m_gradient_top;
m_start_gradient_bottom = m_gradient_bottom;
m_fade_gradient_top = top;
m_fade_gradient_bottom = bottom;
m_fade_total_time = time;
m_fade_time = time;
}

void
Gradient::set_direction(const GradientDirection& direction)
{
Expand Down
8 changes: 8 additions & 0 deletions src/object/gradient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Gradient final :
virtual ObjectSettings get_settings() override;

void set_gradient(Color top, Color bottom);
void fade_gradient(Color top, Color bottom, float time);
Color get_gradient_top() const { return m_gradient_top; }
Color get_gradient_bottom() const { return m_gradient_bottom; }

Expand All @@ -67,6 +68,13 @@ class Gradient final :
private:
Gradient(const Gradient&) = delete;
Gradient& operator=(const Gradient&) = delete;

Color m_start_gradient_top;
Color m_start_gradient_bottom;
Color m_fade_gradient_top;
Color m_fade_gradient_bottom;
float m_fade_total_time;
float m_fade_time = 0;
};

#endif
Expand Down
28 changes: 28 additions & 0 deletions src/scripting/gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ Gradient::set_color2(float red, float green, float blue)
object.set_gradient(object.get_gradient_top(), Color(red, green, blue));
}

void
Gradient::set_colors(float red1, float green1, float blue1, float red2, float green2, float blue2)
{
SCRIPT_GUARD_VOID;
object.set_gradient(Color(red1, green1, blue1), Color(red2, green2, blue2));
}

void
Gradient::fade_color1(float red, float green, float blue, float time)
{
SCRIPT_GUARD_VOID;
object.fade_gradient(Color(red, green, blue), object.get_gradient_bottom(), time);
}

void
Gradient::fade_color2(float red, float green, float blue, float time)
{
SCRIPT_GUARD_VOID;
object.fade_gradient(object.get_gradient_top(), Color(red, green, blue), time);
}

void
Gradient::fade_colors(float red1, float green1, float blue1, float red2, float green2, float blue2, float time)
{
SCRIPT_GUARD_VOID;
object.fade_gradient(Color(red1, green1, blue1), Color(red2, green2, blue2), time);
}

void
Gradient::swap_colors()
{
Expand Down
4 changes: 4 additions & 0 deletions src/scripting/gradient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class Gradient final

void set_color1(float red, float green, float blue);
void set_color2(float red, float green, float blue);
void set_colors(float red1, float green1, float blue1, float red2, float green2, float blue2);
void fade_color1(float red, float green, float blue, float time);
void fade_color2(float red, float green, float blue, float time);
void fade_colors(float red1, float green1, float blue1, float red2, float green2, float blue2, float time);

void swap_colors();
};
Expand Down

0 comments on commit c7518a7

Please sign in to comment.