]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/PublisherShape.java
Simplified approach to write application.
[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 static final int SHAPE_WIDTH = 48;
49         private static final int SHAPE_HEIGHT = 48;
50         private boolean manual;
51         private int incX, incY;
52         
53         private PublProp publisherProperties;
54         private NtpTime persistence;
55         
56         public Publication publication;
57         public BoxType box;
58         
59         /**
60          * Set new {@link Shape}, strength and color.
61          * 
62          * @param s strength, shape
63          * @param c color
64          * @param appDomain
65          * @since 1.0
66          */
67         public PublisherShape(int s, int c, DomainApp appDomain)
68         {
69                 super((s == 0)
70                                 ?new RectShape()
71                                 :((s == 1)
72                                         ?new OvalShape()
73                                         :((s == 2)
74                                                 ?new PathShape(new  Path(), 96, 96)
75                                                 :new RectShape()
76                                         )
77                                 )
78                         );
79                 
80                 if (s == 2) { // fix triangle
81                         this.setShape(new PathShape(PublisherShape.returnTrianglePath(), 96, 96));
82                 }
83                 
84                 this.manual = false;
85                 this.persistence = new NtpTime(5);
86
87                 this.box = new BoxType(appDomain, PublisherShape.getColorName(c));
88                 this.publisherProperties = new PublProp(this.box.getTopic(),
89                                 "BoxType",
90                                 this.persistence,
91                                 s+1);
92                 this.publication = appDomain.createPublication(this.publisherProperties, this.box);
93                 
94                 this.box.shape = (byte) s;
95                 this.box.color = (byte) c;
96
97                 this.getPaint().setColor(PublisherShape.getColorConstant(c));
98                 this.setPadding(0, 0, 0, 0);
99                 this.setBounds(0, 0, SHAPE_WIDTH, SHAPE_HEIGHT);
100                 
101                 //Log.d("PublisherShape", "s: "+s+", c: "+c+", left: "+this.getBounds().left+", top: "+this.getBounds().top+", right: "+this.getBounds().right+", bottom: "+this.getBounds().bottom);
102         }
103         
104         /**
105          * Update parameters of object to send.
106          * 
107          * @return Object to send.
108          * @since 1.0
109          */
110         public BoxType toSend()
111         {
112                 this.box.rectangle.top_left_x = (short) this.getBounds().left;
113                 this.box.rectangle.top_left_y = (short) this.getBounds().top;
114                 this.box.rectangle.bottom_right_x = (short) this.getBounds().right;
115                 this.box.rectangle.bottom_right_y = (short) this.getBounds().bottom;
116                 
117                 return this.box;
118         }
119         
120         /**
121          * Revert proprietary color to android color constant.
122          * 
123          * @param c Proprietary value of color.
124          * @return Android color constant.
125          * @since 1.0
126          */
127         public static int getColorConstant(int c)
128         {
129                 switch (c) {
130                 case 0:
131                         return Color.BLUE;
132                 case 1:
133                         return Color.GREEN;
134                 case 2:
135                         return Color.RED;
136                 case 3:
137                         return Color.BLACK;
138                 case 4:
139                         return Color.YELLOW;
140                 default:
141                         return 0;
142                 }
143         }
144         
145         /**
146          * Revert proprietary color to android color constant.
147          * 
148          * @return Android color constant.
149          * @since 1.0
150          */
151         public int getColorConstant()
152         {
153                 return PublisherShape.getColorConstant(this.box.color);
154         }
155         
156         /**
157          * Revert proprietary color to word.
158          * 
159          * @param c Proprietary value of color.
160          * @return Name of color.
161          * @since 1.0
162          */
163         public static String getColorName(int c)
164         {
165                 // should be done better way
166                 // texts shouldn't be static
167                 // but dynamically mapped to
168                 // strings.xml
169                 
170                 switch (c) {
171                 case 0:
172                         return "Blue";
173                 case 1:
174                         return "Green";
175                 case 2:
176                         return "Red";
177                 case 3:
178                         return "Black";
179                 case 4:
180                         return "Yellow";
181                 default:
182                         return "NONE";
183                 }
184         }
185         
186         /**
187          * Revert proprietary color to word.
188          * 
189          * @return Name of color.
190          * @since 1.0
191          */
192         public String getColorName()
193         {
194                 return PublisherShape.getColorName(this.box.color);
195         }
196         
197         /**
198          * Revert strength to shape.
199          * 
200          * @return Name of shape.
201          * @since 1.0
202          */
203         public String getShapeName()
204         {
205                 switch (this.box.shape) {
206                 case 0:
207                         return "Square";
208                 case 1:
209                         return "Circle";
210                 case 2:
211                         return "Triangle";
212                 default:
213                         return "NONE";
214                 }
215         }
216         
217         /* getters, setters */
218         
219         /**
220          * Set value of incX - how much move object
221          * in x-axis direction.
222          * 
223          * @param x
224          * @since 1.0
225          */
226         public void setIncX(int x) { this.incX = x; }
227         /**
228          * Get value of incX - how much move object
229          * in x-axis direction.
230          * 
231          * @return Distance to move in x-axis. 
232          * @since 1.0
233          */
234         public int getIncX() { return this.incX; }
235         
236         /**
237          * Set value of incY - how much move object
238          * in y-axis direction.
239          * 
240          * @param y
241          * @since 1.0
242          */
243         public void setIncY(int y) { this.incY = y; }
244         /**
245          * Get value of incY - how much move object
246          * in y-axis direction.
247          * 
248          * @return Distance to move in y-axis. 
249          * @since 1.0
250          */
251         public int getIncY() { return this.incY; }
252         
253         /**
254          * Set strength of publishing.
255          * 
256          * @param s
257          * @since 1.0
258          */
259         public void setStrength(int s) {
260                 PublProp properties = this.publication.getProperties();
261                 properties.setStrength(s);
262                 this.publication.setProperties(properties);
263         }
264         /**
265          * Get strength of publishing.
266          * 
267          * @return Strength of publishing.
268          * @since 1.0
269          */
270         public int getStrength() { return this.publication.getProperties().getStrength(); }
271         
272         /**
273          * Set if object is moved automatically or by
274          * user.
275          *      TRUE - by user
276          *  FALSE - automatically
277          *  
278          * @param b
279          * @since 1.0
280          */
281         public void setManual(boolean b) { this.manual = b; }
282         /**
283          * Get if object is moved automatically or by
284          * user.
285          * 
286          * @return How is object moved.
287          * @since 1.0
288          */
289         public boolean getManual() { return this.manual; }
290
291         /**
292          * Make triangle path for new instance.
293          * 
294          * @return Triangle path.
295          * @since 1.0
296          */
297         public static Path returnTrianglePath()
298         {
299                 Path path = new Path();
300                 path.setFillType(FillType.EVEN_ODD);
301                 path.moveTo(48, 0);
302                 path.lineTo(96, 96);
303                 path.lineTo(0, 96);
304                 path.lineTo(48, 0);
305                 path.close();
306                 
307                 return path;
308         }
309         
310         /**
311          * When publisher is removed, destroy publication.
312          * 
313          * @since 1.0
314          */
315         public void killMe()
316         {
317                 this.publication.destroy();
318         }
319         
320         /**
321          * Pass parameters to box.
322          * 
323          * @param currentWidth
324          * @param currentHeight
325          * @since 1.0
326          */
327         public void setScale(int currentWidth, int currentHeight)
328         {
329                 this.box.setScale(currentWidth, currentHeight);
330         }
331 }