代码优化

This commit is contained in:
Nomango 2018-08-23 16:58:32 +08:00
parent 4540520acc
commit a370685dd0
11 changed files with 143 additions and 143 deletions

View File

@ -207,7 +207,7 @@ void e2d::Renderer::endDraw()
e2d::Color e2d::Renderer::getBackgroundColor()
{
return Color(_clearColor.r, _clearColor.g, _clearColor.b, _clearColor.a);
return _clearColor;
}
void e2d::Renderer::setBackgroundColor(Color color)

View File

@ -279,7 +279,7 @@ bool e2d::Image::preload(const Resource& res)
bool e2d::Image::preload(const String & fileName)
{
String actualFilePath = File(fileName).getFilePath();
String actualFilePath = File(fileName).getPath();
if (actualFilePath.isEmpty())
return false;

View File

@ -9,7 +9,7 @@
if (Old) this->removeChild(Old); \
if (New) \
{ \
New->setPivot(_pivotX, _pivotY); \
New->setAnchor(_anchorX, _anchorY); \
this->addChild(New); \
} \
Old = New; \
@ -134,13 +134,13 @@ void e2d::Button::setClickFunc(const Function& func)
_func = func;
}
void e2d::Button::setPivot(float pivotX, float pivotY)
void e2d::Button::setAnchor(float anchorX, float anchorY)
{
Node::setPivot(pivotX, pivotY);
SAFE_SET(_normal, setPivot, pivotX, pivotY);
SAFE_SET(_mouseover, setPivot, pivotX, pivotY);
SAFE_SET(_selected, setPivot, pivotX, pivotY);
SAFE_SET(_disabled, setPivot, pivotX, pivotY);
Node::setAnchor(anchorX, anchorY);
SAFE_SET(_normal, setAnchor, anchorX, anchorY);
SAFE_SET(_mouseover, setAnchor, anchorX, anchorY);
SAFE_SET(_selected, setAnchor, anchorX, anchorY);
SAFE_SET(_disabled, setAnchor, anchorX, anchorY);
}
bool e2d::Button::dispatch(const MouseEvent & e, bool handled)

View File

@ -71,14 +71,14 @@ void e2d::Canvas::setStrokeStyle(Stroke strokeStyle)
}
}
const e2d::Color & e2d::Canvas::getLineColor() const
e2d::Color e2d::Canvas::getLineColor() const
{
return Color(_lineBrush->GetColor());
return _lineBrush->GetColor();
}
const e2d::Color & e2d::Canvas::getFillColor() const
e2d::Color e2d::Canvas::getFillColor() const
{
return Color(_fillBrush->GetColor());
return _fillBrush->GetColor();
}
float e2d::Canvas::getStrokeWidth() const

View File

