Revision 405:15ab258ad1b1

View differences:

src/main/java/cz/agents/alite/vis/layer/common/SimulationControlLayer.java
1
package cz.agents.alite.vis.layer.common;
2

  
3
import cz.agents.alite.simulation.Simulation;
4
import cz.agents.alite.vis.Vis;
5
import cz.agents.alite.vis.layer.AbstractLayer;
6
import cz.agents.alite.vis.layer.VisLayer;
7
import cz.agents.alite.vis.layer.toggle.KeyToggleLayer;
8

  
9
import java.awt.*;
10
import java.awt.event.KeyEvent;
11
import java.awt.event.KeyListener;
12
import java.text.MessageFormat;
13

  
14
/**
15
 * The layer shows the status of the simulation and controls it through various
16
 * key bindings.
17
 *
18
 * The information shown, tells the user, the current simulation speed (ratio of
19
 * the real time and simulation time) and the state of the simulation.
20
 *
21
 * The simulation speed ratio can be controlled by '+' and '-' keys. And
22
 * additionally, Ctrl+'*' sets the fastest possible speed (infinite ratio), and
23
 * '*' pressed sets the ratio to its default value.
24
 *
25
 * All the possible key strokes are described in the internal help showed by the
26
 * {@link HelpLayer}.
27
 *
28
 *
29
 * @author Antonin Komenda
30
 */
31
public class SimulationControlLayer extends AbstractLayer {
32

  
33
    private final Simulation simulation;
34
    private KeyListener keyListener;
35

  
36
    SimulationControlLayer(Simulation simulation) {
37
        this.simulation = simulation;
38
    }
39

  
40
    @Override
41
    public void init(Vis vis) {
42
        super.init(vis);
43

  
44
        keyListener = new KeyListener() {
45

  
46
            @Override
47
            public void keyTyped(KeyEvent e) {
48
            }
49

  
50
            @Override
51
            public void keyReleased(KeyEvent e) {
52
            }
53

  
54
            @Override
55
            public void keyPressed(KeyEvent e) {
56
                if (e.getKeyChar() == '+') {
57
                    simulation.setSimulationSpeed(simulation.getSimulationSpeed() * 0.9);
58
                } else if (e.getKeyChar() == '-') {
59
                    simulation.setSimulationSpeed(simulation.getSimulationSpeed() * 1.1);
60
                } else if (e.getKeyChar() == '*') {
61
                    if ((e.getModifiers() & (KeyEvent.CTRL_MASK | KeyEvent.CTRL_DOWN_MASK)) != 0) {
62
                        simulation.setSimulationSpeed(0);
63
                    } else {
64
                        simulation.setSimulationSpeed(0.2);
65
                    }
66
                } else if (e.getKeyChar() == ' ') {
67
                    if (simulation.isRunning()) {
68
                        simulation.setRunning(false);
69
                    } else {
70
                        simulation.setRunning(true);
71
                    }
72
                }
73
            }
74
        };
75
        vis.addKeyListener(keyListener);
76
    }
77

  
78
    @Override
79
    public void deinit(Vis vis) {
80
        super.deinit(vis);
81

  
82
        vis.removeKeyListener(keyListener);
83
    }
84

  
85
    @Override
86
    public void paint(Graphics2D canvas) {
87
        StringBuilder label = new StringBuilder();
88
        label.append("TIME: ");
89
        label.append(simulation.getCurrentTime() / 1000.0);
90
        label.append(" ");
91
        if (simulation.isFinished()) {
92
            label.append("(FINISHED)");
93
        } else {
94
            if (simulation.getCurrentTime() == 0) {
95
                label.append("(INITIALIZING)");
96

  
97
                canvas.setColor(new Color(0, 0, 0, 200));
98
                canvas.fillRect(200, 400, Vis.getDrawingDimension().width - 400, Vis.getDrawingDimension().height - 800);
99

  
100
                Font oldFont = canvas.getFont();
101
                canvas.setFont(new Font("Arial", 0, 20));
102
                canvas.setColor(Color.WHITE);
103
                canvas.drawString("INITIALIZING...", Vis.getDrawingDimension().width / 2 - 60 , Vis.getDrawingDimension().height / 2 + 7);
104
                canvas.setFont(oldFont);
105
            } else {
106
                if (simulation.isRunning()) {
107
                    label.append("(");
108
                    label.append(MessageFormat.format("{0,number,#.##}", 1/simulation.getSimulationSpeed()/5.0));
109
                    label.append("x)");
110
                } else {
111
                    label.append("(PAUSED)");
112
                }
113
            }
114
        }
115

  
116
        canvas.setColor(Color.BLUE);
117
        canvas.drawString(label.toString(), 15, 20);
118
    }
119

  
120
    @Override
121
    public String getLayerDescription() {
122
        String description = "[Simulation Control] Layer controls the simulation and shows simulation time and speed,\n" +
123
                "by pressing '<space>', the simulation can be paused and unpaused,\n" +
124
                "by pressing '+'/'-', the simulation can be speed up and slow down,\n" +
125
                "by pressing '*', the speed of simulation is set to default value (1x),\n" +
126
                "by pressing Ctrl+'*', the speed of simulation is set to fastest possible speed (∞x).";
127
        return buildLayersDescription(description);
128
    }
129

  
130
    public static VisLayer create(Simulation simulation) {
131
        VisLayer simulationControl = new SimulationControlLayer(simulation);
132

  
133
        KeyToggleLayer toggle = KeyToggleLayer.create("s");
134
        toggle.addSubLayer(simulationControl);
135
        toggle.setHelpOverrideString(simulationControl.getLayerDescription() + "\n" +
136
                "By pressing 's', the simulation info can be turned off and on.");
137

  
138
        return toggle;
139
    }
140

  
141
}

Also available in: Unified diff