working again
[rmh3093.git] / motsim / Controller.java
blob51f0b4052edaee8fb12d536d9953d819d33f7255
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 {
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);
64 /**
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
69 int[] entropy = null;
70 for (int i=0; i<model.targets.size(); i++) {
71 Target t = model.targets.get(i);
72 boolean flip = false;
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)) {
79 t.velX = t.velX * -1;
80 flip = true;
82 if (((t.y + t.velY) > view.screen_size.height-view.trial.mh) ||
83 ((t.y + t.velY - t.bounds.getHeight()) < 0)) {
84 t.velY = t.velY * -1;
85 flip = true;
87 if (flip==true) {
88 t.moveAngle = t.moveAngle + 180;
89 t.move = true;
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;
105 t.move = true;
106 } else {
107 t.steps++;
108 if (t.steps > 5) {
109 t.move = false;
110 t.steps = 0;
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();
120 try {
121 model.duration = model.duration + sleep;
122 Thread.sleep(sleep);
123 } catch (InterruptedException e) {
128 * Main Trial Tread
130 * @author rmh3093
133 class TrialThread implements Runnable {
135 int trials = 0;
137 public void run() {
138 while (true) {
139 // First move the targets around the screen
140 while (model.stage == Model.TrialStage.MOVE) {
141 if (model.duration < model.maxduration) {
142 moveTargets();
143 } else {
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;
153 // Search for target
154 model.startTime = System.currentTimeMillis();
155 while (model.stage == Model.TrialStage.FIND) {
156 synchronized (model.trialThread) {
157 try {
158 model.trialThread.wait();
159 } catch (InterruptedException e1) {
162 view.trial.repaint();
164 trials++;
165 if (trials >= model.trialCount)
166 view.trial.stopTrial();
167 // Trial is over, wait
168 model.waiting = true;
169 synchronized (model.trialThread) {
170 try {
171 model.trialThread.wait();
172 } catch (InterruptedException e1) {
175 model.waiting = false;
181 void startTrials() {
182 /* Initialize trial values */
183 model.maxduration = 1000 * (Integer)view.cp.duration_spinner.getValue();
184 model.entropy = view.cp.entropy_combobox.getSelectedItem().toString();
185 view.startTrial();
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);
192 model.duration = 0;
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 */
206 int max;
207 max = view.screen_size.width - (int)Math.round(t.bounds.getWidth());
208 if (t.x > max)
209 t.x = max - 1;
210 max = view.screen_size.height - view.trial.mh;
211 if (t.y > max)
212 t.y = max - 1;
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();
220 } else {
221 synchronized (model.trialThread) {
222 model.trialThread.notifyAll();