-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractSprite.java
More file actions
122 lines (102 loc) · 3.98 KB
/
AbstractSprite.java
File metadata and controls
122 lines (102 loc) · 3.98 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cn.colintree.aix.CanvasAddons;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Canvas;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.Sprite;
import android.os.Handler;
import cn.colintree.aix.CanvasAddons.util.ReflectUtil;
@SimpleObject(external = true)
public abstract class AbstractSprite<T extends Sprite> extends AndroidNonvisibleComponent {
protected final ComponentContainer container;
protected T sprite;
protected Canvas canvas;
protected Handler androidUIHandler;
public AbstractSprite(ComponentContainer container) {
super(container.$form());
this.container = container;
androidUIHandler = new Handler();
}
/**
* A place holder for the sprite.
*
* The property name "AA_PlaceHolder" is in purpose of making this property
* at the top of the property list, and being executed first before other properties are handled
*
* @param placeHolder
*/
@SimpleProperty(
category = PropertyCategory.BEHAVIOR,
userVisible = false)
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_COMPONENT)
public void AA_PlaceHolder(Sprite placeHolder) {
canvas = (Canvas)ReflectUtil.getField(Sprite.class, "canvas", placeHolder);
if (sprite == null) {
createSprite();
sprite.Initialize();
}
inheritProperties(placeHolder);
placeHolder.onDelete();
}
/**
* Creates a sprite and store it into field 'sprite',
* after this method is executed, field 'sprite' should contains a non-null value
*/
protected abstract void createSprite();
/**
* Handle property inherit
*/
protected abstract void inheritProperties(Sprite sprite);
/**
* Return the sprite that is used in the component
*/
@SimpleProperty
public Sprite Sprite() {
return sprite;
}
protected void postEvent(final String eventName, final Object... args) {
androidUIHandler.post(new Runnable() {
public void run() {
EventDispatcher.dispatchEvent(AbstractSprite.this, eventName, args);
}
});
}
@SimpleFunction(description = "Makes this sprite bounce, as if off a wall. " +
"For normal bouncing, the edge argument should be the one returned by EdgeReached.")
public void Bounce (int edge) {
sprite.Bounce(edge);
}
@SimpleFunction
public boolean CollidingWith(Sprite other) {
return sprite.CollidingWith(other);
}
@SimpleFunction
public void MoveIntoBounds() {
sprite.MoveIntoBounds();
}
@SimpleFunction(
description = "Moves the sprite so that its left top corner is at " +
"the specfied x and y coordinates.")
public void MoveTo(double x, double y) {
sprite.MoveTo(x, y);
}
@SimpleFunction(
description = "Turns the sprite to point towards the point " +
"with coordinates as (x, y).")
public void PointInDirection(double x, double y) {
sprite.PointInDirection(x, y);
}
@SimpleFunction(
description = "Turns the sprite to point towards a designated " +
"target sprite. The new heading will be parallel to the line joining " +
"the centerpoints of the two sprites.")
public void PointTowards(Sprite target) {
sprite.PointTowards(target);
}
}