aboutsummaryrefslogtreecommitdiff
path: root/samplecode/SampleNima.cpp
blob: 69125186aeb9ee5f67ddaeaee2dad439ad2b8dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Sample.h"

#include "Resources.h"
#include "SkAnimTimer.h"
#include "nima/NimaActor.h"

#include <nima/Animation/ActorAnimationInstance.hpp>
#include <cmath>

using namespace nima;

class NimaView : public Sample {
public:
    NimaView()
        : fActor(nullptr) {
    }

protected:
    virtual bool onQuery(Sample::Event* evt) override {
        if (Sample::TitleQ(*evt)) {
            Sample::TitleR(evt, "Nima");
            return true;
        }
        return this->INHERITED::onQuery(evt);
    }

    void onOnceBeforeDraw() override {
        // Create the actor.
        std::string nimaPath(GetResourcePath("nima/Robot.nima").c_str());
        std::string texturePath(GetResourcePath("nima/Robot.png").c_str());

        fActor = std::make_unique<NimaActor>(nimaPath, texturePath);

        // Also available: dance, jump, idle
        fActor->setAnimation("attack");
    }

    void onDrawContent(SkCanvas* canvas) override {
        canvas->save();

        canvas->translate(500, 700);
        canvas->scale(1, -1);

        // Render the actor.
        fActor->render(canvas);

        canvas->restore();
    }

    bool onAnimate(const SkAnimTimer& timer) override {
        if (fActor) {
            float time = std::fmod(timer.secs(), fActor->duration());
            fActor->seek(time);
        }
        return true;
    }

private:
    std::unique_ptr<NimaActor>   fActor;

    typedef Sample INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

DEF_SAMPLE( return new NimaView(); )