中心点默认位置改为(0,0);新增ENode::setDefaultPivot函数设置默认中心点;修复了启用或禁用按钮后按钮消失的bug;更新了判断点是否在节点内的算法;修复了切换开关按钮状态不执行回调函数的bug;修复了子节点与父节点相对位置错误的bug;修复了其他的一些小bug。

This commit is contained in:
Nomango 2017-11-09 18:22:41 +08:00
parent 07abf5cf96
commit ee796af1df
12 changed files with 162 additions and 117 deletions

View File

@ -1,35 +1,53 @@
#include <easy2d.h>
class Scene2 :
public EScene
{
public:
Scene2()
{
auto text = new EText(L"²âÊÔ°´Å¥");
auto text2 = new EText(L"²âÊÔ°´Å¥", L"", 22, EColor::BLUE);
auto button = new EButton(text, text2, text);
button->setPos(EApp::getWidth() / 2, EApp::getHeight() / 2);
button->setCallback([]() {
EApp::backScene(new ETransitionScaleEmerge(1, ETransitionScaleEmerge::ENTER));
});
this->add(button);
}
};
class Scene :
public EScene
{
public:
Scene()
{
/*auto sprite = new ESprite(L"test2.png");
auto button = new EButton(sprite);
button->setPos(EApp::getWidth() / 2, EApp::getHeight() / 2);
button->setCallback([]() {
EApp::enterScene(new Scene2(), new ETransitionScaleEmerge(1, ETransitionScaleEmerge::ENTER));
});
this->add(button);*/
auto sprite = new ESprite(L"test2.png");
sprite->setPivot(-1, 0);
sprite->setPos(EApp::getWidth() / 2, EApp::getHeight() / 2);
this->add(sprite);
sprite->runAction(new EActionLoop(new EActionRotateBy(1, 60)));
}
};
int main()
{
EApp app;
if (app.init(L"Easy2D Demo", 640, 480))
{
app.showConsole();
auto scene = new EScene();
// 创建一个文本
auto btnNormal = new EText(L"");
auto btnSelected = new EText(L"");
btnSelected->movePosY(2);
auto btnNormal2 = new EText(L"");
auto btnSelected2 = new EText(L"");
btnSelected2->movePosY(2);
// 创建一个按钮
auto button = new EButtonToggle(btnNormal, btnNormal2, btnSelected, btnSelected2);
button->setCallback([=]() {
//button->setEnable(false);
});
button->toggle();
button->setPos(EApp::getWidth() / 2, EApp::getHeight() / 2);
scene->add(button);
// 创建按钮
auto button2 = new EButton(new EText(L"关闭"), [=]() { button->setEnable(!button->isEnable()); });
button2->setPos(40, 40);
scene->add(button2);
ENode::setDefaultPiovt(0.5f, 0.5f);
auto scene = new Scene();
app.enterScene(scene);
app.run();

View File

@ -15,8 +15,8 @@ e2d::EScene::EScene()
m_pRoot->retain();
m_pRoot->_onEnter();
m_pRoot->_setParentScene(this);
m_pRoot->setPivot(0, 0);
m_pRoot->_setSize(EApp::getWidth(), EApp::getHeight());
m_pRoot->setPos(EApp::getWidth() / 2, EApp::getHeight() / 2);
}
e2d::EScene::~EScene()

View File

@ -46,7 +46,7 @@ bool e2d::EFont::isItalic() const
return m_bItalic;
}
void e2d::EFont::setFamily(EString fontFamily)
void e2d::EFont::setFamily(const EString & fontFamily)
{
m_sFontFamily = fontFamily;
m_bRecreateNeeded = true;

View File

@ -101,7 +101,7 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
<DebugInformationFormat>None</DebugInformationFormat>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>

View File

@ -4,8 +4,7 @@
#include "..\Win\winbase.h"
e2d::EGeometry::EGeometry()
: m_bTransformed(false)
, m_nCategoryBitmask(0)
: m_nCategoryBitmask(0)
, m_nCollisionBitmask(0)
, m_bIsVisiable(true)
, m_nColor(EColor::RED)
@ -105,7 +104,7 @@ void e2d::EGeometry::_transform()
// 根据父节点转换几何图形
GetFactory()->CreateTransformedGeometry(
_getD2dGeometry(),
m_pParentNode->m_Matri,
m_pParentNode->m_MatriFinal,
&m_pTransformedGeometry
);

View File

@ -73,6 +73,7 @@ void e2d::EButton::setNormal(ENode * normal)
this->addChild(normal);
}
m_pNormal = normal;
_updateVisiable();
}
}
@ -139,6 +140,7 @@ void e2d::EButton::setEnable(bool bEnable)
if (m_bEnable != bEnable)
{
m_bEnable = bEnable;
_updateVisiable();
_updateStatus();
}
}
@ -159,6 +161,12 @@ void e2d::EButton::_setStatus(STATUS status)
}
}
void e2d::EButton::_updateTransform()
{
ENode::_updateTransform();
_updateStatus();
}
void e2d::EButton::_updateVisiable()
{
if (m_pNormal) m_pNormal->setVisiable(false);

View File

@ -63,9 +63,13 @@ e2d::EButtonToggle::EButtonToggle(ENode * toggleOnNormal, ENode * toggleOffNorma
void e2d::EButtonToggle::toggle()
{
m_bToggle = !m_bToggle;
_updateToggle();
_updateVisiable();
// 设置按钮状态
setToggle(!m_bToggle);
// 执行回调函数
if (m_Callback)
{
m_Callback();
}
}
bool e2d::EButtonToggle::isToggleOn() const
@ -79,6 +83,7 @@ void e2d::EButtonToggle::setToggle(bool toggle)
{
m_bToggle = toggle;
_updateToggle();
_updateVisiable();
}
}

View File

@ -6,6 +6,10 @@
#include "..\Win\winbase.h"
#include <algorithm>
// 默认中心点位置
static float s_fDefaultPiovtX = 0;
static float s_fDefaultPiovtY = 0;
e2d::ENode::ENode()
: m_nOrder(0)
, m_fScaleX(1.0f)
@ -15,9 +19,10 @@ e2d::ENode::ENode()
, m_fSkewAngleY(0)
, m_fDisplayOpacity(1.0f)
, m_fRealOpacity(1.0f)
, m_fPivotX(0.5f)
, m_fPivotY(0.5f)
, m_Matri(D2D1::Matrix3x2F::Identity())
, m_fPivotX(s_fDefaultPiovtX)
, m_fPivotY(s_fDefaultPiovtY)
, m_MatriInitial(D2D1::Matrix3x2F::Identity())
, m_MatriFinal(D2D1::Matrix3x2F::Identity())
, m_bVisiable(true)
, m_bDisplayedInScene(false)
, m_pGeometry(nullptr)
@ -25,7 +30,7 @@ e2d::ENode::ENode()
, m_pParentScene(nullptr)
, m_nHashName(0)
, m_bSortChildrenNeeded(false)
, m_bTransformChildrenNeeded(false)
, m_bTransformNeeded(false)
{
}
@ -64,7 +69,7 @@ void e2d::ENode::_callOn()
return;
}
if (m_bTransformChildrenNeeded)
if (m_bTransformNeeded)
{
_updateTransform(this);
}
@ -89,7 +94,7 @@ void e2d::ENode::_callOn()
}
}
GetRenderTarget()->SetTransform(m_Matri);
GetRenderTarget()->SetTransform(m_MatriFinal);
// 渲染自身
this->_onRender();
@ -99,7 +104,7 @@ void e2d::ENode::_callOn()
}
else
{
GetRenderTarget()->SetTransform(m_Matri);
GetRenderTarget()->SetTransform(m_MatriFinal);
// 渲染自身
this->_onRender();
}
@ -169,20 +174,15 @@ void e2d::ENode::_sortChildren()
}
}
void e2d::ENode::_updateTransformToReal()
void e2d::ENode::_updateTransform()
{
// 计算点坐标
// 计算中心点坐标
D2D1_POINT_2F pivot = D2D1::Point2F(
getRealWidth() * m_fPivotX,
getRealHeight() * m_fPivotY
);
// 计算左上角坐标
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(
m_Pos.x - getRealWidth() * m_fPivotX,
m_Pos.y - getRealHeight() * m_fPivotY
);
// 二维矩形变换
m_Matri = D2D1::Matrix3x2F::Scale(
// 初步的二维矩形变换,子节点将根据这个矩阵进行变换
m_MatriInitial = D2D1::Matrix3x2F::Scale(
m_fScaleX,
m_fScaleY,
pivot
@ -194,9 +194,16 @@ void e2d::ENode::_updateTransformToReal()
m_fRotation,
pivot
) * D2D1::Matrix3x2F::Translation(
upperLeftCorner.x,
upperLeftCorner.y
m_Pos.x,
m_Pos.y
);
// 和父节点矩阵相乘
if (m_pParent)
{
m_MatriInitial = m_MatriInitial * m_pParent->m_MatriInitial;
}
// 根据自身中心点做最终变换
m_MatriFinal = m_MatriInitial * D2D1::Matrix3x2F::Translation(-pivot.x, -pivot.y);
}
void e2d::ENode::_updateChildrenTransform()
@ -210,13 +217,8 @@ void e2d::ENode::_updateChildrenTransform()
void e2d::ENode::_updateTransform(ENode * node)
{
// 计算自身的转换矩阵
node->_updateTransformToReal();
// 和父节点矩阵相乘
if (node->m_pParent)
{
node->m_Matri = node->m_Matri * node->m_pParent->m_Matri;
}
// 转换几何形状
node->_updateTransform();
// 绑定于自身的形状也进行相应转换
if (node->m_pGeometry)
{
node->m_pGeometry->_transform();
@ -224,12 +226,7 @@ void e2d::ENode::_updateTransform(ENode * node)
// 遍历子节点下的所有节点
node->_updateChildrenTransform();
// 标志已执行过变换
node->m_bTransformChildrenNeeded = false;
// 绑定于自身的形状也进行相应转换
if (node->m_pGeometry)
{
node->m_pGeometry->m_bTransformed = true;
}
node->m_bTransformNeeded = false;
}
void e2d::ENode::_updateChildrenOpacity()
@ -376,7 +373,7 @@ void e2d::ENode::setPos(float x, float y)
m_Pos.x = x;
m_Pos.y = y;
m_bTransformChildrenNeeded = true;
m_bTransformNeeded = true;
}
void e2d::ENode::movePosX(float x)
@ -421,7 +418,7 @@ void e2d::ENode::_setSize(float width, float height)
m_Size.width = width;
m_Size.height = height;
m_bTransformChildrenNeeded = true;
m_bTransformNeeded = true;
}
void e2d::ENode::setScaleX(float scaleX)
@ -446,7 +443,7 @@ void e2d::ENode::setScale(float scaleX, float scaleY)
m_fScaleX = scaleX;
m_fScaleY = scaleY;
m_bTransformChildrenNeeded = true;
m_bTransformNeeded = true;
}
void e2d::ENode::setSkewX(float angleX)
@ -466,7 +463,7 @@ void e2d::ENode::setSkew(float angleX, float angleY)
m_fSkewAngleX = angleX;
m_fSkewAngleY = angleY;
m_bTransformChildrenNeeded = true;
m_bTransformNeeded = true;
}
void e2d::ENode::setRotation(float angle)
@ -475,7 +472,7 @@ void e2d::ENode::setRotation(float angle)
return;
m_fRotation = angle;
m_bTransformChildrenNeeded = true;
m_bTransformNeeded = true;
}
void e2d::ENode::setOpacity(float opacity)
@ -505,7 +502,7 @@ void e2d::ENode::setPivot(float pivotX, float pivotY)
m_fPivotX = min(max(pivotX, 0), 1);
m_fPivotY = min(max(pivotY, 0), 1);
m_bTransformChildrenNeeded = true;
m_bTransformNeeded = true;
}
void e2d::ENode::setGeometry(EGeometry * geometry)
@ -547,8 +544,6 @@ void e2d::ENode::addChild(ENode * child, int order /* = 0 */)
child->m_pParent = this;
_updateOpacity(child);
if (this->m_pParentScene)
{
child->_setParentScene(this->m_pParentScene);
@ -559,6 +554,11 @@ void e2d::ENode::addChild(ENode * child, int order /* = 0 */)
child->_onEnter();
}
// 更新子节点透明度
_updateOpacity(child);
// 更新节点转换
child->m_bTransformNeeded = true;
// 更新子节点排序
m_bSortChildrenNeeded = true;
}
}
@ -710,27 +710,26 @@ void e2d::ENode::pauseAction(EAction * action)
bool e2d::ENode::isPointIn(EPoint point)
{
if (m_bTransformChildrenNeeded)
if (m_bTransformNeeded)
{
_updateTransform(this);
}
// 保存节点所在矩形
D2D1_POINT_2F leftTop = m_Matri.TransformPoint(
D2D1::Point2F(0, 0)
// 为节点创建一个形状
ID2D1RectangleGeometry * rect;
GetFactory()->CreateRectangleGeometry(
D2D1::RectF(0, 0, getRealWidth(), getRealHeight()),
&rect
);
D2D1_POINT_2F rightBottom = m_Matri.TransformPoint(
D2D1::Point2F(getRealWidth(), getRealHeight())
// 判断点是否在形状内
BOOL ret;
rect->FillContainsPoint(
D2D1::Point2F(
point.x,
point.y),
&m_MatriFinal,
&ret
);
D2D1_RECT_F rt = D2D1::RectF(
leftTop.x,
leftTop.y,
rightBottom.x,
rightBottom.y
);
if (point.x >= rt.left &&
point.x <= rt.right &&
point.y >= rt.top &&
point.y <= rt.bottom)
if (ret)
{
return true;
}
@ -743,6 +742,12 @@ bool e2d::ENode::isPointIn(EPoint point)
return false;
}
void e2d::ENode::setDefaultPiovt(float defaultPiovtX, float defaultPiovtY)
{
s_fDefaultPiovtX = min(max(defaultPiovtX, 0), 1);
s_fDefaultPiovtY = min(max(defaultPiovtY, 0), 1);
}
void e2d::ENode::stopAction(EAction * action)
{
if (action->getTarget() == this)

View File

@ -10,44 +10,44 @@ e2d::ETransitionScaleEmerge::ETransitionScaleEmerge(float duration, SCALE_EMERGE
void e2d::ETransitionScaleEmerge::_setTarget(EScene * prev, EScene * next, bool & transitional)
{
float prevScaleTo;
float nextScaleStart;
float prevScaleBy;
float nextScaleBy;
if (m_Mode == SCALE_EMERGE_MODE::ENTER)
{
prevScaleTo = 1.2f;
nextScaleStart = 0.8f;
prevScaleBy = 0.2f;
nextScaleBy = -0.2f;
}
else
{
prevScaleTo = 0.8f;
nextScaleStart = 1.2f;
prevScaleBy = -0.2f;
nextScaleBy = 0.2f;
}
// 初始化场景属性
next->getRoot()->setScale(nextScaleStart);
next->getRoot()->setScale(next->getRoot()->getScaleX() - nextScaleBy, next->getRoot()->getScaleY() - nextScaleBy);
next->getRoot()->setOpacity(0);
// 第一个场景淡出
auto prevActionFadeOut = new EActionFadeOut(m_fDuration);
auto prevActionScaleTo = new EActionScaleTo(m_fDuration, prevScaleTo);
auto prevActionScaleBy = new EActionScaleBy(m_fDuration, prevScaleBy);
auto action1 = new EActionTwoAtSameTime(
prevActionFadeOut,
prevActionScaleTo);
prevActionScaleBy);
if (prev)
{
prevActionFadeOut->setTarget(prev->getRoot());
prevActionScaleTo->setTarget(prev->getRoot());
prevActionScaleBy->setTarget(prev->getRoot());
}
// 第二个场景淡入
auto nextActionFadeOut = new EActionFadeIn(m_fDuration);
auto nextActionScaleTo = new EActionScaleTo(m_fDuration, 1);
auto nextActionScaleBy = new EActionScaleBy(m_fDuration, nextScaleBy);
auto action2 = new EActionTwoAtSameTime(
nextActionFadeOut,
nextActionScaleTo);
nextActionScaleBy);
nextActionFadeOut->setTarget(next->getRoot());
nextActionScaleTo->setTarget(next->getRoot());
nextActionScaleBy->setTarget(next->getRoot());
// 标志动画结束
auto action3 = new EActionCallback([&, prev, next] {

View File

@ -395,7 +395,7 @@ public:
// ÉèÖÃ×ÖÌå
void setFamily(
EString fontFamily
const EString & fontFamily
);
// ÉèÖÃ×ÖºÅ

View File

@ -69,7 +69,6 @@ protected:
virtual ID2D1Geometry * _getD2dGeometry() const = 0;
protected:
bool m_bTransformed;
bool m_bIsVisiable;
UINT32 m_nCategoryBitmask;
UINT32 m_nCollisionBitmask;

View File

@ -68,10 +68,10 @@ public:
// 获取节点大小(不考虑缩放)
virtual ESize getRealSize() const;
// 获取节点的
// 获取节点的中心
virtual float getPivotX() const;
// 获取节点的
// 获取节点的中心
virtual float getPivotY() const;
// 获取节点大小
@ -226,20 +226,20 @@ public:
float opacity
);
// 设置点的横向位置
// 默认为 0.5f, 范围 [0, 1]
// 设置中心点的横向位置
// 默认为 0, 范围 [0, 1]
virtual void setPivotX(
float pivotX
);
// 设置点的纵向位置
// 默认为 0.5f, 范围 [0, 1]
// 设置中心点的纵向位置
// 默认为 0, 范围 [0, 1]
virtual void setPivotY(
float pivotY
);
// 设置点位置
// 默认为 (0.5f, 0.5f), 范围 [0, 1]
// 设置中心点位置
// 默认为 (0, 0), 范围 [0, 1]
virtual void setPivot(
float pivotX,
float pivotY
@ -306,6 +306,12 @@ public:
EPoint point
);
// 修改节点的默认中心点位置
static void setDefaultPiovt(
float defaultPiovtX,
float defaultPiovtY
);
protected:
// 访问节点
virtual void _callOn();
@ -330,14 +336,14 @@ protected:
EScene * scene
);
// 只考虑自身进行二维矩阵变换
void _updateTransformToReal();
// 自身进行二维矩阵变换
virtual void _updateTransform();
// 更新所有子节点矩阵
void _updateChildrenTransform();
virtual void _updateChildrenTransform();
// 更新所有子节点透明度
void _updateChildrenOpacity();
virtual void _updateChildrenOpacity();
// 更新节点矩阵
static void _updateTransform(ENode * node);
@ -384,11 +390,12 @@ protected:
bool m_bVisiable;
bool m_bDisplayedInScene;
bool m_bSortChildrenNeeded;
bool m_bTransformChildrenNeeded;
bool m_bTransformNeeded;
EGeometry * m_pGeometry;
EScene * m_pParentScene;
ENode * m_pParent;
D2D1::Matrix3x2F m_Matri;
D2D1::Matrix3x2F m_MatriInitial;
D2D1::Matrix3x2F m_MatriFinal;
EVector<ENode*> m_vChildren;
};
@ -646,6 +653,9 @@ protected:
// 设置按钮状态
virtual void _setStatus(STATUS status);
// 对自身进行二维矩阵变换
virtual void _updateTransform() override;
// 刷新按钮显示
virtual void _updateVisiable();
@ -715,13 +725,14 @@ public:
const BUTTON_CLICK_CALLBACK & callback = nullptr
);
// 切换开关状态
// 切换开关状态,并执行回调函数
void toggle();
// 获取开关状态
bool isToggleOn() const;
// 打开或关闭开关
// 仅设置按钮状态,不执行回调函数
void setToggle(
bool toggle
);