增加节点的Border属性

This commit is contained in:
Nomango 2018-09-01 23:00:08 +08:00
parent 3a8428d54e
commit cc2dff2afd
4 changed files with 93 additions and 107 deletions

View File

@ -186,12 +186,12 @@ void e2d::Collider::recreate()
break; break;
} }
ID2D1TransformedGeometry * _transformed; ID2D1TransformedGeometry * transformed;
factory->CreateTransformedGeometry( factory->CreateTransformedGeometry(
_geometry, _geometry,
_parentNode->_finalMatri, _parentNode->_finalMatri,
&_transformed &transformed
); );
SafeRelease(_geometry); SafeRelease(_geometry);
_geometry = _transformed; _geometry = transformed;
} }

View File

@ -66,12 +66,16 @@ e2d::Node::Node()
, _needTransform(false) , _needTransform(false)
, _positionFixed(false) , _positionFixed(false)
, _collider(this) , _collider(this)
, _border(nullptr)
, _borderColor(Color::Red, 0.6f)
, _extrapolate(Property::Origin) , _extrapolate(Property::Origin)
{ {
} }
e2d::Node::~Node() e2d::Node::~Node()
{ {
SafeRelease(_border);
ActionManager::getInstance()->clearAllBindedWith(this); ActionManager::getInstance()->clearAllBindedWith(this);
for (const auto& child : _children) for (const auto& child : _children)
{ {
@ -84,22 +88,18 @@ void e2d::Node::visit()
if (!_visible) if (!_visible)
return; return;
auto game = Game::getInstance(); if (!Game::getInstance()->isPaused())
if (!game->isPaused())
{ {
auto updatableNode = dynamic_cast<Updatable*>(this); auto updatableNode = dynamic_cast<Updatable*>(this);
if (updatableNode) if (updatableNode)
{ {
updatableNode->update(); updatableNode->update();
} }
// 更新转换矩阵
_updateTransform();
// 保留差别属性
_extrapolate = this->getProperty();
} }
_updateTransform();
_extrapolate = this->getProperty();
auto renderTarget = Renderer::getInstance()->getRenderTarget(); auto renderTarget = Renderer::getInstance()->getRenderTarget();
if (_clipEnabled) if (_clipEnabled)
{ {
@ -157,36 +157,38 @@ void e2d::Node::visit()
} }
} }
void e2d::Node::drawOutline() void e2d::Node::_drawBorder()
{ {
if (_visible) if (_visible)
{ {
auto renderer = Renderer::getInstance(); if (_border)
renderer->getRenderTarget()->SetTransform(_finalMatri); {
renderer->getRenderTarget()->DrawRectangle( auto renderer = Renderer::getInstance();
D2D1::RectF(0, 0, _width, _height), auto brush = renderer->getSolidColorBrush();
renderer->getSolidColorBrush(), brush->SetColor(D2D1_COLOR_F(_borderColor));
1.5f renderer->getRenderTarget()->DrawGeometry(
); _border,
brush,
1.5f
);
}
// 渲染所有子节点的轮廓
for (const auto& child : _children) for (const auto& child : _children)
{ {
child->drawOutline(); child->_drawBorder();
} }
} }
} }
void e2d::Node::drawCollider() void e2d::Node::_drawCollider()
{ {
if (_visible) if (_visible)
{ {
_collider.render(); _collider.render();
// 绘制所有子节点的几何碰撞体
for (const auto& child : _children) for (const auto& child : _children)
{ {
child->drawCollider(); child->_drawCollider();
} }
} }
} }
@ -225,6 +227,29 @@ void e2d::Node::_updateTransform()
_finalMatri = _finalMatri * _parent->_initialMatri; _finalMatri = _finalMatri * _parent->_initialMatri;
} }
// 重新构造轮廓
SafeRelease(_border);
ID2D1Factory * factory = Renderer::getFactory();
ID2D1RectangleGeometry * rectangle = nullptr;
ID2D1TransformedGeometry * transformed = nullptr;
ThrowIfFailed(
factory->CreateRectangleGeometry(
D2D1::RectF(0, 0, _width, _height),
&rectangle
)
);
ThrowIfFailed(
factory->CreateTransformedGeometry(
rectangle,
_finalMatri,
&transformed
)
);
_border = transformed;
SafeRelease(rectangle);
// 通知子节点进行转换 // 通知子节点进行转换
for (const auto& child : _children) for (const auto& child : _children)
{ {
@ -620,6 +645,11 @@ void e2d::Node::setClipEnabled(bool enabled)
_clipEnabled = enabled; _clipEnabled = enabled;
} }
void e2d::Node::setBorderColor(const Color & color)
{
_borderColor = color;
}
void e2d::Node::addChild(Node * child, int order /* = 0 */) void e2d::Node::addChild(Node * child, int order /* = 0 */)
{ {
WARN_IF(child == nullptr, "Node::addChild NULL pointer exception."); WARN_IF(child == nullptr, "Node::addChild NULL pointer exception.");
@ -837,90 +867,40 @@ void e2d::Node::stopAction(const String& name)
bool e2d::Node::containsPoint(const Point& point) bool e2d::Node::containsPoint(const Point& point)
{ {
// 更新转换矩阵 if (_width == 0.f || _height == 0.f)
return false;
_updateTransform(); _updateTransform();
// 为节点创建一个轮廓
BOOL ret = 0; BOOL ret = 0;
ID2D1RectangleGeometry * rectGeo = nullptr;
auto factory = Renderer::getFactory();
ThrowIfFailed( ThrowIfFailed(
factory->CreateRectangleGeometry( _border->FillContainsPoint(
D2D1::RectF(0, 0, _width, _height),
&rectGeo
)
);
ThrowIfFailed(
rectGeo->FillContainsPoint(
D2D1::Point2F(point.x, point.y), D2D1::Point2F(point.x, point.y),
_finalMatri, D2D1::Matrix3x2F::Identity(),
&ret &ret
) )
); );
SafeRelease(rectGeo);
return ret != 0; return ret != 0;
} }
bool e2d::Node::intersects(Node * node) bool e2d::Node::intersects(Node * node)
{ {
if (_width == 0.f || _height == 0.f || node->_width == 0.f || node->_height == 0.f)
return false;
// 更新转换矩阵 // 更新转换矩阵
_updateTransform(); _updateTransform();
node->_updateTransform(); node->_updateTransform();
// 为节点创建轮廓 // 获取相交状态
D2D1_GEOMETRY_RELATION relation = D2D1_GEOMETRY_RELATION_UNKNOWN; D2D1_GEOMETRY_RELATION relation = D2D1_GEOMETRY_RELATION_UNKNOWN;
ID2D1RectangleGeometry *rectGeo = nullptr, *rectGeo2 = nullptr;
ID2D1TransformedGeometry *transGeo = nullptr, *transGeo2 = nullptr;
auto factory = Renderer::getFactory();
ThrowIfFailed( ThrowIfFailed(
factory->CreateRectangleGeometry( _border->CompareWithGeometry(
D2D1::RectF(0, 0, _width, _height), node->_border,
&rectGeo
)
);
ThrowIfFailed(
factory->CreateRectangleGeometry(
D2D1::RectF(0, 0, node->_width, node->_height),
&rectGeo2
)
);
ThrowIfFailed(
factory->CreateTransformedGeometry(
rectGeo,
_finalMatri,
&transGeo
)
);
ThrowIfFailed(
factory->CreateTransformedGeometry(
rectGeo2,
node->_finalMatri,
&transGeo2
)
);
ThrowIfFailed(
// 获取相交状态
transGeo->CompareWithGeometry(
transGeo2,
D2D1::Matrix3x2F::Identity(), D2D1::Matrix3x2F::Identity(),
&relation &relation
) )
); );
SafeRelease(rectGeo);
SafeRelease(rectGeo2);
SafeRelease(transGeo);
SafeRelease(transGeo2);
return relation != D2D1_GEOMETRY_RELATION_UNKNOWN && return relation != D2D1_GEOMETRY_RELATION_UNKNOWN &&
relation != D2D1_GEOMETRY_RELATION_DISJOINT; relation != D2D1_GEOMETRY_RELATION_DISJOINT;
} }

View File

@ -3,7 +3,7 @@
#include "..\e2dmanager.h" #include "..\e2dmanager.h"
e2d::Scene::Scene() e2d::Scene::Scene()
: _outlineVisible(false) : _borderVisible(false)
, _colliderVisible(false) , _colliderVisible(false)
{ {
_parentScene = this; _parentScene = this;
@ -13,12 +13,12 @@ e2d::Scene::~Scene()
{ {
} }
void e2d::Scene::setOutlineVisible(bool visible) void e2d::Scene::showBorder(bool visible)
{ {
_outlineVisible = visible; _borderVisible = visible;
} }
void e2d::Scene::setColliderVisible(bool visible) void e2d::Scene::showCollider(bool visible)
{ {
_colliderVisible = visible; _colliderVisible = visible;
} }
@ -27,17 +27,16 @@ void e2d::Scene::visit()
{ {
Node::visit(); Node::visit();
if (_outlineVisible) if (_borderVisible)
{ {
auto brush = Renderer::getInstance()->getSolidColorBrush(); Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
brush->SetColor(D2D1::ColorF(D2D1::ColorF::Red, 0.6f)); Renderer::getInstance()->getSolidColorBrush()->SetOpacity(1.f);
brush->SetOpacity(1.f); this->_drawBorder();
this->drawOutline();
} }
if (_colliderVisible) if (_colliderVisible)
{ {
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity()); Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
this->drawCollider(); this->_drawCollider();
} }
} }

View File

@ -334,6 +334,11 @@ public:
bool enabled bool enabled
); );
// 设置节点边缘颜色
virtual void setBorderColor(
const Color& color
);
// 判断点是否在节点内 // 判断点是否在节点内
bool containsPoint( bool containsPoint(
const Point& point const Point& point
@ -432,15 +437,15 @@ public:
// 遍历节点 // 遍历节点
virtual void visit(); virtual void visit();
// 渲染节点轮廓
virtual void drawOutline();
// 渲染碰撞体轮廓
virtual void drawCollider();
protected: protected:
E2D_DISABLE_COPY(Node); E2D_DISABLE_COPY(Node);
// 渲染节点边缘
void _drawBorder();
// 渲染碰撞体轮廓
void _drawCollider();
// 设置节点所在场景 // 设置节点所在场景
void _setParentScene( void _setParentScene(
Scene * scene Scene * scene
@ -481,6 +486,8 @@ protected:
Scene * _parentScene; Scene * _parentScene;
Node * _parent; Node * _parent;
Property _extrapolate; Property _extrapolate;
Color _borderColor;
ID2D1Geometry* _border;
std::vector<Node*> _children; std::vector<Node*> _children;
D2D1::Matrix3x2F _initialMatri; D2D1::Matrix3x2F _initialMatri;
D2D1::Matrix3x2F _finalMatri; D2D1::Matrix3x2F _finalMatri;
@ -506,15 +513,15 @@ public:
// 说明:返回 false 将阻止窗口关闭 // 说明:返回 false 将阻止窗口关闭
virtual bool onCloseWindow() { return true; } virtual bool onCloseWindow() { return true; }
// 显示或隐藏节点轮廓 // 显示或隐藏节点边缘
// 默认:隐藏 // 默认:隐藏
void setOutlineVisible( void showBorder(
bool visible bool visible
); );
// 打开或关闭碰撞体可视化 // 显示或隐藏碰撞体
// 默认:关闭 // 默认:隐藏
void setColliderVisible( void showCollider(
bool visible bool visible
); );
@ -525,7 +532,7 @@ protected:
E2D_DISABLE_COPY(Scene); E2D_DISABLE_COPY(Scene);
protected: protected:
bool _outlineVisible; bool _borderVisible;
bool _colliderVisible; bool _colliderVisible;
}; };