axmol/extensions/cocostudio/ActionTimeline/CCBoneNode.h

242 lines
8.4 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2015-2017 Chukong Technologies Inc.
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2020-10-17 16:32:16 +08:00
#ifndef __CCBONENODE_H__
#define __CCBONENODE_H__
2019-11-23 20:27:39 +08:00
#include "base/CCProtocols.h"
#include "2d/CCNode.h"
#include "renderer/CCCustomCommand.h"
#include "ActionTimeline/CCTimelineMacro.h"
#include "CocosStudioExport.h"
#include "ActionTimeline/CCSkinNode.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2021-12-25 10:04:45 +08:00
namespace backend
{
class ProgramState;
}
2022-08-08 18:02:17 +08:00
NS_AX_END // namespace axys
2019-11-23 20:27:39 +08:00
NS_TIMELINE_BEGIN
class SkeletonNode;
2022-08-08 18:02:17 +08:00
class CCS_DLL BoneNode : public ax::Node, public ax::BlendProtocol
2019-11-23 20:27:39 +08:00
{
public:
static BoneNode* create();
static BoneNode* create(int length);
using Node::addChild;
// add child, and add child to bone list and skeleton's sub bone map or add it to skin list
2022-08-08 18:02:17 +08:00
virtual void addChild(ax::Node* child, int localZOrder, std::string_view name) override;
virtual void addChild(ax::Node* child, int localZOrder, int tag) override;
2019-11-23 20:27:39 +08:00
// remove child, and remove child from bone list and skeleton's sub bone map or remove it from skin list
virtual void removeChild(Node* child, bool cleanup) override;
// get child bone list
2022-08-08 18:02:17 +08:00
virtual const ax::Vector<BoneNode*>& getChildBones() const { return _childBones; }
virtual ax::Vector<BoneNode*>& getChildBones() { return _childBones; }
2019-11-23 20:27:39 +08:00
// get this bone's root skeleton, return null while bone is not in a skeleton
virtual SkeletonNode* getRootSkeletonNode() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief: get all bones in this bone tree
*/
2022-08-08 18:02:17 +08:00
ax::Vector<BoneNode*> getAllSubBones() const;
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
*@brief: add a skin
*@param: display, whether display this skin
*/
2019-11-23 20:27:39 +08:00
virtual void addSkin(SkinNode* skin, bool display);
/**
2021-12-25 10:04:45 +08:00
*@brief: add a skin
*@param: display, whether display this skin
*@param: hideOthers, whether hide other skins added to this bone
*/
2019-11-23 20:27:39 +08:00
virtual void addSkin(SkinNode* skin, bool display, bool hideOthers);
/**
2021-12-25 10:04:45 +08:00
* @brief: display skin
* @param: hideOthers, set other skins invisible
*/
2019-11-23 20:27:39 +08:00
virtual void displaySkin(SkinNode* skin, bool hideOthers);
/**
2021-12-25 10:04:45 +08:00
* @brief: display all skins named skinName, if hide display only one skin,
* prefer to use display(SkinNode* skin, bool hideOthers = false)
* @param: hideOthers, set other skins invisible
*/
virtual void displaySkin(std::string_view skinName, bool hideOthers);
2019-11-23 20:27:39 +08:00
// get the skins which is visible (displaying skins)
2022-08-08 18:02:17 +08:00
virtual ax::Vector<SkinNode*> getVisibleSkins() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* get skins in this bone's children
*/
2022-08-08 18:02:17 +08:00
virtual const ax::Vector<SkinNode*>& getSkins() const { return _boneSkins; }
virtual ax::Vector<SkinNode*>& getSkins() { return _boneSkins; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* @brief: get all skins in this bone tree
*/
2022-08-08 18:02:17 +08:00
ax::Vector<SkinNode*> getAllSubSkins() const;
2019-11-23 20:27:39 +08:00
// blendFunc
2022-08-08 18:02:17 +08:00
virtual void setBlendFunc(const ax::BlendFunc& blendFunc) override;
virtual const ax::BlendFunc& getBlendFunc() const override { return _blendFunc; }
2019-11-23 20:27:39 +08:00
// debug draw show, bone's debugdraw can be draw when bone is visible
// when bone's added to skeleton, DebugDrawEnabled controlled by skeleton's DebugDrawEnabled
virtual void setDebugDrawEnabled(bool isDebugDraw);
virtual bool isDebugDrawEnabled() const { return _isRackShow; }
// bone's debug draw's length
virtual void setDebugDrawLength(float length);
virtual float getDebugDrawLength() const { return _rackLength; }
// bone's debug draw's width
virtual void setDebugDrawWidth(float width);
virtual float getDebugDrawWidth() const { return _rackWidth; }
// bone's debug draw's width
2022-08-08 18:02:17 +08:00
virtual void setDebugDrawColor(const ax::Color4F& color);
virtual ax::Color4F getDebugDrawColor() const { return _rackColor; }
2019-11-23 20:27:39 +08:00
// get bone's bounding box, depends on getVisibleSkinsRect, apply on node to parent's transform
2022-08-08 18:02:17 +08:00
ax::Rect getBoundingBox() const override;
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
*get displayings rect in self transform
*/
2022-08-08 18:02:17 +08:00
virtual ax::Rect getVisibleSkinsRect() const;
2019-11-23 20:27:39 +08:00
// transform & draw
2022-08-08 18:02:17 +08:00
virtual void draw(ax::Renderer* renderer, const ax::Mat4& transform, uint32_t flags) override;
2019-11-23 20:27:39 +08:00
// set local zorder, and dirty the debugdraw to make debugdraw's render layer right
virtual void setLocalZOrder(int localZOrder) override;
// set name, and replace the subbone map in skeleton
virtual void setName(std::string_view name) override;
2019-11-23 20:27:39 +08:00
// set visible, and dirty the debugdraw to make debugdraw's render layer right
virtual void setVisible(bool visible) override;
// set contentsize, and recalculate debugdraw
2022-08-08 18:02:17 +08:00
virtual void setContentSize(const ax::Size& contentSize) override;
2019-11-23 20:27:39 +08:00
// set localzorder, and recalculate debugdraw
2022-08-08 18:02:17 +08:00
virtual void setAnchorPoint(const ax::Vec2& anchorPoint) override;
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
#ifdef AX_STUDIO_ENABLED_VIEW
2019-11-23 20:27:39 +08:00
// hit test , bonePoint is in self coordinate
2022-08-08 18:02:17 +08:00
virtual bool isPointOnRack(const ax::Vec2& bonePoint);
2019-11-23 20:27:39 +08:00
#endif
BoneNode() = default;
2019-11-23 20:27:39 +08:00
virtual ~BoneNode();
virtual bool init() override;
protected:
2021-12-25 10:04:45 +08:00
virtual void addToChildrenListHelper(Node* child);
virtual void removeFromChildrenListHelper(Node* child);
2019-11-23 20:27:39 +08:00
// add bone to children bone list, and add bone to skeleton's subbone map
virtual void addToBoneList(BoneNode* bone);
// remove bone from children bone list, and remove bone from skeleton's subbone map
virtual void removeFromBoneList(BoneNode* bone);
// add skin to skin list
virtual void addToSkinList(SkinNode* skin);
// remove skin from skin list
virtual void removeFromSkinList(SkinNode* skin);
// sort all _children , bone list and skin list
virtual void sortAllChildren() override;
virtual void updateVertices();
virtual void updateColor() override;
// bone's color and opacity cannot cascade to bone
2022-08-08 18:02:17 +08:00
virtual void updateDisplayedColor(const ax::Color3B& parentColor) override;
2019-11-25 01:35:26 +08:00
virtual void updateDisplayedOpacity(uint8_t parentOpacity) override;
2019-11-23 20:27:39 +08:00
virtual void disableCascadeOpacity() override;
virtual void disableCascadeColor() override;
// override Node::visit, just visit bones in children
2022-08-08 18:02:17 +08:00
virtual void visit(ax::Renderer* renderer,
const ax::Mat4& parentTransform,
2021-12-25 10:04:45 +08:00
uint32_t parentFlags) override;
2019-11-23 20:27:39 +08:00
// a help function for SkeletonNode
// for batch bone's draw to _rootSkeleton
2021-12-25 10:04:45 +08:00
virtual void batchBoneDrawToSkeleton(BoneNode* bone) const;
2019-11-23 20:27:39 +08:00
// a help function for SkeletonNode
// @param bone, visit bone's skins
2022-08-08 18:02:17 +08:00
virtual void visitSkins(ax::Renderer* renderer, BoneNode* bone) const;
2019-11-23 20:27:39 +08:00
// a help function for SkeletonNode
// set bone's rootSkeleton = skeleton
void setRootSkeleton(BoneNode* bone, SkeletonNode* skeleton) const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2022-08-08 18:02:17 +08:00
ax::CustomCommand _customCommand;
ax::backend::UniformLocation _mvpLocation;
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
ax::BlendFunc _blendFunc = ax::BlendFunc::ALPHA_NON_PREMULTIPLIED;
2019-11-25 01:35:26 +08:00
2021-12-25 10:04:45 +08:00
bool _isRackShow = false;
2022-08-08 18:02:17 +08:00
ax::Color4F _rackColor = ax::Color4F::WHITE;
2021-12-25 10:04:45 +08:00
float _rackLength = 50.0f;
float _rackWidth = 20.0f;
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
ax::Vector<BoneNode*> _childBones;
ax::Vector<SkinNode*> _boneSkins;
2021-12-25 10:04:45 +08:00
SkeletonNode* _rootSkeleton = nullptr;
2019-11-23 20:27:39 +08:00
private:
2019-11-25 01:35:26 +08:00
struct VertexData
{
2022-08-08 18:02:17 +08:00
ax::Color4F squareColor;
ax::Vec3 noMVPVertices;
2019-11-25 01:35:26 +08:00
};
2022-08-08 18:02:17 +08:00
ax::Vec2 _squareVertices[4];
2019-11-25 01:35:26 +08:00
VertexData _vertexData[4];
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(BoneNode);
2019-11-23 20:27:39 +08:00
};
NS_TIMELINE_END
2021-12-25 10:04:45 +08:00
#endif //__CCBONENODE_H__