Skip to content

Commit

Permalink
Add originX and originY support to images
Browse files Browse the repository at this point in the history
Add fields for modifying origin x/y to images. Add support for images to freeze. Hopefully this is the right approach.

Editor:

https://github.com/rive-app/rive/assets/186340/838b2c69-bd25-4c55-85ab-611e423c8983

CPP:

https://github.com/rive-app/rive/assets/186340/8e7d76db-d0b5-4fdb-9ea6-a6d921fee8ef

Diffs=
fcccdeccd Add originX and originY support to images (#5624)

Co-authored-by: Philip Chung <philterdesign@gmail.com>
  • Loading branch information
philter and philter committed Jul 28, 2023
1 parent 5ec5d93 commit 7ce350a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5b2a52f44ebf18e44b960202844d56424b777aa3
fcccdeccddd2b9c47992f131f9ba741bccee5da8
20 changes: 20 additions & 0 deletions dev/defs/shapes/image.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@
"string": "assetid"
},
"description": "Image drawable for an image asset"
},
"originX": {
"type": "double",
"initialValue": "0.5",
"animates": true,
"key": {
"int": 380,
"string": "originx"
},
"description": "Origin x in normalized coordinates (0.5 = center, 0 = left, 1 = right)."
},
"originY": {
"type": "double",
"initialValue": "0.5",
"animates": true,
"key": {
"int": 381,
"string": "originy"
},
"description": "Origin y in normalized coordinates (0.5 = center, 0 = top, 1 = bottom)."
}
}
}
12 changes: 12 additions & 0 deletions include/rive/generated/core_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,12 @@ class CoreRegistry
case StarBase::innerRadiusPropertyKey:
object->as<StarBase>()->innerRadius(value);
break;
case ImageBase::originXPropertyKey:
object->as<ImageBase>()->originX(value);
break;
case ImageBase::originYPropertyKey:
object->as<ImageBase>()->originY(value);
break;
case CubicDetachedVertexBase::inRotationPropertyKey:
object->as<CubicDetachedVertexBase>()->inRotation(value);
break;
Expand Down Expand Up @@ -1421,6 +1427,10 @@ class CoreRegistry
return object->as<PolygonBase>()->cornerRadius();
case StarBase::innerRadiusPropertyKey:
return object->as<StarBase>()->innerRadius();
case ImageBase::originXPropertyKey:
return object->as<ImageBase>()->originX();
case ImageBase::originYPropertyKey:
return object->as<ImageBase>()->originY();
case CubicDetachedVertexBase::inRotationPropertyKey:
return object->as<CubicDetachedVertexBase>()->inRotation();
case CubicDetachedVertexBase::inDistancePropertyKey:
Expand Down Expand Up @@ -1768,6 +1778,8 @@ class CoreRegistry
case CubicMirroredVertexBase::distancePropertyKey:
case PolygonBase::cornerRadiusPropertyKey:
case StarBase::innerRadiusPropertyKey:
case ImageBase::originXPropertyKey:
case ImageBase::originYPropertyKey:
case CubicDetachedVertexBase::inRotationPropertyKey:
case CubicDetachedVertexBase::inDistancePropertyKey:
case CubicDetachedVertexBase::outRotationPropertyKey:
Expand Down
37 changes: 37 additions & 0 deletions include/rive/generated/shapes/image_base.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef _RIVE_IMAGE_BASE_HPP_
#define _RIVE_IMAGE_BASE_HPP_
#include "rive/core/field_types/core_double_type.hpp"
#include "rive/core/field_types/core_uint_type.hpp"
#include "rive/drawable.hpp"
namespace rive
Expand Down Expand Up @@ -34,9 +35,13 @@ class ImageBase : public Drawable
uint16_t coreType() const override { return typeKey; }

static const uint16_t assetIdPropertyKey = 206;
static const uint16_t originXPropertyKey = 380;
static const uint16_t originYPropertyKey = 381;

private:
uint32_t m_AssetId = -1;
float m_OriginX = 0.5f;
float m_OriginY = 0.5f;

public:
inline uint32_t assetId() const { return m_AssetId; }
Expand All @@ -50,10 +55,34 @@ class ImageBase : public Drawable
assetIdChanged();
}

inline float originX() const { return m_OriginX; }
void originX(float value)
{
if (m_OriginX == value)
{
return;
}
m_OriginX = value;
originXChanged();
}

inline float originY() const { return m_OriginY; }
void originY(float value)
{
if (m_OriginY == value)
{
return;
}
m_OriginY = value;
originYChanged();
}

Core* clone() const override;
void copy(const ImageBase& object)
{
m_AssetId = object.m_AssetId;
m_OriginX = object.m_OriginX;
m_OriginY = object.m_OriginY;
Drawable::copy(object);
}

Expand All @@ -64,12 +93,20 @@ class ImageBase : public Drawable
case assetIdPropertyKey:
m_AssetId = CoreUintType::deserialize(reader);
return true;
case originXPropertyKey:
m_OriginX = CoreDoubleType::deserialize(reader);
return true;
case originYPropertyKey:
m_OriginY = CoreDoubleType::deserialize(reader);
return true;
}
return Drawable::deserialize(propertyKey, reader);
}

protected:
virtual void assetIdChanged() {}
virtual void originXChanged() {}
virtual void originYChanged() {}
};
} // namespace rive

Expand Down
5 changes: 3 additions & 2 deletions src/shapes/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void Image::draw(Renderer* renderer)
else
{
renderer->transform(worldTransform());
renderer->translate(-width / 2.0f, -height / 2.0f);
renderer->translate(-width * originX(), -height * originY());
renderer->drawImage(renderImage, blendMode(), renderOpacity());
}

Expand All @@ -57,7 +57,8 @@ Core* Image::hitTest(HitInfo* hinfo, const Mat2D& xform)
}
else
{
auto mx = xform * worldTransform() * Mat2D::fromTranslate(-width * 0.5f, -height * 0.5f);
auto mx = xform * worldTransform() *
Mat2D::fromTranslate(-width * originX(), -height * originY());
HitTester tester(hinfo->area);
tester.addRect(AABB(0, 0, (float)width, (float)height), mx);
if (tester.test())
Expand Down

0 comments on commit 7ce350a

Please sign in to comment.