]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/BoxType.java
417e4da2e7722c0d8e544eec5c3d511a7c82904f
[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.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 double DESTINATION_WIDTH = 367.0; //389.0;
37         private static final double DESTINATION_HEIGHT = 261.0; //331.0;
38         
39         public int color;
40         public int shape;
41         public BoxRect rectangle = new BoxRect();
42         
43         public boolean allowScaling;
44         private double scaleWidth;
45         private double scaleHeight;
46         
47         /**
48          * Register new domain type, set default params.
49          * 
50          * @since 1.1
51          */
52         public BoxType(DomainApp appDomain, String newTopic)
53         {
54                 super();
55                 this.setTopic(newTopic);
56                 
57                 if (!appDomain.regNewDataType("BoxType", this.getMaxDataLength())) {
58                         Log.e("BoxType", "Cannot register data type 'BoxType'.");
59                 }
60                 
61                 this.allowScaling = true;
62                 this.buffer.order(null); // set buffer to Little endian (Shape uses this)
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.getInt();
75                 
76                 // get shape
77                 this.shape = buffer.getInt();
78                 
79                 // get rect position (with scaling)
80                 if (this.allowScaling) {
81                         this.rectangle.top_left_x = (short) (buffer.getShort() / this.scaleWidth);
82                         this.rectangle.top_left_y = (short) (buffer.getShort() / this.scaleHeight);
83                         this.rectangle.bottom_right_x = (short) (buffer.getShort() / this.scaleWidth);
84                         this.rectangle.bottom_right_y = (short) (buffer.getShort() / this.scaleHeight);
85                 } else {
86                         this.rectangle.top_left_x = buffer.getShort();
87                         this.rectangle.top_left_y = buffer.getShort();
88                         this.rectangle.bottom_right_x = buffer.getShort();
89                         this.rectangle.bottom_right_y = buffer.getShort();
90                 }
91                 
92                 //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+",}");
93         }
94
95         /* (non-Javadoc)
96          * @see org.ocera.orte.types.MessageData#write()
97          */
98         @Override
99         public void write()
100         {
101                 buffer.rewind();
102                 
103                 // put color
104                 buffer.putInt(this.color);
105                 
106                 // put shape
107                 buffer.putInt(this.shape);
108                 
109                 // put rectange position (with scaling)
110                 if (this.allowScaling) {
111                         buffer.putShort((short) (this.rectangle.top_left_x * this.scaleWidth));
112                         buffer.putShort((short) (this.rectangle.top_left_y * this.scaleHeight));
113                         buffer.putShort((short) (this.rectangle.bottom_right_x * this.scaleWidth));
114                         buffer.putShort((short) (this.rectangle.bottom_right_y * this.scaleHeight));
115                 } else {
116                         buffer.putShort(this.rectangle.top_left_x);
117                         buffer.putShort(this.rectangle.top_left_y);
118                         buffer.putShort(this.rectangle.bottom_right_x);
119                         buffer.putShort(this.rectangle.bottom_right_y);
120                 }
121         }
122
123         /* (non-Javadoc)
124          * @see org.ocera.orte.types.MessageData#getMaxDataLength()
125          */
126         @Override
127         public int getMaxDataLength()
128         {
129                 return 2*ORTEConstant.INT_FIELD_SIZE + 4*ORTEConstant.SHORT_FIELD_SIZE;
130         }
131         
132         /**
133          * When screen rotates, change scale variables to
134          * fit the destination screen.
135          * 
136          * Method of former Box class.
137          * 
138          * @since 1.0
139          */
140         public void setScale(int currentWidth, int currentHeight)
141         {
142                 this.scaleWidth = DESTINATION_WIDTH / currentWidth;
143                 this.scaleHeight = DESTINATION_HEIGHT / currentHeight;
144         }
145         
146         /**
147          * Object parameters to be sent throw ORTE.
148          * 
149          * Class from former Box class.
150          * 
151          * @since 1.0
152          */
153         public class BoxRect
154         {
155                 public short top_left_x;
156                 public short top_left_y;
157                 public short bottom_right_x;
158                 public short bottom_right_y;
159                 
160                 public BoxRect()
161                 {}
162         }
163 }