| 1 | package scriptbuilder.gui.panels; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Dimension; |
|---|
| 4 | import java.awt.Graphics; |
|---|
| 5 | import java.awt.Graphics2D; |
|---|
| 6 | import java.awt.event.MouseEvent; |
|---|
| 7 | import javax.swing.JPanel; |
|---|
| 8 | import javax.swing.event.MouseInputAdapter; |
|---|
| 9 | import scriptbuilder.gui.IncidentEditorFrame; |
|---|
| 10 | import scriptbuilder.gui.ScriptBuilderFrame; |
|---|
| 11 | import scriptbuilder.gui.ScriptBuilderGuiConstants; |
|---|
| 12 | import scriptbuilder.gui.drawers.EventIconDrawer; |
|---|
| 13 | import scriptbuilder.structures.ScriptIncident; |
|---|
| 14 | import scriptbuilder.structures.SimulationScript; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * Represents the underlying panel on the main timeline. All the |
|---|
| 18 | * IncidentTimelinePanel objects sit over it. This panel draws all the |
|---|
| 19 | * per-minute ticks on the timeline, and adjusts and scales them as necessary. |
|---|
| 20 | * |
|---|
| 21 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 22 | * @author Bryan McGuffin |
|---|
| 23 | */ |
|---|
| 24 | public class TimelineTickPanel extends JPanel |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | private int longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 28 | private int x, y; |
|---|
| 29 | private boolean focused = false; |
|---|
| 30 | |
|---|
| 31 | public void setZoom(float zoom) |
|---|
| 32 | { |
|---|
| 33 | repaint(); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Listener for the mouse. Is notified when the mouse enters, exits, or |
|---|
| 38 | * moves around on the panel. |
|---|
| 39 | */ |
|---|
| 40 | public class TimelineTickMouseListener extends MouseInputAdapter |
|---|
| 41 | { |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * When the mouse enters the panel, the panel gets focus. |
|---|
| 45 | * |
|---|
| 46 | * @param e the mouse event |
|---|
| 47 | */ |
|---|
| 48 | @Override |
|---|
| 49 | public void mouseEntered(MouseEvent e) |
|---|
| 50 | { |
|---|
| 51 | focused = true; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * When the mouse leaves the panel, the panel loses focus and refreshes. |
|---|
| 56 | * |
|---|
| 57 | * @param e the mouse event |
|---|
| 58 | */ |
|---|
| 59 | @Override |
|---|
| 60 | public void mouseExited(MouseEvent e) |
|---|
| 61 | { |
|---|
| 62 | focused = false; |
|---|
| 63 | repaint(); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * When the mouse moves around in the panel, the panel refreshes and |
|---|
| 68 | * updates its mouse tracker. |
|---|
| 69 | * |
|---|
| 70 | * @param e |
|---|
| 71 | */ |
|---|
| 72 | @Override |
|---|
| 73 | public void mouseMoved(MouseEvent e) |
|---|
| 74 | { |
|---|
| 75 | x = e.getX(); |
|---|
| 76 | y = e.getY(); |
|---|
| 77 | |
|---|
| 78 | repaint(); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Constructor. Set up the mouse listener. |
|---|
| 84 | */ |
|---|
| 85 | public TimelineTickPanel() |
|---|
| 86 | { |
|---|
| 87 | super(); |
|---|
| 88 | |
|---|
| 89 | TimelineTickMouseListener mouseListener |
|---|
| 90 | = new TimelineTickMouseListener(); |
|---|
| 91 | addMouseMotionListener(mouseListener); |
|---|
| 92 | addMouseListener(mouseListener); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Update the panel's dimensions based on number of events, zoom level, and |
|---|
| 97 | * which events are collapsed. |
|---|
| 98 | * |
|---|
| 99 | * @param script The main script model |
|---|
| 100 | */ |
|---|
| 101 | public void update(SimulationScript script) |
|---|
| 102 | { |
|---|
| 103 | longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 104 | |
|---|
| 105 | // Get the stats on the incidents |
|---|
| 106 | int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4; |
|---|
| 107 | for (ScriptIncident incident : script.incidents) |
|---|
| 108 | { |
|---|
| 109 | if (incident != null) |
|---|
| 110 | { |
|---|
| 111 | height += incident.collapsed |
|---|
| 112 | ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT |
|---|
| 113 | : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT; |
|---|
| 114 | if ((incident.length + incident.offset) > longestLength) |
|---|
| 115 | { |
|---|
| 116 | longestLength = incident.length + incident.offset; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | Dimension newSize = new Dimension(longestLength |
|---|
| 122 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 123 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 124 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50, |
|---|
| 125 | height); |
|---|
| 126 | this.setPreferredSize(newSize); |
|---|
| 127 | this.setSize(newSize); |
|---|
| 128 | |
|---|
| 129 | this.invalidate(); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Update the panel's dimensions based on number of events, zoom level, and |
|---|
| 134 | * which events are collapsed. |
|---|
| 135 | * |
|---|
| 136 | * @param script The main script model |
|---|
| 137 | */ |
|---|
| 138 | public void update(ScriptIncident incident) |
|---|
| 139 | { |
|---|
| 140 | longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 141 | |
|---|
| 142 | // Get the stats on the incidents |
|---|
| 143 | int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4; |
|---|
| 144 | |
|---|
| 145 | if (incident != null) |
|---|
| 146 | { |
|---|
| 147 | height += incident.collapsed |
|---|
| 148 | ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT |
|---|
| 149 | : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT; |
|---|
| 150 | if ((incident.length + incident.offset) > longestLength) |
|---|
| 151 | { |
|---|
| 152 | longestLength = incident.length + incident.offset; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | Dimension newSize = new Dimension(longestLength |
|---|
| 158 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 159 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 160 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50, |
|---|
| 161 | height); |
|---|
| 162 | this.setPreferredSize(newSize); |
|---|
| 163 | this.setSize(newSize); |
|---|
| 164 | |
|---|
| 165 | this.invalidate(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | * Refresh the panel. Redraw the ticks based on zoom level, panel |
|---|
| 170 | * dimensions, and offset. If the user is trying to add an event, draw that |
|---|
| 171 | * event's icon under the mouse. |
|---|
| 172 | * |
|---|
| 173 | * @param g The graphics component |
|---|
| 174 | */ |
|---|
| 175 | @Override |
|---|
| 176 | public void paint(Graphics g) |
|---|
| 177 | { |
|---|
| 178 | super.paint(g); |
|---|
| 179 | |
|---|
| 180 | Graphics2D g2d = (Graphics2D) g; |
|---|
| 181 | |
|---|
| 182 | // Draw the horizontal line |
|---|
| 183 | g2d.setFont(ScriptBuilderGuiConstants.TIMELINE_TICK_TIME_FONT); |
|---|
| 184 | g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR); |
|---|
| 185 | g2d.fillRect(0, ScriptBuilderGuiConstants.TICK_TIMELINE_TOP_MARGIN, |
|---|
| 186 | longestLength, ScriptBuilderGuiConstants.TICK_TIMELINE_HEIGHT); |
|---|
| 187 | |
|---|
| 188 | // Draw the ticks |
|---|
| 189 | int longestLengthPlusMargin = longestLength |
|---|
| 190 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 191 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 192 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 193 | |
|---|
| 194 | // Minutes |
|---|
| 195 | g2d.setColor(ScriptBuilderGuiConstants.MINOR_TICK_COLOR); |
|---|
| 196 | int seconds = 0; |
|---|
| 197 | for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 198 | i <= longestLengthPlusMargin; |
|---|
| 199 | i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION) |
|---|
| 200 | { |
|---|
| 201 | g2d.drawLine(i, ScriptBuilderGuiConstants.TICK_TOP_MARGIN, |
|---|
| 202 | i, ScriptBuilderGuiConstants.TICK_HEIGHT); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | // Major Ticks |
|---|
| 206 | g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR); |
|---|
| 207 | seconds = 0; |
|---|
| 208 | for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 209 | i <= longestLengthPlusMargin; |
|---|
| 210 | i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 211 | * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 212 | * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK) |
|---|
| 213 | { |
|---|
| 214 | g2d.drawLine(i, ScriptBuilderGuiConstants.TICK_TOP_MARGIN, |
|---|
| 215 | i, ScriptBuilderGuiConstants.TICK_HEIGHT); |
|---|
| 216 | |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | paintChildren(g); |
|---|
| 220 | |
|---|
| 221 | if (this.getTopLevelAncestor() instanceof ScriptBuilderFrame) |
|---|
| 222 | { |
|---|
| 223 | if (focused |
|---|
| 224 | && ((ScriptBuilderFrame) this.getTopLevelAncestor()).currentEventType != null) |
|---|
| 225 | { |
|---|
| 226 | EventIconDrawer.DrawEventIcon(g2d, |
|---|
| 227 | ((ScriptBuilderFrame) this.getTopLevelAncestor()).currentEventType, |
|---|
| 228 | x, y); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | if (this.getTopLevelAncestor() instanceof IncidentEditorFrame) |
|---|
| 232 | { |
|---|
| 233 | if (focused |
|---|
| 234 | && ((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType != null) |
|---|
| 235 | { |
|---|
| 236 | EventIconDrawer.DrawEventIcon(g2d, |
|---|
| 237 | ((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType, |
|---|
| 238 | x, y); |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | } |
|---|