@ -12,8 +12,8 @@ e2d::Node::Property e2d::Node::Property::operator+(Property const & prop) const
result.posY = this->posY + prop.posY;
result.width = this->width + prop.width;
result.height = this->height + prop.height;
result.pivotX = this->pivotX + prop.pivotX;
result.pivotY = this->pivotY + prop.pivotY;
result.anchorX = this->anchorX + prop.anchorX;
result.anchorY = this->anchorY + prop.anchorY;
result.scaleX = this->scaleX + prop.scaleX;
result.scaleY = this->scaleY + prop.scaleY;
result.rotation = this->rotation + prop.rotation;
@ -29,8 +29,8 @@ e2d::Node::Property e2d::Node::Property::operator-(Property const & prop) const
result.posY = this->posY - prop.posY;
result.width = this->width - prop.width;
result.height = this->height - prop.height;
result.pivotX = this->pivotX - prop.pivotX;
result.pivotY = this->pivotY - prop.pivotY;
result.anchorX = this->anchorX - prop.anchorX;
result.anchorY = this->anchorY - prop.anchorY;
result.scaleX = this->scaleX - prop.scaleX;
result.scaleY = this->scaleY - prop.scaleY;
result.rotation = this->rotation - prop.rotation;
@ -53,8 +53,8 @@ e2d::Node::Node()
, _skewAngleY(0)
, _displayOpacity(1.f)
, _realOpacity(1.f)
, _pivotX(0.f)
, _pivotY(0.f)
, _anchorX(0.f)
, _anchorY(0.f)
, _initialMatri(D2D1::Matrix3x2F::Identity())
, _finalMatri(D2D1::Matrix3x2F::Identity())
, _visible(true)
@ -197,26 +197,26 @@ void e2d::Node::_updateTransform()
_needTransform = false;
// 计算中心点坐标
D2D1_POINT_2F pivot = { _width * _pivotX, _height * _pivotY };
// 计算点坐标
D2D1_POINT_2F anchor = { _width * _anchorX, _height * _anchorY };
// 变换 Initial 矩阵,子节点将根据这个矩阵进行变换
_initialMatri = D2D1::Matrix3x2F::Scale(
_scaleX,
_scaleY,
pivot
anchor
) * D2D1::Matrix3x2F::Skew(
_skewAngleX,
_skewAngleY,
pivot
anchor
) * D2D1::Matrix3x2F::Rotation(
_rotation,
pivot
anchor
) * D2D1::Matrix3x2F::Translation(
_posX,
_posY
);
// 根据自身中心点变换 Final 矩阵
_finalMatri = _initialMatri * D2D1::Matrix3x2F::Translation(-pivot.x, -pivot.y);
// 根据自身点变换 Final 矩阵
_finalMatri = _initialMatri * D2D1::Matrix3x2F::Translation(-anchor.x, -anchor.y);
// 和父节点矩阵相乘
if (!_positionFixed && _parent)
{
@ -302,7 +302,7 @@ bool e2d::Node::isVisible() const
return _visible;
}
e2d::String e2d::Node::getName() const
const e2d::String& e2d::Node::getName() const
{
return _name;
}
@ -352,14 +352,14 @@ e2d::Size e2d::Node::getRealSize() const
return Size(_width, _height);
}
float e2d::Node::getPivotX() const
float e2d::Node::getAnchorX() const
{
return _pivotX;
return _anchorX;
}
float e2d::Node::getPivotY() const
float e2d::Node::getAnchorY() const
{
return _pivotY;
return _anchorY;
}
e2d::Size e2d::Node::getSize() const
@ -404,8 +404,8 @@ e2d::Node::Property e2d::Node::getProperty() const
prop.posY = _posY;
prop.width = _width;
prop.height = _height;
prop.pivotX = _pivotX;
prop.pivotY = _pivotY;
prop.anchorX = _anchorX;
prop.anchorY = _anchorY;
prop.scaleX = _scaleX;
prop.scaleY = _scaleY;
prop.rotation = _rotation;
@ -559,23 +559,23 @@ void e2d::Node::setOpacity(float opacity)
_updateOpacity();
}
void e2d::Node::setPivotX(float pivotX)
void e2d::Node::setAnchorX(float anchorX)
{
this->setPivot(pivotX, _pivotY);
this->setAnchor(anchorX, _anchorY);
}
void e2d::Node::setPivotY(float pivotY)
void e2d::Node::setAnchorY(float anchorY)
{
this->setPivot(_pivotX, pivotY);
this->setAnchor(_anchorX, anchorY);
}
void e2d::Node::setPivot(float pivotX, float pivotY)
void e2d::Node::setAnchor(float anchorX, float anchorY)
{
if (_pivotX == pivotX && _pivotY == pivotY)
if (_anchorX == anchorX && _anchorY == anchorY)
return;
_pivotX = std::min(std::max(pivotX, 0.f), 1.f);
_pivotY = std::min(std::max(pivotY, 0.f), 1.f);
_anchorX = std::min(std::max(anchorX, 0.f), 1.f);
_anchorY = std::min(std::max(anchorY, 0.f), 1.f);
_needTransform = true;
}
@ -608,7 +608,7 @@ void e2d::Node::setProperty(Property prop)
{
this->setPos(prop.posX, prop.posY);
this->setSize(prop.width, prop.height);
this->setPivot(prop.pivotX, prop.pivotY);
this->setAnchor(prop.anchorX, prop.anchorY);
this->setScale(prop.scaleX, prop.scaleY);
this->setRotation(prop.rotation);
this->setSkew(prop.skewAngleX, prop.skewAngleY);

View File

@ -74,22 +74,22 @@ e2d::Text::~Text()
SafeRelease(_textLayout);
}
e2d::String e2d::Text::getText() const
const e2d::String& e2d::Text::getText() const
{
return _text;
}
e2d::Font e2d::Text::getFont() const
const e2d::Font& e2d::Text::getFont() const
{
return _font;
}
e2d::Text::Style e2d::Text::getStyle() const
const e2d::Text::Style& e2d::Text::getStyle() const
{
return _style;
}
e2d::String e2d::Text::getFontFamily() const
const e2d::String& e2d::Text::getFontFamily() const
{
return _font.family;
}
@ -104,12 +104,12 @@ UINT e2d::Text::getFontWeight() const
return _font.weight;
}
e2d::Color e2d::Text::getColor() const
const e2d::Color& e2d::Text::getColor() const
{
return _style.color;
}
e2d::Color e2d::Text::getOutlineColor() const
const e2d::Color& e2d::Text::getOutlineColor() const
{
return _style.outlineColor;
}

