87
public final int x, y;
88
public final int sx, sy;
90
private int lastWidth, lastHeight;
86
public final Position pos;
92
88
protected boolean selected = false;
94
90
protected ButtonPresses callbacks;
96
public Item(int x, int y, int sx, int sy) {
99
this.sx = sx < 1 ? 1 : sx;
100
this.sy = sy < 1 ? 1 : sy;
103
public final void draw(Canvas c, int width, int height, boolean landscape) {
104
RectF area = computeArea(width, height);
105
Point centre = computeCentre(area);
93
* Grid constructor - use to create a grid item
99
public Item(int x, int y, int sx, int sy) {
100
pos = new GridPosition(x, y, sx < 1 ? 1 : sx, sy < 1 ? 1 : sy);
104
* General constructor
109
* @param free <code>true</code>, this will be a free (floating) item
111
public Item(float x, float y, float sx, float sy, boolean free) {
113
pos = new FreePosition(x, y, sx, sy);
115
pos = new GridPosition((int)x, (int)y, (int)sx < 1 ? 1 : (int)sx, (int)sy < 1 ? 1 : (int)sy);
118
public final void draw(Canvas c, ScreenInfo info) {
119
RectF area = pos.computeArea(info);
120
PointF centre = pos.computeCentre(info);
107
122
c.drawRoundRect(area, 10, 10, bp);
109
drawInternal(c, area, centre, landscape);
110
drawInArea(c, area, centre, landscape);
124
drawInternal(c, area, centre, info.landscape);
125
drawInArea(c, area, centre, info.landscape);
113
protected abstract void drawInArea(Canvas c, RectF area, Point centre, boolean landscape);
128
protected abstract void drawInArea(Canvas c, RectF area, PointF centre, boolean landscape);
115
private void drawInternal(Canvas c, RectF area, Point centre, boolean landscape) {
130
private void drawInternal(Canvas c, RectF area, PointF centre, boolean landscape) {
116
131
c.drawRoundRect(area, 10, 10, isSelected() ? bpS : bp);
119
protected final RectF computeArea(int width, int height) {
123
x * width + BUTTON_GAP,
124
y * height + BUTTON_GAP,
125
(x + sx) * width - BUTTON_GAP,
126
(y + sy) * height - BUTTON_GAP);
128
protected final RectF computeArea() {
129
return computeArea(lastWidth, lastHeight);
132
protected final Point computeCentre(RectF area) {
133
return new Point((int)area.centerX(), (int)area.centerY());
136
protected final Point computeCentre() {
137
return computeCentre(computeArea());
140
134
public boolean isSelected() {
179
173
public abstract void finaliseState();
181
public abstract void onMouseOn(float x, float y);
175
public abstract void onMouseOn(ScreenInfo info, float x, float y);
182
176
public abstract void onMouseOff();
184
178
protected void setCallbacks(ButtonPresses callbacks) {
185
179
this.callbacks = callbacks;
182
public static class ScreenInfo {
184
* The width and height of the entire screen
186
public float width, height;
188
* The width and height of one grid item
190
public float gridWidth, gridHeight;
192
public boolean landscape;
194
public ScreenInfo() {
200
public ScreenInfo(float w, float h, float gw, float gh, boolean landscape) {
205
this.landscape = landscape;
207
public void set(float w, float h, float gw, float gh, boolean landscape) {
212
this.landscape = landscape;
217
* Describes the position of an onscreen button.
218
* Is either grid-based, or positioned absolutely.
220
public static abstract class Position implements Serializable {
221
private static final long serialVersionUID = -7465262293417889805L;
222
private Position() { }
224
public abstract RectF computeArea(ScreenInfo info);
225
public abstract PointF computeCentre(ScreenInfo info);
229
* A position which is locked to the grid
233
public static class GridPosition extends Position {
234
private static final long serialVersionUID = 9207230734790003508L;
237
* Padding on buttons for buttons on a grid
239
public static final int BUTTON_GAP = 10;
241
* The x-coordinate, grid based
245
* The y-coordinate, grid-based
251
public GridPosition(int x, int y, int sx, int sy) {
259
public final RectF computeArea(ScreenInfo info) {
261
x * info.gridWidth + BUTTON_GAP,
262
y * info.gridHeight + BUTTON_GAP,
263
(x + sx) * info.gridWidth - BUTTON_GAP,
264
(y + sy) * info.gridHeight - BUTTON_GAP);
268
public final PointF computeCentre(ScreenInfo info) {
269
return new PointF(((float)x + (float)sx / 2) * info.gridWidth,
270
((float)y + (float)sy / 2) * info.gridHeight);
275
* A completely free position on the screen.
279
public static class FreePosition extends Position {
280
private static final long serialVersionUID = -7306582893921329366L;
283
* Padding for freely positioned buttons
285
public static final int BUTTON_GAP = 0;
288
* The x-coordinate, between 0 and 1
292
* The y-coordinate, between 0 and 1
298
public FreePosition(float x, float y, float sx, float sy) {
306
public final RectF computeArea(ScreenInfo info) {
308
x * info.width + BUTTON_GAP,
309
y * info.height + BUTTON_GAP,
310
(x + sx) * info.width - BUTTON_GAP,
311
(y + sy) * info.height - BUTTON_GAP);
315
public final PointF computeCentre(ScreenInfo info) {
316
return new PointF((x + sx / 2) * info.width,
317
(y + sy / 2) * info.height);