]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape4a/PublisherView.java
d079a9358098626a92715bf217a60668dbb87428
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape4a / PublisherView.java
1 /**
2  * 
3  *      This file is part of shape4a.
4  *
5  *  shape4a 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  *  shape4a 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 shape4a.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 package org.ocera.orte.shape4a;
20
21 import java.util.ArrayList;
22 import java.util.Random;
23
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;
29
30 import org.ocera.orte.DomainApp;
31
32 /**
33  * View, where publisher objects are drawn.
34  * 
35  * @author jiri hubacek <jiri.hubacek@gmail.com>
36  * @version %I%, %G%
37  * @since 1.0
38  */
39 public class PublisherView extends View
40 {
41         private static final int STEP_X = 2;
42         private static final int STEP_Y = 2;
43
44         public ArrayList<PublisherShape> shapes = new ArrayList<PublisherShape>();
45         
46         private Random random = new Random();
47         
48         /**
49          * Just compatibility method.
50          * 
51          * @param context
52          * @param attrs
53          * @since 1.0
54          */
55         public PublisherView(Context context, AttributeSet attrs)
56         {
57                 super(context, attrs);
58         }
59         
60         /**
61          * Update publisher shape's position.
62          * 
63          * @since 1.0
64          */
65         public void countShapes()
66         {
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);
73                         } else {
74                                 shape.setIncX(0);
75                                 shape.setIncY(0);
76                         }
77                         
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());
80                 }
81         }       
82         
83         /**
84          * Draw all publisher objects.
85          * 
86          * @since 1.0
87          */
88         @Override
89         protected void onDraw(Canvas canvas)
90         {
91                 for (PublisherShape shape : shapes) {
92                         shape.draw(canvas);
93                 }
94         }
95
96         @Override
97         public boolean performClick()
98         {
99                 return super.performClick();
100         }
101         
102         /**
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.
107          * 
108          * @since 1.0
109          */
110         @Override
111         public boolean onTouchEvent(MotionEvent event)
112         {
113                 int myX = 0, myY = 0;
114                 
115                 switch (event.getActionMasked()) {
116                 case MotionEvent.ACTION_DOWN:
117                         myX = (int) event.getX();
118                         myY = (int) event.getY();
119                         
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);
126                                         break;
127                                 }
128                         }
129                         
130                         return true;
131                 case MotionEvent.ACTION_MOVE:
132                         myX = (int) event.getX();
133                         myY = (int) event.getY();
134                         
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));
138                                         break;
139                                 }
140                         }
141                         
142                         return true;
143                 case MotionEvent.ACTION_UP:
144                         myX = (int) event.getX();
145                         myY = (int) event.getY();
146                         
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)));
155                                 }
156                         }
157                         
158                         return true;
159                 default:
160                         return super.onTouchEvent(event) || this.performClick();
161                 }
162         }
163         
164         /**
165          * When OK is selected on {@link NewDialogFragment},
166          * this method is called to add new publisher object.
167          *  
168          * @param color
169          * @param appDomain
170          * @since 1.0
171          */
172         public void addShape(int color, DomainApp appDomain)
173         {
174                 this.shapes.add(new PublisherShape(random.nextInt(3), color, appDomain));
175         }
176 }