View Javadoc

1   /*
2    * jcurl java curling software framework https://JCurl.mro.name Copyright (C)
3    * 2005-2009 M. Rohrmoser
4    * 
5    * This program is free software; you can redistribute it and/or modify it under
6    * the terms of the GNU General Public License as published by the Free Software
7    * Foundation; either version 2 of the License, or (at your option) any later
8    * version.
9    * 
10   * This program is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13   * details.
14   * 
15   * You should have received a copy of the GNU General Public License along with
16   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17   * Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  
20  package org.jcurl.demo.tactics.old;
21  
22  import java.awt.geom.Point2D;
23  import java.awt.geom.Rectangle2D;
24  import java.awt.geom.RectangularShape;
25  
26  import org.apache.commons.logging.Log;
27  import org.jcurl.core.api.IceSize;
28  import org.jcurl.core.api.RockProps;
29  import org.jcurl.core.api.Unit;
30  import org.jcurl.core.log.JCLoggerFactory;
31  import org.jcurl.demo.tactics.Zoomable;
32  import org.jcurl.demo.tactics.old.ActionRegistry.JCAction;
33  
34  /**
35   * @author <a href="mailto:JCurl@mro.name">M. Rohrmoser </a>
36   * @version $Id$
37   */
38  @ActionRegistry.JCMenu("&View")
39  public class MenuView {
40  	/** All from back to back */
41  	public static final Rectangle2D CompletePlus;
42  	/** */
43  	private static final int DT = 200;
44  	/** House area plus 1 rock margin plus "out" rock space. */
45  	public static final Rectangle2D HousePlus;
46  	private static final Log log = JCLoggerFactory.getLogger(MenuView.class);
47  	/**
48  	 * Inter-hog area area plus house area plus 1 rock margin plus "out" rock
49  	 * space.
50  	 */
51  	private static final Rectangle2D SheetPlus;
52  	/** 12-foot circle plus 1 rock */
53  	private static final Rectangle2D TwelvePlus;
54  	static {
55  		final double r2 = 2 * RockProps.DEFAULT.getRadius();
56  		final double x = IceSize.SIDE_2_CENTER + r2;
57  		HousePlus = new Rectangle2D.Double(-x, -(IceSize.HOG_2_TEE + r2),
58  				2 * x, IceSize.HOG_2_TEE + IceSize.BACK_2_TEE + 3 * r2 + 2 * r2);
59  		final double c12 = r2 + Unit.f2m(6.0);
60  		TwelvePlus = new Rectangle2D.Double(-c12, -c12, 2 * c12, 2 * c12);
61  		SheetPlus = new Rectangle2D.Double(-x, -(IceSize.HOG_2_HOG
62  				+ IceSize.HOG_2_TEE + r2), 2 * x, IceSize.HOG_2_HOG
63  				+ IceSize.HOG_2_TEE + IceSize.BACK_2_TEE + 3 * r2 + 2 * r2);
64  		CompletePlus = new Rectangle2D.Double(-x, -(IceSize.HOG_2_TEE
65  				+ IceSize.HOG_2_HOG + IceSize.HACK_2_HOG + r2), 2 * x,
66  				IceSize.HOG_2_HOG + 2 * IceSize.HACK_2_HOG);
67  	}
68  	private Zoomable model = null;
69  
70  	public Zoomable getModel() {
71  		return model;
72  	}
73  
74  	private void pan(final double rx, final double ry, final int dt) {
75  		if (getModel() == null)
76  			return;
77  		final RectangularShape src = getModel().getZoom();
78  		src.setFrame(src.getX() + src.getWidth() * rx, src.getY()
79  				+ src.getHeight() * ry, src.getWidth(), src.getHeight());
80  		zoom(src, dt);
81  	}
82  
83  	@JCAction(title = "Pan &East", idx = 90, accelerator = "LEFT")
84  	public void panEast() {
85  		pan(-0.2, 0, DT);
86  	}
87  
88  	@JCAction(title = "Pan &North", idx = 70, accelerator = "DOWN", separated = true)
89  	public void panNorth() {
90  		pan(0, 0.2, DT);
91  	}
92  
93  	@JCAction(title = "Pan &South", idx = 80, accelerator = "UP")
94  	public void panSouth() {
95  		pan(0, -0.2, DT);
96  	}
97  
98  	@JCAction(title = "Pan &West", idx = 100, accelerator = "RIGHT")
99  	public void panWest() {
100 		pan(0.2, 0, DT);
101 	}
102 
103 	public void setModel(final Zoomable model) {
104 		this.model = model;
105 		final boolean a = this.model != null;
106 		final ActionRegistry ah = ActionRegistry.getInstance();
107 		ah.findAction(this, "zoomActive").setEnabled(a);
108 		ah.findAction(this, "zoomHouse").setEnabled(a);
109 		ah.findAction(this, "zoomRink").setEnabled(a);
110 		ah.findAction(this, "zoomTwelve").setEnabled(a);
111 		ah.findAction(this, "zoomIn").setEnabled(a);
112 		ah.findAction(this, "zoomOut").setEnabled(a);
113 		ah.findAction(this, "panNorth").setEnabled(a);
114 		ah.findAction(this, "panSouth").setEnabled(a);
115 		ah.findAction(this, "panEast").setEnabled(a);
116 		ah.findAction(this, "panWest").setEnabled(a);
117 	}
118 
119 	private void zoom(final Point2D center, final double ratio, final int dt) {
120 		if (getModel() == null)
121 			return;
122 		final RectangularShape src = getModel().getZoom();
123 		if (log.isDebugEnabled())
124 			log.debug(src);
125 		final double w = src.getWidth() * ratio;
126 		final double h = src.getHeight() * ratio;
127 		final double cx, cy;
128 		if (center == null) {
129 			cx = src.getCenterX();
130 			cy = src.getCenterY();
131 		} else {
132 			cx = center.getX();
133 			cy = center.getY();
134 		}
135 		zoom(new Rectangle2D.Double(cx - w / 2, cy - h / 2, Math.abs(w), Math
136 				.abs(h)), dt);
137 	}
138 
139 	private void zoom(final RectangularShape viewport, final int dt) {
140 		if (getModel() == null)
141 			return;
142 		getModel().setZoom(viewport, dt);
143 	}
144 
145 	@JCAction(title = "&Active", idx = 40, accelerator = "CTRL-END")
146 	public void zoomActive() {
147 		zoom(SheetPlus, -1);
148 	}
149 
150 	@JCAction(title = "&House", idx = 10, accelerator = "HOME")
151 	public void zoomHouse() {
152 		zoom(HousePlus, -1);
153 	}
154 
155 	@JCAction(title = "Zoom &In", idx = 50, accelerator = "ADD", separated = true)
156 	public void zoomIn() {
157 		zoom(null, 0.75, DT);
158 	}
159 
160 	@JCAction(title = "Zoom &Out", idx = 60, accelerator = "SUBTRACT")
161 	public void zoomOut() {
162 		zoom(null, 1.25, DT);
163 	}
164 
165 	@JCAction(title = "&Complete", idx = 30, accelerator = "CTRL-HOME")
166 	public void zoomRink() {
167 		zoom(CompletePlus, -1);
168 	}
169 
170 	@JCAction(title = "1&2-Foot", idx = 20, accelerator = "END")
171 	public void zoomTwelve() {
172 		zoom(TwelvePlus, -1);
173 	}
174 }