7 Copyright (c) 2008, Ryan M. Hope
10 Redistribution and use in source and binary forms, with or without modification,
11 are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice,
14 this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18 * Neither the name of the project nor the names of its contributors may be
19 used to endorse or promote products derived from this software without
20 specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 import java
.awt
.font
.TextLayout
;
36 import java
.util
.ArrayList
;
37 import java
.util
.Random
;
39 public class Controller
{
49 * Create a new controller object for MOTSIM. MOTSIM_Controller does most
50 * of the grunt works for MOTSIM.
52 * @param model MOTSIM_Model
53 * @param view MOTSIM_View
55 public Controller(Model model
, View view
) {
56 rand
= new Random(); // A random number generator
59 /* Generate some data dynamically for testing purposes */
60 tdd
= new TrialDataDynamic(view
.screen_size
);
65 * Update target positions based on X and Y velocities
67 protected synchronized void moveTargets() {
68 int sleep
= 20; // Sleep for 20ms to maintain ~50fps
70 for (int i
=0; i
<model
.targets
.size(); i
++) {
71 Target t
= model
.targets
.get(i
);
74 * If target hits a border flip is angle and velocity so that it
75 * reflects like a mirror
77 if (((t
.x
+ t
.velX
+ t
.bounds
.getWidth()) > view
.screen_size
.width
) ||
78 ((t
.x
+ t
.velX
) < 0)) {
82 if (((t
.y
+ t
.velY
) > view
.screen_size
.height
-view
.trial
.mh
) ||
83 ((t
.y
+ t
.velY
- t
.bounds
.getHeight()) < 0)) {
88 t
.moveAngle
= t
.moveAngle
+ 180;
93 * Add any entropy to the move angle if necessary
95 if (model
.entropy
!= "None") {
96 if (t
.move
== false) {
97 if (model
.entropy
== "Low") {
98 entropy
= tdd
.generateEntropyLow();
99 } else if (model
.entropy
== "Medium") {
100 entropy
= tdd
.generateEntropyMedium();
101 } else if (model
.entropy
== "High") {
102 entropy
= tdd
.generateEntropyHigh();
104 t
.moveAngle
= (t
.moveAngle
+ entropy
[rand
.nextInt(10000)]) % 360;
113 t
.velX
= tdd
.calcAngleMoveX(t
.moveAngle
);
114 t
.velY
= tdd
.calcAngleMoveY(t
.moveAngle
);
116 t
.x
= t
.x
+ t
.velX
* t
.velocityMOD
;
117 t
.y
= t
.y
+ t
.velY
* t
.velocityMOD
;
119 view
.trial
.repaint();
121 model
.duration
= model
.duration
+ sleep
;
123 } catch (InterruptedException e
) {
133 class TrialThread
implements Runnable
{
139 // First move the targets around the screen
140 while (model
.stage
== Model
.TrialStage
.MOVE
) {
141 if (model
.duration
< model
.maxduration
) {
144 model
.stage
= Model
.TrialStage
.CHOOSE
;
147 // After trial duration mask targets and pick a random target
148 if (model
.stage
== Model
.TrialStage
.CHOOSE
) {
149 view
.trial
.repaint();
150 model
.target
= rand
.nextInt(model
.targets
.size());
151 model
.stage
= Model
.TrialStage
.FIND
;
154 model
.startTime
= System
.currentTimeMillis();
155 while (model
.stage
== Model
.TrialStage
.FIND
) {
156 synchronized (model
.trialThread
) {
158 model
.trialThread
.wait();
159 } catch (InterruptedException e1
) {
162 view
.trial
.repaint();
165 if (trials
>= model
.trialCount
)
166 view
.trial
.stopTrial();
167 // Trial is over, wait
168 model
.waiting
= true;
169 synchronized (model
.trialThread
) {
171 model
.trialThread
.wait();
172 } catch (InterruptedException e1
) {
175 model
.waiting
= false;
182 /* Initialize trial values */
183 model
.maxduration
= 1000 * (Integer
)view
.cp
.duration_spinner
.getValue();
184 model
.entropy
= view
.cp
.entropy_combobox
.getSelectedItem().toString();
186 model
.target_font
= new Font(view
.trial
.getFont().getName(),
187 view
.trial
.getFont().getStyle(),
188 (Integer
)view
.cp
.fontsize_spinner
.getValue());
189 model
.query_font
= new Font(view
.trial
.getFont().getName(),
190 view
.trial
.getFont().getStyle(), model
.query_font_size
);
191 view
.trial
.bufferGraphics
.setFont(model
.target_font
);
193 view
.trial
.mh
= model
.query_font
.getSize() + model
.query_font_size
;
194 model
.trialCount
= Integer
.valueOf(view
.cp
.trials_spinner
.getValue().toString());
195 /* Generate targets */
196 model
.targets
= new ArrayList
<Target
>(tdd
.generate_targets(
197 (Integer
)view
.cp
.targets_spinner
.getValue()));
198 for (int i
=0; i
<model
.targets
.size(); i
++) {
199 Target t
= model
.targets
.get(i
);
200 /* Adjust base speed of targets */
201 t
.velocityMOD
= (Double
)view
.cp
.speed_spinner
.getValue();
202 /* Get bounds of targets */
203 TextLayout tl
= new TextLayout(t
.callsign
, model
.target_font
, view
.trial
.frc
);
204 t
.bounds
= tl
.getBounds();
205 /* Make sure all targets start on screen */
207 max
= view
.screen_size
.width
- (int)Math
.round(t
.bounds
.getWidth());
210 max
= view
.screen_size
.height
- view
.trial
.mh
;
213 if (t
.y
< t
.bounds
.getHeight())
214 t
.y
= t
.bounds
.getHeight() + 1;
216 model
.stage
= Model
.TrialStage
.MOVE
;
217 if (model
.trialThread
== null) {
218 model
.trialThread
= new Thread(new TrialThread());
219 model
.trialThread
.start();
221 synchronized (model
.trialThread
) {
222 model
.trialThread
.notifyAll();