mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Cleanup image and entity references.
This commit is contained in:
parent
cfd970b6b5
commit
fb7f373866
@ -8,65 +8,13 @@
|
||||
namespace rl {
|
||||
namespace entity {
|
||||
|
||||
Entity::Entity(core::Name identifier, UpdateCallback updateCallback)
|
||||
: _identifier(identifier),
|
||||
_anchorPoint(geom::Point(0.5, 0.5)),
|
||||
_opacity(1.0),
|
||||
_strokeSize(1.0),
|
||||
_updateCallback(updateCallback) {}
|
||||
Entity::Entity()
|
||||
: _anchorPoint(geom::Point(0.5, 0.5)), _opacity(1.0), _strokeSize(1.0) {}
|
||||
|
||||
Entity::Entity(Entity&&) = default;
|
||||
|
||||
Entity::~Entity() = default;
|
||||
|
||||
void Entity::mergeProperties(const Entity& entity, PropertyMaskType only) {
|
||||
RL_ASSERT(_identifier == entity._identifier);
|
||||
|
||||
if (only & PropertyMask::BoundsMask) {
|
||||
_bounds = entity._bounds;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::PositionMask) {
|
||||
_position = entity._position;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::AnchorPointMask) {
|
||||
_anchorPoint = entity._anchorPoint;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::TransformationMask) {
|
||||
_transformation = entity._transformation;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::BackgroundColorMask) {
|
||||
_backgroundColor = entity._backgroundColor;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::OpacityMask) {
|
||||
_opacity = entity._opacity;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::StrokeSizeMask) {
|
||||
_strokeSize = entity._strokeSize;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::StrokeColorMask) {
|
||||
_strokeColor = entity._strokeColor;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::ContentsMask) {
|
||||
_contents = entity._contents;
|
||||
}
|
||||
|
||||
if (only & PropertyMask::PathMask) {
|
||||
_path = entity._path;
|
||||
}
|
||||
}
|
||||
|
||||
core::Name Entity::identifier() const {
|
||||
return _identifier;
|
||||
}
|
||||
|
||||
geom::Rect Entity::frame() const {
|
||||
geom::Point origin(_position.x - (_bounds.size.width * _anchorPoint.x),
|
||||
_position.y - (_bounds.size.height * _anchorPoint.y));
|
||||
@ -87,7 +35,6 @@ const geom::Rect& Entity::bounds() const {
|
||||
|
||||
void Entity::setBounds(const geom::Rect& bounds) {
|
||||
_bounds = bounds;
|
||||
notifyInterfaceIfNecessary(Property::Bounds);
|
||||
}
|
||||
|
||||
const geom::Point& Entity::position() const {
|
||||
@ -96,7 +43,6 @@ const geom::Point& Entity::position() const {
|
||||
|
||||
void Entity::setPosition(const geom::Point& position) {
|
||||
_position = position;
|
||||
notifyInterfaceIfNecessary(Property::Position);
|
||||
}
|
||||
|
||||
const geom::Point& Entity::anchorPoint() const {
|
||||
@ -105,7 +51,6 @@ const geom::Point& Entity::anchorPoint() const {
|
||||
|
||||
void Entity::setAnchorPoint(const geom::Point& anchorPoint) {
|
||||
_anchorPoint = anchorPoint;
|
||||
notifyInterfaceIfNecessary(Property::AnchorPoint);
|
||||
}
|
||||
|
||||
const geom::Matrix& Entity::transformation() const {
|
||||
@ -114,7 +59,6 @@ const geom::Matrix& Entity::transformation() const {
|
||||
|
||||
void Entity::setTransformation(const geom::Matrix& transformation) {
|
||||
_transformation = transformation;
|
||||
notifyInterfaceIfNecessary(Property::Transformation);
|
||||
}
|
||||
|
||||
geom::Matrix Entity::modelMatrix() const {
|
||||
@ -151,7 +95,6 @@ const Color& Entity::backgroundColor() const {
|
||||
|
||||
void Entity::setBackgroundColor(const Color& backgroundColor) {
|
||||
_backgroundColor = backgroundColor;
|
||||
notifyInterfaceIfNecessary(Property::BackgroundColor);
|
||||
}
|
||||
|
||||
const double& Entity::opacity() const {
|
||||
@ -160,7 +103,6 @@ const double& Entity::opacity() const {
|
||||
|
||||
void Entity::setOpacity(double opacity) {
|
||||
_opacity = opacity;
|
||||
notifyInterfaceIfNecessary(Property::Opacity);
|
||||
}
|
||||
|
||||
const Color& Entity::strokeColor() const {
|
||||
@ -169,7 +111,6 @@ const Color& Entity::strokeColor() const {
|
||||
|
||||
void Entity::setStrokeColor(const Color& strokeColor) {
|
||||
_strokeColor = strokeColor;
|
||||
notifyInterfaceIfNecessary(Property::StrokeColor);
|
||||
}
|
||||
|
||||
double Entity::strokeSize() const {
|
||||
@ -178,7 +119,6 @@ double Entity::strokeSize() const {
|
||||
|
||||
void Entity::setStrokeSize(double strokeSize) {
|
||||
_strokeSize = strokeSize;
|
||||
notifyInterfaceIfNecessary(Property::StrokeSize);
|
||||
}
|
||||
|
||||
const image::Image& Entity::contents() const {
|
||||
@ -187,7 +127,6 @@ const image::Image& Entity::contents() const {
|
||||
|
||||
void Entity::setContents(image::Image image) {
|
||||
_contents = std::move(image);
|
||||
notifyInterfaceIfNecessary(Property::Contents);
|
||||
}
|
||||
|
||||
const geom::Path& Entity::path() const {
|
||||
@ -196,104 +135,6 @@ const geom::Path& Entity::path() const {
|
||||
|
||||
void Entity::setPath(geom::Path path) {
|
||||
_path = std::move(path);
|
||||
notifyInterfaceIfNecessary(Property::Path);
|
||||
}
|
||||
|
||||
void Entity::notifyInterfaceIfNecessary(Property property,
|
||||
core::Name other) const {
|
||||
if (_updateCallback) {
|
||||
_updateCallback(*this, property, other);
|
||||
}
|
||||
}
|
||||
|
||||
enum ArchiveKey {
|
||||
Identifier,
|
||||
Bounds,
|
||||
Position,
|
||||
AnchorPoint,
|
||||
Transformation,
|
||||
BackgroundColor,
|
||||
Opacity,
|
||||
StrokeColor,
|
||||
StrokeSize,
|
||||
};
|
||||
|
||||
const core::ArchiveDef Entity::ArchiveDefinition = {
|
||||
/* .superClass = */ nullptr,
|
||||
/* .className = */ "Entity",
|
||||
/* .autoAssignName = */ false,
|
||||
/* .members = */
|
||||
{
|
||||
ArchiveKey::Identifier, //
|
||||
ArchiveKey::Bounds, //
|
||||
ArchiveKey::Position, //
|
||||
ArchiveKey::AnchorPoint, //
|
||||
ArchiveKey::Transformation, //
|
||||
ArchiveKey::BackgroundColor, //
|
||||
ArchiveKey::Opacity, //
|
||||
ArchiveKey::StrokeColor, //
|
||||
ArchiveKey::StrokeSize, //
|
||||
},
|
||||
};
|
||||
|
||||
Entity::ArchiveName Entity::archiveName() const {
|
||||
return *_identifier.handle();
|
||||
}
|
||||
|
||||
bool Entity::serialize(core::ArchiveItem& item) const {
|
||||
RL_RETURN_IF_FALSE(item.encode(ArchiveKey::Identifier, _identifier));
|
||||
|
||||
RL_RETURN_IF_FALSE(item.encode(ArchiveKey::Bounds, _bounds.toString()));
|
||||
|
||||
RL_RETURN_IF_FALSE(item.encode(ArchiveKey::Position, _position.toString()));
|
||||
|
||||
RL_RETURN_IF_FALSE(
|
||||
item.encode(ArchiveKey::AnchorPoint, _anchorPoint.toString()));
|
||||
|
||||
RL_RETURN_IF_FALSE(
|
||||
item.encode(ArchiveKey::Transformation, _transformation.toString()));
|
||||
|
||||
RL_RETURN_IF_FALSE(
|
||||
item.encode(ArchiveKey::BackgroundColor, _backgroundColor.toString()));
|
||||
|
||||
RL_RETURN_IF_FALSE(item.encode(ArchiveKey::Opacity, _opacity));
|
||||
|
||||
RL_RETURN_IF_FALSE(
|
||||
item.encode(ArchiveKey::StrokeColor, _strokeColor.toString()));
|
||||
|
||||
RL_RETURN_IF_FALSE(item.encode(ArchiveKey::StrokeSize, _strokeSize));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Entity::deserialize(core::ArchiveItem& item, core::Namespace* ns) {
|
||||
std::string decoded;
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Identifier, _identifier, ns));
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Bounds, decoded));
|
||||
_bounds.fromString(decoded);
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Position, decoded));
|
||||
_position.fromString(decoded);
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::AnchorPoint, decoded));
|
||||
_anchorPoint.fromString(decoded);
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Transformation, decoded));
|
||||
_transformation.fromString(decoded);
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::BackgroundColor, decoded));
|
||||
_backgroundColor.fromString(decoded);
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Opacity, _opacity));
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::StrokeColor, decoded));
|
||||
_strokeColor.fromString(decoded);
|
||||
|
||||
RL_RETURN_IF_FALSE(item.decode(ArchiveKey::StrokeSize, _strokeSize));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace entity
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
namespace rl {
|
||||
namespace entity {
|
||||
|
||||
class Entity : public core::ArchiveSerializable {
|
||||
class Entity {
|
||||
public:
|
||||
using PropertyMaskType = uint16_t;
|
||||
|
||||
@ -56,17 +56,11 @@ class Entity : public core::ArchiveSerializable {
|
||||
};
|
||||
#undef RL_MASK
|
||||
|
||||
using UpdateCallback = std::function<void(const Entity& /*entity*/,
|
||||
Entity::Property /*property*/,
|
||||
core::Name /*otherIdentifier*/)>;
|
||||
|
||||
Entity(core::Name identifier, UpdateCallback updateCallback = nullptr);
|
||||
Entity();
|
||||
|
||||
Entity(Entity&& entity);
|
||||
|
||||
virtual ~Entity();
|
||||
|
||||
core::Name identifier() const;
|
||||
~Entity();
|
||||
|
||||
/**
|
||||
* The frame specifies the origin and size of the entity in the coordinate
|
||||
@ -194,18 +188,7 @@ class Entity : public core::ArchiveSerializable {
|
||||
|
||||
void setPath(geom::Path path);
|
||||
|
||||
void mergeProperties(const Entity& entity, PropertyMaskType only);
|
||||
|
||||
static const core::ArchiveDef ArchiveDefinition;
|
||||
|
||||
ArchiveName archiveName() const override;
|
||||
|
||||
bool serialize(core::ArchiveItem& item) const override;
|
||||
|
||||
bool deserialize(core::ArchiveItem& item, core::Namespace* ns) override;
|
||||
|
||||
protected:
|
||||
core::Name _identifier;
|
||||
private:
|
||||
geom::Rect _bounds;
|
||||
geom::Point _position;
|
||||
geom::Point _anchorPoint;
|
||||
@ -217,13 +200,6 @@ class Entity : public core::ArchiveSerializable {
|
||||
Color _strokeColor;
|
||||
double _strokeSize;
|
||||
|
||||
void notifyInterfaceIfNecessary(
|
||||
Property property,
|
||||
core::Name identifier = core::Name() /* dead name */) const;
|
||||
|
||||
private:
|
||||
UpdateCallback _updateCallback;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Entity);
|
||||
};
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ namespace image {
|
||||
|
||||
Image::Image() = default;
|
||||
|
||||
Image::Image(core::Allocation sourceAllocation)
|
||||
Image::Image(fml::RefPtr<const fml::Mapping> sourceAllocation)
|
||||
: _source(ImageSource::Create(std::move(sourceAllocation))) {}
|
||||
|
||||
Image::Image(core::FileHandle sourceFile)
|
||||
@ -20,45 +20,6 @@ Image::Image(core::FileHandle sourceFile)
|
||||
|
||||
Image::~Image() = default;
|
||||
|
||||
bool Image::serialize(core::Message& message) const {
|
||||
if (_source == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Encode the type.
|
||||
*/
|
||||
RL_RETURN_IF_FALSE(message.encode(_source->type()));
|
||||
|
||||
/*
|
||||
* Encode the image source.
|
||||
*/
|
||||
RL_RETURN_IF_FALSE(_source->serialize(message));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Image::deserialize(core::Message& message, core::Namespace* ns) {
|
||||
auto type = ImageSource::Type::Unknown;
|
||||
|
||||
/*
|
||||
* Decode the type.
|
||||
*/
|
||||
RL_RETURN_IF_FALSE(message.decode(type, ns))
|
||||
|
||||
/*
|
||||
* Create and decode the image source of that type.
|
||||
*/
|
||||
auto source = ImageSource::ImageSourceForType(type);
|
||||
if (source == nullptr) {
|
||||
return false;
|
||||
}
|
||||
RL_RETURN_IF_FALSE(source->deserialize(message, ns));
|
||||
_source = source;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ImageResult Image::decode() const {
|
||||
if (_source == nullptr) {
|
||||
return {};
|
||||
@ -83,8 +44,8 @@ ImageResult Image::decode() const {
|
||||
&comps, // Out: Components
|
||||
STBI_default);
|
||||
|
||||
auto destinationAllocation =
|
||||
core::Allocation{decoded, width * height * comps * sizeof(stbi_uc)};
|
||||
auto destinationAllocation = fml::RefPtr<const fml::Mapping>{
|
||||
decoded, width * height * comps * sizeof(stbi_uc)};
|
||||
|
||||
/*
|
||||
* If either the decoded allocation is null or the size works out to be zero,
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
#include "ImageResult.h"
|
||||
#include "Size.h"
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/fml/mapping.h"
|
||||
|
||||
namespace rl {
|
||||
namespace image {
|
||||
@ -17,9 +19,7 @@ class Image {
|
||||
public:
|
||||
Image();
|
||||
|
||||
Image(core::Allocation sourceAllocation);
|
||||
|
||||
Image(core::FileHandle sourceFile);
|
||||
Image(fml::RefPtr<const fml::Mapping> sourceAllocation);
|
||||
|
||||
~Image();
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ namespace image {
|
||||
|
||||
ImageResult::ImageResult(geom::Size size,
|
||||
Components components,
|
||||
core::Allocation allocation)
|
||||
fml::RefPtr<const fml::Mapping> allocation)
|
||||
: _success(true),
|
||||
_size(size),
|
||||
_components(components),
|
||||
@ -47,7 +47,7 @@ ImageResult::Components ImageResult::components() const {
|
||||
return _components;
|
||||
}
|
||||
|
||||
const core::Allocation& ImageResult::allocation() const {
|
||||
const fml::RefPtr<const fml::Mapping>& ImageResult::allocation() const {
|
||||
return _allocation;
|
||||
}
|
||||
|
||||
|
||||
@ -6,6 +6,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "Size.h"
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/fml/mapping.h"
|
||||
#include "flutter/fml/memory/ref_counted.h"
|
||||
|
||||
namespace rl {
|
||||
namespace image {
|
||||
@ -24,7 +27,7 @@ class ImageResult {
|
||||
|
||||
ImageResult(geom::Size size,
|
||||
Components components,
|
||||
core::Allocation allocation);
|
||||
fml::RefPtr<const fml::Mapping> allocation);
|
||||
|
||||
ImageResult(ImageResult&&);
|
||||
|
||||
@ -38,13 +41,13 @@ class ImageResult {
|
||||
|
||||
Components components() const;
|
||||
|
||||
const core::Allocation& allocation() const;
|
||||
const fml::RefPtr<const fml::Mapping>& allocation() const;
|
||||
|
||||
private:
|
||||
bool _success;
|
||||
geom::Size _size;
|
||||
Components _components;
|
||||
core::Allocation _allocation;
|
||||
fml::RefPtr<const fml::Mapping> _allocation;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ImageResult);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user