]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/PublisherShape.java
Android Shape: Add support for multitouch gestures
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape_android / PublisherShape.java
1 /**
2  * 
3  *      This file is part of shape_android.
4  *
5  *  shape_android is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  shape_android is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with shape_android.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 package org.ocera.orte.shape_android;
20
21 import org.ocera.orte.DomainApp;
22 import org.ocera.orte.Publication;
23 import org.ocera.orte.types.NtpTime;
24 import org.ocera.orte.types.PublProp;
25
26 import android.graphics.Color;
27 import android.graphics.Path;
28 import android.graphics.Path.FillType;
29 import android.graphics.drawable.ShapeDrawable;
30 import android.graphics.drawable.shapes.OvalShape;
31 import android.graphics.drawable.shapes.PathShape;
32 import android.graphics.drawable.shapes.RectShape;
33 import android.graphics.drawable.shapes.Shape;
34
35 /**
36  * Some additional parameters for {@link ShapeDrawable}
37  * to make it Publisher-friendly.
38  * 
39  * @author jiri hubacek <jiri.hubacek@gmail.com>
40  * @version %I%, %G%
41  * @since 1.0
42  */
43 public class PublisherShape extends ShapeDrawable
44 {
45         // Prefer at least 48x48 shape size (Full asset), for more
46         //  info, please refer to http://developer.android.com/
47         
48         private boolean manual;
49         private int pointerId;
50         private int incX, incY;
51         
52         private PublProp publisherProperties;
53         private NtpTime persistence;
54         
55         public Publication publication;
56         public BoxType box;
57         
58         /**
59          * Set new {@link Shape}, strength and color.
60          * 
61          * @param s strength, shape
62          * @param c color
63          * @param appDomain
64          * @since 1.0
65          */
66         public PublisherShape(int s, int c, DomainApp appDomain)
67         {
68                 super((s == 0)
69                                 ?new RectShape()
70                                 :((s == 1)
71                                         ?new OvalShape()
72                                         :((s == 2)
73                                                 ?new PathShape(new  Path(), PublisherActivity.SHAPE_WIDTH, PublisherActivity.SHAPE_HEIGHT)
74                                                 :new RectShape()
75                                         )
76                                 )
77                         );
78                 
79                 if (s == 2) { // fix triangle
80                         this.setShape(new PathShape(PublisherShape.returnTrianglePath(), 2*PublisherActivity.SHAPE_WIDTH, 2*PublisherActivity.SHAPE_HEIGHT));
81                 }
82                 
83                 this.manual = false;
84                 this.persistence = new NtpTime(5);
85
86                 this.box = new BoxType(appDomain, PublisherShape.getColorName(c));
87                 this.publisherProperties = new PublProp(this.box.getTopic(),
88                                 "BoxType",
89                                 this.persistence,
90                                 s+1);
91                 this.publication = appDomain.createPublication(this.publisherProperties, this.box);
92                 
93                 this.box.shape = s;
94                 this.box.color = c;
95
96                 this.getPaint().setColor(PublisherShape.getColorConstant(c));
97                 this.setPadding(0, 0, 0, 0);
98                 this.setBounds(0, 0, PublisherActivity.SHAPE_WIDTH, PublisherActivity.SHAPE_HEIGHT);
99                 
100                 //Log.d("PublisherShape", "s: "+s+", c: "+c+", left: "+this.getBounds().left+", top: "+this.getBounds().top+", right: "+this.getBounds().right+", bottom: "+this.getBounds().bottom);
101         }
102         
103         /**
104          * Update parameters of object to send.
105          * 
106          * @return Object to send.
107          * @since 1.0
108          */
109         public BoxType toSend()
110         {
111                 this.box.rectangle.top_left_x = (short) this.getBounds().left;
112                 this.box.rectangle.top_left_y = (short) this.getBounds().top;
113                 this.box.rectangle.bottom_right_x = (short) this.getBounds().right;
114                 this.box.rectangle.bottom_right_y = (short) this.getBounds().bottom;
115                 
116                 return this.box;
117         }
118         
119         /**
120          * Revert proprietary color to android color constant.
121          * 
122          * @param c Proprietary value of color.
123          * @return Android color constant.
124          * @since 1.0
125          */
126         public static int getColorConstant(int c)
127         {
128                 switch (c) {
129                 case 0:
130                         return Color.BLUE;
131                 case 1:
132                         return Color.GREEN;
133                 case 2:
134                         return Color.RED;
135                 case 3:
136                         return Color.BLACK;
137                 case 4:
138                         return Color.YELLOW;
139                 default:
140                         return 0;
141                 }
142         }
143         
144         /**
145          * Revert proprietary color to android color constant.
146          * 
147          * @return Android color constant.
148          * @since 1.0
149          */
150         public int getColorConstant()
151         {
152                 return PublisherShape.getColorConstant(this.box.color);
153         }
154         
155         /**
156          * Revert proprietary color to word.
157          * 
158          * @param c Proprietary value of color.
159          * @return Name of color.
160          * @since 1.0
161          */
162         public static String getColorName(int c)
163         {
164                 // should be done better way
165                 // texts shouldn't be static
166                 // but dynamically mapped to
167                 // strings.xml
168                 
169                 switch (c) {
170                 case 0:
171                         return "Blue";
172                 case 1:
173                         return "Green";
174                 case 2:
175                         return "Red";
176                 case 3:
177                         return "Black";
178                 case 4:
179                         return "Yellow";
180                 default:
181                         return "NONE";
182                 }
183         }
184         
185         /**
186          * Revert proprietary color to word.
187          * 
188          * @return Name of color.
189          * @since 1.0
190          */
191         public String getColorName()
192         {
193                 return PublisherShape.getColorName(this.box.color);
194         }
195         
196         /**
197          * Revert strength to shape.
198          * 
199          * @return Name of shape.
200          * @since 1.0
201          */
202         public String getShapeName()
203         {
204                 switch (this.box.shape) {
205                 case 0:
206                         return "Square";
207                 case 1:
208                         return "Circle";
209                 case 2:
210                         return "Triangle";
211                 default:
212                         return "NONE";
213                 }
214         }
215         
216         /* getters, setters */
217         
218         /**
219          * Set value of incX - how much move object
220          * in x-axis direction.
221          * 
222          * @param x
223          * @since 1.0
224          */
225         public void setIncX(int x) { this.incX = x; }
226         /**
227          * Get value of incX - how much move object
228          * in x-axis direction.
229          * 
230          * @return Distance to move in x-axis. 
231          * @since 1.0
232          */
233         public int getIncX() { return this.incX; }
234         
235         /**
236          * Set value of incY - how much move object
237          * in y-axis direction.
238          * 
239          * @param y
240          * @since 1.0
241          */
242         public void setIncY(int y) { this.incY = y; }
243         /**
244          * Get value of incY - how much move object
245          * in y-axis direction.
246          * 
247          * @return Distance to move in y-axis. 
248          * @since 1.0
249          */
250         public int getIncY() { return this.incY; }
251         
252         /**
253          * Set strength of publishing.
254          * 
255          * @param s
256          * @since 1.0
257          */
258         public void setStrength(int s) {
259                 PublProp properties = this.publication.getProperties();
260                 properties.setStrength(s);
261                 this.publication.setProperties(properties);
262         }
263         /**
264          * Get strength of publishing.
265          * 
266          * @return Strength of publishing.
267          * @since 1.0
268          */
269         public int getStrength() { return this.publication.getProperties().getStrength(); }
270         
271         /**
272          * Set if object is moved automatically or by
273          * user.
274          *      TRUE - by user
275          *  FALSE - automatically
276          *  
277          * @param b
278          * @since 1.0
279          */
280         public void setManual(boolean b) { this.manual = b; }
281         /**
282          * Get if object is moved automatically or by
283          * user.
284          * 
285          * @return How is object moved.
286          * @since 1.0
287          */
288         public boolean getManual() { return this.manual; }
289
290         /**
291          * Set the touch pointer ID associated with
292          * this shape.
293          *
294          * @param pointerId
295          * @since 1.0
296          */
297         public void setPointerId(int pointerId) { this.pointerId = pointerId; }
298
299         /**
300          * Get the touch pointer ID associated with
301          * this shape.
302          *
303          * @return Pointer ID.
304          * @since 1.0
305          */
306         public int getPointerId() { return this.pointerId; }
307
308         /**
309          * Make triangle path for new instance.
310          * 
311          * @return Triangle path.
312          * @since 1.0
313          */
314         public static Path returnTrianglePath()
315         {
316                 Path path = new Path();
317                 path.setFillType(FillType.EVEN_ODD);
318                 path.moveTo(PublisherActivity.SHAPE_WIDTH, 0); // triangle top center
319                 path.lineTo(2*PublisherActivity.SHAPE_WIDTH, 2*PublisherActivity.SHAPE_HEIGHT); // triangle right bottom corner
320                 path.lineTo(0, 2*PublisherActivity.SHAPE_HEIGHT); // triangle left bottom corner
321                 path.lineTo(PublisherActivity.SHAPE_WIDTH, 0); // back to triangle top center
322                 path.close();
323                 
324                 return path;
325         }
326         
327         /**
328          * When publisher is removed, destroy publication.
329          * 
330          * @since 1.0
331          */
332         public void killMe()
333         {
334                 if (this.publication != null) {
335                         this.publication.destroy();
336                         this.publication = null;
337                 }
338         }
339         
340         /**
341          * Pass parameters to box.
342          * 
343          * @param currentWidth
344          * @param currentHeight
345          * @since 1.0
346          */
347         public void setScale(int currentWidth, int currentHeight)
348         {
349                 this.box.setScale(currentWidth, currentHeight);
350                 this.setBounds(this.box.rectangle.top_left_x, this.box.rectangle.top_left_y, this.box.rectangle.top_left_x+PublisherActivity.SHAPE_WIDTH, this.box.rectangle.top_left_y+PublisherActivity.SHAPE_HEIGHT);
351         }
352 }