View File

@ -8,7 +8,7 @@
if (Old) this->removeChild(Old); \
if (New) \
{ \
New->setPivot(_pivotX, _pivotY); \
New->setAnchor(_anchorX, _anchorY); \
this->addChild(New); \
} \
Old = New; \
@ -166,17 +166,17 @@ void e2d::ToggleButton::setDisabledOff(Node * disabled)
SET_BUTTON_NODE(_disabledOff, disabled);
}
void e2d::ToggleButton::setPivot(float pivotX, float pivotY)
void e2d::ToggleButton::setAnchor(float anchorX, float anchorY)
{
Node::setPivot(pivotX, pivotY);
SAFE_SET(_normalOn, setPivot, pivotX, pivotY);
SAFE_SET(_mouseoverOn, setPivot, pivotX, pivotY);
SAFE_SET(_selectedOn, setPivot, pivotX, pivotY);
SAFE_SET(_disabledOn, setPivot, pivotX, pivotY);
SAFE_SET(_normalOff, setPivot, pivotX, pivotY);
SAFE_SET(_mouseoverOff, setPivot, pivotX, pivotY);
SAFE_SET(_selectedOff, setPivot, pivotX, pivotY);
SAFE_SET(_disabledOff, setPivot, pivotX, pivotY);
Node::setAnchor(anchorX, anchorY);
SAFE_SET(_normalOn, setAnchor, anchorX, anchorY);
SAFE_SET(_mouseoverOn, setAnchor, anchorX, anchorY);
SAFE_SET(_selectedOn, setAnchor, anchorX, anchorY);
SAFE_SET(_disabledOn, setAnchor, anchorX, anchorY);
SAFE_SET(_normalOff, setAnchor, anchorX, anchorY);
SAFE_SET(_mouseoverOff, setAnchor, anchorX, anchorY);
SAFE_SET(_selectedOff, setAnchor, anchorX, anchorY);
SAFE_SET(_disabledOff, setAnchor, anchorX, anchorY);
}
void e2d::ToggleButton::_updateStatus()

View File

