~w-shackleton/droidpad-android/stable

« back to all changes in this revision

Viewing changes to src/uk/digitalsquid/droidpad/buttons/Item.java

  • Committer: William Shackleton
  • Date: 2013-04-12 14:42:12 UTC
  • mfrom: (51.3.5 json)
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: w.shackleton@gmail.com-20130412144212-4f7pueqwee0lc3yd
Imported code to load JSON layouts

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import android.graphics.Paint;
25
25
import android.graphics.Paint.Align;
26
26
import android.graphics.Paint.Style;
27
 
import android.graphics.Point;
 
27
import android.graphics.PointF;
28
28
import android.graphics.RectF;
29
29
 
30
30
public abstract class Item implements Serializable {
39
39
        public static final int FLAG_IS_RESET           = 0x40;
40
40
        
41
41
        protected static final int TEXT_SIZE = 14;
42
 
        public static final int BUTTON_GAP = 10;
43
42
        
44
43
        protected static final Paint bp = new Paint();
45
44
        protected static final Paint pText = new Paint();
84
83
 
85
84
    }
86
85
        
87
 
        public final int x, y;
88
 
        public final int sx, sy;
89
 
        
90
 
        private int lastWidth, lastHeight;
 
86
        public final Position pos;
91
87
        
92
88
        protected boolean selected = false;
93
89
        
94
90
        protected ButtonPresses callbacks;
95
 
        
96
 
        public Item(int x, int y, int sx, int sy) {
97
 
                this.x = x;
98
 
                this.y = y;
99
 
                this.sx = sx < 1 ? 1 : sx;
100
 
                this.sy = sy < 1 ? 1 : sy;
101
 
        }
102
 
        
103
 
        public final void draw(Canvas c, int width, int height, boolean landscape) {
104
 
                RectF area = computeArea(width, height);
105
 
                Point centre = computeCentre(area);
 
91
        
 
92
        /**
 
93
         * Grid constructor - use to create a grid item
 
94
         * @param x
 
95
         * @param y
 
96
         * @param sx
 
97
         * @param sy
 
98
         */
 
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);
 
101
        }
 
102
        
 
103
        /**
 
104
         * General constructor
 
105
         * @param x
 
106
         * @param y
 
107
         * @param sx
 
108
         * @param sy
 
109
         * @param free <code>true</code>, this will be a free (floating) item
 
110
         */
 
111
        public Item(float x, float y, float sx, float sy, boolean free) {
 
112
                if(free)
 
113
                        pos = new FreePosition(x, y, sx, sy);
 
114
                else
 
115
                        pos = new GridPosition((int)x, (int)y, (int)sx < 1 ? 1 : (int)sx, (int)sy < 1 ? 1 : (int)sy);
 
116
        }
 
117
        
 
118
        public final void draw(Canvas c, ScreenInfo info) {
 
119
                RectF area = pos.computeArea(info);
 
120
                PointF centre = pos.computeCentre(info);
106
121
                
107
122
                c.drawRoundRect(area, 10, 10, bp);
108
123
                
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);
111
126
        }
112
127
        
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);
114
129
        
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);
117
132
        }
118
133
        
119
 
        protected final RectF computeArea(int width, int height) {
120
 
                lastWidth = width;
121
 
                lastHeight = height;
122
 
                return new RectF(
123
 
                                x * width + BUTTON_GAP,
124
 
                                y * height + BUTTON_GAP,
125
 
                                (x + sx) * width - BUTTON_GAP,
126
 
                                (y + sy) * height - BUTTON_GAP);
127
 
        }
128
 
        protected final RectF computeArea() {
129
 
                return computeArea(lastWidth, lastHeight);
130
 
        }
131
 
        
132
 
        protected final Point computeCentre(RectF area) {
133
 
                return new Point((int)area.centerX(), (int)area.centerY());
134
 
        }
135
 
        
136
 
        protected final Point computeCentre() {
137
 
                return computeCentre(computeArea());
138
 
        }
139
 
 
140
134
        public boolean isSelected() {
141
135
                return selected;
142
136
        }
164
158
        abstract int getData2();
165
159
        abstract int getData3();
166
160
        
