package NET.worlds.scape; import NET.worlds.console.Console; import NET.worlds.console.DialogDisabled; import NET.worlds.console.ExposedPanel; import NET.worlds.console.Main; import NET.worlds.console.MainCallback; import NET.worlds.console.Window; import java.awt.Color; import java.awt.FlowLayout; import java.util.Enumeration; import java.util.Vector; class ToolBar extends ExposedPanel implements MainCallback, DialogDisabled { private Vector buttons = new Vector(); private WidgetButton active; private boolean activeDown; private WidgetButton entered; private WObject curWObj; private boolean haveDelta = false; private WObjectHighlighter highlight; private boolean initialDrag; private boolean isDialogDisabled; private boolean testConstrained; private boolean constrainToX; private boolean constrainToY; private int cumx; private int cumy; private float motionDivisor = 1.0F; private static final int constrainThresh = 5; public ToolBar() { this.setLayout(new FlowLayout(0, 1, 1)); this.addButton(new HTransWidget(this)); this.addButton(new VTransWidget(this)); this.addButton(new PitchWidget(this)); this.addButton(new RollWidget(this)); this.addButton(new YawWidget(this)); this.addButton(new ScaleWidget(this)); this.addButton(new CutWidget(this)); this.addButton(new CopyWidget(this)); this.addButton(new PasteWidget(this)); this.addButton(new SaveWidget(this)); this.addButton(new UndoWidget(this)); this.setBackground(Color.lightGray); } public void done() { if (this.highlight != null) { this.highlight.stop(); } } private void addButton(WidgetButton button) { this.add(button); this.buttons.addElement(button); } public void setCurrentObject(Object obj) { this.curWObj = obj instanceof WObject ? (WObject)obj : null; if (this.highlight != null) { this.highlight.stop(); this.highlight = null; } if (this.curWObj != null) { this.highlight = new WObjectHighlighter(this.curWObj); } Enumeration e = this.buttons.elements(); this.clearPrompt(); while (e.hasMoreElements()) { ((WidgetButton)e.nextElement()).repaint(); } } public WObject getCurrentWObject() { return this.curWObj; } @Override public void dialogDisable(boolean disable) { this.isDialogDisabled = disable; } @Override public boolean handleEvent(java.awt.Event event) { return this.isDialogDisabled ? false : super.handleEvent(event); } @Override public synchronized boolean mouseDown(java.awt.Event event, int x, int y) { WidgetButton button; if (event.target instanceof WidgetButton && (event.modifiers & 4) == 0 && this.active == null && (button = (WidgetButton)event.target).available()) { this.active = button; this.active.draw(true); if (this.active.usesDrag()) { this.initialDrag = true; this.highlight.stop(); Window.hideCursor(); Main.register(this); this.haveDelta = true; } else { this.activeDown = true; } } return true; } @Override public synchronized boolean mouseDrag(java.awt.Event event, int x, int y) { if (this.active != null) { if (this.active.usesDrag()) { if ((event.modifiers & 1) != 0 && !this.constrainToX && !this.constrainToY) { this.testConstrained = true; this.cumx = this.cumy = 0; } this.motionDivisor = (event.modifiers & 2) != 0 ? 10.0F : 1.0F; this.haveDelta = true; } else { boolean inside = this.active.getBounds().inside(x, y); if (inside != this.activeDown) { this.active.draw(this.activeDown = inside); } } } return true; } @Override public synchronized void mainCallback() { if (this.haveDelta && this.active != null) { int[] pos = Window.getHiddenCursorDelta(); if (this.testConstrained) { this.cumx = this.cumx + pos[0]; this.cumy = this.cumy + pos[1]; int acumx = Math.abs(this.cumx); int acumy = Math.abs(this.cumy); if (acumx > acumy + 5) { this.constrainToX = true; } else { if (acumy <= acumx + 5) { return; } this.constrainToY = true; } this.testConstrained = false; } if (this.constrainToX) { pos[0] += this.cumx; this.cumx = 0; pos[1] = 0; } else if (this.constrainToY) { pos[1] += this.cumy; this.cumy = 0; pos[0] = 0; } this.setPrompt(this.active.drag(this.initialDrag, pos[0] / this.motionDivisor, -pos[1] / this.motionDivisor)); this.initialDrag = false; this.haveDelta = false; } } @Override public synchronized boolean mouseUp(java.awt.Event event, int x, int y) { if (this.active != null) { this.active.draw(false); boolean needUpdate = true; if (this.active.usesDrag()) { this.highlight.start(); Main.unregister(this); } else if (this.activeDown) { this.active.perform(); this.activeDown = false; } else { needUpdate = false; } this.active = null; if (needUpdate) { Console.getFrame().getEditTile().update(); } } return true; } @Override public boolean mouseEnter(java.awt.Event event, int x, int y) { WidgetButton button; if (this.active == null && event.target instanceof WidgetButton && (button = (WidgetButton)event.target).available()) { this.entered = button; this.setPrompt(this.entered.getPrompt()); } return true; } @Override public boolean mouseMove(java.awt.Event event, int x, int y) { return this.mouseEnter(event, x, y); } @Override public synchronized boolean keyUp(java.awt.Event event, int key) { if ((event.modifiers & 1) == 0) { this.testConstrained = this.constrainToX = this.constrainToY = false; } return true; } public void setPrompt(String prompt) { Console.getFrame().getEditTile().setPrompt(prompt); } private void clearPrompt() { this.entered = null; Console.getFrame().getEditTile().setPrompt(null); } @Override public boolean mouseExit(java.awt.Event event, int x, int y) { if (this.active == null && event.target == this.entered) { this.clearPrompt(); } return true; } }