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