| 1 | package scriptbuilder.gui.panels; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Color; |
|---|
| 4 | import java.awt.Dimension; |
|---|
| 5 | import java.awt.Graphics; |
|---|
| 6 | import java.awt.Graphics2D; |
|---|
| 7 | import javax.swing.JPanel; |
|---|
| 8 | import scriptbuilder.gui.IncidentEditorFrame; |
|---|
| 9 | import scriptbuilder.gui.ScriptBuilderGuiConstants; |
|---|
| 10 | import scriptbuilder.structures.ScriptIncident; |
|---|
| 11 | import scriptbuilder.structures.SimulationScript; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * Represents the timestamp bar at the top of the GUI timeline. Displays |
|---|
| 15 | * timestamps at significant intervals. Updates based on zoom level and scroll |
|---|
| 16 | * position. |
|---|
| 17 | * |
|---|
| 18 | * @author Greg Eddington |
|---|
| 19 | * @author Bryan McGuffin |
|---|
| 20 | */ |
|---|
| 21 | public class TimeStampPanel extends JPanel |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | public int offset = 0; |
|---|
| 25 | |
|---|
| 26 | private boolean isAbsolute = false; |
|---|
| 27 | |
|---|
| 28 | private int longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Just refresh the panel. |
|---|
| 32 | * |
|---|
| 33 | * @param zoom UNUSED |
|---|
| 34 | */ |
|---|
| 35 | public void setZoom(float zoom) |
|---|
| 36 | { |
|---|
| 37 | repaint(); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Constructor. |
|---|
| 42 | */ |
|---|
| 43 | public TimeStampPanel() |
|---|
| 44 | { |
|---|
| 45 | super(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public void setOffset(int newOffset) |
|---|
| 49 | { |
|---|
| 50 | offset = newOffset; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | public void setAbsolute(boolean abs) |
|---|
| 54 | { |
|---|
| 55 | isAbsolute = abs; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Update the length interval and the dimensions of the panel. This version |
|---|
| 60 | * of the method is used in the main script builder window. |
|---|
| 61 | * |
|---|
| 62 | * @param script The simulation script model |
|---|
| 63 | */ |
|---|
| 64 | public void update(SimulationScript script) |
|---|
| 65 | { |
|---|
| 66 | longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 67 | |
|---|
| 68 | // Get the stats on the incidents |
|---|
| 69 | int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4; |
|---|
| 70 | for (ScriptIncident incident : script.incidents) |
|---|
| 71 | { |
|---|
| 72 | if (incident != null) |
|---|
| 73 | { |
|---|
| 74 | height += incident.collapsed |
|---|
| 75 | ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT |
|---|
| 76 | : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT; |
|---|
| 77 | if ((incident.length + incident.offset) > longestLength) |
|---|
| 78 | { |
|---|
| 79 | longestLength = incident.length + incident.offset + IncidentTimelinePanel.requestedScriptBuilderFillerTime; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | for (ScriptIncident i : script.incidents) |
|---|
| 84 | { |
|---|
| 85 | if (i != null && i.number == 100) |
|---|
| 86 | { |
|---|
| 87 | i.length = longestLength; |
|---|
| 88 | i.offset = 0; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | Dimension newSize = new Dimension(longestLength |
|---|
| 94 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 95 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 96 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50, |
|---|
| 97 | height); |
|---|
| 98 | this.setPreferredSize(newSize); |
|---|
| 99 | this.setSize(newSize); |
|---|
| 100 | |
|---|
| 101 | this.invalidate(); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Update the length interval and the dimensions of the panel. This version |
|---|
| 106 | * of the method is used in the individual incident editor window. |
|---|
| 107 | * |
|---|
| 108 | * @param incident the incident which this window is editing. |
|---|
| 109 | */ |
|---|
| 110 | public void update(ScriptIncident incident, IncidentTimelinePanel timeline) |
|---|
| 111 | { |
|---|
| 112 | longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 113 | |
|---|
| 114 | // Get the stats on the incidents |
|---|
| 115 | int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4; |
|---|
| 116 | if (incident != null) |
|---|
| 117 | { |
|---|
| 118 | height += incident.collapsed |
|---|
| 119 | ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT |
|---|
| 120 | : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT; |
|---|
| 121 | if ((incident.length + incident.offset + timeline.requestedEditorFillerTime) > longestLength) |
|---|
| 122 | { |
|---|
| 123 | longestLength = incident.length + incident.offset + timeline.requestedEditorFillerTime; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | if (incident != null && incident.number == 100) |
|---|
| 128 | { |
|---|
| 129 | incident.length = longestLength; |
|---|
| 130 | incident.offset = 0; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | Dimension newSize = new Dimension(longestLength |
|---|
| 134 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 135 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 136 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50, |
|---|
| 137 | height); |
|---|
| 138 | this.setPreferredSize(newSize); |
|---|
| 139 | this.setSize(newSize); |
|---|
| 140 | |
|---|
| 141 | this.invalidate(); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /** |
|---|
| 145 | * Refresh the panel. Draw the timestamps for the appropriate intervals |
|---|
| 146 | * based on zoom level. |
|---|
| 147 | * |
|---|
| 148 | * @param g The graphics component |
|---|
| 149 | */ |
|---|
| 150 | @Override |
|---|
| 151 | public void paint(Graphics g) |
|---|
| 152 | { |
|---|
| 153 | super.paint(g); |
|---|
| 154 | String title = ""; |
|---|
| 155 | Graphics2D g2d = (Graphics2D) g; |
|---|
| 156 | g2d.setFont(ScriptBuilderGuiConstants.TIMELINE_TICK_TIME_FONT); |
|---|
| 157 | g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR); |
|---|
| 158 | int longestLengthPlusMargin = longestLength |
|---|
| 159 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 160 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 161 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 162 | int seconds = offset; |
|---|
| 163 | if(getTopLevelAncestor() instanceof IncidentEditorFrame) |
|---|
| 164 | { |
|---|
| 165 | title = "Incident:"; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | // Major Ticks |
|---|
| 169 | if (isAbsolute) |
|---|
| 170 | { |
|---|
| 171 | g2d.setColor(Color.getHSBColor(.5f, .9f, .5f)); |
|---|
| 172 | title = "Script:"; |
|---|
| 173 | } |
|---|
| 174 | else |
|---|
| 175 | { |
|---|
| 176 | g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR); |
|---|
| 177 | } |
|---|
| 178 | g2d.drawString(title, 0, 18); |
|---|
| 179 | for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 180 | i <= longestLengthPlusMargin; |
|---|
| 181 | i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 182 | * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 183 | * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK) |
|---|
| 184 | { |
|---|
| 185 | g2d.drawString(seconds / 3600 + ":" |
|---|
| 186 | + (seconds / 60 % 60 > 9 ? seconds / 60 % 60 |
|---|
| 187 | : "0" + seconds / 60 % 60) |
|---|
| 188 | + ":00", i - 25, 18); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | } |
|---|