001 package com.croftsoft.apps.cyborg;
002
003 import java.awt.*;
004 import java.awt.event.*;
005
006 import com.croftsoft.core.gui.controller.NilController;
007 import com.croftsoft.core.lang.NullArgumentException;
008
009 /*********************************************************************
010 * Receives and processes user input.
011 *
012 * @version
013 * $Id: CyborgController.java,v 1.20 2008/04/19 21:30:58 croft Exp $
014 * @since
015 * 2005-03-16
016 * @author
017 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
018 *********************************************************************/
019
020 public final class CyborgController
021 extends NilController
022 //////////////////////////////////////////////////////////////////////
023 //////////////////////////////////////////////////////////////////////
024 {
025
026 private final CyborgModel cyborgModel;
027
028 private final CyborgOperator cyborgOperator;
029
030 //
031
032 private String actionCommand;
033
034 private Integer max;
035
036 private Point point;
037
038 private String transform;
039
040 private Double
041 alpha,
042 offset;
043
044 //
045
046 private boolean manual;
047
048 private double x;
049
050 private double y;
051
052 //////////////////////////////////////////////////////////////////////
053 //////////////////////////////////////////////////////////////////////
054
055 public CyborgController (
056 CyborgModel cyborgModel,
057 CyborgOperator cyborgOperator )
058 //////////////////////////////////////////////////////////////////////
059 {
060 NullArgumentException.check ( this.cyborgModel = cyborgModel );
061
062 NullArgumentException.check (
063 this.cyborgOperator = cyborgOperator );
064
065 point = new Point ( );
066 }
067
068 //////////////////////////////////////////////////////////////////////
069 //////////////////////////////////////////////////////////////////////
070
071 public void changeMax ( Integer max )
072 //////////////////////////////////////////////////////////////////////
073 {
074 this.max = max;
075 }
076
077 public void setAlpha ( Double alpha )
078 //////////////////////////////////////////////////////////////////////
079 {
080 this.alpha = alpha;
081 }
082
083 public void setOffset ( Double offset )
084 //////////////////////////////////////////////////////////////////////
085 {
086 this.offset = offset;
087 }
088
089 public void changeTransform ( String transform )
090 //////////////////////////////////////////////////////////////////////
091 {
092 this.transform = transform;
093 }
094
095 public void actionPerformed ( ActionEvent actionEvent )
096 //////////////////////////////////////////////////////////////////////
097 {
098 actionCommand = actionEvent.getActionCommand ( );
099 }
100
101 public void mouseMoved ( MouseEvent mouseEvent )
102 //////////////////////////////////////////////////////////////////////
103 {
104 point = mouseEvent.getPoint ( );
105 }
106
107 public void update ( )
108 //////////////////////////////////////////////////////////////////////
109 {
110 if ( alpha != null )
111 {
112 cyborgModel.setAlpha ( alpha.doubleValue ( ) );
113
114 alpha = null;
115 }
116
117 if ( offset != null )
118 {
119 cyborgModel.setOffset ( offset.doubleValue ( ) );
120
121 offset = null;
122 }
123
124 if ( max != null )
125 {
126 cyborgModel.setMax ( max.intValue ( ) );
127
128 max = null;
129 }
130
131 if ( actionCommand != null )
132 {
133 if ( actionCommand.equals (
134 CyborgConfig.ACTION_COMMAND_ANIMATE ) )
135 {
136 cyborgModel.setAnimate ( !cyborgModel.getAnimate ( ) );
137 }
138 else if ( actionCommand.equals (
139 CyborgConfig.ACTION_COMMAND_AUTOMATIC ) )
140 {
141 manual = false;
142
143 CyborgConfig.INSTANCE.getLog ( ).record ( actionCommand );
144
145 cyborgModel.reset ( );
146 }
147 else if ( actionCommand.equals (
148 CyborgConfig.ACTION_COMMAND_FORCE_LENGTH ) )
149 {
150 cyborgModel.setForceLength ( !cyborgModel.getForceLength ( ) );
151
152 cyborgModel.reset ( );
153 }
154 else if ( actionCommand.equals (
155 CyborgConfig.ACTION_COMMAND_MANUAL ) )
156 {
157 manual = true;
158
159 CyborgConfig.INSTANCE.getLog ( ).record ( actionCommand );
160
161 cyborgModel.reset ( );
162 }
163 else if ( actionCommand.equals (
164 CyborgConfig.ACTION_COMMAND_PAUSE ) )
165 {
166 cyborgModel.setPaused ( true );
167 }
168 else if ( actionCommand.equals (
169 CyborgConfig.ACTION_COMMAND_RESUME ) )
170 {
171 cyborgModel.setPaused ( false );
172 }
173 else if ( actionCommand.equals (
174 CyborgConfig.ACTION_COMMAND_REALTIME ) )
175 {
176 cyborgModel.setRealTime ( !cyborgModel.getRealTime ( ) );
177 }
178
179 actionCommand = null;
180 }
181
182 if ( transform != null )
183 {
184 cyborgModel.setTransform ( transform );
185
186 transform = null;
187 }
188
189 if ( !manual )
190 {
191 cyborgOperator.update ( );
192
193 return;
194 }
195
196 if ( point == null )
197 {
198 return;
199 }
200
201 x = CyborgAnimator.toX ( point.x );
202
203 y = CyborgAnimator.toY ( point.y );
204
205 if ( x < -1.0 )
206 {
207 x = -1.0;
208 }
209 else if ( x > 1.0 )
210 {
211 x = 1.0;
212 }
213
214 if ( y < -1.0 )
215 {
216 y = -1.0;
217 }
218 else if ( y > 1.0 )
219 {
220 y = 1.0;
221 }
222
223 cyborgModel.setAimX ( x );
224
225 cyborgModel.setAimY ( y );
226
227 point = null;
228 }
229
230 //////////////////////////////////////////////////////////////////////
231 //////////////////////////////////////////////////////////////////////
232 }