View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic 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   * Vectomatic 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 Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.client.rep.controller;
19  
20  import java.util.ArrayList;
21  
22  import org.vectomatic.client.rep.RepApplication;
23  import org.vectomatic.client.rep.command.ICommand;
24  import org.vectomatic.client.rep.command.NewShapeCommand;
25  import org.vectomatic.client.rep.view.Cursor;
26  import org.vectomatic.client.rep.view.DrawingView;
27  import org.vectomatic.common.model.Attribute;
28  import org.vectomatic.common.model.Shape;
29  import org.vectomatic.common.model.geometry.Ellipse;
30  import org.vectomatic.common.model.geometry.Point;
31  import org.vectomatic.common.model.geometry.TransformMatrix;
32  
33  /**
34   * Controller to respond to new ellipse requests and turn
35   * them into NewShapeCommand
36   */
37  public class NewEllipseController extends ControllerBase {
38  	
39  	private enum State {
40  		S0 {
41  			@Override
42  			State processActivate(NewEllipseController controller, DrawingView view) {
43  				controller._ellipse = null;
44  				controller._app.getSelection().select(new ArrayList<Shape>());
45  				view.setCursor(Cursor.CURSOR_POINTER);
46  				return S1;
47  			}
48  		},
49  		S1 {
50  			@Override
51  			State processMouseDown(NewEllipseController controller, DrawingView view, Point p, int modifiers) {
52  				controller._ellipse = new Ellipse();
53  				controller._ellipse.setAttribute(Attribute.LINE_STYLE, controller._app.getLineStyleController().getStyle());
54  				controller._ellipse.setAttribute(Attribute.LINE_OPACITY, controller._app.getLineStyleController().getOpacity());
55  				controller._ellipse.setAttribute(Attribute.FILL_STYLE, controller._app.getFillStyleController().getStyle());
56  				controller._ellipse.setAttribute(Attribute.FILL_OPACITY, controller._app.getFillStyleController().getOpacity());
57  				controller._ellipse.setAttribute(Attribute.LINE_WIDTH, controller._app.getLineWidthController().getLineWidth());
58  
59  				controller._ellipse.setTranslation(view.toModelCoordinates(p));
60  				controller._ellipse.setRotation(- view.getRotation());
61  				controller._m.rotation(view.getRotation());
62  				p.copyTo(controller._p0);
63  				return S2;
64  			}
65  		},
66  		S2 {
67  			@Override
68  			State processMouseMove(NewEllipseController controller, DrawingView view, Point p, int modifiers) {
69  				view.toModelCoordinates(p).add(controller._p0).multiply(0.5f);
70  				controller._ellipse.setTranslation(p);
71  				controller._ellipse.setScaling(p.subtract(controller._p0).transform(controller._m));
72  				return S2;
73  			}
74  
75  			@Override
76  			State processMouseUp(NewEllipseController controller, DrawingView view, Point p, int modifiers) {
77  				processMouseMove(controller, view, p, modifiers);
78  				return S3.processDeactivate(controller, view);
79  			}
80  
81  		},
82  		S3 {
83  			@Override
84  			State processDeactivate(NewEllipseController controller, DrawingView view) {
85  				// Discard shapes which are smaller than 5 pixels
86  				if (controller._ellipse.getScaling(controller._p0).length() > view.convertToReferenceLength(5)) {
87  					ICommand command = new NewShapeCommand(controller._app, controller._ellipse);
88  					command.execute();
89  					controller._app.getHistory().addCommand(command);
90  				}
91  				return S0.processActivate(controller, view);
92  			}
93  		};
94  		
95  		State processActivate(NewEllipseController controller, DrawingView view) {
96  			throw new IllegalStateException("activate");
97  		}
98  		State processMouseDown(NewEllipseController controller, DrawingView view, Point p, int modifiers) {
99  			return this;
100 		}
101 		State processMouseUp(NewEllipseController controller, DrawingView view, Point p, int modifiers) {
102 			return this;
103 		}
104 		State processMouseMove(NewEllipseController controller, DrawingView view, Point p, int modifiers) {
105 			return this;
106 		}
107 		State processDeactivate(NewEllipseController controller, DrawingView view) {
108 			return this;
109 		}
110 	}
111 	
112 	/**
113 	 * Ellipse currently being created, if any
114 	 */
115 	private Ellipse _ellipse;
116 	private Point _p0;
117 	private TransformMatrix _m;
118 	private MouseControllerButton _button;
119 	private State _state;
120 
121 	public NewEllipseController(RepApplication app) {
122 		super(app);
123 		_p0 = new Point();
124 		_m = new TransformMatrix();
125 		_button = new MouseControllerButton(_app.getIcons().ellipseIcon().createImage(), _app.getConstants().newEllipseCommand(), this);
126 	}
127 
128 	public MouseControllerButton getButton() {
129 		return _button;
130 	}
131 
132 	@Override
133 	public void render(DrawingView view) {
134 		if (_state == State.S2) {
135 			view.getRenderer().visitEllipse(_ellipse);
136 		}
137 	}
138 	
139 	@Override
140 	public void activate(DrawingView view) {
141 		_state = State.S0;
142 		_state = _state.processActivate(this, view);
143 	}
144 
145 	@Override
146 	public void mouseDown(DrawingView view, Point p, int modifiers) {
147 		_state = _state.processMouseDown(this, view, p, modifiers);
148 	}
149 	
150 	@Override
151 	public void mouseMove(DrawingView view, Point p, int modifiers) {
152 		_state = _state.processMouseMove(this, view, p, modifiers);
153 	}
154 
155 	@Override
156 	public void mouseUp(DrawingView view, Point p, int modifiers) {
157 		_state = _state.processMouseUp(this, view, p, modifiers);
158 	}
159 	
160 	@Override
161 	public void deactivate(DrawingView view) {
162 		_state = _state.processDeactivate(this, view);
163 	}
164 }