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