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