| 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 | * |
|---|
| 17 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 18 | */ |
|---|
| 19 | public class TimelineTickPanel extends JPanel |
|---|
| 20 | { |
|---|
| 21 | private int longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 22 | private int x, y; |
|---|
| 23 | private boolean focused = false; |
|---|
| 24 | |
|---|
| 25 | public void setZoom(float zoom) |
|---|
| 26 | { |
|---|
| 27 | repaint(); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | public class TimelineTickMouseListener extends MouseInputAdapter |
|---|
| 31 | { |
|---|
| 32 | @Override |
|---|
| 33 | public void mouseEntered(MouseEvent e) |
|---|
| 34 | { |
|---|
| 35 | focused = true; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | @Override |
|---|
| 39 | public void mouseExited(MouseEvent e) |
|---|
| 40 | { |
|---|
| 41 | focused = false; |
|---|
| 42 | repaint(); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | @Override |
|---|
| 46 | public void mouseMoved(MouseEvent e) |
|---|
| 47 | { |
|---|
| 48 | x = e.getX(); |
|---|
| 49 | y = e.getY(); |
|---|
| 50 | |
|---|
| 51 | repaint(); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public TimelineTickPanel() |
|---|
| 56 | { |
|---|
| 57 | super(); |
|---|
| 58 | |
|---|
| 59 | TimelineTickMouseListener mouseListener = |
|---|
| 60 | new TimelineTickMouseListener(); |
|---|
| 61 | addMouseMotionListener(mouseListener); |
|---|
| 62 | addMouseListener(mouseListener); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public void update(SimulationScript script) |
|---|
| 66 | { |
|---|
| 67 | longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 68 | |
|---|
| 69 | // Get the stats on the incidents |
|---|
| 70 | int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4; |
|---|
| 71 | for (ScriptIncident incident : script.incidents) |
|---|
| 72 | { |
|---|
| 73 | if (incident != null) |
|---|
| 74 | { |
|---|
| 75 | height += incident.collapsed ? |
|---|
| 76 | ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT : |
|---|
| 77 | ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT; |
|---|
| 78 | if (incident.length > longestLength) |
|---|
| 79 | longestLength = incident.length; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | Dimension newSize = new Dimension(longestLength / |
|---|
| 84 | ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION * |
|---|
| 85 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK + |
|---|
| 86 | ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50, |
|---|
| 87 | height); |
|---|
| 88 | this.setPreferredSize(newSize); |
|---|
| 89 | this.setSize(newSize); |
|---|
| 90 | |
|---|
| 91 | this.invalidate(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | @Override |
|---|
| 95 | public void paint(Graphics g) |
|---|
| 96 | { |
|---|
| 97 | super.paint(g); |
|---|
| 98 | |
|---|
| 99 | Graphics2D g2d = (Graphics2D)g; |
|---|
| 100 | |
|---|
| 101 | // Draw the horizontal line |
|---|
| 102 | g2d.setFont(ScriptBuilderGuiConstants.TIMELINE_TICK_TIME_FONT); |
|---|
| 103 | g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR); |
|---|
| 104 | g2d.fillRect(0, ScriptBuilderGuiConstants.TICK_TIMELINE_TOP_MARGIN, |
|---|
| 105 | longestLength, ScriptBuilderGuiConstants.TICK_TIMELINE_HEIGHT); |
|---|
| 106 | |
|---|
| 107 | // Draw the ticks |
|---|
| 108 | int longestLengthPlusMargin = longestLength / |
|---|
| 109 | ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION * |
|---|
| 110 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK + |
|---|
| 111 | ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 112 | |
|---|
| 113 | // Minutes |
|---|
| 114 | g2d.setColor(ScriptBuilderGuiConstants.MINOR_TICK_COLOR); |
|---|
| 115 | int seconds = 0; |
|---|
| 116 | for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 117 | i <= longestLengthPlusMargin; |
|---|
| 118 | i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK, |
|---|
| 119 | seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION) |
|---|
| 120 | { |
|---|
| 121 | g2d.drawLine(i, ScriptBuilderGuiConstants.TICK_TOP_MARGIN, |
|---|
| 122 | i, ScriptBuilderGuiConstants.TICK_HEIGHT); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | // Major Ticks |
|---|
| 126 | g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR); |
|---|
| 127 | seconds = 0; |
|---|
| 128 | for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 129 | i <= longestLengthPlusMargin; |
|---|
| 130 | i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK * |
|---|
| 131 | ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK, |
|---|
| 132 | seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION * |
|---|
| 133 | ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK) |
|---|
| 134 | { |
|---|
| 135 | g2d.drawLine(i, ScriptBuilderGuiConstants.TICK_TOP_MARGIN, |
|---|
| 136 | i, ScriptBuilderGuiConstants.TICK_HEIGHT); |
|---|
| 137 | |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | paintChildren(g); |
|---|
| 141 | |
|---|
| 142 | if (focused && |
|---|
| 143 | ((ScriptBuilderFrame)this.getTopLevelAncestor()).currentEventType != null) |
|---|
| 144 | EventIconDrawer.DrawEventIcon(g2d, |
|---|
| 145 | ((ScriptBuilderFrame)this.getTopLevelAncestor()).currentEventType, |
|---|
| 146 | x+5, y+10); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|