Blame view

ios/cocos2d/cocos/editor-support/cocostudio/ActionTimeline/CCBoneNode.h 8.7 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  /****************************************************************************
  Copyright (c) 2015-2016 Chukong Technologies Inc.
  Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
   
  http://www.cocos2d-x.org
  
  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.
  ****************************************************************************/
  #pragma once
  
  #include "base/CCProtocols.h"
  #include "2d/CCNode.h"
  #include "renderer/CCCustomCommand.h"
  #include "editor-support/cocostudio/ActionTimeline/CCTimelineMacro.h"
  #include "editor-support/cocostudio/CocosStudioExport.h"
  #include "editor-support/cocostudio/ActionTimeline/CCSkinNode.h"
  
  namespace cocos2d{ namespace backend {
      class ProgramState;
  }}
  
  NS_TIMELINE_BEGIN
  
  class SkeletonNode;
  
  class CC_STUDIO_DLL BoneNode : public cocos2d::Node, public cocos2d::BlendProtocol
  {
  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
      virtual void addChild(cocos2d::Node* child, int localZOrder, const std::string &name) override;
      virtual void addChild(cocos2d::Node* child, int localZOrder, int tag) override;
  
      // 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
      virtual const  cocos2d::Vector<BoneNode*>& getChildBones() const { return _childBones; }
      virtual cocos2d::Vector<BoneNode*>&  getChildBones() { return _childBones; }
  
      
      // get this bone's root skeleton, return null while bone is not in a skeleton
      virtual SkeletonNode* getRootSkeletonNode() const;
      
      /**
       * @brief: get all bones in this bone tree
       */
      cocos2d::Vector<BoneNode*> getAllSubBones() const;
  
      /**
      *@brief: add a skin
      *@param: display, whether display this skin
      */
      virtual void addSkin(SkinNode* skin, bool display);
  
      /**
      *@brief: add a skin
      *@param: display, whether display this skin
      *@param: hideOthers, whether hide other skins added to this bone
      */
      virtual void addSkin(SkinNode* skin, bool display, bool hideOthers);
  
      /**
      * @brief: display skin
      * @param: hideOthers, set other skins invisible 
      */
      virtual void displaySkin(SkinNode* skin, bool hideOthers);
  
      /**
      * @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(const std::string &skinName, bool hideOthers );
  
      // get the skins which is visible (displaying skins)
      virtual cocos2d::Vector<SkinNode*> getVisibleSkins() const;
      
      /**
       * get skins in this bone's children
       */
      virtual const  cocos2d::Vector<SkinNode*>& getSkins() const { return _boneSkins; }
      virtual  cocos2d::Vector<SkinNode*>&  getSkins() { return _boneSkins; }
      
      /**
      * @brief: get all skins in this bone tree
      */
      cocos2d::Vector<SkinNode*> getAllSubSkins() const;
  
      // blendFunc
      virtual void setBlendFunc(const cocos2d::BlendFunc &blendFunc) override;
      virtual const cocos2d::BlendFunc & getBlendFunc() const override { return _blendFunc; }
  
      // 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
      virtual void setDebugDrawColor(const cocos2d::Color4F &color);
      virtual cocos2d::Color4F getDebugDrawColor() const { return _rackColor; }
  
      // get bone's bounding box, depends on getVisibleSkinsRect, apply on node to parent's transform
      cocos2d::Rect getBoundingBox() const override;
  
      /**
      *get displayings rect in self transform
      */
      virtual cocos2d::Rect getVisibleSkinsRect() const;
  
      // transform & draw
      virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override;
  
      // 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(const std::string& name) override;
  
      // set visible, and dirty the debugdraw to make debugdraw's render layer right
      virtual void setVisible(bool visible) override;
  
      // set contentsize, and recalculate debugdraw
      virtual void setContentSize(const cocos2d::Size& contentSize) override;
  
      // set localzorder, and recalculate debugdraw
      virtual void setAnchorPoint(const cocos2d::Vec2& anchorPoint) override;
      
  #ifdef CC_STUDIO_ENABLED_VIEW
      // hit test , bonePoint is in self coordinate
      virtual bool isPointOnRack(const cocos2d::Vec2& bonePoint);
  #endif
  
  CC_CONSTRUCTOR_ACCESS:
      BoneNode() = default;
      virtual ~BoneNode();
      virtual bool init() override;
  
  protected:
  
      virtual void addToChildrenListHelper(Node * child);
      virtual void removeFromChildrenListHelper(Node * child);
  
      // 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
      virtual void updateDisplayedColor(const cocos2d::Color3B& parentColor) override;
      virtual void updateDisplayedOpacity(uint8_t parentOpacity) override;
      virtual void disableCascadeOpacity() override;
      virtual void disableCascadeColor() override;
  
      // override Node::visit, just visit bones in children
      virtual void visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags) override;
  
      // a help function for SkeletonNode
      // for batch bone's draw to _rootSkeleton
      virtual void batchBoneDrawToSkeleton(BoneNode* bone) const; 
  
      // a help function for SkeletonNode
      // @param bone, visit bone's skins
      virtual void visitSkins(cocos2d::Renderer* renderer, BoneNode* bone) const;
  
      // a help function for SkeletonNode
      // set bone's rootSkeleton = skeleton
      void setRootSkeleton(BoneNode* bone, SkeletonNode* skeleton) const;
  protected:
      cocos2d::CustomCommand _customCommand;
      cocos2d::backend::UniformLocation _mvpLocation;
  
      cocos2d::BlendFunc _blendFunc = cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED;
  
      bool              _isRackShow = false;
      cocos2d::Color4F  _rackColor = cocos2d::Color4F::WHITE;
      float               _rackLength = 50.0f;
      float               _rackWidth = 20.0f;
  
      cocos2d::Vector<BoneNode*> _childBones;
      cocos2d::Vector<SkinNode*> _boneSkins;
      SkeletonNode*              _rootSkeleton = nullptr;
  private:
      struct VertexData
      {
          cocos2d::Color4F squareColor;
          cocos2d::Vec3 noMVPVertices;
      };
  
      cocos2d::Vec2 _squareVertices[4];
      VertexData _vertexData[4];
  
      CC_DISALLOW_COPY_AND_ASSIGN(BoneNode);
  };
  
  NS_TIMELINE_END