增加节点的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;
}
ID2D1TransformedGeometry * _transformed;
ID2D1TransformedGeometry * transformed;
factory->CreateTransformedGeometry(
_geometry,
_parentNode->_finalMatri,
&_transformed
&transformed
);
SafeRelease(_geometry);
_geometry = _transformed;
_geometry = transformed;
}

View File

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

View File

@ -3,7 +3,7 @@
#include "..\e2dmanager.h"
e2d::Scene::Scene()
: _outlineVisible(false)
: _borderVisible(false)
, _colliderVisible(false)
{
_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;
}
@ -27,17 +27,16 @@ void e2d::Scene::visit()
{
Node::visit();
if (_outlineVisible)
if (_borderVisible)
{
auto brush = Renderer::getInstance()->getSolidColorBrush();
brush->SetColor(D2D1::ColorF(D2D1::ColorF::Red, 0.6f));
brush->SetOpacity(1.f);
this->drawOutline();
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
Renderer::getInstance()->getSolidColorBrush()->SetOpacity(1.f);
this->_drawBorder();
}
if (_colliderVisible)
{
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
this->drawCollider();
this->_drawCollider();
}
}

View File

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