Revision 407:93af066d13a8

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
}
src/main/java/cz/agents/alite/vis/visualizable/Point2dAdapter.java
1
package cz.agents.alite.vis.visualizable;
2

  
3
import javax.vecmath.Point2d;
4

  
5
/**
6
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
7
 */
8
public interface Point2dAdapter<T> {
9

  
10
    public Point2d convert(T t);
11

  
12
}
src/main/java/cz/agents/alite/vis/visualizable/Visualizable.java
1
package cz.agents.alite.vis.visualizable;
2

  
3
import java.awt.*;
4
import java.awt.geom.Rectangle2D;
5

  
6
/**
7
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
8
 */
9
public interface Visualizable {
10

  
11
    public void paint(Graphics2D canvas, Rectangle2D drawingRectangle);
12

  
13
}
src/main/java/cz/agents/alite/vis/visualizable/VisualizableLayer.java
1
package cz.agents.alite.vis.visualizable;
2

  
3
import cz.agents.alite.vis.Vis;
4
import cz.agents.alite.vis.layer.AbstractLayer;
5

  
6
import java.awt.*;
7
import java.awt.geom.Rectangle2D;
8

  
9
/**
10
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
11
 */
12
public class VisualizableLayer extends AbstractLayer {
13

  
14
    private final VisualizableProvider provider;
15

  
16
    public VisualizableLayer(VisualizableProvider provider) {
17
        this.provider = provider;
18
    }
19

  
20
    @Override
21
    public void paint(Graphics2D canvas) {
22
        Dimension dim = Vis.getDrawingDimension();
23
        Rectangle2D drawingRectangle = new Rectangle(dim);
24
        for (Visualizable visualizable : provider.getData()) {
25
            visualizable.paint(canvas, drawingRectangle);
26
        }
27
    }
28
}
src/main/java/cz/agents/alite/vis/visualizable/VisualizableProvider.java
1
package cz.agents.alite.vis.visualizable;
2

  
3
/**
4
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
5
 */
6
public interface VisualizableProvider {
7

  
8
    public Iterable<? extends Visualizable> getData();
9

  
10
}
src/main/java/cz/agents/alite/vis/visualizable/implementation/PointVis.java
1
package cz.agents.alite.vis.visualizable.implementation;
2

  
3
import cz.agents.alite.vis.Vis;
4
import cz.agents.alite.vis.visualizable.Point2dAdapter;
5
import cz.agents.alite.vis.visualizable.Visualizable;
6

  
7
import javax.vecmath.Point2d;
8
import java.awt.*;
9
import java.awt.geom.Rectangle2D;
10

  
11
/**
12
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
13
 */
14
public class PointVis<T> implements Visualizable {
15

  
16
    private final T centerPosition;
17
    private final int radiusInPx;
18
    private final Point2dAdapter<T> adapter;
19
    private final Color color;
20

  
21
    public PointVis(T centerPosition, int radiusInPx, Color color, Point2dAdapter<T> adapter) {
22
        this.centerPosition = centerPosition;
23
        this.radiusInPx = radiusInPx;
24
        this.adapter = adapter;
25
        this.color = color;
26
    }
27

  
28
    @Override
29
    public void paint(Graphics2D canvas, Rectangle2D drawingRectangle) {
30
        Point2d center = adapter.convert(centerPosition);
31
        int x = Vis.transX(center.x);
32
        int y = Vis.transX(center.y);
33
        canvas.setColor(color);
34

  
35
        Rectangle2D point = new Rectangle2D.Double(x - radiusInPx, y - radiusInPx,
36
                2 * radiusInPx + 1, 2 * radiusInPx + 1);
37

  
38
        if (drawingRectangle.intersects(point)) {
39
            canvas.fill(point);
40
        }
41
    }
42
}

Also available in: Unified diff