diff --git a/core/Common/Config.cpp b/core/Base/Config.cpp similarity index 73% rename from core/Common/Config.cpp rename to core/Base/Config.cpp index 8e25fb3e..89f31e92 100644 --- a/core/Common/Config.cpp +++ b/core/Base/Config.cpp @@ -3,7 +3,6 @@ e2d::Config::Config() : _gameName() - , _defaultNodePivot() , _soundEnabled(true) , _frameInterval(15) , _showFps(false) @@ -11,7 +10,6 @@ e2d::Config::Config() , _outlineVisible(false) , _collisionEnabled(false) , _colliderVisible(false) - , _defaultColliderShape(Collider::Shape::None) { } @@ -54,19 +52,6 @@ void e2d::Config::setCollisionEnabled(bool enabled) _collisionEnabled = enabled; } -void e2d::Config::setNodeDefaultPivot(Point pivot) -{ - _defaultNodePivot = Point( - std::min(std::max(pivot.x, 0.f), 1.f), - std::min(std::max(pivot.y, 0.f), 1.f) - ); -} - -void e2d::Config::setDefaultColliderShape(Collider::Shape shape) -{ - _defaultColliderShape = shape; -} - void e2d::Config::setColliderVisible(bool visible) { _colliderVisible = visible; @@ -107,16 +92,6 @@ bool e2d::Config::isCollisionEnabled() const return _collisionEnabled; } -e2d::Point e2d::Config::getNodeDefaultPivot() const -{ - return _defaultNodePivot; -} - -e2d::Collider::Shape e2d::Config::getDefaultColliderShape() const -{ - return _defaultColliderShape; -} - bool e2d::Config::isColliderVisible() const { return _colliderVisible; diff --git a/core/Common/Duration.cpp b/core/Common/Duration.cpp index d518bd36..ef691efe 100644 --- a/core/Common/Duration.cpp +++ b/core/Common/Duration.cpp @@ -12,7 +12,7 @@ e2d::Duration::Duration(int ms) { } -e2d::Duration::Duration(std::chrono::milliseconds ms) +e2d::Duration::Duration(const std::chrono::milliseconds& ms) : _ms(ms) { } diff --git a/core/Common/Time.cpp b/core/Common/Time.cpp index 14c9d27a..a5c03744 100644 --- a/core/Common/Time.cpp +++ b/core/Common/Time.cpp @@ -7,7 +7,7 @@ e2d::Time::Time() { } -e2d::Time::Time(std::chrono::steady_clock::time_point time) +e2d::Time::Time(const std::chrono::steady_clock::time_point& time) : _timePoint(time) { } diff --git a/core/Node/Button.cpp b/core/Node/Button.cpp index 23678502..d739a9b3 100644 --- a/core/Node/Button.cpp +++ b/core/Node/Button.cpp @@ -95,6 +95,7 @@ void e2d::Button::setNormal(Node * normal) // 添加新的 if (normal) { + normal->setPivot(_pivotX, _pivotY); this->addChild(normal); this->setSize(normal->getWidth(), normal->getHeight()); } @@ -116,6 +117,7 @@ void e2d::Button::setMouseOver(Node * mouseover) // 添加新的 if (mouseover) { + mouseover->setPivot(_pivotX, _pivotY); this->addChild(mouseover); } _mouseover = mouseover; @@ -135,6 +137,7 @@ void e2d::Button::setSelected(Node * selected) // 添加新的 if (selected) { + selected->setPivot(_pivotX, _pivotY); this->addChild(selected); } _selected = selected; @@ -154,6 +157,7 @@ void e2d::Button::setDisabled(Node * disabled) // 添加新的 if (disabled) { + disabled->setPivot(_pivotX, _pivotY); this->addChild(disabled); } _disabled = disabled; @@ -175,6 +179,15 @@ void e2d::Button::setClickFunc(const Function& func) _func = func; } +void e2d::Button::setPivot(float pivotX, float pivotY) +{ + 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); +} + void e2d::Button::_update() { Node::_update(); diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 3f3cce27..97822f46 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -69,10 +69,6 @@ e2d::Node::Node() , _collider(this) , _extrapolate(Property::Origin) { - Point defPivot = Game::getInstance()->getConfig().getNodeDefaultPivot(); - _pivotX = defPivot.x; - _pivotY = defPivot.y; - _collider.setShape(Game::getInstance()->getConfig().getDefaultColliderShape()); } e2d::Node::~Node() diff --git a/core/Node/ToggleButton.cpp b/core/Node/ToggleButton.cpp index 4fe663df..90c7421a 100644 --- a/core/Node/ToggleButton.cpp +++ b/core/Node/ToggleButton.cpp @@ -8,8 +8,8 @@ if (Old) this->removeChild(Old); \ if (New) \ { \ + New->setPivot(_pivotX, _pivotY); \ this->addChild(New); \ - this->setSize(New->getWidth(), New->getHeight()); \ } \ Old = New; \ _updateState(); \ @@ -19,7 +19,7 @@ e2d::ToggleButton::ToggleButton() : Button() - , _toggle(true) + , _checked(true) , _normalOn(nullptr) , _mouseoverOn(nullptr) , _selectedOn(nullptr) @@ -33,7 +33,7 @@ e2d::ToggleButton::ToggleButton() e2d::ToggleButton::ToggleButton(Node * toggleOnNormal, Node * toggleOffNormal, const Function& func) : Button() - , _toggle(true) + , _checked(true) , _normalOn(nullptr) , _mouseoverOn(nullptr) , _selectedOn(nullptr) @@ -50,7 +50,7 @@ e2d::ToggleButton::ToggleButton(Node * toggleOnNormal, Node * toggleOffNormal, c e2d::ToggleButton::ToggleButton(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnSelected, Node * toggleOffSelected, const Function& func) : Button() - , _toggle(true) + , _checked(true) , _normalOn(nullptr) , _mouseoverOn(nullptr) , _selectedOn(nullptr) @@ -69,7 +69,7 @@ e2d::ToggleButton::ToggleButton(Node * toggleOnNormal, Node * toggleOffNormal, N e2d::ToggleButton::ToggleButton(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnMouseOver, Node * toggleOffMouseOver, Node * toggleOnSelected, Node * toggleOffSelected, const Function& func) : Button() - , _toggle(true) + , _checked(true) , _normalOn(nullptr) , _mouseoverOn(nullptr) , _selectedOn(nullptr) @@ -90,7 +90,7 @@ e2d::ToggleButton::ToggleButton(Node * toggleOnNormal, Node * toggleOffNormal, N e2d::ToggleButton::ToggleButton(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnMouseOver, Node * toggleOffMouseOver, Node * toggleOnSelected, Node * toggleOffSelected, Node * toggleOnDisabled, Node * toggleOffDisabled, const Function& func) : Button() - , _toggle(true) + , _checked(true) , _normalOff(nullptr) , _mouseoverOff(nullptr) , _selectedOff(nullptr) @@ -107,16 +107,16 @@ e2d::ToggleButton::ToggleButton(Node * toggleOnNormal, Node * toggleOffNormal, N this->setClickFunc(func); } -bool e2d::ToggleButton::getState() const +bool e2d::ToggleButton::isChecked() const { - return _toggle; + return _checked; } -void e2d::ToggleButton::setState(bool bState) +void e2d::ToggleButton::setChecked(bool checked) { - if (_toggle != bState) + if (_checked != checked) { - _toggle = bState; + _checked = checked; _updateState(); _updateVisible(); } @@ -125,6 +125,7 @@ void e2d::ToggleButton::setState(bool bState) void e2d::ToggleButton::setNormal(Node * normal) { SET_BUTTON_NODE(_normalOn, normal); + this->setSize(_normalOn->getWidth(), _normalOn->getHeight()); } void e2d::ToggleButton::setMouseOver(Node * mouseover) @@ -162,9 +163,22 @@ void e2d::ToggleButton::setDisabledOff(Node * disabled) SET_BUTTON_NODE(_disabledOff, disabled); } +void e2d::ToggleButton::setPivot(float pivotX, float pivotY) +{ + 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); +} + void e2d::ToggleButton::_updateState() { - if (_toggle) + if (_checked) { _normal = _normalOn; _mouseover = _mouseoverOn; @@ -192,7 +206,7 @@ void e2d::ToggleButton::_updateState() void e2d::ToggleButton::_runCallback() { - _toggle = !_toggle; + _checked = !_checked; _updateState(); if (_func) diff --git a/core/e2dbase.h b/core/e2dbase.h index ae285b5b..aad0844c 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -10,7 +10,99 @@ namespace e2d { -// 游戏控制 +// 配置 +class Config +{ +public: + Config(); + + virtual ~Config(); + + // 修改游戏名称 + // 默认:空 + void setGameName( + const String& name + ); + + // 显示或隐藏 FPS + // 默认:隐藏 + void showFps( + bool show + ); + + // 打开或关闭垂直同步 + // 默认:打开 + void setVSyncEnabled( + bool enabled + ); + + // 设置帧率刷新间隔(关闭垂直同步时生效) + // 默认:15 + void setFrameInterval( + int interval + ); + + // 显示或隐藏节点轮廓 + // 默认:隐藏 + void setOutlineVisible( + bool visible + ); + + // 打开或关闭声音 + // 默认:打开 + void setSoundEnabled( + bool enabled + ); + + // 打开或关闭碰撞监听 + // 默认:关闭 + void setCollisionEnabled( + bool enabled + ); + + // 打开或关闭碰撞体可视化 + // 默认:关闭 + void setColliderVisible( + bool visible + ); + + // 获取游戏名称 + String getGameName() const; + + // 获取声音打开状态 + bool isSoundEnabled() const; + + // 获取垂直同步打开状态 + bool isVSyncEnabled() const; + + // 获取 FPS 显示状态 + bool isFpsShow() const; + + // 获取帧率刷新间隔 + int getFrameInterval() const; + + // 获取节点轮廓显示状态 + bool isOutlineVisible() const; + + // 获取碰撞监听状态 + bool isCollisionEnabled() const; + + // 获取碰撞体可视化状态 + bool isColliderVisible() const; + +protected: + bool _showFps; + bool _vSyncEnabled; + bool _soundEnabled; + bool _outlineVisible; + bool _collisionEnabled; + bool _colliderVisible; + int _frameInterval; + String _gameName; +}; + + +// 游戏主体 class Game { public: diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 4d4eb5a4..64d42ec3 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -418,7 +418,7 @@ public: ); explicit Duration( - std::chrono::milliseconds ms + const std::chrono::milliseconds& ms ); // 获取毫秒数 @@ -449,7 +449,7 @@ public: Time(); explicit Time( - std::chrono::steady_clock::time_point time + const std::chrono::steady_clock::time_point& time ); // 获取时间戳 @@ -1048,118 +1048,6 @@ protected: }; -// 游戏配置 -class Config -{ -public: - Config(); - - virtual ~Config(); - - // 修改游戏名称 - // 默认:空 - void setGameName( - const String& name - ); - - // 显示或隐藏 FPS - // 默认:隐藏 - void showFps( - bool show - ); - - // 打开或关闭垂直同步 - // 默认:打开 - void setVSyncEnabled( - bool enabled - ); - - // 设置帧率刷新间隔(关闭垂直同步时生效) - // 默认:15 - void setFrameInterval( - int interval - ); - - // 显示或隐藏节点轮廓 - // 默认:隐藏 - void setOutlineVisible( - bool visible - ); - - // 打开或关闭声音 - // 默认:打开 - void setSoundEnabled( - bool enabled - ); - - // 打开或关闭碰撞监听 - // 默认:关闭 - void setCollisionEnabled( - bool enabled - ); - - // 设置节点的默认中心点位置 - // 默认:(0,0) - void setNodeDefaultPivot( - Point pivot - ); - - // 设置节点的默认碰撞体形状 - // 默认:Collider::Shape::None - void setDefaultColliderShape( - Collider::Shape shape - ); - - // 打开或关闭碰撞体可视化 - // 默认:关闭 - void setColliderVisible( - bool visible - ); - - // 获取游戏名称 - String getGameName() const; - - // 获取声音打开状态 - bool isSoundEnabled() const; - - // 获取垂直同步打开状态 - bool isVSyncEnabled() const; - - // 获取 FPS 显示状态 - bool isFpsShow() const; - - // 获取帧率刷新间隔 - int getFrameInterval() const; - - // 获取节点轮廓显示状态 - bool isOutlineVisible() const; - - // 获取碰撞监听状态 - bool isCollisionEnabled() const; - - // 获取节点的默认中心点位置 - Point getNodeDefaultPivot() const; - - // 获取节点的默认碰撞体类型 - Collider::Shape getDefaultColliderShape() const; - - // 获取碰撞体可视化状态 - bool isColliderVisible() const; - -protected: - bool _showFps; - bool _vSyncEnabled; - bool _soundEnabled; - bool _outlineVisible; - bool _collisionEnabled; - bool _colliderVisible; - int _frameInterval; - String _gameName; - Point _defaultNodePivot; - Collider::Shape _defaultColliderShape; -}; - - } diff --git a/core/e2dnode.h b/core/e2dnode.h index 3de5e1d4..83042934 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -820,6 +820,13 @@ public: const Function& func ); + // 设置中心点位置 + // 默认为 (0, 0), 范围 [0, 1] + virtual void setPivot( + float pivotX, + float pivotY + ) override; + protected: E2D_DISABLE_COPY(Button); @@ -892,12 +899,12 @@ public: const Function& func = nullptr /* 按钮点击后的执行函数 */ ); - // 获取开关状态(打开或关闭) - bool getState() const; + // 获取开关状态 + bool isChecked() const; - // 设置开关按钮的状态(打开或关闭) - void setState( - bool bState + // 设置开关按钮的状态 + void setChecked( + bool checked ); // 设置按钮打开状态下显示的按钮 @@ -940,6 +947,13 @@ public: Node * disabled ); + // 设置中心点位置 + // 默认为 (0, 0), 范围 [0, 1] + virtual void setPivot( + float pivotX, + float pivotY + ) override; + protected: E2D_DISABLE_COPY(ToggleButton); @@ -958,7 +972,7 @@ protected: Node* _mouseoverOff; Node* _selectedOff; Node* _disabledOff; - bool _toggle; + bool _checked; }; diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index b2092b05..e1522094 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -51,6 +51,7 @@ + @@ -58,7 +59,6 @@ - diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 9602e8a8..adc4b8ad 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -124,9 +124,6 @@ Common - - Common - Common @@ -259,5 +256,8 @@ Event + + Base + \ No newline at end of file diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index f7d162fd..72e3899b 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -195,6 +195,7 @@ + @@ -202,7 +203,6 @@ - diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 5f46b7f1..c4d2a342 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -124,9 +124,6 @@ Common - - Common - Common @@ -259,5 +256,8 @@ Event + + Base + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index b5f050a0..a4af2d6e 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -215,6 +215,7 @@ + @@ -222,7 +223,6 @@ - diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 4792a873..40b24ac5 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -216,9 +216,6 @@ Custom - - Common - Common @@ -252,6 +249,9 @@ Common + + Base +