From a3d425b0081c211fd2229d180e17cebb1dd52173 Mon Sep 17 00:00:00 2001 From: Nomango Date: Thu, 9 Jan 2020 08:45:00 +0800 Subject: [PATCH 01/17] Update Button --- projects/kiwano/kiwano.vcxproj | 6 +- projects/kiwano/kiwano.vcxproj.filters | 21 +-- src/3rd-party/OuterC/oc/function/details.h | 16 +- src/kiwano-audio/Sound.h | 2 +- src/kiwano-audio/SoundPlayer.h | 2 +- src/kiwano-network/HttpRequest.h | 2 +- src/kiwano-network/HttpResponse.hpp | 2 +- src/kiwano-physics/Body.cpp | 28 ++-- src/kiwano-physics/Body.h | 72 +++++---- src/kiwano-physics/Joint.cpp | 177 ++++++--------------- src/kiwano-physics/Joint.h | 81 ++++++---- src/kiwano/2d/Actor.cpp | 58 ++++--- src/kiwano/2d/Actor.h | 81 +++++----- src/kiwano/{ui => 2d}/Button.cpp | 139 +++++++++++----- src/kiwano/{ui => 2d}/Button.h | 75 ++++++++- src/kiwano/2d/DebugActor.cpp | 29 ++-- src/kiwano/2d/DebugActor.h | 7 +- src/kiwano/2d/Frame.h | 2 +- src/kiwano/2d/FrameSequence.h | 2 +- src/kiwano/2d/TextActor.h | 9 ++ src/kiwano/2d/Transition.h | 2 +- src/kiwano/2d/action/Action.h | 2 +- src/kiwano/core/AsyncTask.h | 2 +- src/kiwano/core/EventListener.h | 2 +- src/kiwano/core/ObjectBase.h | 2 +- src/kiwano/core/Timer.h | 2 +- src/kiwano/core/event/MouseEvent.cpp | 1 - src/kiwano/core/event/MouseEvent.h | 3 - src/kiwano/kiwano.h | 9 +- src/kiwano/platform/Director.cpp | 9 +- src/kiwano/renderer/Brush.cpp | 4 +- src/kiwano/renderer/Brush.h | 2 +- src/kiwano/renderer/Font.h | 2 +- src/kiwano/renderer/GifImage.h | 2 +- src/kiwano/renderer/RenderTarget.h | 2 +- src/kiwano/renderer/Renderer.cpp | 18 +-- src/kiwano/renderer/Texture.h | 2 +- src/kiwano/ui/Menu.cpp | 91 ----------- src/kiwano/ui/Menu.h | 67 -------- 39 files changed, 476 insertions(+), 559 deletions(-) rename src/kiwano/{ui => 2d}/Button.cpp (54%) rename src/kiwano/{ui => 2d}/Button.h (63%) delete mode 100644 src/kiwano/ui/Menu.cpp delete mode 100644 src/kiwano/ui/Menu.h diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index eab6029a..4d28a923 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -9,6 +9,7 @@ + @@ -82,8 +83,6 @@ - - @@ -96,6 +95,7 @@ + @@ -149,8 +149,6 @@ - - diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index dce7c87c..48dff23d 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -7,9 +7,6 @@ {68eac919-ee87-4030-a033-c251731928f5} - - {07b6d541-4a1b-472a-aae0-daf9d082fe84} - {c2654ccc-59f6-4c17-bb6b-99b07fc78702} @@ -36,12 +33,6 @@ - - ui - - - ui - 2d @@ -288,14 +279,11 @@ core\event + + 2d + - - ui - - - ui - 2d @@ -485,5 +473,8 @@ core\event + + 2d + \ No newline at end of file diff --git a/src/3rd-party/OuterC/oc/function/details.h b/src/3rd-party/OuterC/oc/function/details.h index 9abefaa1..66813ea6 100644 --- a/src/3rd-party/OuterC/oc/function/details.h +++ b/src/3rd-party/OuterC/oc/function/details.h @@ -115,23 +115,23 @@ public: virtual _Ret invoke(_Args... args) const override { - return (static_cast<_Ty*>(ptr_)->*func_)(::std::forward<_Args>(args)...); + return (ptr_->*func_)(::std::forward<_Args>(args)...); } - static inline callable<_Ret, _Args...>* make(void* ptr, _FuncType func) + static inline callable<_Ret, _Args...>* make(_Ty* ptr, _FuncType func) { return new (::std::nothrow) proxy_mem_callable<_Ty, _Ret, _Args...>(ptr, func); } protected: - proxy_mem_callable(void* ptr, _FuncType func) + proxy_mem_callable(_Ty* ptr, _FuncType func) : ptr_(ptr) , func_(func) { } protected: - void* ptr_; + _Ty* ptr_; _FuncType func_; }; @@ -144,23 +144,23 @@ public: virtual _Ret invoke(_Args... args) const override { - return (static_cast<_Ty*>(ptr_)->*func_)(::std::forward<_Args>(args)...); + return (ptr_->*func_)(::std::forward<_Args>(args)...); } - static inline callable<_Ret, _Args...>* make(void* ptr, _FuncType func) + static inline callable<_Ret, _Args...>* make(_Ty* ptr, _FuncType func) { return new (::std::nothrow) proxy_const_mem_callable<_Ty, _Ret, _Args...>(ptr, func); } protected: - proxy_const_mem_callable(void* ptr, _FuncType func) + proxy_const_mem_callable(_Ty* ptr, _FuncType func) : ptr_(ptr) , func_(func) { } protected: - void* ptr_; + _Ty* ptr_; _FuncType func_; }; diff --git a/src/kiwano-audio/Sound.h b/src/kiwano-audio/Sound.h index c1a1a1ac..61d40983 100644 --- a/src/kiwano-audio/Sound.h +++ b/src/kiwano-audio/Sound.h @@ -43,7 +43,7 @@ namespace kiwano * @brief 音频 */ class KGE_API Sound - : public ObjectBase + : public virtual ObjectBase { friend class AudioEngine; diff --git a/src/kiwano-audio/SoundPlayer.h b/src/kiwano-audio/SoundPlayer.h index f3bd7809..523a37bc 100644 --- a/src/kiwano-audio/SoundPlayer.h +++ b/src/kiwano-audio/SoundPlayer.h @@ -38,7 +38,7 @@ namespace kiwano * @brief 音频播放器 */ class KGE_API SoundPlayer - : public ObjectBase + : public virtual ObjectBase { public: SoundPlayer(); diff --git a/src/kiwano-network/HttpRequest.h b/src/kiwano-network/HttpRequest.h index 89dae6fa..36362a49 100644 --- a/src/kiwano-network/HttpRequest.h +++ b/src/kiwano-network/HttpRequest.h @@ -41,7 +41,7 @@ namespace kiwano * @brief HTTP请求 */ class KGE_API HttpRequest - : public ObjectBase + : public virtual ObjectBase { public: /// \~chinese diff --git a/src/kiwano-network/HttpResponse.hpp b/src/kiwano-network/HttpResponse.hpp index 14f80831..42106e2e 100644 --- a/src/kiwano-network/HttpResponse.hpp +++ b/src/kiwano-network/HttpResponse.hpp @@ -37,7 +37,7 @@ namespace kiwano * @brief HTTP响应 */ class KGE_API HttpResponse - : public ObjectBase + : public virtual ObjectBase { public: HttpResponse(HttpRequestPtr request); diff --git a/src/kiwano-physics/Body.cpp b/src/kiwano-physics/Body.cpp index 37cf93e4..cd4055c7 100644 --- a/src/kiwano-physics/Body.cpp +++ b/src/kiwano-physics/Body.cpp @@ -36,25 +36,12 @@ namespace kiwano { } - Body::Body(b2Body* body, Actor* actor) - : Body() - { - SetB2Body(body); - SetActor(actor); - } - - Body::Body(World* world, Actor* actor) - : Body() - { - Init(world, actor); - } - Body::~Body() { Destroy(); } - void Body::Init(World* world, Actor* actor) + bool Body::InitBody(World* world, Actor* actor) { KGE_ASSERT(world); @@ -64,9 +51,14 @@ namespace kiwano b2BodyDef def; b2Body* b2body = world->GetB2World()->CreateBody(&def); - SetB2Body(b2body); - SetActor(actor); - UpdateFromActor(); + if (b2body) + { + SetB2Body(b2body); + SetActor(actor); + UpdateFromActor(); + return true; + } + return false; } Fixture Body::AddFixture(Shape* shape, const Fixture::Param& param) @@ -253,7 +245,7 @@ namespace kiwano void Body::Destroy() { - if (world_ && body_) + if (world_) { world_->RemoveBody(this); } diff --git a/src/kiwano-physics/Body.h b/src/kiwano-physics/Body.h index b8049afd..6409bca4 100644 --- a/src/kiwano-physics/Body.h +++ b/src/kiwano-physics/Body.h @@ -40,7 +40,7 @@ namespace kiwano /// \~chinese /// @brief 物体 class KGE_API Body - : public virtual RefCounter + : public virtual ObjectBase { public: /// \~chinese @@ -53,16 +53,20 @@ namespace kiwano }; Body(); - Body(b2Body* body, Actor* actor); - Body(World* world, Actor* actor); - Body(World* world, ActorPtr actor); + virtual ~Body(); /// \~chinese /// @brief 初始化 /// @param[in] world 物理世界 /// @param[in] actor 绑定的角色 - void Init(World* world, Actor* actor); + bool InitBody(World* world, ActorPtr actor); + + /// \~chinese + /// @brief 初始化 + /// @param[in] world 物理世界 + /// @param[in] actor 绑定的角色 + bool InitBody(World* world, Actor* actor); /// \~chinese /// @brief 添加夹具 @@ -319,62 +323,62 @@ namespace kiwano /** @} */ - inline Body::Body(World* world, ActorPtr actor) : Body(world, actor.get()) {} + inline bool Body::InitBody(World* world, ActorPtr actor) { return InitBody(world, actor.get()); } - inline FixtureList Body::GetFixtureList() const { KGE_ASSERT(body_); return FixtureList(Fixture(body_->GetFixtureList())); } + inline FixtureList Body::GetFixtureList() const { KGE_ASSERT(body_); return FixtureList(Fixture(body_->GetFixtureList())); } - inline ContactEdgeList Body::GetContactList() const { KGE_ASSERT(body_); return ContactEdgeList(ContactEdge(body_->GetContactList())); } + inline ContactEdgeList Body::GetContactList() const { KGE_ASSERT(body_); return ContactEdgeList(ContactEdge(body_->GetContactList())); } - inline uint16_t Body::GetCategoryBits() const { return category_bits_; } + inline uint16_t Body::GetCategoryBits() const { return category_bits_; } - inline uint16_t Body::GetMaskBits() const { return mask_bits_; } + inline uint16_t Body::GetMaskBits() const { return mask_bits_; } - inline int16_t Body::GetGroupIndex() const { return group_index_; } + inline int16_t Body::GetGroupIndex() const { return group_index_; } - inline float Body::GetBodyRotation() const { KGE_ASSERT(body_); return math::Radian2Degree(body_->GetAngle()); } + inline float Body::GetBodyRotation() const { KGE_ASSERT(body_); return math::Radian2Degree(body_->GetAngle()); } - inline void Body::SetBodyRotation(float angle) { SetBodyTransform(GetBodyPosition(), angle); } + inline void Body::SetBodyRotation(float angle) { SetBodyTransform(GetBodyPosition(), angle); } - inline void Body::SetBodyPosition(Point const& pos) { SetBodyTransform(pos, GetBodyRotation()); } + inline void Body::SetBodyPosition(Point const& pos) { SetBodyTransform(pos, GetBodyRotation()); } - inline float Body::GetMass() const { KGE_ASSERT(body_); return body_->GetMass(); } + inline float Body::GetMass() const { KGE_ASSERT(body_); return body_->GetMass(); } - inline float Body::GetInertia() const { KGE_ASSERT(body_); return body_->GetInertia(); } + inline float Body::GetInertia() const { KGE_ASSERT(body_); return body_->GetInertia(); } - inline Body::Type Body::GetType() const { KGE_ASSERT(body_); return Type(body_->GetType()); } + inline Body::Type Body::GetType() const { KGE_ASSERT(body_); return Type(body_->GetType()); } - inline void Body::SetType(Type type) { KGE_ASSERT(body_); body_->SetType(static_cast(type)); } + inline void Body::SetType(Type type) { KGE_ASSERT(body_); body_->SetType(static_cast(type)); } - inline float Body::GetGravityScale() const { KGE_ASSERT(body_); return body_->GetGravityScale(); } + inline float Body::GetGravityScale() const { KGE_ASSERT(body_); return body_->GetGravityScale(); } - inline void Body::SetGravityScale(float scale) { KGE_ASSERT(body_); body_->SetGravityScale(scale); } + inline void Body::SetGravityScale(float scale) { KGE_ASSERT(body_); body_->SetGravityScale(scale); } - inline bool Body::IsIgnoreRotation() const { KGE_ASSERT(body_); return body_->IsFixedRotation(); } + inline bool Body::IsIgnoreRotation() const { KGE_ASSERT(body_); return body_->IsFixedRotation(); } - inline void Body::SetIgnoreRotation(bool flag) { KGE_ASSERT(body_); body_->SetFixedRotation(flag); } + inline void Body::SetIgnoreRotation(bool flag) { KGE_ASSERT(body_); body_->SetFixedRotation(flag); } - inline bool Body::IsBullet() const { KGE_ASSERT(body_); return body_->IsBullet(); } + inline bool Body::IsBullet() const { KGE_ASSERT(body_); return body_->IsBullet(); } - inline void Body::SetBullet(bool flag) { KGE_ASSERT(body_); body_->SetBullet(flag); } + inline void Body::SetBullet(bool flag) { KGE_ASSERT(body_); body_->SetBullet(flag); } - inline bool Body::IsAwake() const { KGE_ASSERT(body_); return body_->IsAwake(); } + inline bool Body::IsAwake() const { KGE_ASSERT(body_); return body_->IsAwake(); } - inline void Body::SetAwake(bool flag) { KGE_ASSERT(body_); body_->SetAwake(flag); } + inline void Body::SetAwake(bool flag) { KGE_ASSERT(body_); body_->SetAwake(flag); } - inline bool Body::IsSleepingAllowed() const { KGE_ASSERT(body_); return body_->IsSleepingAllowed(); } + inline bool Body::IsSleepingAllowed() const { KGE_ASSERT(body_); return body_->IsSleepingAllowed(); } - inline void Body::SetSleepingAllowed(bool flag) { KGE_ASSERT(body_); body_->SetSleepingAllowed(flag); } + inline void Body::SetSleepingAllowed(bool flag) { KGE_ASSERT(body_); body_->SetSleepingAllowed(flag); } - inline bool Body::IsActive() const { KGE_ASSERT(body_); return body_->IsActive(); } + inline bool Body::IsActive() const { KGE_ASSERT(body_); return body_->IsActive(); } - inline void Body::SetActive(bool flag) { KGE_ASSERT(body_); body_->SetActive(flag); } + inline void Body::SetActive(bool flag) { KGE_ASSERT(body_); body_->SetActive(flag); } - inline Actor* Body::GetActor() const { return actor_; } + inline Actor* Body::GetActor() const { return actor_; } - inline void Body::SetActor(Actor* actor) { actor_ = actor; } + inline void Body::SetActor(Actor* actor) { actor_ = actor; } - inline b2Body* Body::GetB2Body() const { return body_; } + inline b2Body* Body::GetB2Body() const { return body_; } - inline World* Body::GetWorld() const { return world_; } + inline World* Body::GetWorld() const { return world_; } } } diff --git a/src/kiwano-physics/Joint.cpp b/src/kiwano-physics/Joint.cpp index 4005e078..2c9e7890 100644 --- a/src/kiwano-physics/Joint.cpp +++ b/src/kiwano-physics/Joint.cpp @@ -36,28 +36,17 @@ namespace kiwano { } - Joint::Joint(b2Joint* joint) - : Joint() - { - SetB2Joint(joint); - } - - Joint::Joint(World* world, b2JointDef* joint_def) - : Joint() - { - Init(world, joint_def); - } - Joint::~Joint() { - if (world_) - { - world_->RemoveJoint(this); - } + Destroy(); } - void Joint::Init(World* world, b2JointDef* joint_def) + bool Joint::InitJoint(World* world, b2JointDef* joint_def) { + KGE_ASSERT(world); + + Destroy(); + world_ = world; if (world_) { @@ -65,7 +54,10 @@ namespace kiwano b2Joint* joint = world_->GetB2World()->CreateJoint(joint_def); SetB2Joint(joint); + + return joint != nullptr; } + return false; } BodyPtr Joint::GetBodyA() const @@ -93,6 +85,14 @@ namespace kiwano } } + void Joint::Destroy() + { + if (world_) + { + world_->RemoveJoint(this); + } + } + // // DistanceJoint // @@ -103,15 +103,7 @@ namespace kiwano { } - DistanceJoint::DistanceJoint(World* world, b2DistanceJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - DistanceJoint::DistanceJoint(World* world, DistanceJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool DistanceJoint::InitJoint(World* world, DistanceJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -120,8 +112,9 @@ namespace kiwano def.frequencyHz = param.frequency_hz; def.dampingRatio = param.damping_ratio; - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } void DistanceJoint::SetLength(float length) @@ -146,15 +139,7 @@ namespace kiwano { } - FrictionJoint::FrictionJoint(World* world, b2FrictionJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - FrictionJoint::FrictionJoint(World* world, FrictionJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool FrictionJoint::InitJoint(World* world, FrictionJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -163,8 +148,9 @@ namespace kiwano def.maxForce = param.max_force; def.maxTorque = world->Stage2World(param.max_torque); - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } void FrictionJoint::SetMaxForce(float length) @@ -201,15 +187,7 @@ namespace kiwano { } - GearJoint::GearJoint(World* world, b2GearJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - GearJoint::GearJoint(World* world, GearJoint::Param param) - : Joint() - , raw_joint_(nullptr) + bool GearJoint::InitJoint(World* world, GearJoint::Param const& param) { KGE_ASSERT(param.joint_a && param.joint_b); @@ -218,8 +196,9 @@ namespace kiwano def.joint2 = param.joint_b->GetB2Joint(); def.ratio = param.ratio; - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } void GearJoint::SetRatio(float ratio) @@ -244,15 +223,7 @@ namespace kiwano { } - MotorJoint::MotorJoint(World* world, b2MotorJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - MotorJoint::MotorJoint(World* world, MotorJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool MotorJoint::InitJoint(World* world, MotorJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -262,8 +233,9 @@ namespace kiwano def.maxTorque = world->Stage2World(param.max_torque); def.correctionFactor = param.correction_factor; - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } void MotorJoint::SetMaxForce(float length) @@ -300,15 +272,7 @@ namespace kiwano { } - PrismaticJoint::PrismaticJoint(World* world, b2PrismaticJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - PrismaticJoint::PrismaticJoint(World* world, PrismaticJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool PrismaticJoint::InitJoint(World* world, PrismaticJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -321,8 +285,9 @@ namespace kiwano def.maxMotorForce = param.max_motor_force; def.motorSpeed = world->Stage2World(param.motor_speed); - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } float PrismaticJoint::GetJointTranslation() const @@ -365,15 +330,7 @@ namespace kiwano { } - PulleyJoint::PulleyJoint(World* world, b2PulleyJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - PulleyJoint::PulleyJoint(World* world, PulleyJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool PulleyJoint::InitJoint(World* world, PulleyJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -381,8 +338,9 @@ namespace kiwano def.Initialize(param.body_a->GetB2Body(), param.body_b->GetB2Body(), world->Stage2World(param.ground_anchor_a), world->Stage2World(param.ground_anchor_b), world->Stage2World(param.anchor_a), world->Stage2World(param.anchor_b), param.ratio); - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } Point PulleyJoint::GetGroundAnchorA() const @@ -437,15 +395,7 @@ namespace kiwano { } - RevoluteJoint::RevoluteJoint(World* world, b2RevoluteJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - RevoluteJoint::RevoluteJoint(World* world, RevoluteJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool RevoluteJoint::InitJoint(World* world, RevoluteJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -458,8 +408,9 @@ namespace kiwano def.maxMotorTorque = world->Stage2World(param.max_motor_torque); def.motorSpeed = math::Degree2Radian(param.motor_speed); - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } float RevoluteJoint::GetJointAngle() const @@ -514,15 +465,7 @@ namespace kiwano { } - RopeJoint::RopeJoint(World* world, b2RopeJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - RopeJoint::RopeJoint(World* world, RopeJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool RopeJoint::InitJoint(World* world, RopeJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -533,8 +476,9 @@ namespace kiwano def.localAnchorB = world->Stage2World(param.local_anchor_b); def.maxLength = world->Stage2World(param.max_length); - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } void RopeJoint::SetMaxLength(float length) @@ -559,15 +503,7 @@ namespace kiwano { } - WeldJoint::WeldJoint(World* world, b2WeldJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - WeldJoint::WeldJoint(World* world, WeldJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool WeldJoint::InitJoint(World* world, WeldJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -576,8 +512,9 @@ namespace kiwano def.frequencyHz = param.frequency_hz; def.dampingRatio = param.damping_ratio; - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } @@ -591,15 +528,7 @@ namespace kiwano { } - WheelJoint::WheelJoint(World* world, b2WheelJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - WheelJoint::WheelJoint(World* world, WheelJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool WheelJoint::InitJoint(World* world, WheelJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -611,8 +540,9 @@ namespace kiwano def.frequencyHz = param.frequency_hz; def.dampingRatio = param.damping_ratio; - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } float WheelJoint::GetJointTranslation() const @@ -649,15 +579,7 @@ namespace kiwano { } - MouseJoint::MouseJoint(World* world, b2MouseJointDef* def) - : Joint(world, def) - , raw_joint_(nullptr) - { - } - - MouseJoint::MouseJoint(World* world, MouseJoint::Param const& param) - : Joint() - , raw_joint_(nullptr) + bool MouseJoint::InitJoint(World* world, MouseJoint::Param const& param) { KGE_ASSERT(param.body_a && param.body_b); @@ -669,8 +591,9 @@ namespace kiwano def.frequencyHz = param.frequency_hz; def.dampingRatio = param.damping_ratio; - Init(world, &def); + Joint::InitJoint(world, &def); raw_joint_ = static_cast(GetB2Joint()); + return raw_joint_ != nullptr; } void MouseJoint::SetMaxForce(float length) diff --git a/src/kiwano-physics/Joint.h b/src/kiwano-physics/Joint.h index 85881ecc..cae20a96 100644 --- a/src/kiwano-physics/Joint.h +++ b/src/kiwano-physics/Joint.h @@ -47,7 +47,7 @@ namespace kiwano /// \~chinese /// @brief 关节 class KGE_API Joint - : public virtual RefCounter + : public virtual ObjectBase { public: /// \~chinese @@ -87,13 +87,12 @@ namespace kiwano }; Joint(); - Joint(b2Joint* joint); - Joint(World* world, b2JointDef* joint_def); + virtual ~Joint(); /// \~chinese /// @brief 初始化关节 - void Init(World* world, b2JointDef* joint_def); + bool InitJoint(World* world, b2JointDef* joint_def); /// \~chinese /// @brief 获取关节连接的物体A @@ -107,6 +106,10 @@ namespace kiwano /// @brief 获取物理世界 World* GetWorld() const; + /// \~chinese + /// @brief 销毁关节 + void Destroy(); + b2Joint* GetB2Joint() const; void SetB2Joint(b2Joint* joint); @@ -160,8 +163,10 @@ namespace kiwano }; DistanceJoint(); - DistanceJoint(World* world, b2DistanceJointDef* def); - DistanceJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 设置关节长度 @@ -229,8 +234,10 @@ namespace kiwano }; FrictionJoint(); - FrictionJoint(World* world, b2FrictionJointDef* def); - FrictionJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 设置最大摩擦力 @@ -263,8 +270,8 @@ namespace kiwano /// @brief 齿轮关节参数 struct Param : public Joint::ParamBase { - JointPtr joint_a; ///< 关节A(旋转关节/平移关节) - JointPtr joint_b; ///< 关节B(旋转关节/平移关节) + Joint* joint_a; ///< 关节A(旋转关节/平移关节) + Joint* joint_b; ///< 关节B(旋转关节/平移关节) float ratio; ///< 齿轮传动比 Param( @@ -288,8 +295,10 @@ namespace kiwano }; GearJoint(); - GearJoint(World* world, b2GearJointDef* def); - GearJoint(World* world, Param param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 设定齿轮传动比 @@ -343,8 +352,10 @@ namespace kiwano }; MotorJoint(); - MotorJoint(World* world, b2MotorJointDef* def); - MotorJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 设置最大摩擦力 @@ -426,8 +437,10 @@ namespace kiwano }; PrismaticJoint(); - PrismaticJoint(World* world, b2PrismaticJointDef* def); - PrismaticJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 获取参考角 @@ -537,8 +550,10 @@ namespace kiwano }; PulleyJoint(); - PulleyJoint(World* world, b2PulleyJointDef* def); - PulleyJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 物体A对应的滑轮的位置 @@ -628,8 +643,10 @@ namespace kiwano }; RevoluteJoint(); - RevoluteJoint(World* world, b2RevoluteJointDef* def); - RevoluteJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 获取参考角 @@ -731,8 +748,10 @@ namespace kiwano }; RopeJoint(); - RopeJoint(World* world, b2RopeJointDef* def); - RopeJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 设置关节最大长度 @@ -786,8 +805,10 @@ namespace kiwano }; WeldJoint(); - WeldJoint(World* world, b2WeldJointDef* def); - WeldJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 获取物体B相对于物体A的角度 @@ -869,8 +890,10 @@ namespace kiwano }; WheelJoint(); - WheelJoint(World* world, b2WheelJointDef* def); - WheelJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 获取关节当前的平移距离 @@ -970,8 +993,10 @@ namespace kiwano }; MouseJoint(); - MouseJoint(World* world, b2MouseJointDef* def); - MouseJoint(World* world, Param const& param); + + /// \~chinese + /// @brief 初始化关节 + bool InitJoint(World* world, Param const& param); /// \~chinese /// @brief 设定最大摩擦力 [N] diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp index 44411653..324f245a 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -60,6 +60,10 @@ namespace kiwano { } + Actor::~Actor() + { + } + void Actor::Update(Duration dt) { UpdateActions(this, dt); @@ -176,35 +180,47 @@ namespace kiwano if (!visible_) return; - ActorPtr prev; - for (auto child = children_.last_item(); child; child = prev) + // Dispatch to children those are greater than 0 in Z-Order + Actor* child = children_.last_item().get(); + while (child) { - prev = child->prev_item(); + if (child->GetZOrder() < 0) + break; + child->Dispatch(evt); + child = child->prev_item().get(); } + HandleEvent(evt); + + while (child) + { + child->Dispatch(evt); + child = child->prev_item().get(); + } + } + + void Actor::HandleEvent(Event& evt) + { + EventDispatcher::Dispatch(evt); + if (responsible_) { if (evt.IsType()) { auto& mouse_evt = evt.SafeCast(); - if (!mouse_evt.target && ContainsPoint(mouse_evt.pos)) + bool contains = ContainsPoint(mouse_evt.pos); + if (!hover_ && contains) { - mouse_evt.target = this; + hover_ = true; - if (!hover_) - { - hover_ = true; - - MouseHoverEvent hover; - hover.pos = mouse_evt.pos; - hover.left_btn_down = mouse_evt.left_btn_down; - hover.right_btn_down = mouse_evt.right_btn_down; - hover.target = this; - EventDispatcher::Dispatch(hover); - } + MouseHoverEvent hover; + hover.pos = mouse_evt.pos; + hover.left_btn_down = mouse_evt.left_btn_down; + hover.right_btn_down = mouse_evt.right_btn_down; + EventDispatcher::Dispatch(hover); } - else if (hover_) + else if (hover_ && !contains) { hover_ = false; pressed_ = false; @@ -213,7 +229,6 @@ namespace kiwano out.pos = mouse_evt.pos; out.left_btn_down = mouse_evt.left_btn_down; out.right_btn_down = mouse_evt.right_btn_down; - out.target = this; EventDispatcher::Dispatch(out); } } @@ -221,27 +236,22 @@ namespace kiwano if (evt.IsType() && hover_) { pressed_ = true; - evt.SafeCast().target = this; } if (evt.IsType() && pressed_) { pressed_ = false; - auto mouse_up_evt = evt.SafeCast(); - mouse_up_evt.target = this; + auto& mouse_up_evt = evt.SafeCast(); MouseClickEvent click; click.pos = mouse_up_evt.pos; click.left_btn_down = mouse_up_evt.left_btn_down; click.right_btn_down = mouse_up_evt.right_btn_down; - click.target = this; click.button = mouse_up_evt.button; EventDispatcher::Dispatch(click); } } - - EventDispatcher::Dispatch(evt); } Matrix3x2 const & Actor::GetTransformMatrix() const diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index 30564cf0..07399669 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -19,14 +19,13 @@ // THE SOFTWARE. #pragma once -#include +#include #include #include -#include -#include -#include #include #include +#include +#include namespace kiwano { @@ -52,11 +51,11 @@ namespace kiwano * @details 角色是舞台上最基本的元素,是完成渲染、更新、事件分发等功能的最小单位,也是动画、定时器、事件监听等功能的载体 */ class KGE_API Actor - : public ObjectBase + : public virtual ObjectBase , public TimerManager , public ActionManager , public EventDispatcher - , public IntrusiveListItem + , protected IntrusiveListItem { friend class Director; friend class Transition; @@ -73,6 +72,8 @@ namespace kiwano Actor(); + virtual ~Actor(); + /// \~chinese /// @brief 更新角色 /// @details 每帧画面刷新前调用该函数,重载该函数以实现角色的更新处理 @@ -117,34 +118,6 @@ namespace kiwano /// @brief 获取 y 坐标 float GetPositionY() const; - /// \~chinese - /// @brief 获取缩放比例 - Point const& GetScale() const; - - /// \~chinese - /// @brief 获取横向缩放比例 - float GetScaleX() const; - - /// \~chinese - /// @brief 获取纵向缩放比例 - float GetScaleY() const; - - /// \~chinese - /// @brief 获取错切角度 - Point const& GetSkew() const; - - /// \~chinese - /// @brief 获取横向错切角度 - float GetSkewX() const; - - /// \~chinese - /// @brief 获取纵向错切角度 - float GetSkewY() const; - - /// \~chinese - /// @brief 获取旋转角度 - float GetRotation() const; - /// \~chinese /// @brief 获取宽度 float GetWidth() const; @@ -189,6 +162,34 @@ namespace kiwano /// @brief 获取显示透明度 float GetDisplayedOpacity() const; + /// \~chinese + /// @brief 获取旋转角度 + float GetRotation() const; + + /// \~chinese + /// @brief 获取缩放比例 + Point const& GetScale() const; + + /// \~chinese + /// @brief 获取横向缩放比例 + float GetScaleX() const; + + /// \~chinese + /// @brief 获取纵向缩放比例 + float GetScaleY() const; + + /// \~chinese + /// @brief 获取错切角度 + Point const& GetSkew() const; + + /// \~chinese + /// @brief 获取横向错切角度 + float GetSkewX() const; + + /// \~chinese + /// @brief 获取纵向错切角度 + float GetSkewY() const; + /// \~chinese /// @brief 获取变换 Transform GetTransform() const; @@ -362,10 +363,6 @@ namespace kiwano /// @brief 从父角色移除 void RemoveFromParent(); - /// \~chinese - /// @brief 判断点是否在角色内 - virtual bool ContainsPoint(const Point& point) const; - /// \~chinese /// @brief 暂停角色更新 void PauseUpdating(); @@ -386,6 +383,10 @@ namespace kiwano /// @brief 获取更新时的回调函数 UpdateCallback GetCallbackOnUpdate() const; + /// \~chinese + /// @brief 判断点是否在角色内 + virtual bool ContainsPoint(const Point& point) const; + /// \~chinese /// @brief 渲染角色边界 void ShowBorder(bool show); @@ -435,6 +436,10 @@ namespace kiwano /// @brief 设置节点所在舞台 void SetStage(Stage* stage); + /// \~chinese + /// @brief 处理事件 + void HandleEvent(Event& evt); + private: bool visible_; bool update_pausing_; diff --git a/src/kiwano/ui/Button.cpp b/src/kiwano/2d/Button.cpp similarity index 54% rename from src/kiwano/ui/Button.cpp rename to src/kiwano/2d/Button.cpp index 6342d1f8..424ec53f 100644 --- a/src/kiwano/ui/Button.cpp +++ b/src/kiwano/2d/Button.cpp @@ -18,7 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include +#include #include #include @@ -26,16 +26,8 @@ namespace kiwano { Button::Button() : enabled_(true) - , is_selected_(false) - , click_callback_(nullptr) , status_(Status::Normal) { - SetResponsible(true); - - AddListener(Closure(this, &Button::UpdateStatus)); - AddListener(Closure(this, &Button::UpdateStatus)); - AddListener(Closure(this, &Button::UpdateStatus)); - AddListener(Closure(this, &Button::UpdateStatus)); } Button::Button(const Callback& click) @@ -99,50 +91,119 @@ namespace kiwano { if (status_ != status) { - status_ = status; - } - } + Status old_status = status_; - void Button::UpdateStatus(Event& evt) - { - KGE_ASSERT(evt.IsType()); - - if (enabled_ && (evt.SafeCast().target == this)) - { - if (evt.IsType()) + if (status == Status::Normal) { - SetStatus(Status::Hover); - Window::instance().SetCursor(CursorType::Hand); - - if (mouse_over_callback_) - mouse_over_callback_(this); - } - else if (evt.IsType()) - { - SetStatus(Status::Normal); Window::instance().SetCursor(CursorType::Arrow); if (mouse_out_callback_) mouse_out_callback_(this); } - else if (evt.IsType() && status_ == Status::Hover) + else if (status == Status::Hover) { - SetStatus(Status::Pressed); + Window::instance().SetCursor(CursorType::Hand); + if (old_status == Status::Pressed) + { + if (released_callback_) + released_callback_(this); + } + else + { + if (mouse_over_callback_) + mouse_over_callback_(this); + } + } + else if (status == Status::Pressed) + { if (pressed_callback_) pressed_callback_(this); } - else if (evt.IsType() && status_ == Status::Pressed) - { - SetStatus(Status::Hover); - if (released_callback_) - released_callback_(this); - - if (click_callback_) - click_callback_(this); - } + status_ = status; } } + Button::Status Button::GetStatus() const + { + return status_; + } + + void Button::UpdateStatus(Event& evt) + { + if (!enabled_) + return; + + if (evt.IsType()) + { + SetStatus(Status::Hover); + } + else if (evt.IsType()) + { + SetStatus(Status::Normal); + } + else if (evt.IsType() && status_ == Status::Hover) + { + SetStatus(Status::Pressed); + } + else if (evt.IsType() && status_ == Status::Pressed) + { + SetStatus(Status::Hover); + } + else if (evt.IsType()) + { + if (click_callback_) + click_callback_(this); + } + } + + + SpriteButton::SpriteButton() + : SpriteButton(nullptr, nullptr, nullptr, nullptr) + { + } + + SpriteButton::SpriteButton(Callback const& click) + : SpriteButton(click, nullptr, nullptr, nullptr) + { + } + + SpriteButton::SpriteButton(Callback const& click, Callback const& pressed, Callback const& mouse_over, Callback const& mouse_out) + : Button(click, pressed, mouse_over, mouse_out) + { + SetResponsible(true); + + EventListener::Callback handler = Closure(this, &SpriteButton::UpdateStatus); + AddListener(handler); + AddListener(handler); + AddListener(handler); + AddListener(handler); + AddListener(handler); + } + + + TextButton::TextButton() + : TextButton(nullptr, nullptr, nullptr, nullptr) + { + } + + TextButton::TextButton(Callback const& click) + : TextButton(click, nullptr, nullptr, nullptr) + { + } + + TextButton::TextButton(Callback const& click, Callback const& pressed, Callback const& mouse_over, Callback const& mouse_out) + : Button(click, pressed, mouse_over, mouse_out) + { + SetResponsible(true); + + EventListener::Callback handler = Closure(this, &TextButton::UpdateStatus); + AddListener(handler); + AddListener(handler); + AddListener(handler); + AddListener(handler); + AddListener(handler); + } + } diff --git a/src/kiwano/ui/Button.h b/src/kiwano/2d/Button.h similarity index 63% rename from src/kiwano/ui/Button.h rename to src/kiwano/2d/Button.h index 5f724955..73247ea9 100644 --- a/src/kiwano/ui/Button.h +++ b/src/kiwano/2d/Button.h @@ -20,17 +20,20 @@ #pragma once #include +#include namespace kiwano { KGE_DECLARE_SMART_PTR(Button); + KGE_DECLARE_SMART_PTR(SpriteButton); + KGE_DECLARE_SMART_PTR(TextButton); /** * \~chinese * @brief 按钮 */ class KGE_API Button - : public Sprite + : public virtual ObjectBase { public: /// \~chinese @@ -45,7 +48,7 @@ namespace kiwano explicit Button(Callback const& click); /// \~chinese - /// @brief 获取按钮状态是启用还是禁用 + /// @brief 构造按钮 /// @param click 按钮点击回调函数 /// @param pressed 按钮按下回调函数 /// @param mouse_over 按钮移入回调函数 @@ -82,16 +85,30 @@ namespace kiwano /// @brief 设置鼠标移出按钮时的回调函数 void SetMouseOutCallback(const Callback& func); - private: - enum class Status { Normal, Hover, Pressed }; + /// \~chinese + /// @brief 按钮状态 + enum class Status + { + Normal, ///< 普通 + Hover, ///< 鼠标在按钮内 + Pressed ///< 被按下 + }; + /// \~chinese + /// @brief 设置按钮状态 void SetStatus(Status status); + /// \~chinese + /// @brief 获取按钮状态 + Status GetStatus() const; + + protected: + /// \~chinese + /// @brief 更新按钮状态 void UpdateStatus(Event& evt); private: bool enabled_; - bool is_selected_; Status status_; Callback click_callback_; Callback pressed_callback_; @@ -99,4 +116,52 @@ namespace kiwano Callback mouse_over_callback_; Callback mouse_out_callback_; }; + + + /// \~chinese + /// @brief 精灵按钮 + class SpriteButton + : public Sprite + , public Button + { + public: + SpriteButton(); + + /// \~chinese + /// @brief 构造精灵按钮 + /// @param click 按钮点击回调函数 + explicit SpriteButton(Callback const& click); + + /// \~chinese + /// @brief 构造精灵按钮 + /// @param click 按钮点击回调函数 + /// @param pressed 按钮按下回调函数 + /// @param mouse_over 按钮移入回调函数 + /// @param mouse_out 按钮移出回调函数 + SpriteButton(Callback const& click, Callback const& pressed, Callback const& mouse_over, Callback const& mouse_out); + }; + + + /// \~chinese + /// @brief 文字按钮 + class TextButton + : public TextActor + , public Button + { + public: + TextButton(); + + /// \~chinese + /// @brief 构造文字按钮 + /// @param click 按钮点击回调函数 + explicit TextButton(Callback const& click); + + /// \~chinese + /// @brief 构造文字按钮 + /// @param click 按钮点击回调函数 + /// @param pressed 按钮按下回调函数 + /// @param mouse_over 按钮移入回调函数 + /// @param mouse_out 按钮移出回调函数 + TextButton(Callback const& click, Callback const& pressed, Callback const& mouse_over, Callback const& mouse_out); + }; } diff --git a/src/kiwano/2d/DebugActor.cpp b/src/kiwano/2d/DebugActor.cpp index ae47b7a5..81923483 100644 --- a/src/kiwano/2d/DebugActor.cpp +++ b/src/kiwano/2d/DebugActor.cpp @@ -42,8 +42,6 @@ namespace kiwano return "\03"; } }; - - std::locale comma_locale(std::locale(), new comma_numpunct); } DebugActor::DebugActor() @@ -53,20 +51,21 @@ namespace kiwano SetResponsible(true); SetCascadeOpacityEnabled(true); + comma_locale_ = std::locale(std::locale(), new comma_numpunct); + background_brush_ = new Brush; background_brush_->SetColor(Color(0.0f, 0.0f, 0.0f, 0.7f)); - debug_text_ = new TextActor; - debug_text_->SetPosition(Point{ 10, 10 }); - this->AddChild(debug_text_); + BrushPtr fill_brush = new Brush; + fill_brush->SetColor(Color::White); TextStyle style; style.font_family = L"Arial"; style.font_size = 16.f; style.font_weight = FontWeight::Normal; style.line_spacing = 20.f; - debug_text_->SetStyle(style); - debug_text_->SetFillColor(Color::White); + style.fill_brush = fill_brush; + debug_text_.SetStyle(style); AddListener([=](Event&) { SetOpacity(0.4f); }); AddListener([=](Event&) { SetOpacity(1.f); }); @@ -80,6 +79,8 @@ namespace kiwano { rt->SetCurrentBrush(background_brush_); rt->FillRoundedRectangle(GetBounds(), Vec2{ 5.f, 5.f }); + + rt->DrawTextLayout(debug_text_, Point(10, 10)); } void DebugActor::OnUpdate(Duration dt) @@ -95,7 +96,7 @@ namespace kiwano StringStream ss; // For formatting integers with commas - (void)ss.imbue(comma_locale); + (void)ss.imbue(comma_locale_); ss << "Fps: " << frame_time_.size() << std::endl; @@ -124,16 +125,18 @@ namespace kiwano ss << pmc.PrivateUsage / 1024 << "Kb"; } - debug_text_->SetText(ss.str()); + debug_text_.SetText(ss.str()); + debug_text_.Update(); - if (debug_text_->GetWidth() > GetWidth() - 20) + Size layout_size = debug_text_.GetLayoutSize(); + if (layout_size.x > GetWidth() - 20) { - SetWidth(20 + debug_text_->GetWidth()); + SetWidth(20 + layout_size.x); } - if (debug_text_->GetHeight() > GetHeight() - 20) + if (layout_size.y > GetHeight() - 20) { - SetHeight(20 + debug_text_->GetHeight()); + SetHeight(20 + layout_size.y); } } diff --git a/src/kiwano/2d/DebugActor.h b/src/kiwano/2d/DebugActor.h index e35f2344..46144297 100644 --- a/src/kiwano/2d/DebugActor.h +++ b/src/kiwano/2d/DebugActor.h @@ -20,9 +20,7 @@ #pragma once #include -#include -#include -#include +#include namespace kiwano { @@ -51,8 +49,9 @@ namespace kiwano bool CheckVisibilty(RenderTarget* rt) const override; private: + std::locale comma_locale_; BrushPtr background_brush_; - TextActorPtr debug_text_; + TextLayout debug_text_; Vector