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 java.util.ArrayList;
22 import java.util.Random;
24 import android.content.Context;
25 import android.graphics.Canvas;
26 import android.util.AttributeSet;
27 import android.view.MotionEvent;
28 import android.view.View;
30 import org.ocera.orte.DomainApp;
33 * View, where publisher objects are drawn.
35 * @author jiri hubacek <jiri.hubacek@gmail.com>
39 public class PublisherView extends View
41 //TODO maybe different speeds of shapes?
42 private static final int STEP_X = 2;
43 private static final int STEP_Y = 2;
45 public ArrayList<PublisherShape> shapes = new ArrayList<PublisherShape>();
47 private Random random = new Random();
50 * Just compatibility method.
56 public PublisherView(Context context, AttributeSet attrs)
58 super(context, attrs);
62 * Update publisher shape's position.
66 public void countShapes()
68 for (PublisherShape shape : shapes) {
69 if (!shape.getManual()) {
70 if (shape.getBounds().left <= 0) shape.setIncX(STEP_X);
71 if (shape.getBounds().top <= 0) shape.setIncY(STEP_Y);
72 if (shape.getBounds().right >= getWidth()) shape.setIncX(-STEP_X);
73 if (shape.getBounds().bottom >= getHeight()) shape.setIncY(-STEP_Y);
79 shape.setBounds(shape.getBounds().left + shape.getIncX(), shape.getBounds().top + shape.getIncY(), shape.getBounds().right + shape.getIncX(), shape.getBounds().bottom + shape.getIncY());
80 shape.publication.send(shape.toSend());
85 * Draw all publisher objects.
90 protected void onDraw(Canvas canvas)
92 for (PublisherShape shape : shapes) {
98 public boolean performClick()
100 return super.performClick();
104 * When touch the publisher object, start manual
105 * mode and let user change position of object. When
106 * user finishes, just generate random direction and
107 * let object moving automatically again.
112 public boolean onTouchEvent(MotionEvent event)
114 int myX = 0, myY = 0;
116 switch (event.getActionMasked()) {
117 case MotionEvent.ACTION_DOWN:
118 myX = (int) event.getX();
119 myY = (int) event.getY();
121 for (PublisherShape shape : shapes) {
122 if (myX <= shape.getBounds().right
123 && myX >= shape.getBounds().left
124 && myY >= shape.getBounds().top
125 && myY <= shape.getBounds().bottom) {
126 shape.setManual(true);
132 case MotionEvent.ACTION_MOVE:
133 myX = (int) event.getX();
134 myY = (int) event.getY();
136 for (PublisherShape shape : shapes) {
137 if (shape.getManual()) {
138 shape.setBounds((int) (myX - shape.getShape().getWidth()/2), (int) (myY - shape.getShape().getHeight()/2), (int) (myX + shape.getShape().getWidth()/2), (int) (myY + shape.getShape().getHeight()/2));
144 case MotionEvent.ACTION_UP:
145 myX = (int) event.getX();
146 myY = (int) event.getY();
148 for (PublisherShape shape : shapes) {
149 if (myX <= shape.getBounds().right
150 && myX >= shape.getBounds().left
151 && myY >= shape.getBounds().top
152 && myY <= shape.getBounds().bottom) {
153 shape.setManual(false);
154 shape.setIncX(STEP_X * (1 -2*random.nextInt(2)));
155 shape.setIncY(STEP_Y * (1 -2*random.nextInt(2)));
161 return super.onTouchEvent(event) || this.performClick();
166 * When OK is selected on {@link NewDialogFragment},
167 * this method is called to add new publisher object.
173 public void addShape(int color, DomainApp appDomain)
175 this.shapes.add(new PublisherShape(random.nextInt(3), color, appDomain));