source: tmcsimulator-scriptbuilder/src/scriptbuilder/gui/panels/TimeStampPanel.java @ 1

Revision 1, 4.0 KB checked in by bmcguffin, 9 years ago (diff)

2017/07/18: Uploaded entire prototype to SVN repo.

Line 
1package scriptbuilder.gui.panels;
2
3import java.awt.Dimension;
4import java.awt.Graphics;
5import java.awt.Graphics2D;
6import javax.swing.JPanel;
7import scriptbuilder.gui.ScriptBuilderGuiConstants;
8import scriptbuilder.structures.ScriptIncident;
9import scriptbuilder.structures.SimulationScript;
10
11/**
12 * Represents the timestamp bar at the top of the GUI timeline. Displays
13 * timestamps at significant intervals. Updates based on zoom level and scroll
14 * position.
15 *
16 * @author Greg Eddington
17 * @author Bryan McGuffin
18 */
19public class TimeStampPanel extends JPanel
20{
21
22    private int longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;
23
24    /**
25     * Just refresh the panel.
26     *
27     * @param zoom UNUSED
28     */
29    public void setZoom(float zoom)
30    {
31        repaint();
32    }
33
34    /**
35     * Constructor.
36     */
37    public TimeStampPanel()
38    {
39        super();
40    }
41
42    /**
43     * Update the length interval and the dimensions of the panel. NOTE: This
44     * method implementation is an exact duplication of the update method in
45     * panels.TimelineTickPanel. I'm not sure if it actually accomplishes
46     * anything here.
47     *
48     * @param script The simulation script model
49     */
50    public void update(SimulationScript script)
51    {
52        longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;
53
54        // Get the stats on the incidents
55        int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4;
56        for (ScriptIncident incident : script.incidents)
57        {
58            if (incident != null)
59            {
60                height += incident.collapsed
61                        ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT
62                        : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT;
63                if ((incident.length + incident.offset) > longestLength)
64                {
65                    longestLength = incident.length + incident.offset;
66                }
67            }
68        }
69        for (ScriptIncident i : script.incidents)
70        {
71            if (i != null && i.number == 100)
72            {
73                i.length = longestLength;
74                i.offset = 0;
75            }
76
77        }
78
79        Dimension newSize = new Dimension(longestLength
80                / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
81                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
82                + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50,
83                height);
84        this.setPreferredSize(newSize);
85        this.setSize(newSize);
86
87        this.invalidate();
88    }
89
90    /**
91     * Refresh the panel. Draw the timestamps for the appropriate intervals
92     * based on zoom level.
93     *
94     * @param g The graphics component
95     */
96    @Override
97    public void paint(Graphics g)
98    {
99        super.paint(g);
100
101        Graphics2D g2d = (Graphics2D) g;
102        g2d.setFont(ScriptBuilderGuiConstants.TIMELINE_TICK_TIME_FONT);
103        g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR);
104        int longestLengthPlusMargin = longestLength
105                / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
106                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
107                + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN;
108        int seconds = 0;
109
110        // Major Ticks
111        g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR);
112        seconds = 0;
113        for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN;
114                i <= longestLengthPlusMargin;
115                i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
116                * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
117                * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK)
118        {
119            g2d.drawString(seconds / 3600 + ":"
120                    + (seconds / 60 % 60 > 9 ? seconds / 60 % 60
121                    : "0" + seconds / 60 % 60)
122                    + ":00", i - 25, 18);
123        }
124    }
125}
Note: See TracBrowser for help on using the repository browser.