]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/BoxType.java
Files which should be omitted.
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape_android / BoxType.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 org.ocera.orte.DomainApp;
22 import org.ocera.orte.types.MessageData;
23 import org.ocera.orte.types.ORTEConstant;
24
25 import android.content.SharedPreferences;
26 import android.preference.PreferenceManager;
27 import android.util.Log;
28
29 /**
30  * Class for object sent and received throw the ORTE middleware.
31  * 
32  * @author jiri hubacek <jiri.hubacek@gmail.com>
33  * @version %I, %G
34  * @since 1.1
35  */
36 public class BoxType extends MessageData
37 {
38         private static final byte FUZZY = 0;
39         private static final double DESTINATION_WIDTH = 367.0; //389.0;
40         private static final double DESTINATION_HEIGHT = 261.0; //331.0;
41         
42         public byte color;
43         public byte shape;
44         public BoxRect rectangle = new BoxRect();
45         
46         public boolean allowScaling;
47         private double scaleWidth;
48         private double scaleHeight;
49         
50         /**
51          * Register new domain type, set default params.
52          * 
53          * @since 1.1
54          */
55         public BoxType(DomainApp appDomain, String newTopic)
56         {
57                 super();
58                 this.setTopic(newTopic);
59                 
60                 if (!appDomain.regNewDataType("BoxType", this.getMaxDataLength())) {
61                         Log.e("BoxType", "Cannot register data type 'BoxType'.");
62                 }
63                 
64                 this.allowScaling = true;
65         }
66
67         /* (non-Javadoc)
68          * @see org.ocera.orte.types.MessageData#read()
69          */
70         @Override
71         public void read() 
72         {
73                 buffer.rewind();
74                 
75                 // get color
76                 this.color = buffer.get();
77                 
78                 // skip fuzzy bytes
79                 buffer.get();buffer.get();buffer.get();
80                 
81                 // get shape
82                 this.shape = buffer.get();
83                 
84                 // skip fuzzy bytes
85                 buffer.get();buffer.get();buffer.get();
86                 
87                 // get rect position (with scaling)
88                 if (this.allowScaling) {
89                         this.rectangle.top_left_x = (short) (buffer.getShort() / this.scaleWidth);
90                         this.rectangle.top_left_y = (short) (buffer.getShort() / this.scaleHeight);
91                         this.rectangle.bottom_right_x = (short) (buffer.getShort() / this.scaleWidth);
92                         this.rectangle.bottom_right_y = (short) (buffer.getShort() / this.scaleHeight);
93                 } else {
94                         this.rectangle.top_left_x = buffer.getShort();
95                         this.rectangle.top_left_y = buffer.getShort();
96                         this.rectangle.bottom_right_x = buffer.getShort();
97                         this.rectangle.bottom_right_y = buffer.getShort();
98                 }
99                 
100                 // don't care about last fuzzy byte because of buffer.rewind()
101                 
102                 //Log.d("BoxType", "receiving - color: "+this.color+", shape: "+this.shape+ ", rectangle:{"+this.rectangle.top_left_x+","+this.rectangle.top_left_y+","+this.rectangle.bottom_right_x+","+this.rectangle.bottom_right_y+",}");
103         }
104
105         /* (non-Javadoc)
106          * @see org.ocera.orte.types.MessageData#write()
107          */
108         @Override
109         public void write()
110         {
111                 buffer.rewind();
112                 
113                 // put color
114                 buffer.put(this.color);
115                 
116                 // put fuzzy bytes
117                 buffer.put(BoxType.FUZZY);buffer.put(BoxType.FUZZY);buffer.put(BoxType.FUZZY);
118                 
119                 // put shape
120                 buffer.put(this.shape);
121                 
122                 // put fuzzy bytes
123                 buffer.put(BoxType.FUZZY);buffer.put(BoxType.FUZZY);buffer.put(BoxType.FUZZY);
124                 
125                 // put rectange position (with scaling)
126                 if (this.allowScaling) {
127                         buffer.putShort((short) (this.rectangle.top_left_x * this.scaleWidth));
128                         buffer.putShort((short) (this.rectangle.top_left_y * this.scaleHeight));
129                         buffer.putShort((short) (this.rectangle.bottom_right_x * this.scaleWidth));
130                         buffer.putShort((short) (this.rectangle.bottom_right_y * this.scaleHeight));
131                 } else {
132                         buffer.putShort(this.rectangle.top_left_x);
133                         buffer.putShort(this.rectangle.top_left_y);
134                         buffer.putShort(this.rectangle.bottom_right_x);
135                         buffer.putShort(this.rectangle.bottom_right_y);
136                 }
137                 
138                 // put fuzzy byte
139                 buffer.put(BoxType.FUZZY);
140         }
141
142         /* (non-Javadoc)
143          * @see org.ocera.orte.types.MessageData#getMaxDataLength()
144          */
145         @Override
146         public int getMaxDataLength()
147         {
148                 return ORTEConstant.BYTE_FIELD_SIZE + ORTEConstant.LONG_FIELD_SIZE + 4*ORTEConstant.SHORT_FIELD_SIZE;
149         }
150         
151         /**
152          * When screen rotates, change scale variables to
153          * fit the destination screen.
154          * 
155          * Method of former Box class.
156          * 
157          * @since 1.0
158          */
159         public void setScale(int currentWidth, int currentHeight)
160         {
161                 this.scaleWidth = DESTINATION_WIDTH / currentWidth;
162                 this.scaleHeight = DESTINATION_HEIGHT / currentHeight;
163         }
164         
165         /**
166          * Object parameters to be sent throw ORTE.
167          * 
168          * Class from former Box class.
169          * 
170          * @since 1.0
171          */
172         public class BoxRect
173         {
174                 public short top_left_x;
175                 public short top_left_y;
176                 public short bottom_right_x;
177                 public short bottom_right_y;
178                 
179                 public BoxRect()
180                 {}
181         }
182 }