]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/PublisherView.java
Fix scaling.
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape_android / PublisherView.java
1 /**
2  * 
3  *      This file is part of shape_android.
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  */
19 package org.ocera.orte.shape_android;
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         //TODO maybe different speeds of shapes?
42         private static final int STEP_X = 2;
43         private static final int STEP_Y = 2;
44
45         public ArrayList<PublisherShape> shapes = new ArrayList<PublisherShape>();
46         
47         private Random random = new Random();
48         
49         /**
50          * Just compatibility method.
51          * 
52          * @param context
53          * @param attrs
54          * @since 1.0
55          */
56         public PublisherView(Context context, AttributeSet attrs)
57         {
58                 super(context, attrs);
59         }
60         
61         /**
62          * Update publisher shape's position.
63          * 
64          * @since 1.0
65          */
66         public void countShapes()
67         {
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);
74                         } else {
75                                 shape.setIncX(0);
76                                 shape.setIncY(0);
77                         }
78                         
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());
81                 }
82         }       
83         
84         /**
85          * Draw all publisher objects.
86          * 
87          * @since 1.0
88          */
89         @Override
90         protected void onDraw(Canvas canvas)
91         {
92                 for (PublisherShape shape : shapes) {
93                         shape.draw(canvas);
94                 }
95         }
96
97         @Override
98         public boolean performClick()
99         {
100                 return super.performClick();
101         }
102         
103         /**
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.
108          * 
109          * @since 1.0
110          */
111         @Override
112         public boolean onTouchEvent(MotionEvent event)
113         {
114                 int myX = 0, myY = 0;
115                 
116                 switch (event.getActionMasked()) {
117                 case MotionEvent.ACTION_DOWN:
118                         myX = (int) event.getX();
119                         myY = (int) event.getY();
120                         
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);
127                                         break;
128                                 }
129                         }
130                         
131                         return true;
132                 case MotionEvent.ACTION_MOVE:
133                         myX = (int) event.getX();
134                         myY = (int) event.getY();
135                         
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));
139                                         break;
140                                 }
141                         }
142                         
143                         return true;
144                 case MotionEvent.ACTION_UP:
145                         myX = (int) event.getX();
146                         myY = (int) event.getY();
147                         
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)));
156                                 }
157                         }
158                         
159                         return true;
160                 default:
161                         return super.onTouchEvent(event) || this.performClick();
162                 }
163         }
164         
165         /**
166          * When OK is selected on {@link NewDialogFragment},
167          * this method is called to add new publisher object.
168          *  
169          * @param color
170          * @param appDomain
171          * @since 1.0
172          */
173         public void addShape(int color, DomainApp appDomain)
174         {
175                 this.shapes.add(new PublisherShape(random.nextInt(3), color, appDomain));
176         }
177 }