1 /* 2 * Copyright 2018 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef NimaActor_DEFINED 9 #define NimaActor_DEFINED 10 11 #include <nima/Actor.hpp> 12 #include <nima/ActorImage.hpp> 13 #include <nima/Vec2D.hpp> 14 15 #include "SkCanvas.h" 16 #include "SkData.h" 17 #include "SkImage.h" 18 19 #include <string> 20 21 class NimaActor; 22 class NimaActorImage; 23 24 enum RenderFlags { 25 kImmediate_RenderFlag = 0x1, 26 kCache_RenderFlag = 0x2, 27 kBounds_RenderFlag = 0x4, 28 }; 29 30 /** \class NimaActor 31 NimaActor acts as a breidge between Skia and a nima::Actor object. 32 The Actor object essentially is a set of bones and textures. 33 34 NimaActor knows how to draw itself (the Actor) to an SkCanvas 35 at various time stamps. 36 37 NimaActor is also aware of the different animation types the 38 Actor has and coordinates switching between them. For example, 39 an animation might have an "idle" and a "jump" animation it can 40 switch between. 41 */ 42 class NimaActor : public nima::Actor { 43 public: 44 45 NimaActor(std::string nimaPath, std::string texturePath); 46 NimaActor(sk_sp<SkData> nimaBytes, sk_sp<SkData> textureBytes); 47 48 ~NimaActor() = default; 49 50 /** 51 * Render draws itself to the given canvas, at whatever 52 * the current time position is (see seek). 53 */ 54 void render(SkCanvas* canvas, uint32_t renderFlags = 0); 55 /** 56 * Updates the animation state to be at time t. 57 * This does not re-draw anything, another call to render() is required. 58 * 59 * @param t - number of second in (modulo total duration) 60 * 61 */ 62 void seek(SkScalar t); 63 64 /** 65 * Returns the duration of the current Actor's animation in seconds. 66 */ 67 SkScalar duration() const; 68 69 /** 70 * Sets the animation type based on the index given. The default 71 * animation index is 0. If index is invalid, nothing changes. 72 */ 73 void setAnimation(uint8_t index); 74 75 /** 76 * Sets the animation type to be one that matches the provided 77 * name. If the name does not match any of the existing animation 78 * types, nothing changes. 79 */ 80 void setAnimation(std::string name); 81 82 /** 83 * Returns all possible animation names. Use with setAnimation(). 84 */ getAnimationNames()85 const std::vector<std::string>& getAnimationNames() const { 86 return fAnimationNames; 87 } 88 89 private: 90 void init(); 91 sk_sp<SkImage> fTexture; 92 std::vector<NimaActorImage> fActorImages; 93 std::unique_ptr<SkPaint> fPaint; 94 std::vector<std::string> fAnimationNames; 95 nima::ActorAnimationInstance* fAnimationInstance; 96 uint8_t fAnimationIndex; 97 98 typedef nima::Actor INHERITED; 99 }; 100 101 // A wrapper class that handles rendering of ActorImages (renderable components NIMA Actors). 102 class NimaActorImage { 103 public: 104 NimaActorImage(nima::ActorImage* actorImage, SkImage* texture, SkPaint* paint); 105 ~NimaActorImage() = default; 106 107 void render(SkCanvas* canvas, uint32_t renderFlags); 108 drawOrder()109 int drawOrder() const { return fActorImage->drawOrder(); } 110 111 private: 112 nima::ActorImage* fActorImage; 113 SkImage* fTexture; 114 SkPaint* fPaint; 115 116 bool fSkinned; 117 std::vector<SkPoint> fPositions; 118 std::vector<SkPoint> fTexs; 119 std::vector<SkVertices::BoneIndices> fBoneIdx; 120 std::vector<SkVertices::BoneWeights> fBoneWgt; 121 std::vector<uint16_t> fIndices; 122 123 std::vector<SkVertices::Bone> fBones; 124 sk_sp<SkVertices> fVertices; 125 126 uint32_t fRenderFlags; 127 128 void updateVertices(bool isVolatile); 129 void updateBones(); 130 void drawVerticesObject(SkVertices* vertices, SkCanvas* canvas, bool useBones) const; 131 }; 132 133 #endif 134