mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 00:37:55 +00:00
Bugfixes & slick UI
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
package org.parabot.core.logging;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
|
||||
public class LabelLogHandler extends Handler {
|
||||
public final JLabel label;
|
||||
private final Color defaultColor;
|
||||
|
||||
public LabelLogHandler() {
|
||||
this.label = new JLabel();
|
||||
this.defaultColor = label.getForeground();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(final LogRecord record) {
|
||||
StringBuilder b = new StringBuilder(record.getMessage());
|
||||
|
||||
if (record.getLevel().intValue() > Level.WARNING.intValue()) {
|
||||
label.setForeground(new Color(0xcc0000));
|
||||
} else {
|
||||
label.setForeground(defaultColor);
|
||||
b.append(" ...");
|
||||
}
|
||||
|
||||
label.setText(new String(b));
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package org.parabot.core.logging;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Formatter;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
public class LogFormatter extends Formatter {
|
||||
private static final String LINE_SEPARATOR = System
|
||||
.getProperty("line.separator");
|
||||
private final boolean appendNewLine;
|
||||
|
||||
public LogFormatter() {
|
||||
this(true);
|
||||
}
|
||||
|
||||
public LogFormatter(final boolean appendNewLine) {
|
||||
this.appendNewLine = appendNewLine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format(final LogRecord record) {
|
||||
final StringBuilder result = new StringBuilder().append("[")
|
||||
.append(record.getLevel().getName()).append("] ")
|
||||
.append(new Date(record.getMillis())).append(": ")
|
||||
.append(record.getLoggerName()).append(": ")
|
||||
.append(record.getMessage())
|
||||
.append(throwableToString(record.getThrown()));
|
||||
if (appendNewLine) {
|
||||
result.append(LogFormatter.LINE_SEPARATOR);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatMessage(final LogRecord record) {
|
||||
return String.format(record.getMessage());
|
||||
}
|
||||
|
||||
private String throwableToString(final Throwable t) {
|
||||
if (t != null) {
|
||||
final Writer exception = new StringWriter();
|
||||
final PrintWriter printWriter = new PrintWriter(exception);
|
||||
t.printStackTrace(printWriter);
|
||||
return exception.toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package org.parabot.core.logging;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author Paris
|
||||
*/
|
||||
public class LogOutputStream extends OutputStream {
|
||||
protected boolean hasBeenClosed;
|
||||
protected Logger category;
|
||||
protected Level priority;
|
||||
protected StringBuilder buffer;
|
||||
|
||||
public LogOutputStream(final Logger category, final Level priority) {
|
||||
this.priority = priority;
|
||||
this.category = category;
|
||||
buffer = new StringBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
flush();
|
||||
hasBeenClosed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
final String txt = buffer.toString().replace("\\s+$", "");
|
||||
if (txt.trim().length() != 0) {
|
||||
category.log(priority, txt);
|
||||
}
|
||||
reset();
|
||||
}
|
||||
|
||||
private void reset() {
|
||||
buffer.setLength(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(final int b) throws IOException {
|
||||
if (hasBeenClosed) {
|
||||
throw new IOException("The stream has been closed.");
|
||||
} else if (b != 0) {
|
||||
buffer.append((char) (b & 0xff));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,257 +0,0 @@
|
||||
package org.parabot.core.logging;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.Font;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Rectangle;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Formatter;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import javax.swing.AbstractListModel;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JTextPane;
|
||||
import javax.swing.ListCellRenderer;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.border.Border;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
/**
|
||||
* Non swing methods are thread safe.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class LogTextArea extends JList {
|
||||
private static final int MAX_ENTRIES = 100;
|
||||
private static final Rectangle BOTTOM_OF_WINDOW = new Rectangle(0,
|
||||
Integer.MAX_VALUE, 0, 0);
|
||||
private static final long serialVersionUID = 0;
|
||||
private static final Formatter formatter = new Formatter() {
|
||||
private final SimpleDateFormat dateFormat = new SimpleDateFormat(
|
||||
"hh:mm:ss");
|
||||
|
||||
@Override
|
||||
public String format(final LogRecord record) {
|
||||
final String[] className = record.getLoggerName().split("\\.");
|
||||
final String name = className[className.length - 1];
|
||||
final int maxLen = 16;
|
||||
final String append = "...";
|
||||
|
||||
return String.format(
|
||||
"[%s] %-" + maxLen + "s %s %s",
|
||||
dateFormat.format(record.getMillis()),
|
||||
name.length() > maxLen ? name.substring(0,
|
||||
maxLen - append.length())
|
||||
+ append : name, record.getMessage(),
|
||||
throwableToString(record.getThrown()));
|
||||
}
|
||||
};
|
||||
private static final Formatter copyPasteFormatter = new LogFormatter(false);
|
||||
|
||||
private final LogQueue logQueue;
|
||||
private final LogAreaListModel model;
|
||||
private final Runnable scrollToBottom;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public LogTextArea() {
|
||||
logQueue = new LogQueue();
|
||||
model = new LogAreaListModel();
|
||||
scrollToBottom = new Runnable() {
|
||||
public void run() {
|
||||
scrollRectToVisible(LogTextArea.BOTTOM_OF_WINDOW);
|
||||
}
|
||||
};
|
||||
|
||||
setModel(model);
|
||||
setCellRenderer(new Renderer());
|
||||
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
||||
String fontName = Font.MONOSPACED;
|
||||
for (final Font font : GraphicsEnvironment
|
||||
.getLocalGraphicsEnvironment().getAllFonts()) {
|
||||
final String name = font.getName();
|
||||
if (name.matches("Monaco|Consolas")) {
|
||||
fontName = name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
setFont(new Font(fontName, Font.PLAIN, 12));
|
||||
|
||||
new Thread(logQueue, "LogGuiQueue").start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a new entry to be shown in the list. Thread safe.
|
||||
*
|
||||
* @param logRecord
|
||||
* The entry.
|
||||
*/
|
||||
public void log(final LogRecord logRecord) {
|
||||
logQueue.queue(new WrappedLogRecord(logRecord));
|
||||
}
|
||||
|
||||
private class LogAreaListModel extends AbstractListModel {
|
||||
private static final long serialVersionUID = 0;
|
||||
|
||||
private List<WrappedLogRecord> records = new ArrayList<WrappedLogRecord>(
|
||||
LogTextArea.MAX_ENTRIES);
|
||||
|
||||
public void addAllElements(final List<WrappedLogRecord> obj) {
|
||||
records.addAll(obj);
|
||||
if (getSize() > LogTextArea.MAX_ENTRIES) {
|
||||
records.subList(0, (getSize() - LogTextArea.MAX_ENTRIES))
|
||||
.clear();
|
||||
|
||||
fireContentsChanged(this, 0, (getSize() - 1));
|
||||
} else {
|
||||
fireIntervalAdded(this, (getSize() - 1), (getSize() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
public Object getElementAt(final int index) {
|
||||
return records.get(index);
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return records.size();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes every #FLUSH_RATE (milliseconds)
|
||||
*/
|
||||
private class LogQueue implements Runnable {
|
||||
public static final int FLUSH_RATE = 1000;
|
||||
private final Object lock = new Object();
|
||||
private List<WrappedLogRecord> queue = new ArrayList<WrappedLogRecord>(
|
||||
100);
|
||||
|
||||
public void queue(final WrappedLogRecord record) {
|
||||
synchronized (lock) {
|
||||
queue.add(record);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while (true) {
|
||||
List<WrappedLogRecord> toFlush = null;
|
||||
|
||||
synchronized (lock) {
|
||||
if (queue.size() != 0) {
|
||||
toFlush = new ArrayList<WrappedLogRecord>(queue);
|
||||
queue = queue.subList(0, 0);
|
||||
}
|
||||
}
|
||||
if (toFlush != null) { // Hold the lock for as little time as
|
||||
// possible
|
||||
model.addAllElements(toFlush);
|
||||
SwingUtilities.invokeLater(scrollToBottom);
|
||||
}
|
||||
try {
|
||||
Thread.sleep(LogQueue.FLUSH_RATE);
|
||||
} catch (final InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class Renderer implements ListCellRenderer {
|
||||
private final Border EMPTY_BORDER = new EmptyBorder(1, 1, 1, 1);
|
||||
private final Border SELECTED_BORDER = UIManager
|
||||
.getBorder("List.focusCellHighlightBorder");
|
||||
private final Color DARK_GREEN = new Color(0, 0xcc, 0),
|
||||
DARK_RED = new Color(0xcc, 0, 0), DARK_PINK = new Color(0xff,
|
||||
0, 0x66);
|
||||
|
||||
public Component getListCellRendererComponent(final JList list,
|
||||
final Object value, final int index, final boolean isSelected,
|
||||
final boolean cellHasFocus) {
|
||||
if (!(value instanceof WrappedLogRecord)) {
|
||||
return new JLabel();
|
||||
}
|
||||
final WrappedLogRecord wlr = (WrappedLogRecord) value;
|
||||
|
||||
final JTextPane result = new JTextPane();
|
||||
result.setDragEnabled(true);
|
||||
result.setText(wlr.formatted);
|
||||
result.setComponentOrientation(list.getComponentOrientation());
|
||||
result.setFont(list.getFont());
|
||||
result.setBorder(cellHasFocus || isSelected ? SELECTED_BORDER
|
||||
: EMPTY_BORDER);
|
||||
|
||||
result.setForeground(Color.BLACK);
|
||||
|
||||
if (wlr.record.getLevel() == Level.SEVERE) {
|
||||
result.setBackground(DARK_RED);
|
||||
result.setForeground(Color.BLACK);
|
||||
}
|
||||
|
||||
if (wlr.record.getLevel() == Level.WARNING) {
|
||||
result.setForeground(DARK_PINK);
|
||||
}
|
||||
|
||||
if (wlr.record.getLevel() == Level.FINE
|
||||
|| wlr.record.getLevel() == Level.FINER
|
||||
|| wlr.record.getLevel() == Level.FINEST) {
|
||||
result.setForeground(DARK_GREEN);
|
||||
}
|
||||
|
||||
final Object[] parameters = wlr.record.getParameters();
|
||||
if (parameters != null) {
|
||||
for (final Object parameter : parameters) {
|
||||
if (parameter == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parameter instanceof Color) {
|
||||
result.setForeground((Color) parameter);
|
||||
} else if (parameter instanceof Font) {
|
||||
result.setFont((Font) parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the log records so we can control the copy paste text (via
|
||||
* #toString)
|
||||
*/
|
||||
private class WrappedLogRecord {
|
||||
public final LogRecord record;
|
||||
public final String formatted;
|
||||
|
||||
public WrappedLogRecord(final LogRecord record) {
|
||||
this.record = record;
|
||||
formatted = LogTextArea.formatter.format(record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return LogTextArea.copyPasteFormatter.format(record);
|
||||
}
|
||||
}
|
||||
|
||||
private static String throwableToString(final Throwable t) {
|
||||
if (t != null) {
|
||||
final Writer exception = new StringWriter();
|
||||
final PrintWriter printWriter = new PrintWriter(exception);
|
||||
t.printStackTrace(printWriter);
|
||||
return exception.toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package org.parabot.core.logging;
|
||||
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
/**
|
||||
* Logs to System.out
|
||||
*/
|
||||
public class SystemConsoleHandler extends ConsoleHandler {
|
||||
public SystemConsoleHandler() {
|
||||
super();
|
||||
setOutputStream(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(final LogRecord logRecord) {
|
||||
System.out.println("PUBLISH");
|
||||
System.out.println(logRecord.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package org.parabot.core.logging;
|
||||
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
|
||||
public class TextAreaLogHandler extends Handler {
|
||||
public static final LogTextArea TEXT_AREA = new LogTextArea();
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(final LogRecord record) {
|
||||
TextAreaLogHandler.TEXT_AREA.log(record);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.parabot.core.ui;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import javax.swing.JDialog;
|
||||
|
||||
import org.parabot.core.ui.components.PaintComponent;
|
||||
@@ -18,9 +17,7 @@ public class BotDialog extends JDialog {
|
||||
private BotDialog(BotUI botUI) {
|
||||
super(botUI);
|
||||
|
||||
|
||||
botUI.setDialog(this);
|
||||
|
||||
setUndecorated(true);
|
||||
getRootPane().setOpaque(false);
|
||||
setBackground(new Color(0, 0, 0, 0));
|
||||
@@ -30,7 +27,7 @@ public class BotDialog extends JDialog {
|
||||
setVisible(true);
|
||||
setContentPane(PaintComponent.getInstance(botUI.getSize()));
|
||||
botUI.setVisible(true);
|
||||
//setAlwaysOnTop(true);
|
||||
|
||||
}
|
||||
|
||||
public static BotDialog getInstance(BotUI botUI) {
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package org.parabot.core.ui;
|
||||
|
||||
import org.parabot.core.Configuration;
|
||||
import org.parabot.core.ui.components.BotToolbar;
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.ui.components.GamePanel;
|
||||
import org.parabot.core.ui.components.LogArea;
|
||||
import org.parabot.core.ui.components.VerboseLoader;
|
||||
import org.parabot.core.ui.images.Images;
|
||||
import org.parabot.environment.scripts.Script;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
@@ -29,6 +27,9 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
private static final long serialVersionUID = -2126184292879805519L;
|
||||
private static BotUI instance;
|
||||
private static JDialog dialog;
|
||||
|
||||
private JMenuItem run, pause, stop;
|
||||
private boolean runScript, pauseScript;
|
||||
|
||||
public static BotUI getInstance() {
|
||||
return instance == null ? instance = new BotUI() : instance;
|
||||
@@ -38,67 +39,58 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
|
||||
|
||||
this.setTitle("Parabot");
|
||||
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||
this.setResizable(false);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setIconImage(Images.getResource("/org/parabot/core/ui/images/icon.png"));
|
||||
this.setLayout(new BorderLayout());
|
||||
this.addComponentListener(this);
|
||||
this.addWindowListener(this);
|
||||
this.setIgnoreRepaint(true);
|
||||
|
||||
int iToolbarHeight = 24;
|
||||
int iGameHeight = 503;
|
||||
int iLogHeight = 128;
|
||||
JMenuBar menuBar = new JMenuBar();
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLocation(0, 0);
|
||||
panel.setPreferredSize(new Dimension(765, iToolbarHeight + iGameHeight + iLogHeight));
|
||||
|
||||
JMenuBar menubar = new JMenuBar();
|
||||
|
||||
JMenu mnuFile = new JMenu("File");
|
||||
JMenu file = new JMenu("File");
|
||||
JMenu scripts = new JMenu("Script");
|
||||
|
||||
JMenuItem proxy = new JMenuItem("Network");
|
||||
JMenuItem exit = new JMenuItem("Exit");
|
||||
|
||||
run = new JMenuItem("Run");
|
||||
run.setIcon(new ImageIcon(Images.getResource("/org/parabot/core/ui/images/run.png")));
|
||||
|
||||
pause = new JMenuItem("Pause");
|
||||
pause.setEnabled(false);
|
||||
pause.setIcon(new ImageIcon(Images.getResource("/org/parabot/core/ui/images/pause.png")));
|
||||
|
||||
stop = new JMenuItem("Stop");
|
||||
stop.setEnabled(false);
|
||||
stop.setIcon(new ImageIcon(Images.getResource("/org/parabot/core/ui/images/stop.png")));
|
||||
|
||||
proxy.addActionListener(this);
|
||||
exit.addActionListener(this);
|
||||
|
||||
run.addActionListener(this);
|
||||
pause.addActionListener(this);
|
||||
stop.addActionListener(this);
|
||||
|
||||
file.add(proxy);
|
||||
file.add(exit);
|
||||
|
||||
scripts.add(run);
|
||||
scripts.add(pause);
|
||||
scripts.add(stop);
|
||||
|
||||
menuBar.add(file);
|
||||
menuBar.add(scripts);
|
||||
|
||||
mnuFile.add(proxy);
|
||||
mnuFile.add(exit);
|
||||
menubar.add(mnuFile);
|
||||
this.setJMenuBar(menuBar);
|
||||
|
||||
this.setJMenuBar(menubar);
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
JToolBar toolbar = BotToolbar.getInstance();
|
||||
toolbar.setPreferredSize(new Dimension(765, iToolbarHeight));
|
||||
toolbar.setLocation(x, y);
|
||||
|
||||
y += iToolbarHeight;
|
||||
|
||||
GamePanel gamePanel = GamePanel.getInstance();
|
||||
gamePanel.setPreferredSize(new Dimension(765, iGameHeight));
|
||||
toolbar.setLocation(x, y);
|
||||
|
||||
y += iGameHeight;
|
||||
|
||||
JScrollPane scrlConsole = LogArea.getInstance();
|
||||
scrlConsole.setPreferredSize(new Dimension(765, iLogHeight - 15));
|
||||
toolbar.setLocation(x, y);
|
||||
|
||||
panel.add(toolbar);
|
||||
panel.add(gamePanel);
|
||||
gamePanel.add(VerboseLoader.get());
|
||||
panel.add(scrlConsole);
|
||||
|
||||
this.add(panel, BorderLayout.CENTER);
|
||||
add(GamePanel.getInstance());
|
||||
GamePanel.getInstance().addLoader();
|
||||
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
BotDialog.getInstance(this);
|
||||
|
||||
LogArea.log("parabot " + Configuration.BOT_VERSION + " started");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -113,6 +105,25 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
NetworkUI UI = new NetworkUI();
|
||||
UI.frame.setVisible(true);
|
||||
break;
|
||||
case "Run":
|
||||
if(pauseScript) {
|
||||
pauseScript = false;
|
||||
pause.setEnabled(true);
|
||||
run.setEnabled(false);
|
||||
setScriptState(Script.STATE_RUNNING);
|
||||
break;
|
||||
}
|
||||
new ScriptSelector().setVisible(true);
|
||||
break;
|
||||
case "Pause":
|
||||
setScriptState(Script.STATE_PAUSE);
|
||||
pause.setEnabled(false);
|
||||
run.setEnabled(true);
|
||||
pauseScript = true;
|
||||
break;
|
||||
case "Stop":
|
||||
setScriptState(Script.STATE_STOPPED);
|
||||
break;
|
||||
default:
|
||||
System.out.println("Invalid command: " + command);
|
||||
}
|
||||
@@ -130,42 +141,58 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
Point gameLocation = GamePanel.getInstance().getLocationOnScreen();
|
||||
dialog.setLocation(gameLocation.x, gameLocation.y);
|
||||
}
|
||||
|
||||
public void toggleRun() {
|
||||
runScript = !runScript;
|
||||
if(runScript) {
|
||||
scriptRunning();
|
||||
} else {
|
||||
scriptStopped();
|
||||
}
|
||||
}
|
||||
|
||||
private void scriptRunning() {
|
||||
run.setEnabled(false);
|
||||
pause.setEnabled(true);
|
||||
stop.setEnabled(true);
|
||||
}
|
||||
|
||||
private void scriptStopped() {
|
||||
run.setEnabled(true);
|
||||
pause.setEnabled(false);
|
||||
stop.setEnabled(false);
|
||||
}
|
||||
|
||||
private void setScriptState(int state) {
|
||||
Context.getInstance().getRunningScript().setState(state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentShown(ComponentEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentHidden(ComponentEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowActivated(WindowEvent arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowClosed(WindowEvent arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
System.out.println("close");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowDeactivated(WindowEvent arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@@ -173,18 +200,14 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
public void windowDeiconified(WindowEvent arg0) {
|
||||
BotDialog.getInstance().setVisible(false);
|
||||
BotDialog.getInstance().setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowIconified(WindowEvent arg0) {
|
||||
//BotDialog.getInstance().setVisible(false);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowOpened(WindowEvent arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,210 +0,0 @@
|
||||
package org.parabot.core.ui.components;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Font;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionAdapter;
|
||||
import javax.swing.Box;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JToolBar;
|
||||
import javax.swing.SwingConstants;
|
||||
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.ui.ScriptSelector;
|
||||
import org.parabot.core.ui.ServerSelector;
|
||||
import org.parabot.core.ui.images.Images;
|
||||
import org.parabot.environment.scripts.Script;
|
||||
|
||||
/**
|
||||
* Bot toolbar
|
||||
*
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class BotToolbar extends JToolBar {
|
||||
private static final long serialVersionUID = 5373484845104212180L;
|
||||
private static BotToolbar instance;
|
||||
|
||||
private JButton tab;
|
||||
private final JButton run;
|
||||
private final JButton stop;
|
||||
private boolean runScript;
|
||||
private boolean pauseScript;
|
||||
|
||||
public BotToolbar() {
|
||||
this.run = new JButton();
|
||||
this.stop = new JButton();
|
||||
setFloatable(false);
|
||||
tab = new JButton();
|
||||
tab.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ServerSelector.getInstance().setVisible(true);
|
||||
}
|
||||
|
||||
});
|
||||
run.setFocusable(false);
|
||||
stop.setFocusable(false);
|
||||
tab.setFocusable(false);
|
||||
try {
|
||||
run.setIcon(new ImageIcon(Images.getResource("/org/parabot/core/ui/images/run.png")));
|
||||
stop.setIcon(new ImageIcon(Images.getResource("/org/parabot/core/ui/images/stop.png")));
|
||||
tab.setIcon(new ImageIcon(Images.getResource("/org/parabot/core/ui/images/add.png")));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
run.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
runButtonClicked();
|
||||
}
|
||||
|
||||
});
|
||||
stop.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
stopButtonClicked();
|
||||
}
|
||||
|
||||
});
|
||||
//add(tab);
|
||||
add(Box.createHorizontalGlue());
|
||||
add(run);
|
||||
add(stop);
|
||||
}
|
||||
|
||||
protected void stopButtonClicked() {
|
||||
if(!runScript){
|
||||
// obviously do nothing ;d
|
||||
return;
|
||||
}
|
||||
setScriptState(Script.STATE_STOPPED);
|
||||
}
|
||||
|
||||
protected void runButtonClicked() {
|
||||
if(runScript && pauseScript) {
|
||||
// unpause
|
||||
this.pauseScript = false;
|
||||
scriptRunning();
|
||||
setScriptState(Script.STATE_RUNNING);
|
||||
return;
|
||||
} else if(runScript) {
|
||||
// pause
|
||||
this.pauseScript = true;
|
||||
scriptStopped();
|
||||
setScriptState(Script.STATE_PAUSE);
|
||||
} else {
|
||||
new ScriptSelector().setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void setScriptState(int state) {
|
||||
Context.getInstance().getRunningScript().setState(state);
|
||||
}
|
||||
|
||||
|
||||
public void toggleRun() {
|
||||
runScript = !runScript;
|
||||
if(runScript) {
|
||||
scriptRunning();
|
||||
} else {
|
||||
scriptStopped();
|
||||
}
|
||||
}
|
||||
|
||||
private void scriptRunning() {
|
||||
// sets pause icon
|
||||
run.setIcon(new ImageIcon(Images.getResource("/org/parabot/core/ui/images/pause.png")));
|
||||
}
|
||||
|
||||
private void scriptStopped() {
|
||||
// sets start icon
|
||||
run.setIcon(new ImageIcon(Images.getResource("/org/parabot/core/ui/images/run.png")));
|
||||
}
|
||||
|
||||
public static BotToolbar getInstance() {
|
||||
return instance == null ? instance = new BotToolbar() : instance;
|
||||
}
|
||||
|
||||
public void addTab(final Context context, final String name) {
|
||||
TabButton b = new TabButton(name);
|
||||
b.setActive(true);
|
||||
add(b, 0);
|
||||
repaint();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tab button
|
||||
* @author Clisprail
|
||||
*
|
||||
*/
|
||||
private final class TabButton extends JButton {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public TabButton(final String name) {
|
||||
super(name);
|
||||
setFocusable(false);
|
||||
setHorizontalTextPosition(SwingConstants.LEFT);
|
||||
setIcon(getBlackClose());
|
||||
final Component c = this;
|
||||
addMouseMotionListener(new MouseMotionAdapter() {
|
||||
@Override
|
||||
public void mouseMoved(final MouseEvent e) {
|
||||
if (e.getX() > getWidth() - getIcon().getIconWidth()
|
||||
&& e.getX() < getWidth() - getIconTextGap()) {
|
||||
setIcon(getRedClose());
|
||||
} else {
|
||||
setIcon(getBlackClose());
|
||||
}
|
||||
}
|
||||
});
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(final MouseEvent e) {
|
||||
@SuppressWarnings("unused")
|
||||
final int n = getComponentIndex(c);
|
||||
if (e.getX() > getWidth() - getIcon().getIconWidth()
|
||||
&& e.getX() < getWidth() - getIconTextGap()) {
|
||||
// close
|
||||
} else {
|
||||
// enable
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(final MouseEvent e) {
|
||||
setIcon(getBlackClose());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private final ImageIcon getBlackClose() {
|
||||
try {
|
||||
return new ImageIcon(Images.getResource("/org/parabot/core/ui/images/close.png"));
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException("Unable to load icon image: " + t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private final ImageIcon getRedClose() {
|
||||
try {
|
||||
return new ImageIcon(Images.getResource("/org/parabot/core/ui/images/close_red.png"));
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException("Unable to load icon image: " + t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void setActive(final boolean active) {
|
||||
setFont(getFont().deriveFont(active ? Font.BOLD : Font.PLAIN));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package org.parabot.core.ui.components;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
|
||||
import javax.swing.GroupLayout;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
@@ -34,22 +35,18 @@ public class GamePanel extends JPanel {
|
||||
GroupLayout.Alignment.LEADING).addGap(0, 418, Short.MAX_VALUE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates context of this panel and adds a different Applet to the panel
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public void setContext(final Context c) {
|
||||
add(c.getApplet());
|
||||
add(c.getApplet(), BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets instance of this panel
|
||||
*
|
||||
* @return instance of this panel
|
||||
*/
|
||||
public static GamePanel getInstance() {
|
||||
@@ -60,7 +57,7 @@ public class GamePanel extends JPanel {
|
||||
* Adds the loader applet
|
||||
*/
|
||||
public void addLoader() {
|
||||
add(loader);
|
||||
add(loader, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,21 +1,4 @@
|
||||
package org.parabot.core.ui.components;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.LogManager;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
|
||||
import org.parabot.core.Core;
|
||||
import org.parabot.core.logging.LogFormatter;
|
||||
import org.parabot.core.logging.LogTextArea;
|
||||
import org.parabot.core.logging.SystemConsoleHandler;
|
||||
import org.parabot.core.logging.TextAreaLogHandler;
|
||||
|
||||
/**
|
||||
*
|
||||
* The LogArea of the BotUI
|
||||
@@ -23,66 +6,17 @@ import org.parabot.core.logging.TextAreaLogHandler;
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class LogArea extends JScrollPane {
|
||||
private static final long serialVersionUID = 6571141103751675714L;
|
||||
private static LogTextArea logArea = new LogTextArea();
|
||||
private static LogArea instance;
|
||||
|
||||
private LogArea() {
|
||||
super(TextAreaLogHandler.TEXT_AREA,
|
||||
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
|
||||
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
||||
setVisible(true);
|
||||
registerLogging();
|
||||
}
|
||||
|
||||
public static LogArea getInstance() {
|
||||
return instance == null ? instance = new LogArea() : instance;
|
||||
}
|
||||
|
||||
private static final Logger log = Logger.getLogger("Bot");
|
||||
@Deprecated
|
||||
public class LogArea {
|
||||
|
||||
@Deprecated
|
||||
public static void log(String s) {
|
||||
log.info(s);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void error(String s) {
|
||||
log.severe(s);
|
||||
}
|
||||
|
||||
public static void clearLog() {
|
||||
logArea.clearSelection();
|
||||
}
|
||||
|
||||
public static void hideLog() {
|
||||
logArea.setVisible(false);
|
||||
}
|
||||
|
||||
public static void showLog() {
|
||||
logArea.setVisible(true);
|
||||
}
|
||||
|
||||
public void registerLogging() {
|
||||
Core.verbose("Registering logging...");
|
||||
final Properties logging = new Properties();
|
||||
final String logFormatter = LogFormatter.class.getCanonicalName();
|
||||
final String fileHandler = FileHandler.class.getCanonicalName();
|
||||
logging.setProperty("handlers",
|
||||
TextAreaLogHandler.class.getCanonicalName() + "," + fileHandler);
|
||||
logging.setProperty(".level", "INFO");
|
||||
logging.setProperty(SystemConsoleHandler.class.getCanonicalName()
|
||||
+ ".formatter", logFormatter);
|
||||
logging.setProperty(fileHandler + ".formatter", logFormatter);
|
||||
logging.setProperty(TextAreaLogHandler.class.getCanonicalName()
|
||||
+ ".formatter", logFormatter);
|
||||
final ByteArrayOutputStream logout = new ByteArrayOutputStream();
|
||||
try {
|
||||
logging.store(logout, "");
|
||||
LogManager.getLogManager().readConfiguration(
|
||||
new ByteArrayInputStream(logout.toByteArray()));
|
||||
} catch (final Exception ignored) {
|
||||
}
|
||||
Core.verbose("Done.");
|
||||
System.err.println();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,9 @@ import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import org.parabot.core.paint.PaintDebugger;
|
||||
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.environment.api.interfaces.Paintable;
|
||||
import org.parabot.environment.api.utils.Time;
|
||||
|
||||
/**
|
||||
@@ -24,7 +26,7 @@ public class PaintComponent extends JComponent implements Runnable {
|
||||
private BufferedImage buffer;
|
||||
private Graphics2D g2;
|
||||
private Dimension dimensions;
|
||||
private PaintDebugger paintDebugger;
|
||||
private Context context;
|
||||
|
||||
private PaintComponent(Dimension dimensions) {
|
||||
this.dimensions = dimensions;
|
||||
@@ -45,8 +47,8 @@ public class PaintComponent extends JComponent implements Runnable {
|
||||
return getInstance(null);
|
||||
}
|
||||
|
||||
public void startPainting(PaintDebugger paintDebugger) {
|
||||
this.paintDebugger = paintDebugger;
|
||||
public void startPainting(Context context) {
|
||||
this.context = context;
|
||||
new Thread(this).start();
|
||||
}
|
||||
|
||||
@@ -56,8 +58,11 @@ public class PaintComponent extends JComponent implements Runnable {
|
||||
g2.fillRect(0, 0, dimensions.width, dimensions.height);
|
||||
g2.setComposite(AlphaComposite.SrcOver);
|
||||
|
||||
if(paintDebugger != null) {
|
||||
paintDebugger.debug(g2);
|
||||
if(context != null) {
|
||||
for(Paintable p : context.getPaintables()) {
|
||||
p.paint(g);
|
||||
}
|
||||
context.getPaintDebugger().debug(g2);
|
||||
}
|
||||
g.drawImage(buffer, 0, 0, null);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.parabot.core.ui.components;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
@@ -30,6 +31,7 @@ public class VerboseLoader extends JPanel implements ProgressListener {
|
||||
this.bot_image = Images.getResource("/org/parabot/core/ui/images/para.png");
|
||||
this.p = new ProgressBar(400, 20);
|
||||
setSize(775, 510);
|
||||
setPreferredSize(new Dimension(775, 510));
|
||||
setBackground(Color.black);
|
||||
setDoubleBuffered(true);
|
||||
setOpaque(false);
|
||||
|
||||
@@ -2,8 +2,7 @@ package org.parabot.environment.scripts;
|
||||
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.Core;
|
||||
import org.parabot.core.ui.components.BotToolbar;
|
||||
import org.parabot.core.ui.components.LogArea;
|
||||
import org.parabot.core.ui.BotUI;
|
||||
import org.parabot.environment.api.utils.Time;
|
||||
import org.parabot.environment.scripts.framework.AbstractFramework;
|
||||
import org.parabot.environment.scripts.framework.LoopTask;
|
||||
@@ -78,7 +77,7 @@ public class Script implements Runnable {
|
||||
|
||||
Core.verbose("Detecting script framework...");
|
||||
context.setRunningScript(this);
|
||||
BotToolbar.getInstance().toggleRun();
|
||||
BotUI.getInstance().toggleRun();
|
||||
if(this instanceof LoopTask) {
|
||||
Core.verbose("Script framework detected: LoopTask");
|
||||
frameWorkType = TYPE_LOOP;
|
||||
@@ -92,7 +91,7 @@ public class Script implements Runnable {
|
||||
frameWorkType = TYPE_OTHER;
|
||||
}
|
||||
Core.verbose("Running script...");
|
||||
LogArea.log("Script started.");
|
||||
System.out.println("Script started.");
|
||||
try {
|
||||
while(this.state != STATE_STOPPED) {
|
||||
if(context.getRandomHandler().checkAndRun()) {
|
||||
@@ -112,11 +111,11 @@ public class Script implements Runnable {
|
||||
}
|
||||
Core.verbose("Script stopped/finished, unloading and stopping...");
|
||||
onFinish();
|
||||
LogArea.log("Script stopped.");
|
||||
System.out.println("Script stopped.");
|
||||
context.getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
context.setRunningScript(null);
|
||||
BotToolbar.getInstance().toggleRun();
|
||||
BotUI.getInstance().toggleRun();
|
||||
Core.verbose("Done.");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.parabot.environment.servers;
|
||||
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.ui.components.BotToolbar;
|
||||
import org.parabot.core.ui.components.PaintComponent;
|
||||
|
||||
/**
|
||||
@@ -21,9 +20,8 @@ public abstract class ServerExecuter {
|
||||
public void run() {
|
||||
try {
|
||||
Context context = Context.getInstance(provider);
|
||||
BotToolbar.getInstance().addTab(context, serverName);
|
||||
context.load();
|
||||
PaintComponent.getInstance().startPainting(context.getPaintDebugger());
|
||||
PaintComponent.getInstance().startPainting(context);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user