blob: c9e1d62199405f40104ac283c2e3318f661d0a01 [file] [log] [blame]
Brian Osmanc069a572018-06-19 16:05:09 -04001/*
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 Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
Ruiqi Maoc5c3df62018-06-21 14:40:28 -04009#include "SampleNimaActor.h"
Brian Osmanc069a572018-06-19 16:05:09 -040010#include "SkAnimTimer.h"
Ruiqi Mao9ac1b722018-06-21 11:24:13 -040011#include <nima/Animation/ActorAnimationInstance.hpp>
Brian Osmanc069a572018-06-19 16:05:09 -040012#include <cmath>
13
14using namespace nima;
15
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040016class NimaView : public Sample {
Brian Osmanc069a572018-06-19 16:05:09 -040017public:
18 NimaView()
Ruiqi Mao9ac1b722018-06-21 11:24:13 -040019 : fActor(nullptr)
20 , fAnimation(nullptr) {
Brian Osmanc069a572018-06-19 16:05:09 -040021 }
22
23protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040024 virtual bool onQuery(Sample::Event* evt) override {
25 if (Sample::TitleQ(*evt)) {
26 Sample::TitleR(evt, "Nima");
Brian Osmanc069a572018-06-19 16:05:09 -040027 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 Mao9ac1b722018-06-21 11:24:13 -040037 fAnimation = fActor->animationInstance("attack");
Brian Osmanc069a572018-06-19 16:05:09 -040038 }
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 Mao9ac1b722018-06-21 11:24:13 -040055 float time = std::fmod(timer.secs(), fAnimation->max());
56 fAnimation->time(time);
57 fAnimation->apply(1.0f);
Brian Osmanc069a572018-06-19 16:05:09 -040058 }
59 return true;
60 }
61
62private:
63 std::unique_ptr<SampleActor> fActor;
Ruiqi Mao9ac1b722018-06-21 11:24:13 -040064 ActorAnimationInstance* fAnimation;
Brian Osmanc069a572018-06-19 16:05:09 -040065
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040066 typedef Sample INHERITED;
Brian Osmanc069a572018-06-19 16:05:09 -040067};
68
69//////////////////////////////////////////////////////////////////////////////
70
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040071DEF_SAMPLE( return new NimaView(); )