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