Source code: com/xerox/VTM/engine/Camera.java
1 /* FILE: Camera.java
2 * DATE OF CREATION: Jul 11 2000
3 * AUTHOR : Emmanuel Pietriga (emmanuel.pietriga@xrce.xerox.com)
4 * MODIF: Tue Jul 22 15:54:58 2003 by Emmanuel Pietriga (emmanuel@w3.org, emmanuel@claribole.net)
5 * Copyright (c) Xerox Corporation, XRCE/Contextual Computing, 2002. All Rights Reserved
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * For full terms see the file COPYING.
18 */
19
20 package com.xerox.VTM.engine;
21
22 import net.claribole.zvtm.engine.Location;
23
24 /**
25 * a Camera is used to observe the virtual space which owns it - all cameras have unique IDs, as glyph - (x,y) coordinates, observation altitude and focal distance can be changed
26 * @author Emmanuel Pietriga
27 **/
28
29 public class Camera {
30
31 /** camera ID */
32 Integer ID;
33 /** camera index (wrt the owning virtual space)*/
34 int index;
35 /** coordinates in virtual space*/
36 public long posx,posy;
37 /** altitude of observation*/
38 public float altitude;
39 /** focal distance*/
40 public float focal;
41 /**camera is enabled or not (disabling does not destroy)*/
42 boolean enabled;
43 /** virtual space to which this camera belongs to*/
44 VirtualSpace parentSpace;
45 /**View using this camera as one of its layer(s)*/
46 View view;
47
48 /**Lazy camera, true if the camera only repaints when explicitely asked*/
49 boolean eager=true;
50
51 /**when in lazy mode, tells the camera to repaint next time the owning view goes through its paint loop*/
52 boolean shouldRepaint=false;
53
54 /**
55 * @param x initial X coordinate
56 * @param y initial Y coordinate
57 * @param alt initial altitude
58 * @param f initial focal distance
59 * @param i camera index (wrt the owning virtual space)
60 */
61 Camera(long x,long y,float alt,float f,int i){
62 posx=x;
63 posy=y;
64 altitude=alt;
65 focal=f;
66 index=i;
67 enabled=true;
68 }
69
70 /**
71 * @param x initial X coordinate
72 * @param y initial Y coordinate
73 * @param alt initial altitude
74 * @param f initial focal distance
75 * @param i camera index (wrt the owning virtual space)
76 * @param l lazy camera, will only repaint when explicitely told to do so (default is false)
77 */
78 Camera(long x,long y,float alt,float f,int i, boolean l){
79 posx=x;
80 posy=y;
81 altitude=alt;
82 focal=f;
83 index=i;
84 enabled=true;
85 eager=!l;
86 }
87
88 /**
89 * set camera position (absolute value) - will trigger a repaint, whereas directly assigning values to posx,posy will not
90 */
91 public void setLocation(long x,long y){
92 posx=x;
93 posy=y;
94 parentSpace.vsm.repaintNow();
95 }
96
97 /**
98 * set camera altitude (absolute value)
99 */
100 public void setAltitude(float a){
101 if (a>=parentSpace.vsm.zoomFloor){altitude=a;} //test prevents incorrect altitudes
102 else {altitude=parentSpace.vsm.zoomFloor;}
103 }
104
105 /**
106 * set camera altitude (relative value)
107 */
108 public void altitudeOffset(float a){
109 if ((altitude+a)>parentSpace.vsm.zoomFloor){altitude+=a;} //test prevents incorrect altitudes
110 else {altitude=parentSpace.vsm.zoomFloor;}
111 }
112
113 /**
114 * get camera altitude
115 */
116 public float getAltitude(){
117 return altitude;
118 }
119
120 /**
121 * get camera location
122 */
123 public Location getLocation(){
124 return new Location(posx,posy,altitude);
125 }
126
127 /**
128 * set camera focal distance (absolute value)
129 */
130 public void setFocal(float f){
131 if (f<0) {f=0;}
132 focal=f;
133 }
134
135 /**
136 * get camera focal distance
137 */
138 public float getFocal(){
139 return focal;
140 }
141
142 /**
143 * get camera index (w.r.t owning virtual space)
144 */
145 public int getIndex(){
146 return index;
147 }
148
149 /**
150 * get camera ID
151 */
152 public Integer getID(){
153 return ID;
154 }
155
156 /**
157 * set new ID for this camera (make sure there is no conflict)
158 */
159 public void setID(Integer ident){
160 ID=ident;
161 }
162
163 /**
164 * set virtual space owning this camera
165 */
166 protected void setOwningSpace(VirtualSpace vs){
167 parentSpace=vs;
168 }
169
170 /**
171 * get virtual space owning this camera
172 */
173 public VirtualSpace getOwningSpace(){
174 return parentSpace;
175 }
176
177 /**
178 * set view owning this camera
179 */
180 protected void setOwningView(View vi){
181 view=vi;
182 }
183
184 /**
185 * get view owning this camera
186 */
187 public View getOwningView(){
188 return view;
189 }
190
191 /**
192 * enable camera
193 */
194 public void enable(){enabled=true;}
195
196 /**
197 * disable camera
198 */
199 public void disable(){enabled=false;}
200
201 /**
202 * set eager or lazy mode
203 * @param b true=lazy, false=eager
204 */
205 public void setLaziness(boolean b){eager=!b;}
206
207 /**
208 * get camera repaint mode (eager or lazy)
209 * @returns true=lazy, false=eager
210 */
211 public boolean getLaziness(){return !eager;}
212
213 /**
214 * the content seen thourhg this camera will be repainted in the next owning view's paint loop
215 */
216 public void repaintNow(){
217 shouldRepaint=true;
218 }
219
220 protected boolean shouldRepaint(){
221 if (shouldRepaint){
222 shouldRepaint=false;
223 return true;
224 }
225 else {return false;}
226 }
227
228 /**
229 * returns a String with ID, position, altitude and focal distance
230 */
231 public String toString() {
232 return new String("Camera "+ID+" position ("+posx+","+posy+") alt "+altitude+" focal "+focal);
233 }
234
235 }