Skip to content

Commit

Permalink
Merge pull request #298 from Laguna1989/feature/DrawableOutlines
Browse files Browse the repository at this point in the history
Add Outline functionality to all Drawables. Fix Rotation orientation
  • Loading branch information
Laguna1989 authored Nov 1, 2023
2 parents e000fc7 + fef25c3 commit 9242fa4
Show file tree
Hide file tree
Showing 37 changed files with 583 additions and 90 deletions.
49 changes: 36 additions & 13 deletions impl/jamtemplate/common/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void jt::Animation::add(std::string const& fileName, std::string const& animName
for (auto const idx : frameIndices) {
jt::Recti const rect { static_cast<int>(idx * imageSize.x), 0,
static_cast<int>(imageSize.x), static_cast<int>(imageSize.y) };
Sprite::Sptr sptr = std::make_shared<Sprite>(fileName, rect, textureManager);
auto sptr = std::make_shared<Sprite>(fileName, rect, textureManager);
m_frames[animName].push_back(sptr);
}
m_time[animName] = frameTimesInSeconds;
Expand Down Expand Up @@ -226,12 +226,14 @@ jt::Color jt::Animation::getColor() const
}

void jt::Animation::setPosition(jt::Vector2f const& pos) { m_position = pos; }

jt::Vector2f jt::Animation::getPosition() const { return m_position; }

jt::Rectf jt::Animation::getGlobalBounds() const
{
return getCurrentSprite(m_frames, m_currentAnimName, m_currentIdx)->getGlobalBounds();
}

jt::Rectf jt::Animation::getLocalBounds() const
{
return getCurrentSprite(m_frames, m_currentAnimName, m_currentIdx)->getLocalBounds();
Expand All @@ -245,6 +247,7 @@ void jt::Animation::setScale(jt::Vector2f const& scale)
}
}
}

jt::Vector2f jt::Animation::getScale() const
{
return getCurrentSprite(m_frames, m_currentAnimName, m_currentIdx)->getScale();
Expand All @@ -259,12 +262,22 @@ void jt::Animation::setOriginInternal(jt::Vector2f const& origin)
}
}

