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 org.vectomatic.client.rep.RepApplication;
21  import org.vectomatic.client.rep.command.ICommand;
22  import org.vectomatic.client.rep.command.NewShapeCommand;
23  import org.vectomatic.client.rep.view.Cursor;
24  import org.vectomatic.client.rep.view.DrawingView;
25  import org.vectomatic.common.model.Attribute;
26  import org.vectomatic.common.model.geometry.Point;
27  import org.vectomatic.common.model.geometry.Polyline;
28  
29  /**
30   * Controller to respond to new polyline requests and turn
31   * them into NewShapeCommand
32   */
33  public class NewPolylineController extends ControllerBase {
34  	private Point[] _pts;
35  	private int _count;
36  	private MouseControllerButton _button;
37  
38  	public NewPolylineController(RepApplication app) {
39  		super(app);
40  		_pts = new Point[10];
41  		for (int i = 0; i < _pts.length; i++) {
42  			_pts[i] = new Point();
43  		}
44  		_button = new MouseControllerButton(_app.getIcons().polylineIcon().createImage(), _app.getConstants().newPolylineCommand(), this);
45  	}
46  	
47  	public MouseControllerButton getButton() {
48  		return _button;
49  	}
50  
51  	@Override
52  	public void activate(DrawingView view) {
53  		_count = 0;
54  		view.setCursor(Cursor.CURSOR_CROSSHAIR);
55  	}
56  
57  	private void addPoint(Point p) {
58  		if (_count == _pts.length) {
59  			// double the buffer capacity
60  			Point[] pts = new Point[2 * _pts.length];
61  			for (int i = 0; i < _pts.length; i++) {
62  				pts[i] = _pts[i];
63  			}
64  			for (int i = _pts.length; i < pts.length; i++) {
65  				pts[i] = new Point();
66  			}
67  			_pts = pts;
68  		}
69  		p.copyTo(_pts[_count]);
70  		_count++;
71  	}
72  	
73  	@Override
74  	public void mouseDown(DrawingView view, Point p, int modifiers) {
75  		view.toModelCoordinates(p);
76  		if (_count == 0) {
77  			p.copyTo(_pts[0]);
78  			p.copyTo(_pts[1]);
79  			_count = 2;
80  		} else {
81  			// add a new vertex to the polyline
82  			addPoint(p);
83  			
84  			// end of polyline test
85  			float tol = view.convertToReferenceLength(3);
86  			if (_pts[_count - 2].subtract(_pts[0], p).length() < tol) {
87  				// closed polyline if pt(N-1) == pt(0) with a certain tolerance
88  				_pts[0].copyTo(_pts[_count - 2]);
89  				deactivate(view);
90  			} else if (_pts[_count - 2].subtract(_pts[_count - 3], p).length() < tol) {
91  				// open polyline if pt(N) == pt(N-1) (double click of the user)
92  				_count--;
93  				deactivate(view);
94  			}
95  		}
96  	}
97  
98  	@Override
99  	public void mouseMove(DrawingView view, Point p, int modifiers) {
100 		view.toModelCoordinates(p);
101 		if (_count > 1) {
102 			p.copyTo(_pts[_count - 1]);
103 			if (_count > 2) {
104 				float tol = view.convertToReferenceLength(3);
105 				if (_pts[_count - 1].subtract(_pts[0], p).length() < tol) {
106 					view.setCursor(Cursor.CURSOR_CLOSED_POLYLINE);
107 				} else if (_pts[_count - 1].subtract(_pts[_count - 2], p).length() < tol) {
108 					view.setCursor(Cursor.CURSOR_OPEN_POLYLINE);
109 				} else {
110 					view.setCursor(Cursor.CURSOR_CROSSHAIR);					
111 				}
112 			}
113 		}
114 	}
115 
116 
117 	@Override
118 	public void render(DrawingView view) {
119 		if (_count > 0) {
120 //			view.beginPath();
121 			_app.getLineStyleController().getStyle().acceptVisitor(view.getStrokeStyleVisitor());
122 
123 			view.setLineWidth(_app.getLineWidthController().getLineWidth().getValue());
124 			view.moveTo(_pts[0].x, _pts[0].y);
125 			for (int i = 1; i < _count; i++) {
126 				view.lineTo(_pts[i].x, _pts[i].y);
127 			}
128 //			view.closePath();
129 			view.stroke();
130 		}
131 	}
132 
133 	@Override
134 	public void deactivate(DrawingView view) {
135 		if (_count > 2) {
136 			Polyline polyline = new Polyline(_pts, _count - 1);
137 			polyline.setAttribute(Attribute.LINE_STYLE, _app.getLineStyleController().getStyle());
138 			polyline.setAttribute(Attribute.LINE_OPACITY, _app.getLineStyleController().getOpacity());
139 			polyline.setAttribute(Attribute.FILL_STYLE, _app.getFillStyleController().getStyle());
140 			polyline.setAttribute(Attribute.FILL_OPACITY, _app.getFillStyleController().getOpacity());
141 			polyline.setAttribute(Attribute.LINE_WIDTH, _app.getLineWidthController().getLineWidth());
142 			ICommand command = new NewShapeCommand(_app, polyline);
143 			command.execute();
144 			_app.getHistory().addCommand(command);
145 			activate(view);
146 		}
147 	}
148 }