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 case MotionEvent.ACTION_POINTER_DOWN:
119 myX = (int) event.getX(event.getActionIndex());
120 myY = (int) event.getY(event.getActionIndex());
122 for (PublisherShape shape : shapes) {
123 if (myX <= shape.getBounds().right
124 && myX >= shape.getBounds().left
125 && myY >= shape.getBounds().top
126 && myY <= shape.getBounds().bottom) {
127 shape.setManual(true);
128 shape.setPointerId(event.getPointerId(event.getActionIndex()));
134 case MotionEvent.ACTION_MOVE:
135 for (PublisherShape shape : shapes) {
136 if (shape.getManual()) {
137 myX = (int) event.getX(event.findPointerIndex(shape.getPointerId()));
138 myY = (int) event.getY(event.findPointerIndex(shape.getPointerId()));
140 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));
145 case MotionEvent.ACTION_UP:
146 case MotionEvent.ACTION_POINTER_UP:
147 myX = (int) event.getX(event.getActionIndex());
148 myY = (int) event.getY(event.getActionIndex());
150 for (PublisherShape shape : shapes) {
151 if (myX <= shape.getBounds().right
152 && myX >= shape.getBounds().left
153 && myY >= shape.getBounds().top
154 && myY <= shape.getBounds().bottom) {
155 shape.setManual(false);
156 shape.setIncX(STEP_X * (1 -2*random.nextInt(2)));
157 shape.setIncY(STEP_Y * (1 -2*random.nextInt(2)));
163 return super.onTouchEvent(event) || this.performClick();
168 * When OK is selected on {@link NewDialogFragment},
169 * this method is called to add new publisher object.
175 public void addShape(int color, DomainApp appDomain)
177 this.shapes.add(new PublisherShape(random.nextInt(3), color, appDomain));