aboutsummaryrefslogtreecommitdiffstats
path: root/sample-android-qt/src/main/cpp/NativeDrawable.cpp
blob: 41d1b70a87b8471366bc297465dbc538c83bef99 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
 * File generated by Jenny -- https://github.com/LanderlYoung/Jenny
 *
 * For bug report, please refer to github issue tracker https://github.com/LanderlYoung/Jenny/issues,
 * or contact author landerlyoung@gmail.com.
 */
#include <NativeDrawableProxy.h>
#include "NativeDrawable.h"
#include "gen/jenny_GraphicsProxy.h"
#include "gen/android_StyleProxy.h"
#include "gen/android_RectProxy.h"
#include "gen/GenericProxy.h"

class State {
private:
    uint _color;
public:
    jobject const paint;

    State(jobject paint) : _color(0xFF), paint(paint) {}

    int color() {
        return _color | 0xFF000000;
    }

    void change() {
        _color *= 97;
        if (_color == 0) {
            _color = 0xFF;
        }
    }
};


/*
 * Class:     io_github_landerlyoung_jennysampleapp_NativeDrawable
 * Method:    private final long nativeInit()
 * Signature: ()J
 */
jlong NativeDrawable::nativeInit(JNIEnv *env, jobject thiz) {
    using jenny::GraphicsProxy;
    using android::StyleProxy;
    GenericProxy::initClazz(env);

    auto fillType = android::StyleProxy::getFILL(env);
    auto paint = GraphicsProxy::newPaint(env);
    GraphicsProxy::paintSetStyle(env, paint, fillType);

    auto paintGlobal = env->NewGlobalRef(paint);

    return reinterpret_cast<jlong>(new State(paintGlobal));
}

/*
 * Class:     io_github_landerlyoung_jennysampleapp_NativeDrawable
 * Method:    public final void onClick()
 * Signature: ()V
 */
void NativeDrawable::onClick(JNIEnv *env, jobject thiz) {
    State* state = reinterpret_cast<State*>(NativeDrawableProxy::getNativeHandle(env, thiz));
    state->change();
}

/*
 * Class:     io_github_landerlyoung_jennysampleapp_NativeDrawable
 * Method:    public void draw(android.graphics.Canvas canvas)
 * Signature: (Landroid/graphics/Canvas;)V
 */
void NativeDrawable::draw(JNIEnv *env, jobject thiz, jobject _canvas) {
    using namespace android;
    using jenny::GraphicsProxy;
    State* state = reinterpret_cast<State*>(NativeDrawableProxy::getNativeHandle(env, thiz));

    auto bounds = GraphicsProxy::drawableGetBounds(env, thiz);

    GraphicsProxy::setColor(env, state->paint, state->color());
    GraphicsProxy::drawableCircle(
        env, _canvas,
        RectProxy::exactCenterX(env, bounds),
        RectProxy::exactCenterY(env, bounds),
        std::min(RectProxy::exactCenterX(env, bounds),
                 RectProxy::exactCenterY(env, bounds)) * 0.7f,
        state->paint
    );
}

/*
 * Class:     io_github_landerlyoung_jennysampleapp_NativeDrawable
 * Method:    public final void release()
 * Signature: ()V
 */
void NativeDrawable::release(JNIEnv *env, jobject thiz) {
    State* state = reinterpret_cast<State*>(NativeDrawableProxy::getNativeHandle(env, thiz));
    env->DeleteGlobalRef(state->paint);
    delete state;
}