3 * This file is part of shape_android.
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.
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.
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/>.
19 package org.ocera.orte.shape_android;
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;
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;
36 * Some additional parameters for {@link ShapeDrawable}
37 * to make it Publisher-friendly.
39 * @author jiri hubacek <jiri.hubacek@gmail.com>
43 public class PublisherShape extends ShapeDrawable
45 // Prefer at least 48x48 shape size (Full asset), for more
46 // info, please refer to http://developer.android.com/
48 private boolean manual;
49 private int incX, incY;
51 private PublProp publisherProperties;
52 private NtpTime persistence;
54 public Publication publication;
58 * Set new {@link Shape}, strength and color.
60 * @param s strength, shape
65 public PublisherShape(int s, int c, DomainApp appDomain)
72 ?new PathShape(new Path(), PublisherActivity.SHAPE_WIDTH, PublisherActivity.SHAPE_HEIGHT)
78 if (s == 2) { // fix triangle
79 this.setShape(new PathShape(PublisherShape.returnTrianglePath(), 2*PublisherActivity.SHAPE_WIDTH, 2*PublisherActivity.SHAPE_HEIGHT));
83 this.persistence = new NtpTime(5);
85 this.box = new BoxType(appDomain, PublisherShape.getColorName(c));
86 this.publisherProperties = new PublProp(this.box.getTopic(),
90 this.publication = appDomain.createPublication(this.publisherProperties, this.box);
95 this.getPaint().setColor(PublisherShape.getColorConstant(c));
96 this.setPadding(0, 0, 0, 0);
97 this.setBounds(0, 0, PublisherActivity.SHAPE_WIDTH, PublisherActivity.SHAPE_HEIGHT);
99 //Log.d("PublisherShape", "s: "+s+", c: "+c+", left: "+this.getBounds().left+", top: "+this.getBounds().top+", right: "+this.getBounds().right+", bottom: "+this.getBounds().bottom);
103 * Update parameters of object to send.
105 * @return Object to send.
108 public BoxType toSend()
110 this.box.rectangle.top_left_x = (short) this.getBounds().left;
111 this.box.rectangle.top_left_y = (short) this.getBounds().top;
112 this.box.rectangle.bottom_right_x = (short) this.getBounds().right;
113 this.box.rectangle.bottom_right_y = (short) this.getBounds().bottom;
119 * Revert proprietary color to android color constant.
121 * @param c Proprietary value of color.
122 * @return Android color constant.
125 public static int getColorConstant(int c)
144 * Revert proprietary color to android color constant.
146 * @return Android color constant.
149 public int getColorConstant()
151 return PublisherShape.getColorConstant(this.box.color);
155 * Revert proprietary color to word.
157 * @param c Proprietary value of color.
158 * @return Name of color.
161 public static String getColorName(int c)
163 // should be done better way
164 // texts shouldn't be static
165 // but dynamically mapped to
185 * Revert proprietary color to word.
187 * @return Name of color.
190 public String getColorName()
192 return PublisherShape.getColorName(this.box.color);
196 * Revert strength to shape.
198 * @return Name of shape.
201 public String getShapeName()
203 switch (this.box.shape) {
215 /* getters, setters */
218 * Set value of incX - how much move object
219 * in x-axis direction.
224 public void setIncX(int x) { this.incX = x; }
226 * Get value of incX - how much move object
227 * in x-axis direction.
229 * @return Distance to move in x-axis.
232 public int getIncX() { return this.incX; }
235 * Set value of incY - how much move object
236 * in y-axis direction.
241 public void setIncY(int y) { this.incY = y; }
243 * Get value of incY - how much move object
244 * in y-axis direction.
246 * @return Distance to move in y-axis.
249 public int getIncY() { return this.incY; }
252 * Set strength of publishing.
257 public void setStrength(int s) {
258 PublProp properties = this.publication.getProperties();
259 properties.setStrength(s);
260 this.publication.setProperties(properties);
263 * Get strength of publishing.
265 * @return Strength of publishing.
268 public int getStrength() { return this.publication.getProperties().getStrength(); }
271 * Set if object is moved automatically or by
274 * FALSE - automatically
279 public void setManual(boolean b) { this.manual = b; }
281 * Get if object is moved automatically or by
284 * @return How is object moved.
287 public boolean getManual() { return this.manual; }
290 * Make triangle path for new instance.
292 * @return Triangle path.
295 public static Path returnTrianglePath()
297 Path path = new Path();
298 path.setFillType(FillType.EVEN_ODD);
299 path.moveTo(PublisherActivity.SHAPE_WIDTH, 0); // triangle top center
300 path.lineTo(2*PublisherActivity.SHAPE_WIDTH, 2*PublisherActivity.SHAPE_HEIGHT); // triangle right bottom corner
301 path.lineTo(0, 2*PublisherActivity.SHAPE_HEIGHT); // triangle left bottom corner
302 path.lineTo(PublisherActivity.SHAPE_WIDTH, 0); // back to triangle top center
309 * When publisher is removed, destroy publication.
315 if (this.publication != null) {
316 this.publication.destroy();
317 this.publication = null;
322 * Pass parameters to box.
324 * @param currentWidth
325 * @param currentHeight
328 public void setScale(int currentWidth, int currentHeight)
330 this.box.setScale(currentWidth, currentHeight);
331 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);