blob: 8a1ab11b3be623963254409add4ba3b92b2066af [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "samplecode/Sample.h"
Kevin Lubickd9699322018-10-30 15:08:53 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "experimental/nima/NimaActor.h"
11#include "tools/Resources.h"
12#include "tools/timer/AnimTimer.h"
Kevin Lubickd9699322018-10-30 15:08:53 -040013
Ruiqi Mao9ac1b722018-06-21 11:24:13 -040014#include <nima/Animation/ActorAnimationInstance.hpp>
Brian Osmanc069a572018-06-19 16:05:09 -040015#include <cmath>
16
17using namespace nima;
18
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040019class NimaView : public Sample {
Brian Osmanc069a572018-06-19 16:05:09 -040020public:
21 NimaView()
Kevin Lubickd9699322018-10-30 15:08:53 -040022 : fActor(nullptr) {
Brian Osmanc069a572018-06-19 16:05:09 -040023 }
24
25protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040026 virtual bool onQuery(Sample::Event* evt) override {
27 if (Sample::TitleQ(*evt)) {
28 Sample::TitleR(evt, "Nima");
Brian Osmanc069a572018-06-19 16:05:09 -040029 return true;
30 }
31 return this->INHERITED::onQuery(evt);
32 }
33
34 void onOnceBeforeDraw() override {
35 // Create the actor.
Kevin Lubickd9699322018-10-30 15:08:53 -040036 std::string nimaPath(GetResourcePath("nima/Robot.nima").c_str());
37 std::string texturePath(GetResourcePath("nima/Robot.png").c_str());
Brian Osmanc069a572018-06-19 16:05:09 -040038
Kevin Lubickd9699322018-10-30 15:08:53 -040039 fActor = std::make_unique<NimaActor>(nimaPath, texturePath);
40
41 // Also available: dance, jump, idle
42 fActor->setAnimation("attack");
Brian Osmanc069a572018-06-19 16:05:09 -040043 }
44
45 void onDrawContent(SkCanvas* canvas) override {
46 canvas->save();
47
Kevin Lubickd9699322018-10-30 15:08:53 -040048 canvas->translate(500, 700);
Brian Osmanc069a572018-06-19 16:05:09 -040049 canvas->scale(1, -1);
50
51 // Render the actor.
52 fActor->render(canvas);
53
54 canvas->restore();
55 }
56
Mike Kleincd5104e2019-03-20 11:55:08 -050057 bool onAnimate(const AnimTimer& timer) override {
Kevin Lubickd9699322018-10-30 15:08:53 -040058 if (fActor) {
59 float time = std::fmod(timer.secs(), fActor->duration());
60 fActor->seek(time);
Brian Osmanc069a572018-06-19 16:05:09 -040061 }
62 return true;
63 }
64
65private:
Kevin Lubickd9699322018-10-30 15:08:53 -040066 std::unique_ptr<NimaActor> fActor;
Brian Osmanc069a572018-06-19 16:05:09 -040067
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040068 typedef Sample INHERITED;
Brian Osmanc069a572018-06-19 16:05:09 -040069};
70
71//////////////////////////////////////////////////////////////////////////////
72
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040073DEF_SAMPLE( return new NimaView(); )