void jt::Animation::setShadow(jt::Color const& col, jt::Vector2f const& offset)
void jt::Animation::setOutline(jt::Color const& color, int width)
{
DrawableImpl::setShadow(col, offset);
DrawableImpl::setOutline(color, width);
for (auto const& kvp : m_frames) {
for (auto const& sptr : kvp.second) {
sptr->setShadow(col, offset);
for (auto const& sprite : kvp.second) {
sprite->setOutline(color, width);
}
}
}

void jt::Animation::setShadow(jt::Color const& color, jt::Vector2f const& offset)
{
DrawableImpl::setShadow(color, offset);
for (auto const& kvp : m_frames) {
for (auto const& sprite : kvp.second) {
sprite->setShadow(color, offset);
}
}
}
Expand All @@ -273,14 +286,16 @@ void jt::Animation::setShadowActive(bool active)
{
DrawableImpl::setShadowActive(active);
for (auto const& kvp : m_frames) {
for (auto const& sptr : kvp.second) {
sptr->setShadowActive(active);
for (auto const& sprite : kvp.second) {
sprite->setShadowActive(active);
}
}
}

void jt::Animation::doDrawShadow(std::shared_ptr<jt::RenderTargetLayer> const /*sptr*/) const { }

void jt::Animation::doDrawOutline(std::shared_ptr<jt::RenderTargetLayer> const /*sptr*/) const { }

void jt::Animation::doDraw(std::shared_ptr<jt::RenderTargetLayer> const sptr) const
{
if (!m_isValid) {
Expand All @@ -297,8 +312,8 @@ void jt::Animation::doDrawFlash(std::shared_ptr<jt::RenderTargetLayer> const /*s
void jt::Animation::doFlashImpl(float t, jt::Color col)
{
for (auto& kvp : m_frames) {
for (auto& spr : kvp.second) {
spr->flash(t, col);
for (auto& sprite : kvp.second) {
sprite->flash(t, col);
}
}
}
Expand Down Expand Up @@ -333,32 +348,36 @@ void jt::Animation::doUpdate(float elapsed)

// update all sprites
for (auto& kvp : m_frames) {
for (auto& spr : kvp.second) {
spr->update(elapsed);
for (auto& sprite : kvp.second) {
sprite->update(elapsed);
}
}
}

void jt::Animation::doRotate(float rot)
{
for (auto& kvp : m_frames) {
for (auto& spr : kvp.second) {
spr->setRotation(rot);
for (auto& sprite : kvp.second) {
sprite->setRotation(rot);
}
}
}

float jt::Animation::getCurrentAnimationSingleFrameTime() const
{
return m_time.at(m_currentAnimName).at(m_currentIdx);
}

float jt::Animation::getCurrentAnimTotalTime() const
{
return getCurrentAnimationSingleFrameTime() * getNumberOfFramesInCurrentAnimation();
}

std::size_t jt::Animation::getNumberOfFramesInCurrentAnimation() const
{
return m_frames.at(m_currentAnimName).size();
}

std::string jt::Animation::getCurrentAnimationName() const { return m_currentAnimName; }

bool jt::Animation::getIsLooping() const
Expand All @@ -368,13 +387,15 @@ bool jt::Animation::getIsLooping() const
}
return m_isLooping.at(m_currentAnimName);
}

void jt::Animation::setLooping(std::string const& animName, bool isLooping)
{
if (!hasAnimation(animName)) {
throw std::invalid_argument { "invalid animation name: " + animName };
}
m_isLooping[animName] = isLooping;
}

std::size_t jt::Animation::getCurrentAnimationFrameIndex() const { return m_currentIdx; }

void jt::Animation::setFrameTimes(
Expand All @@ -389,5 +410,7 @@ void jt::Animation::setFrameTimes(
}
m_time[animationName] = frameTimes;
}

void jt::Animation::setAnimationSpeedFactor(float factor) { m_animationplaybackSpeed = factor; }

float jt::Animation::getAnimationSpeedFactor() const { return m_animationplaybackSpeed; }
3 changes: 3 additions & 0 deletions impl/jamtemplate/common/animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class Animation : public DrawableImpl {
void setShadowActive(bool active) override;
void setShadow(jt::Color const& color, jt::Vector2f const& offset) override;

void setOutline(jt::Color const& color, int width) override;

/// Get the frame time for one single frame in the current animation
///
/// \return the time set in add for the currently playing animation
Expand Down Expand Up @@ -158,6 +160,7 @@ class Animation : public DrawableImpl {
float m_animationplaybackSpeed { 1.0f };

void doDrawShadow(std::shared_ptr<jt::RenderTargetLayer> const sptr) const override;
void doDrawOutline(std::shared_ptr<jt::RenderTargetLayer> const sptr) const override;
void doDraw(std::shared_ptr<jt::RenderTargetLayer> const sptr) const override;
void doDrawFlash(std::shared_ptr<jt::RenderTargetLayer> const /*sptr*/) const override;

Expand Down
34 changes: 34 additions & 0 deletions impl/jamtemplate/common/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jt::Bar::Bar(
}

void jt::Bar::setFrontColor(jt::Color const& col) { m_shapeProgress->setColor(col); }

void jt::Bar::setBackColor(jt::Color const& col) { m_shapeFull->setColor(col); }

jt::Color jt::Bar::getBackColor() const { return m_shapeFull->getColor(); }
Expand Down Expand Up @@ -66,19 +67,44 @@ void jt::Bar::doDraw(std::shared_ptr<jt::RenderTargetLayer> const sptr) const
}

void jt::Bar::doDrawFlash(std::shared_ptr<jt::RenderTargetLayer> const /*sptr*/) const { }

void jt::Bar::doDrawShadow(std::shared_ptr<jt::RenderTargetLayer> const sptr) const
{
jt::Vector2f const oldPos = m_shapeFull->getPosition();
jt::Color const oldCol = m_shapeFull->getColor();

m_shapeFull->setPosition(oldPos + getShadowOffset());
m_shapeFull->setColor(getShadowColor());
m_shapeFull->update(0.0f);
m_shapeFull->draw(sptr);

m_shapeFull->setPosition(oldPos);
m_shapeFull->setColor(oldCol);
}

void jt::Bar::doDrawOutline(std::shared_ptr<jt::RenderTargetLayer> const sptr) const
{
jt::Vector2f const oldPos = m_shapeFull->getPosition();
jt::Color const oldCol = m_shapeFull->getColor();

m_shapeFull->setColor(getOutlineColor());

auto const maxWidth = getOutlineWidth();
for (auto currentWidth = 1; currentWidth != maxWidth + 1; ++currentWidth) {
for (auto i = -currentWidth; i != currentWidth; ++i) {
for (auto j = -currentWidth; j != currentWidth; ++j) {
m_shapeFull->setPosition(
oldPos + jt::Vector2f { static_cast<float>(i), static_cast<float>(j) });
m_shapeFull->update(0.0f);
m_shapeFull->draw(sptr);
}
}
}

m_shapeFull->setPosition(oldPos);
m_shapeFull->setColor(oldCol);
}

void jt::Bar::doUpdate(float elapsed)
{
float const value = static_cast<float>(m_valueCurrent) / m_valueMax;
Expand All @@ -101,13 +127,15 @@ void jt::Bar::doUpdate(float elapsed)
void jt::Bar::doRotate(float /*rot*/) { }

void jt::Bar::setColor(jt::Color const& col) { setFrontColor(col); }

jt::Color jt::Bar::getColor() const { return m_shapeProgress->getColor(); }

void jt::Bar::setPosition(jt::Vector2f const& pos) { m_position = pos; }

jt::Vector2f jt::Bar::getPosition() const { return m_position; }

jt::Rectf jt::Bar::getGlobalBounds() const { return m_shapeFull->getGlobalBounds(); }

jt::Rectf jt::Bar::getLocalBounds() const { return m_shapeFull->getLocalBounds(); }

void jt::Bar::setScale(jt::Vector2f const& scale)
Expand All @@ -129,3 +157,9 @@ void jt::Bar::setShadow(jt::Color const& color, jt::Vector2f const& offset)
m_shapeFull->setShadow(color, offset);
DrawableImpl::setShadow(color, offset);
}

void jt::Bar::setOutline(jt::Color const& color, int width)
{
m_shapeFull->setOutline(color, width);
DrawableImpl::setOutline(color, width);
}
3 changes: 3 additions & 0 deletions impl/jamtemplate/common/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Bar : public jt::DrawableImpl {

void setShadowActive(bool active) override;
void setShadow(jt::Color const& color, jt::Vector2f const& offset) override;
void setOutline(jt::Color const& color, int width) override;

private:
float m_valueMax;
Expand All @@ -84,6 +85,8 @@ class Bar : public jt::DrawableImpl {
virtual void doDrawFlash(std::shared_ptr<jt::RenderTargetLayer> const sptr) const override;
virtual void doDrawShadow(std::shared_ptr<jt::RenderTargetLayer> const sptr) const override;

void doDrawOutline(std::shared_ptr<jt::RenderTargetLayer> const sptr) const override;

// overwrite this method:
// things to take care of:
// - make sure flash object and normal object are at the same position
Expand Down
4 changes: 4 additions & 0 deletions impl/jamtemplate/common/color/palette_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ PaletteBuilder& PaletteBuilder::addGradientH(
m_colors.insert(m_colors.end(), colors.cbegin(), colors.cend());
return *this;
}

PaletteBuilder& PaletteBuilder::addGradientS(
float h, float smin, float smax, float v, std::size_t steps)
{
Expand All @@ -74,6 +75,7 @@ PaletteBuilder& PaletteBuilder::addGradientS(
m_colors.insert(m_colors.end(), colors.cbegin(), colors.cend());
return *this;
}

PaletteBuilder& PaletteBuilder::addGradientV(
float h, float s, float vmin, float vmax, std::size_t steps)
{
Expand All @@ -87,6 +89,7 @@ PaletteBuilder& PaletteBuilder::addGradientV(
m_colors.insert(m_colors.end(), colors.cbegin(), colors.cend());
return *this;
}

PaletteBuilder& PaletteBuilder::addColor(jt::Color const& col)
{
m_colors.push_back(col);
Expand Down Expand Up @@ -116,6 +119,7 @@ PaletteBuilder& PaletteBuilder::makeUnique()
SystemHelper::remove_duplicates(m_colors.begin(), m_colors.end()), m_colors.end());
return *this;
}

PaletteBuilder& PaletteBuilder::addColorsFromAseprite(std::string const& filepath)
{
aselib::AsepriteData ase { filepath };
Expand Down
Loading

0 comments on commit 9242fa4

Please sign in to comment.