blob: f7bee1bb3b211e1f616905918f18fb27bc4ef857 [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
8#include "SampleCode.h"
Ruiqi Maoc5c3df62018-06-21 14:40:28 -04009#include "SampleNimaActor.h"
Brian Osmanc069a572018-06-19 16:05:09 -040010#include "SkAnimTimer.h"
11#include "SkView.h"
Ruiqi Mao9ac1b722018-06-21 11:24:13 -040012#include <nima/Animation/ActorAnimationInstance.hpp>
Brian Osmanc069a572018-06-19 16:05:09 -040013#include <cmath>
14
15using namespace nima;
16
17class NimaView : public SampleView {
18public:
19 NimaView()
Ruiqi Mao9ac1b722018-06-21 11:24:13 -040020 : fActor(nullptr)
21 , fAnimation(nullptr) {
Brian Osmanc069a572018-06-19 16:05:09 -040022 }
23
24protected:
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 Mao9ac1b722018-06-21 11:24:13 -040039 fAnimation = fActor->animationInstance("attack");
Brian Osmanc069a572018-06-19 16:05:09 -040040 }
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 Mao9ac1b722018-06-21 11:24:13 -040057 float time = std::fmod(timer.secs(), fAnimation->max());
58 fAnimation->time(time);
59 fAnimation->apply(1.0f);
Brian Osmanc069a572018-06-19 16:05:09 -040060 }
61 return true;
62 }
63
64private:
65 std::unique_ptr<SampleActor> fActor;
Ruiqi Mao9ac1b722018-06-21 11:24:13 -040066 ActorAnimationInstance* fAnimation;
Brian Osmanc069a572018-06-19 16:05:09 -040067
68 typedef SampleView INHERITED;
69};
70
71//////////////////////////////////////////////////////////////////////////////
72
73static SkView* MyFactory() { return new NimaView; }
74static SkViewRegister reg(MyFactory);