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 #include "Sample.h" 9 10 #include "Resources.h" 11 #include "SkAnimTimer.h" 12 #include "nima/NimaActor.h" 13 14 #include <nima/Animation/ActorAnimationInstance.hpp> 15 #include <cmath> 16 17 using namespace nima; 18 19 class NimaView : public Sample { 20 public: NimaView()21 NimaView() 22 : fActor(nullptr) { 23 } 24 25 protected: onQuery(Sample::Event * evt)26 virtual bool onQuery(Sample::Event* evt) override { 27 if (Sample::TitleQ(*evt)) { 28 Sample::TitleR(evt, "Nima"); 29 return true; 30 } 31 return this->INHERITED::onQuery(evt); 32 } 33 onOnceBeforeDraw()34 void onOnceBeforeDraw() override { 35 // Create the actor. 36 std::string nimaPath(GetResourcePath("nima/Robot.nima").c_str()); 37 std::string texturePath(GetResourcePath("nima/Robot.png").c_str()); 38 39 fActor = std::make_unique<NimaActor>(nimaPath, texturePath); 40 41 // Also available: dance, jump, idle 42 fActor->setAnimation("attack"); 43 } 44 onDrawContent(SkCanvas * canvas)45 void onDrawContent(SkCanvas* canvas) override { 46 canvas->save(); 47 48 canvas->translate(500, 700); 49 canvas->scale(1, -1); 50 51 // Render the actor. 52 fActor->render(canvas); 53 54 canvas->restore(); 55 } 56 onAnimate(const SkAnimTimer & timer)57 bool onAnimate(const SkAnimTimer& timer) override { 58 if (fActor) { 59 float time = std::fmod(timer.secs(), fActor->duration()); 60 fActor->seek(time); 61 } 62 return true; 63 } 64 65 private: 66 std::unique_ptr<NimaActor> fActor; 67 68 typedef Sample INHERITED; 69 }; 70 71 ////////////////////////////////////////////////////////////////////////////// 72 73 DEF_SAMPLE( return new NimaView(); ) 74