167
 
        public boolean pointIsInArea(float x2, float y2) {
168
 
                return computeArea().contains(x2, y2);
 
161
        public boolean pointIsInArea(ScreenInfo info, float x2, float y2) {
 
162
                return pos.computeArea(info).contains(x2, y2);
169
163
        }
170
164
        
171
165
        /**
178
172
         */
179
173
        public abstract void finaliseState();
180
174
        
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();
183
177
 
184
178
        protected void setCallbacks(ButtonPresses callbacks) {
185
179
                this.callbacks = callbacks;
 
180
        }
 
181
        
 
182
        public static class ScreenInfo {
 
183
                /**
 
184
                 * The width and height of the entire screen
 
185
                 */
 
186
                public float width, height;
 
187
                /**
 
188
                 * The width and height of one grid item
 
189
                 */
 
190
                public float gridWidth, gridHeight;
 
191
                
 
192
                public boolean landscape;
 
193
                
 
194
                public ScreenInfo() {
 
195
                        width = 1000;
 
196
                        height = 1000;
 
197
                        gridWidth = 100;
 
198
                        gridHeight = 100;
 
199
                }
 
200
                public ScreenInfo(float w, float h, float gw, float gh, boolean landscape) {
 
201
                        width = w;
 
202
                        height = h;
 
203
                        gridWidth = gw;
 
204
                        gridHeight = gh;
 
205
                        this.landscape = landscape;
 
206
                }
 
207
                public void set(float w, float h, float gw, float gh, boolean landscape) {
 
208
                        width = w;
 
209
                        height = h;
 
210
                        gridWidth = gw;
 
211
                        gridHeight = gh;
 
212
                        this.landscape = landscape;
 
213
                }
 
214
        }
 
215
        
 
216
        /**
 
217
         * Describes the position of an onscreen button.
 
218
         * Is either grid-based, or positioned absolutely.
 
219
         */
 
220
        public static abstract class Position implements Serializable {
 
221
                private static final long serialVersionUID = -7465262293417889805L;
 
222
                private Position() { }
 
223
                
 
224
                public abstract RectF computeArea(ScreenInfo info);
 
225
                public abstract PointF computeCentre(ScreenInfo info);
 
226
        }
 
227
        
 
228
        /**
 
229
         * A position which is locked to the grid
 
230
         * @author william
 
231
         *
 
232
         */
 
233
        public static class GridPosition extends Position {
 
234
                private static final long serialVersionUID = 9207230734790003508L;
 
235
                
 
236
                /**
 
237
                 * Padding on buttons for buttons on a grid
 
238
                 */
 
239
                public static final int BUTTON_GAP = 10;
 
240
                /**
 
241
                 * The x-coordinate, grid based
 
242
                 */
 
243
                int x;
 
244
                /**
 
245
                 * The y-coordinate, grid-based
 
246
                 */
 
247
                int y;
 
248
                
 
249
                int sx, sy;
 
250
                
 
251
                public GridPosition(int x, int y, int sx, int sy) {
 
252
                        this.x = x;
 
253
                        this.y = y;
 
254
                        this.sx = sx;
 
255
                        this.sy = sy;
 
256
                }
 
257
                
 
258
                @Override
 
259
                public final RectF computeArea(ScreenInfo info) {
 
260
                        return new RectF(
 
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);
 
265
                }
 
266
                
 
267
                @Override
 
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);
 
271
                }
186
272
        }
 
273
        
 
274
        /**
 
275
         * A completely free position on the screen.
 
276
         * @author william
 
277
         *
 
278
         */
 
279
        public static class FreePosition extends Position {
 
280
                private static final long serialVersionUID = -7306582893921329366L;
 
281
                
 
282
                /**
 
283
                 * Padding for freely positioned buttons
 
284
                 */
 
285
                public static final int BUTTON_GAP = 0;
 
286
                
 
287
                /**
 
288
                 * The x-coordinate, between 0 and 1
 
289
                 */
 
290
                float x;
 
291
                /**
 
292
                 * The y-coordinate, between 0 and 1
 
293
                 */
 
294
                float y;
 
295
                
 
296
                float sx, sy;
 
297
                
 
298
                public FreePosition(float x, float y, float sx, float sy) {
 
299
                        this.x = x;
 
300
                        this.y = y;
 
301
                        this.sx = sx;
 
302
                        this.sy = sy;
 
303
                }
 
304
                
 
305
                @Override
 
306
                public final RectF computeArea(ScreenInfo info) {
 
307
                        return new RectF(
 
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);
 
312
                }
 
313
                
 
314
                @Override
 
315
                public final PointF computeCentre(ScreenInfo info) {
 
316
                        return new PointF((x + sx / 2) * info.width,
 
317
                                        (y + sy / 2) * info.height);
 
318
                }
 
319
        }
187
320
}