]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/PublisherView.java
Merge branch 'shape_android_features' of https://github.com/Vajnar/orte
[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                 case MotionEvent.ACTION_POINTER_DOWN:
119                         myX = (int) event.getX(event.getActionIndex());
120                         myY = (int) event.getY(event.getActionIndex());
121                         
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()));
129                                         break;
130                                 }
131                         }
132                         
133                         return true;
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()));
139
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));
141                                 }
142                         }
143                         
144                         return true;
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());
149                         
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)));
158                                 }
159                         }
160                         
161                         return true;
162                 default:
163                         return super.onTouchEvent(event) || this.performClick();
164                 }
165         }
166         
167         /**
168          * When OK is selected on {@link NewDialogFragment},
169          * this method is called to add new publisher object.
170          *  
171          * @param color
172          * @param appDomain
173          * @since 1.0
174          */
175         public void addShape(int color, DomainApp appDomain)
176         {
177                 this.shapes.add(new PublisherShape(random.nextInt(3), color, appDomain));
178         }
179 }