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