at end of trial select a random target and make it blink
[rmh3093.git] / motsim / MOTSIM_Controller.java
blobe0d815e0cdcdc75fadc40beec5fb2bc0548716d0
1 /**
2 * MOTSIM_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.Color;
35 import java.awt.Font;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.util.HashSet;
39 import java.util.Iterator;
40 import java.util.Random;
42 public class MOTSIM_Controller {
44 MOTSIM_Model model;
45 MOTSIM_View view;
47 TrialDataDynamic tdd;
49 Random rand;
51 public MOTSIM_Controller(MOTSIM_Model model, MOTSIM_View view) {
52 rand = new Random();
53 this.model = model;
54 this.view = view;
55 tdd = new TrialDataDynamic(view.screen_size);
58 public void registerActionListeners() {
59 view.cp.addStartListener(new StartListener());
62 private void pickTargetTarget() {
63 int count = 0;
64 int target = rand.nextInt(model.targets.size());
65 for (Target t : model.targets) {
66 if (count==target)
67 model.target = t;
68 else
69 count++;
73 protected synchronized void moveTargets() {
74 int sleep = 20;
75 int[] entropy = null;
76 int spawn = 0;
77 HashSet<Target> remove = new HashSet<Target>();
78 for (Target t : model.targets) {
79 boolean flip = false;
80 if (model.borderstyle == "Spawn") {
81 if (((t.x + t.velX) > (view.screen_size.width - t.string_width)) ||
82 ((t.x + t.velX) < 0) ||
83 ((t.y + t.velY) > view.screen_size.height) ||
84 ((t.y + t.velY - (2*t.font_size/3)) < 0)) {
85 remove.add(t);
86 spawn++;
87 continue;
89 } else {
90 if (((t.x + t.velX) > (view.screen_size.width - t.string_width)) ||
91 ((t.x + t.velX) < 0)) {
92 t.velX = t.velX * -1;
93 flip = true;
95 if (((t.y + t.velY) > view.screen_size.height) ||
96 ((t.y + t.velY - (2*t.font_size/3)) < 0)) {
97 t.velY = t.velY * -1;
98 flip = true;
100 if (flip==true) {
101 t.moveAngle = t.moveAngle + 180;
102 t.move = true;
105 if (model.entropy != "None") {
106 if (t.move == false) {
107 if (model.entropy == "Low") {
108 entropy = tdd.generateEntropyLow();
109 } else if (model.entropy == "Medium") {
110 entropy = tdd.generateEntropyMedium();
111 } else if (model.entropy == "High") {
112 entropy = tdd.generateEntropyHigh();
114 t.moveAngle = (t.moveAngle + entropy[rand.nextInt(10000)]) % 360;
115 t.move = true;
116 } else {
117 t.steps++;
118 if (t.steps > 5) {
119 t.move = false;
120 t.steps = 0;
123 t.velX = tdd.calcAngleMoveX(t.moveAngle);
124 t.velY = tdd.calcAngleMoveY(t.moveAngle);
126 t.x = t.x + t.velX * t.velocityMOD;
127 t.y = t.y + t.velY * t.velocityMOD;
129 if (model.borderstyle == "Spawn") {
130 model.targets.removeAll(remove);
131 tdd.spawnTargetOnBorder(model.targets, spawn);
133 view.tp.repaint();
134 try {
135 model.duration = model.duration + sleep;
136 Thread.sleep(sleep);
137 } catch (InterruptedException e) {
142 private void flipTargetColor() {
143 if (model.blink)
144 model.blink = false;
145 else
146 model.blink = true;
149 class TargetMover implements Runnable {
151 public void run() {
152 while (true) {
153 while (model.move_targets) {
154 if (model.duration < model.maxduration) {
155 moveTargets();
156 } else {
157 model.move_targets = false;
158 model.mask_targets = true;
161 if (model.mask_targets) {
162 view.tp.repaint();
163 try {
164 Thread.sleep(2000);
165 } catch (InterruptedException e1) {
167 pickTargetTarget();
168 model.blink_targets = true;
170 while (model.blink_targets && model.mask_targets) {
171 flipTargetColor();
172 view.tp.repaint();
173 try {
174 Thread.sleep(500);
175 } catch (InterruptedException e1) {
178 synchronized (model.targetMoverThread) {
179 try {
180 model.targetMoverThread.wait();
181 } catch (InterruptedException e1) {
189 class StartListener implements ActionListener {
191 public void actionPerformed(ActionEvent e) {
192 model.targets = tdd.generate_targets((Integer)view.cp.
193 targets_spinner.getValue());
194 model.maxduration = 1000 *
195 (Integer)view.cp.duration_spinner.getValue();
196 model.entropy =
197 view.cp.entropy_combobox.getSelectedItem().toString();
198 model.defaultvelocities =
199 view.cp.velocity_combobox.getSelectedItem().toString();
200 view.startTrial();
201 model.target_font = new Font(view.tp.getFont().getName(),
202 view.tp.getFont().getStyle(),
203 (Integer)view.cp.fontsize_spinner.getValue());
204 view.tp.bufferGraphics.setFont(model.target_font);
205 for (Target t : model.targets) {
206 t.velocityMOD = (Double)view.cp.speed_spinner.getValue();
207 if (model.defaultvelocities == "Gaussian") {
208 t.velocityMOD = t.velocityMOD * (1+Math.round(Math.pow(rand.nextGaussian(),2)))/2;
210 t.string_width = view.tp.bufferGraphics.getFontMetrics().
211 stringWidth(t.callsign);
212 t.font_size = view.tp.bufferGraphics.getFontMetrics().
213 getHeight();
214 int max;
215 max = view.screen_size.width - t.string_width;
216 if (t.x > max)
217 t.x = max;
218 max = view.screen_size.height - t.font_size;
219 if (t.y > max)
220 t.y = max;
221 if (t.y < t.font_size)
222 t.y = t.font_size;
224 model.move_targets = true;
225 model.duration = 0;
226 model.borderstyle =
227 view.cp.borderstyle_combobox.getSelectedItem().toString();
228 if (model.targetMoverThread == null) {
229 model.targetMoverThread = new Thread(new TargetMover());
230 model.targetMoverThread.start();
231 } else {
232 synchronized (model.targetMoverThread) {
233 model.targetMoverThread.notifyAll();