make multiple trials works
[rmh3093.git] / motsim / Controller.java
blob22830ac678b38436e5a700f4881e35ad7b754c40
1 /**
2 * Controller.java
3 * motsim
4 */
6 /*
7 Copyright (c) 2008, Ryan M. Hope
8 All rights reserved.
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.
34 import java.awt.Font;
35 import java.awt.font.TextLayout;
36 import java.util.ArrayList;
37 import java.util.Random;
39 public class Controller implements Runnable {
41 Model model;
42 View view;
44 TrialDataDynamic tdd;
46 Random rand;
48 /**
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
57 this.model = model;
58 this.view = view;
59 /* Generate some data dynamically for testing purposes */
60 tdd = new TrialDataDynamic(view.screen_size);
61 model.trialThread = new Thread(this);
62 model.trialThread.start();
65 /**
66 * Update target positions based on X and Y velocities
68 protected synchronized void moveTargets() {
69 int sleep = 20; // Sleep for 20ms to maintain ~50fps
70 int[] entropy = null;
71 for (int i=0; i<model.targets.size(); i++) {
72 Target t = model.targets.get(i);
73 boolean flip = false;
75 * If target hits a border flip is angle and velocity so that it
76 * reflects like a mirror
78 if (((t.x + t.velX + t.bounds.getWidth()) > view.screen_size.width) ||
79 ((t.x + t.velX) < 0)) {
80 t.velX = t.velX * -1;
81 flip = true;
83 if (((t.y + t.velY) > view.screen_size.height-view.trial.mh) ||
84 ((t.y + t.velY - t.bounds.getHeight()) < 0)) {
85 t.velY = t.velY * -1;
86 flip = true;
88 if (flip==true) {
89 t.moveAngle = t.moveAngle + 180;
90 t.move = true;
94 * Add any entropy to the move angle if necessary
96 if (model.entropy != "None") {
97 if (t.move == false) {
98 if (model.entropy == "Low") {
99 entropy = tdd.generateEntropyLow();
100 } else if (model.entropy == "Medium") {
101 entropy = tdd.generateEntropyMedium();
102 } else if (model.entropy == "High") {
103 entropy = tdd.generateEntropyHigh();
105 t.moveAngle = (t.moveAngle + entropy[rand.nextInt(10000)]) % 360;
106 t.move = true;
107 } else {
108 t.steps++;
109 if (t.steps > 5) {
110 t.move = false;
111 t.steps = 0;
114 t.velX = tdd.calcAngleMoveX(t.moveAngle);
115 t.velY = tdd.calcAngleMoveY(t.moveAngle);
117 t.x = t.x + t.velX * t.velocityMOD;
118 t.y = t.y + t.velY * t.velocityMOD;
120 view.trial.repaint();
121 try {
122 model.duration = model.duration + sleep;
123 Thread.sleep(sleep);
124 } catch (InterruptedException e) {
128 private void generateTargets() {
129 model.targets = new ArrayList<Target>(tdd.generate_targets(
130 (Integer)view.cp.targets_spinner.getValue()));
131 for (int i=0; i<model.targets.size(); i++) {
132 Target t = model.targets.get(i);
133 /* Adjust base speed of targets */
134 t.velocityMOD = (Double)view.cp.speed_spinner.getValue();
135 /* Get bounds of targets */
136 TextLayout tl = new TextLayout(t.callsign, model.target_font, view.trial.frc);
137 t.bounds = tl.getBounds();
138 /* Make sure all targets start on screen */
139 int max;
140 max = view.screen_size.width - (int)Math.round(t.bounds.getWidth());
141 if (t.x > max)
142 t.x = max - 1;
143 max = view.screen_size.height - view.trial.mh;
144 if (t.y > max)
145 t.y = max - 1;
146 if (t.y < t.bounds.getHeight())
147 t.y = t.bounds.getHeight() + 1;
152 * Main Trial Process
154 * @author rmh3093
157 public void run() {
158 while (true) {
159 switch(model.stage) {
161 case WAIT:
162 synchronized (model.trialThread) {
163 try {
164 model.trialThread.wait();
165 } catch (InterruptedException e1) {
169 case START_TRIAL:
170 model.trialCount++;
171 startTrials();
172 model.stage = Model.TrialStage.GENERATE_TARGETS;
174 case GENERATE_TARGETS:
175 generateTargets();
176 model.stage = Model.TrialStage.MOVE_TARGETS;
178 case MOVE_TARGETS:
179 if (model.duration < model.maxduration) {
180 moveTargets();
181 break;
182 } else {
183 model.stage = Model.TrialStage.CHOOSE_TARGET;
186 case CHOOSE_TARGET:
187 view.trial.repaint();
188 model.target = rand.nextInt(model.targets.size());
189 model.stage = Model.TrialStage.START_SEARCH;
191 case START_SEARCH:
192 model.startTime = (int)System.currentTimeMillis();
193 model.stage = Model.TrialStage.FIND_TARGET;
195 case FIND_TARGET:
196 view.trial.repaint();
197 break;
199 case STOP_SEARCH:
200 model.stopTime = (int)System.currentTimeMillis();
201 System.out.println("Trial " + model.trialCount + ": " +
202 (model.stopTime - model.startTime) + " ms");
203 model.stage = Model.TrialStage.TRIAL_CLEANUP;
205 case TRIAL_CLEANUP:
206 if (model.trialCount >= model.trialMax) {
207 view.trial.stopTrial();
208 model.stage = Model.TrialStage.WAIT;
209 break;
210 } else {
211 model.stage = Model.TrialStage.START_TRIAL;
218 void startTrials() {
219 view.startTrial();
220 model.target_font = new Font(view.trial.getFont().getName(),
221 view.trial.getFont().getStyle(),
222 (Integer)view.cp.fontsize_spinner.getValue());
223 model.query_font = new Font(view.trial.getFont().getName(),
224 view.trial.getFont().getStyle(), model.query_font_size);
225 view.trial.bufferGraphics.setFont(model.target_font);
226 model.duration = 0;
227 view.trial.mh = model.query_font.getSize() + model.query_font_size;
228 model.trialMax = Integer.valueOf(view.cp.trials_spinner.getValue().toString());