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