]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape4a/Box.java
Initial commit of shape4a v1.0 - shape for android.
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape4a / Box.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.*;
22 import org.ocera.orte.types.*;
23
24 import android.util.Log;
25
26 /**
27  * Object to be send throw ORTE.
28  * 
29  * @author jiri hubacek <jiri.hubacek@gmail.com>
30  * @version %I%, %G%
31  * @since 1.0
32  */
33 public class Box extends MessageData
34 {
35         private static final double DESTINATION_WIDTH = 389.0;
36         private static final double DESTINATION_HEIGHT = 256.0;
37         
38         public boolean allowScaling;
39         public int color;
40         // is more shape than strength,
41         // strength is solved throw
42         // publisherProperties or
43         // throw subscriberProperties...
44         public int strength;
45         public BoxRect rectangle;
46         
47         private double scaleWidth;
48         private double scaleHeight;
49         
50         /**
51          * Add topic of new object.
52          * 
53          * @param appDomain
54          * @param newTopic
55          * @since 1.0
56          */
57         public Box(DomainApp appDomain, String newTopic) {
58                 super();
59                 this.setTopic(newTopic);
60                 
61                 if (!appDomain.regNewDataType("Box", this.getMaxDataLength())) {
62                         Log.e("Box", "Cannot register data type 'Box'.");
63                 }
64                 
65                 this.rectangle = new BoxRect();
66                 this.allowScaling = true;
67                 this.scaleWidth = 1;
68                 this.scaleHeight = 1;
69         }
70
71         /**
72          * Read data from buffer.
73          * @since 1.0
74          */
75         @Override
76         public void read() {
77                 buffer.rewind();
78                 if (this.allowScaling) {
79                         this.rectangle.top_left_x = (short) (buffer.getShort() / this.scaleWidth);
80                         this.rectangle.top_left_y = (short) (buffer.getShort() / this.scaleHeight);
81                         this.rectangle.bottom_right_x = (short) (buffer.getShort() / this.scaleWidth);
82                         this.rectangle.bottom_right_y = (short) (buffer.getShort() / this.scaleHeight);
83                 } else {
84                         this.rectangle.top_left_x = buffer.getShort();
85                         this.rectangle.top_left_y = buffer.getShort();
86                         this.rectangle.bottom_right_x = buffer.getShort();
87                         this.rectangle.bottom_right_y = buffer.getShort();
88                 }
89                 
90                 this.strength = (int) buffer.getShort();
91         }
92
93         /**
94          * Write data to buffer.
95          * @since 1.0
96          */
97         @Override
98         public void write() {
99                 buffer.rewind();
100                 if (this.allowScaling) {
101                         buffer.putShort((short) (rectangle.top_left_x * this.scaleWidth));
102                         buffer.putShort((short) (rectangle.top_left_y * this.scaleHeight));
103                         buffer.putShort((short) (rectangle.bottom_right_x * this.scaleWidth));
104                         buffer.putShort((short) (rectangle.bottom_right_y * this.scaleHeight));
105                 } else {
106                         buffer.putShort(rectangle.top_left_x);
107                         buffer.putShort(rectangle.top_left_y);
108                         buffer.putShort(rectangle.bottom_right_x);
109                         buffer.putShort(rectangle.bottom_right_y);
110                 }
111                 
112                 buffer.putShort((short) this.strength);
113         }
114
115         /**
116          * Get maximum length of data.
117          * @since 1.0
118          */
119         @Override
120         public int getMaxDataLength() {
121                 return 5 * ORTEConstant.SHORT_FIELD_SIZE;
122         }
123         
124         /**
125          * Object to be truly send throw ORTE.
126          * 
127          * @since 1.0
128          */
129         public class BoxRect
130         {
131                 public short top_left_x;
132                 public short top_left_y;
133                 public short bottom_right_x;
134                 public short bottom_right_y;
135                 
136                 public BoxRect()
137                 {}
138         }
139         
140         /**
141          * When screen rotates, change scale variables to
142          * fit the destination screen.
143          * 
144          * @since 1.0
145          */
146         public void setScale(int currentWidth, int currentHeight)
147         {
148                 this.scaleWidth = DESTINATION_WIDTH / currentWidth;
149                 this.scaleHeight = DESTINATION_HEIGHT / currentHeight;
150         }
151 }