27
27
import com.eteks.sweethome3d.model.AspectRatio;
28
28
import com.eteks.sweethome3d.model.Camera;
29
29
import com.eteks.sweethome3d.model.Home;
30
import com.eteks.sweethome3d.model.HomeEnvironment;
31
30
import com.eteks.sweethome3d.model.UserPreferences;
34
33
* The controller of the photo creation view.
35
34
* @author Emmanuel Puybaret
37
public class PhotoController implements Controller {
36
public class PhotoController extends AbstractPhotoController {
39
38
* The properties that may be edited by the view associated to this controller.
41
public enum Property {ASPECT_RATIO, WIDTH, HEIGHT, QUALITY, VIEW_3D_ASPECT_RATIO, TIME, LENS, CEILING_LIGHT_COLOR}
40
public enum Property {TIME, LENS}
43
42
private final Home home;
44
43
private final UserPreferences preferences;
45
private final View view3D;
46
44
private final ViewFactory viewFactory;
47
private final ContentManager contentManager;
48
45
private final PropertyChangeSupport propertyChangeSupport;
49
46
private final CameraChangeListener cameraChangeListener;
50
47
private DialogView photoView;
52
private AspectRatio aspectRatio;
56
private float view3DAspectRatio;
58
50
private Camera.Lens lens;
59
private int ceilingLightColor;
61
52
public PhotoController(Home home,
62
53
UserPreferences preferences,
63
54
View view3D, ViewFactory viewFactory,
64
55
ContentManager contentManager) {
56
super(home, preferences, view3D, contentManager);
66
58
this.preferences = preferences;
68
59
this.viewFactory = viewFactory;
69
this.contentManager = contentManager;
70
60
this.propertyChangeSupport = new PropertyChangeSupport(this);
71
this.view3DAspectRatio = 1;
73
62
this.cameraChangeListener = new CameraChangeListener(this);
74
63
home.getCamera().addPropertyChangeListener(this.cameraChangeListener);
75
64
home.addPropertyChangeListener(Home.Property.CAMERA, new HomeCameraChangeListener(this));
76
home.getEnvironment().addPropertyChangeListener(HomeEnvironment.Property.CEILING_LIGHT_COLOR, new HomeEnvironmentChangeListener(this));
77
65
updateProperties();
106
94
* with a weak reference to avoid strong link between home and this controller.
108
96
private static class CameraChangeListener implements PropertyChangeListener {
109
private WeakReference<PhotoController> photoController;
97
private WeakReference<AbstractPhotoController> photoController;
111
public CameraChangeListener(PhotoController photoController) {
112
this.photoController = new WeakReference<PhotoController>(photoController);
99
public CameraChangeListener(AbstractPhotoController photoController) {
100
this.photoController = new WeakReference<AbstractPhotoController>(photoController);
115
103
public void propertyChange(PropertyChangeEvent ev) {
116
104
// If controller was garbage collected, remove this listener from camera
117
final PhotoController controller = this.photoController.get();
105
final AbstractPhotoController controller = this.photoController.get();
118
106
if (controller == null) {
119
107
((Camera)ev.getSource()).removePropertyChangeListener(this);
127
* Home environment listener that updates properties. This listener is bound to this controller
128
* with a weak reference to avoid strong link between home and this controller.
130
private static class HomeEnvironmentChangeListener implements PropertyChangeListener {
131
private WeakReference<PhotoController> photoController;
133
public HomeEnvironmentChangeListener(PhotoController photoController) {
134
this.photoController = new WeakReference<PhotoController>(photoController);
137
public void propertyChange(PropertyChangeEvent ev) {
138
// If controller was garbage collected, remove this listener from home
139
final PhotoController controller = this.photoController.get();
140
if (controller == null) {
141
((HomeEnvironment)ev.getSource()).removePropertyChangeListener(HomeEnvironment.Property.CEILING_LIGHT_COLOR, this);
142
} else if (ev.getPropertyName().equals(Camera.Property.LENS.name())
143
|| ev.getPropertyName().equals(Camera.Property.TIME.name())) {
144
controller.updateProperties();
150
115
* Returns the view associated with this controller.
152
117
public DialogView getView() {
189
147
* Updates edited properties from the photo creation preferences.
191
149
protected void updateProperties() {
192
HomeEnvironment homeEnvironment = this.home.getEnvironment();
193
setAspectRatio(homeEnvironment.getPhotoAspectRatio());
194
setWidth(homeEnvironment.getPhotoWidth(), false);
195
setHeight(homeEnvironment.getPhotoHeight(), false);
196
setQuality(homeEnvironment.getPhotoQuality());
197
setTime(this.home.getCamera().getTime());
198
setLens(this.home.getCamera().getLens());
199
setCeilingLightColor(homeEnvironment.getCeillingLightColor());
203
* Sets the aspect ratio of the photo.
205
public void setAspectRatio(AspectRatio aspectRatio) {
206
if (this.aspectRatio != aspectRatio) {
207
AspectRatio oldAspectRatio = this.aspectRatio;
208
this.aspectRatio = aspectRatio;
209
this.propertyChangeSupport.firePropertyChange(Property.ASPECT_RATIO.name(), oldAspectRatio, aspectRatio);
210
this.home.getEnvironment().setPhotoAspectRatio(this.aspectRatio);
211
if (this.aspectRatio == AspectRatio.VIEW_3D_RATIO) {
212
if (this.view3DAspectRatio != Float.POSITIVE_INFINITY) {
213
setHeight(Math.round(width / this.view3DAspectRatio), false);
215
} else if (this.aspectRatio.getValue() != null) {
216
setHeight(Math.round(width / this.aspectRatio.getValue()), false);
222
* Returns the aspect ratio of the photo.
224
public AspectRatio getAspectRatio() {
225
return this.aspectRatio;
229
* Sets the width of the photo.
231
public void setWidth(int width) {
232
setWidth(width, true);
235
private void setWidth(int width, boolean updateHeight) {
236
if (this.width != width) {
237
int oldWidth = this.width;
239
this.propertyChangeSupport.firePropertyChange(Property.WIDTH.name(), oldWidth, width);
241
if (this.aspectRatio == AspectRatio.VIEW_3D_RATIO) {
242
if (this.view3DAspectRatio != Float.POSITIVE_INFINITY) {
243
setHeight(Math.round(width / this.view3DAspectRatio), false);
245
} else if (this.aspectRatio.getValue() != null) {
246
setHeight(Math.round(width / this.aspectRatio.getValue()), false);
249
this.home.getEnvironment().setPhotoWidth(this.width);
254
* Returns the width of the photo.
256
public int getWidth() {
261
* Sets the height of the photo.
263
public void setHeight(int height) {
264
setHeight(height, true);
267
private void setHeight(int height, boolean updateWidth) {
268
if (this.height != height) {
269
int oldHeight = this.height;
270
this.height = height;
271
this.propertyChangeSupport.firePropertyChange(Property.HEIGHT.name(), oldHeight, height);
273
if (this.aspectRatio == AspectRatio.VIEW_3D_RATIO) {
274
if (this.view3DAspectRatio != Float.POSITIVE_INFINITY) {
275
setWidth(Math.round(height * this.view3DAspectRatio), false);
277
} else if (this.aspectRatio.getValue() != null) {
278
setWidth(Math.round(height * this.aspectRatio.getValue()), false);
281
this.home.getEnvironment().setPhotoHeight(this.height);
286
* Returns the height of the photo.
288
public int getHeight() {
293
* Sets the rendering quality of the photo.
295
public void setQuality(int quality) {
296
if (this.quality != quality) {
297
int oldQuality = this.quality;
298
this.quality = Math.min(quality, getQualityLevelCount() - 1);
299
this.propertyChangeSupport.firePropertyChange(Property.QUALITY.name(), oldQuality, quality);
300
this.home.getEnvironment().setPhotoQuality(this.quality);
305
* Returns the rendering quality of the photo.
307
public int getQuality() {
312
* Returns the maximum value for quality.
314
public int getQualityLevelCount() {
150
// Update properties only once this object is initialized
151
if (this.home != null) {
152
super.updateProperties();
153
setTime(this.home.getCamera().getTime());
154
setLens(this.home.getCamera().getLens());
320
159
* Sets the edited time in UTC time zone.
358
200
public Camera.Lens getLens() {
359
201
return this.lens;
363
* Sets the edited ceiling light color.
365
public void setCeilingLightColor(int ceilingLightColor) {
366
if (this.ceilingLightColor != ceilingLightColor) {
367
int oldCeilingLightColor = this.ceilingLightColor;
368
this.ceilingLightColor = ceilingLightColor;
369
this.propertyChangeSupport.firePropertyChange(Property.CEILING_LIGHT_COLOR.name(), oldCeilingLightColor, ceilingLightColor);
370
this.home.getEnvironment().setCeillingLightColor(ceilingLightColor);
375
* Returns the edited ceiling light color.
377
public int getCeilingLightColor() {
378
return this.ceilingLightColor;
382
* Sets the aspect ratio of the 3D view.
384
public void set3DViewAspectRatio(float view3DAspectRatio) {
385
if (this.view3DAspectRatio != view3DAspectRatio) {
386
float oldAspectRatio = this.view3DAspectRatio;
387
this.view3DAspectRatio = view3DAspectRatio;
388
this.propertyChangeSupport.firePropertyChange(Property.ASPECT_RATIO.name(), oldAspectRatio, view3DAspectRatio);
389
if (this.aspectRatio == AspectRatio.VIEW_3D_RATIO
390
&& this.view3DAspectRatio != Float.POSITIVE_INFINITY) {
391
setHeight(Math.round(this.width / this.view3DAspectRatio), false);
397
* Returns the aspect ratio of the 3D view.
399
public float get3DViewAspectRatio() {
400
return this.view3DAspectRatio;
405
* Returns the 3D view used to compute aspect ratio bound to it.
407
public View get3DView() {
412
* Controls the change of value of a visual property in home.
414
public void setVisualProperty(String propertyName,
415
Object propertyValue) {
416
this.home.setVisualProperty(propertyName, propertyValue);