@ -61,7 +61,7 @@ bool e2d::File::isFolder() const
return (_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
e2d::String e2d::File::getFilePath() const
const e2d::String& e2d::File::getPath() const
{
return _fileName;
}
@ -82,7 +82,7 @@ e2d::String e2d::File::getExtension() const
return fileExtension;
}
bool e2d::File::deleteFile()
bool e2d::File::del()
{
if (::DeleteFile((LPCWSTR)_fileName))
return true;

View File

@ -130,7 +130,7 @@ bool e2d::Music::open(const e2d::String & filePath)
return false;
}
String actualFilePath = File(filePath).getFilePath();
String actualFilePath = File(filePath).getPath();
if (actualFilePath.isEmpty())
{
WARN("MusicInfo::open File not found.");

View File

@ -68,8 +68,8 @@ public:
float posY; // Y 坐标
float width; // 宽度
float height; // 高度
float pivotX; // 中心点 X 坐标
float pivotY; // 中心点 Y 坐标
float anchorX; // 锚点 X 坐标
float anchorY; // 锚点 Y 坐标
float scaleX; // 横向缩放
float scaleY; // 纵向缩放
float rotation; // 旋转角度
@ -88,20 +88,10 @@ public:
virtual ~Node();
// 获取节点显示状态
virtual bool isVisible() const;
// 判断点是否在节点内
bool containsPoint(
const Point& point
);
// 判断两物体是否相交
bool intersects(
Node * node
);
bool isVisible() const;
// 获取节点名称
String getName() const;
const String& getName() const;
// 获取节点名称的 Hash 值
size_t getHashName() const;
@ -133,11 +123,11 @@ public:
// 获取节点大小(不考虑缩放)
Size getRealSize() const;
// 获取节点的中心
float getPivotX() const;
// 获取节点的
float getAnchorX() const;
// 获取节点的中心
float getPivotY() const;
// 获取节点的
float getAnchorY() const;
// 获取节点大小
Size getSize() const;
@ -175,38 +165,6 @@ public:
// 获取节点所在场景
Scene * getParentScene() const;
// 获取所有名称相同的子节点
std::vector<Node*> getChildren(
const String& name
) const;
// 获取名称相同的子节点
Node* getChild(
const String& name
) const;
// 获取所有子节点
const std::vector<Node*>& getAllChildren() const;
// 获取子节点数量
int getChildrenCount() const;
// 移除子节点
bool removeChild(
Node * child
);
// 移除所有名称相同的子节点
void removeChildren(
const String& childName
);
// 移除所有节点
void removeAllChildren();
// 从父节点移除
void removeFromParent();
// 设置节点是否显示
void setVisible(
bool value
@ -326,23 +284,23 @@ public:
float opacity
);
// 设置中心点的横向位置
// 设置点的横向位置
// 默认为 0, 范围 [0, 1]
virtual void setPivotX(
float pivotX
virtual void setAnchorX(
float anchorX
);
// 设置中心点的纵向位置
// 设置点的纵向位置
// 默认为 0, 范围 [0, 1]
virtual void setPivotY(
float pivotY
virtual void setAnchorY(
float anchorY
);
// 设置中心点位置
// 设置点位置
// 默认为 (0, 0), 范围 [0, 1]
virtual void setPivot(
float pivotX,
float pivotY
virtual void setAnchor(
float anchorX,
float anchorY
);
// 修改节点宽度
@ -376,6 +334,16 @@ public:
bool enabled
);
// 判断点是否在节点内
bool containsPoint(
const Point& point
);
// 判断两物体是否相交
bool intersects(
Node * node
);
// 添加子节点
void addChild(
Node * child,
@ -388,6 +356,38 @@ public:
int order = 0 /* 渲染顺序 */
);
// 获取所有名称相同的子节点
std::vector<Node*> getChildren(
const String& name
) const;
// 获取名称相同的子节点
Node* getChild(
const String& name
) const;
// 获取所有子节点
const std::vector<Node*>& getAllChildren() const;
// 获取子节点数量
int getChildrenCount() const;
// 移除子节点
bool removeChild(
Node * child
);
// 移除所有名称相同的子节点
void removeChildren(
const String& childName
);
// 移除所有节点
void removeAllChildren();
// 从父节点移除
void removeFromParent();
// 执行动作
void runAction(
Action * action
@ -473,8 +473,8 @@ protected:
float _skewAngleY;
float _displayOpacity;
float _realOpacity;
float _pivotX;
float _pivotY;
float _anchorX;
float _anchorY;
int _order;
bool _visible;
bool _clipEnabled;
@ -589,7 +589,7 @@ public:
);
// 获取 Image 对象
virtual Image * getImage() const;
Image * getImage() const;
// 渲染精灵
virtual void draw(
@ -664,16 +664,16 @@ public:
virtual ~Text();
// 获取文本
String getText() const;
const String& getText() const;
// 获取字体
Font getFont() const;
const Font& getFont() const;
// 获取文本样式
Style getStyle() const;
const Style& getStyle() const;
// 获取字体族
String getFontFamily() const;
const String& getFontFamily() const;
// 获取当前字号
float getFontSize() const;
@ -682,10 +682,10 @@ public:
UINT getFontWeight() const;
// 获取文字颜色
Color getColor() const;
const Color& getColor() const;
// 获取描边颜色
Color getOutlineColor() const;
const Color& getOutlineColor() const;
// 获取描边线宽
float getOutlineWidth() const;
@ -889,11 +889,11 @@ public:
const Function& func
);
// 设置中心点位置
// 设置点位置
// 默认为 (0, 0), 范围 [0, 1]
virtual void setPivot(
float pivotX,
float pivotY
virtual void setAnchor(
float anchorX,
float anchorY
) override;
// 分发鼠标消息
@ -1024,11 +1024,11 @@ public:
Node * disabled
);
// 设置中心点位置
// 设置点位置
// 默认为 (0, 0), 范围 [0, 1]
virtual void setPivot(
float pivotX,
float pivotY
virtual void setAnchor(
float anchorX,
float anchorY
) override;
protected:
@ -1130,10 +1130,10 @@ public:
);
// 获取线条颜色
const Color& getLineColor() const;
Color getLineColor() const;
// 获取填充颜色
const Color& getFillColor() const;
Color getFillColor() const;
// 获取线条宽度
float getStrokeWidth() const;

View File

@ -458,10 +458,10 @@ public:
bool isFolder() const;
// 删除文件
bool deleteFile();
bool del();
// 获取文件路径
String getFilePath() const;
const String& getPath() const;
// 获取文件扩展名
String getExtension() const;