remove shit
[rmh3093.git] / motsim / MOTSIM_Controller.java
blob61ddeb2f1d8e1dfc0538721f998ad060f12c88b6
1 /**
2 * MOTSIM_Controller.java
3 * motsim
4 */
6 import java.awt.Font;
7 import java.awt.Graphics;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
12 Copyright (c) 2008, Ryan M. Hope
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without modification,
16 are permitted provided that the following conditions are met:
18 * Redistributions of source code must retain the above copyright notice,
19 this list of conditions and the following disclaimer.
20 * Redistributions in binary form must reproduce the above copyright notice,
21 this list of conditions and the following disclaimer in the documentation
22 and/or other materials provided with the distribution.
23 * Neither the name of the project nor the names of its contributors may be
24 used to endorse or promote products derived from this software without
25 specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
31 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
34 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 public class MOTSIM_Controller {
41 MOTSIM_Model model;
42 MOTSIM_View view;
44 TrialDataDynamic tdd;
46 Thread targetMoverThread;
48 public MOTSIM_Controller(MOTSIM_Model model, MOTSIM_View view) {
49 this.model = model;
50 this.view = view;
51 tdd = new TrialDataDynamic(view.screen_size);
54 public void registerActionListeners() {
55 view.cp.addStartListener(new StartListener());
58 class TargetMover implements Runnable {
59 public void run() {
60 while(model.move_targets == true) {
61 for (Target t : model.targets) {
62 if (((t.coordinate.x + t.velX) > (view.screen_size.width -
63 t.string_width)) ||
64 ((t.coordinate.x + t.velX) < 0)) {
65 t.velX = t.velX * -1;
67 if (((t.coordinate.y + t.velY) > view.screen_size.height) ||
68 ((t.coordinate.y + t.velY - t.font_size) < 0)) {
69 t.velY = t.velY * -1;
71 t.coordinate.x = (int)Math.round(t.coordinate.x + t.velX);
72 t.coordinate.y = (int)Math.round(t.coordinate.y + t.velY);
74 view.tp.repaint();
75 try {
76 Thread.sleep(20);
77 } catch (InterruptedException e) {
83 class StartListener implements ActionListener {
84 public void actionPerformed(ActionEvent e) {
85 model.targets = tdd.generate_targets((Integer)view.cp.
86 targets_spinner.getValue());
87 view.startTrial();
88 model.target_font = new Font(view.tp.getFont().getName(),
89 view.tp.getFont().getStyle(),
90 (Integer)view.cp.fontsize_spinner.getValue());
91 view.tp.bufferGraphics.setFont(model.target_font);
92 for (Target t : model.targets) {
93 t.string_width = view.tp.bufferGraphics.getFontMetrics().
94 stringWidth(t.callsign);
95 t.font_size = view.tp.bufferGraphics.getFontMetrics().
96 getHeight();
97 int max;
98 max = view.screen_size.width - t.string_width;
99 if (t.coordinate.x > max)
100 t.coordinate.x = max;
101 max = view.screen_size.height - t.font_size;
102 if (t.coordinate.y > max)
103 t.coordinate.y = max;
104 if (t.coordinate.y < t.font_size)
105 t.coordinate.y = t.font_size;
107 model.move_targets = true;
108 model.borderstyle =
109 view.cp.borderstyle_combobox.getSelectedItem().toString();
110 targetMoverThread = new Thread(new TargetMover());
111 targetMoverThread.start();