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 private static final int STEP_X = 2;
42 private static final int STEP_Y = 2;
44 public ArrayList<PublisherShape> shapes = new ArrayList<PublisherShape>();
46 private Random random = new Random();
49 * Just compatibility method.
55 public PublisherView(Context context, AttributeSet attrs)
57 super(context, attrs);
61 * Update publisher shape's position.
65 public void countShapes()
67 for (PublisherShape shape : shapes) {
68 if (!shape.getManual()) {
69 if (shape.getBounds().left <= 0) shape.setIncX(STEP_X);
70 if (shape.getBounds().top <= 0) shape.setIncY(STEP_Y);
71 if (shape.getBounds().right >= getWidth()) shape.setIncX(-STEP_X);
72 if (shape.getBounds().bottom >= getHeight()) shape.setIncY(-STEP_Y);
78 shape.setBounds(shape.getBounds().left + shape.getIncX(), shape.getBounds().top + shape.getIncY(), shape.getBounds().right + shape.getIncX(), shape.getBounds().bottom + shape.getIncY());
79 shape.publication.send(shape.toSend());
84 * Draw all publisher objects.
89 protected void onDraw(Canvas canvas)
91 for (PublisherShape shape : shapes) {
97 public boolean performClick()
99 return super.performClick();
103 * When touch the publisher object, start manual
104 * mode and let user change position of object. When
105 * user finishes, just generate random direction and
106 * let object moving automatically again.
111 public boolean onTouchEvent(MotionEvent event)
113 int myX = 0, myY = 0;
115 switch (event.getActionMasked()) {
116 case MotionEvent.ACTION_DOWN:
117 myX = (int) event.getX();
118 myY = (int) event.getY();
120 for (PublisherShape shape : shapes) {
121 if (myX <= shape.getBounds().right
122 && myX >= shape.getBounds().left
123 && myY >= shape.getBounds().top
124 && myY <= shape.getBounds().bottom) {
125 shape.setManual(true);
131 case MotionEvent.ACTION_MOVE:
132 myX = (int) event.getX();
133 myY = (int) event.getY();
135 for (PublisherShape shape : shapes) {
136 if (shape.getManual()) {
137 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));
143 case MotionEvent.ACTION_UP:
144 myX = (int) event.getX();
145 myY = (int) event.getY();
147 for (PublisherShape shape : shapes) {
148 if (myX <= shape.getBounds().right
149 && myX >= shape.getBounds().left
150 && myY >= shape.getBounds().top
151 && myY <= shape.getBounds().bottom) {
152 shape.setManual(false);
153 shape.setIncX(STEP_X * (1 -2*random.nextInt(2)));
154 shape.setIncY(STEP_Y * (1 -2*random.nextInt(2)));
160 return super.onTouchEvent(event) || this.performClick();
165 * When OK is selected on {@link NewDialogFragment},
166 * this method is called to add new publisher object.
172 public void addShape(int color, DomainApp appDomain)
174 this.shapes.add(new PublisherShape(random.nextInt(3), color, appDomain));