Skip to content

Commit

Permalink
feat(UI): Rotate interface images (#9943)
Browse files Browse the repository at this point in the history
  • Loading branch information
warp-core committed Mar 24, 2024
1 parent d380112 commit 59cfe45
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions source/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,14 @@ void Interface::ImageElement::Draw(const Rectangle &rect, const Information &inf
return;

float frame = info.GetSpriteFrame(name);
Point unit = info.GetSpriteUnit(name);
if(isOutline)
{
Color color = (isColored ? info.GetOutlineColor() : Color(1.f, 1.f));
Point unit = info.GetSpriteUnit(name);
OutlineShader::Draw(sprite, rect.Center(), rect.Dimensions(), color, unit, frame);
}
else
SpriteShader::Draw(sprite, rect.Center(), rect.Width() / sprite->Width(), 0, frame);
SpriteShader::Draw(sprite, rect.Center(), rect.Width() / sprite->Width(), 0, frame, unit);
}


Expand Down
19 changes: 12 additions & 7 deletions source/SpriteShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ this program. If not, see <https://www.gnu.org/licenses/>.

#include "SpriteShader.h"

#include "Point.h"
#include "Screen.h"
#include "Shader.h"
#include "Sprite.h"
Expand Down Expand Up @@ -293,20 +292,21 @@ void SpriteShader::Init()



void SpriteShader::Draw(const Sprite *sprite, const Point &position, float zoom, int swizzle, float frame)
void SpriteShader::Draw(const Sprite *sprite, const Point &position,
float zoom, int swizzle, float frame, const Point &unit)
{
if(!sprite)
return;

Bind();
Add(Prepare(sprite, position, zoom, swizzle, frame));
Add(Prepare(sprite, position, zoom, swizzle, frame, unit));
Unbind();
}



SpriteShader::Item SpriteShader::Prepare(const Sprite *sprite, const Point &position,
float zoom, int swizzle, float frame)
float zoom, int swizzle, float frame, const Point &unit)
{
if(!sprite)
return {};
Expand All @@ -319,9 +319,14 @@ SpriteShader::Item SpriteShader::Prepare(const Sprite *sprite, const Point &posi
// Position.
item.position[0] = static_cast<float>(position.X());
item.position[1] = static_cast<float>(position.Y());
// Rotation (none) and scale.
item.transform[0] = sprite->Width() * zoom;
item.transform[3] = sprite->Height() * zoom;
// Rotation and scale.
Point scaledUnit = unit * zoom;
Point uw = scaledUnit * sprite->Width();
Point uh = scaledUnit * sprite->Height();
item.transform[0] = static_cast<float>(-uw.Y());
item.transform[1] = static_cast<float>(uw.X());
item.transform[2] = static_cast<float>(-uh.X());
item.transform[3] = static_cast<float>(-uh.Y());
// Swizzle.
item.swizzle = swizzle;

Expand Down
11 changes: 6 additions & 5 deletions source/SpriteShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef SPRITE_SHADER_H_
#define SPRITE_SHADER_H_

#include "Point.h"

class Sprite;
class Point;

#include <cstdint>

Expand Down Expand Up @@ -50,10 +51,10 @@ class SpriteShader {
static void Init();

// Draw a sprite.
static void Draw(const Sprite *sprite, const Point &position, float zoom = 1.f, int swizzle = 0,
float frame = 0.f);
static Item Prepare(const Sprite *sprite, const Point &position, float zoom = 1.f, int swizzle = 0,
float frame = 0.f);
static void Draw(const Sprite *sprite, const Point &position, float zoom = 1.f,
int swizzle = 0, float frame = 0.f, const Point &unit = Point(0., -1.));
static Item Prepare(const Sprite *sprite, const Point &position, float zoom = 1.f,
int swizzle = 0, float frame = 0.f, const Point &unit = Point(0., -1.));

static void Bind();
static void Add(const Item &item, bool withBlur = false);
Expand Down

0 comments on commit 59cfe45

Please sign in to comment.