/* * Created by JFormDesigner on Thu Mar 12 17:40:36 CET 2009 */ package com.kbot2.UI; import com.kbot2.accessors.GameCanvas; import com.kbot2.bot.Bot; import com.kbot2.bot.BotCallback; import com.kbot2.handlers.ScriptHandler; import com.kbot2.handlers.SettingsHandler; import com.kbot2.handlers.settings.BotConstants; import com.kbot2.handlers.settings.ScriptLoader; import com.kbot2.handlers.settings.ScriptSetting; import com.kbot2.handlers.settings.Setting; import com.kbot2.loader.Loader; import com.kbot2.out.Console; import com.kbot2.scriptable.Script; import com.kbot2.scriptable.LoadedScript; import org.jdesktop.layout.GroupLayout; import org.jdesktop.layout.LayoutStyle; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.plaf.metal.MetalIconFactory; import javax.imageio.ImageIO; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.File; import java.util.LinkedList; import java.util.List; import java.util.Iterator; import java.util.ArrayList; /** * @author Jan Ove */ public class BotUI extends JFrame implements Thread.UncaughtExceptionHandler { public static final List botList = new LinkedList(); private static final JMenuItem[] recentScripts = new JMenuItem[5]; private BotSpectateUI botSpectate; public BotUI() { JPopupMenu.setDefaultLightWeightPopupEnabled(false); initComponents(); createTray(); // remove the switch tab listener from the tabbedpane InputMap im = tabbedPane.getInputMap(JComponent.WHEN_FOCUSED); im.clear(); /*while (im != null) { KeyStroke stroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0); im.remove(stroke1); stroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0); im.remove(stroke1); stroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0); im.remove(stroke1); stroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); im.remove(stroke1); im = im.getParent(); }*/ this.setVisible(true); Console.setTextArea(consoleTextArea); Console.writeLine(this, "KBot2 by Kosaki started."); ScriptLoader.loadScripts(); Thread.setDefaultUncaughtExceptionHandler(this); editorPane1.setText("Loading MOTD..."); try { editorPane1.setPage("http://kbot.info/bot/MOTD.php"); editorPane1.updateUI(); } catch (IOException e) { editorPane1.setText("Could not load MOTD."); } updateRecentRanScripts(); fillDebugMenu(); BotConstants.loadWorlds(); } private void createTray() { try{ //if (!System.getProperty("os.name").toLowerCase().contains("mac")) { TrayIcon trayIcon = new TrayIcon(getTrayImage(), "KBot", createAWTPopupMenu()); trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openTrayActionPerformed(); } }); SystemTray.getSystemTray().add(trayIcon); //} }catch(Exception e){ Console.writeLine(this, "Could not create the tray icon."); toTrayMenuItem.setEnabled(false); } } private PopupMenu createAWTPopupMenu(){ PopupMenu menu = new PopupMenu(); MenuItem open = new MenuItem("Open"); open.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openTrayActionPerformed(); } }); menu.add(open); MenuItem hide = new MenuItem("Hide"); hide.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { hideTrayActionPerformed(); } }); menu.add(hide); MenuItem exit = new MenuItem("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu.add(exit); return menu; } public void openTrayActionPerformed(){ this.setVisible(true); } public void hideTrayActionPerformed(){ this.setVisible(false); } private Image getTrayImage() { Icon defaultIcon = MetalIconFactory.getTreeComputerIcon(); Image img = new BufferedImage(defaultIcon.getIconWidth(), defaultIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0); return img; } public void fillDebugMenu() { for (Setting setting : SettingsHandler.getSetting("loadedScripts", new String[]{"script"})) { final ScriptSetting script = new ScriptSetting(setting); for (String tag : script.getTags()) { if (tag == null) continue; if (tag.equalsIgnoreCase("debug")) { final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(script.getName()); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Bot selectedBot = getSelectedBot(); if (selectedBot != null) { try { bot.getClient().getMyPlayer().getPosX(); } catch (Exception e) { return; } Script scriptObj = selectedBot.getScriptHandler().loadScript(script.getFileName()); selectedBot.getScriptHandler().runScript(scriptObj); } else { JOptionPane.showMessageDialog(null, "Please select a bot first.", "Error", JOptionPane.ERROR_MESSAGE); menuItem.setSelected(false); } } }); debugMenu.add(menuItem); break; } } } } public static void updateRecentRanScripts() { scriptMenu.removeAll(); scriptMenu.add(startScriptMenuItem); scriptMenu.add(stopScriptMenuItem); scriptMenu.addSeparator(); scriptMenu.add(accontSelectorMenuItem); scriptMenu.addSeparator(); Setting setting = SettingsHandler.getSetting("recentScripts", null)[0]; if (!setting.children.isEmpty()) { int counter = 0; for (Setting child : setting.children) { String scriptName = ""; String fileName = ""; for (Setting child2 : child.children) { if (child2.name.equals("name")) { scriptName = child2.value; } else if (child2.name.equals("fileName")) { fileName = child2.value; } } final String fileName1 = fileName; recentScripts[counter] = new JMenuItem(scriptName); recentScripts[counter].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Bot selectedBot = getSelectedBot(); if (selectedBot != null) { Script script = selectedBot.getScriptHandler().loadScript(fileName1); selectedBot.getScriptHandler().runScript(script); } else { JOptionPane.showMessageDialog(null, "Please select a bot first.", "Error", JOptionPane.ERROR_MESSAGE); } } }); scriptMenu.add(recentScripts[counter]); } counter++; } scriptMenu.updateUI(); } public void uncaughtException(Thread t, Throwable e) { Console.writeLine(this, "[" + e + "][" + t.getName() + "]"); for (StackTraceElement element : e.getStackTrace()) { Console.writeLine(this, element.toString()); } e.printStackTrace(System.out); } public void newBot() { Bot bot = new Bot("No account chosen"); bot.setPreferredSize(new Dimension(756, 504)); botList.add(bot); Loader loader = bot.getLoader(); tabbedPane.addTab("RuneScape - the massive online adventure game by Jagex Ltd. - Minefield", bot); tabbedPane.setSelectedComponent(bot); tabbedPane.updateUI(); if (JOptionPane.showConfirmDialog(this, "Do you want to select an account for this bot?", "Account selection", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { new SelectAccount(bot); } } public static BotCallback getBotCallback(GameCanvas gameCanvas) { BotCallback out; for (Bot bot : botList) { out = bot.getBotCallback(gameCanvas); if (out != null) return out; } return null; } private void newBotMenuItemActionPerformed() { newBot(); } public static Bot getSelectedBot() { Component selectedComp = tabbedPane.getSelectedComponent(); if (selectedComp instanceof Bot) return (Bot) selectedComp; return null; } public static void setInputEnabled(Bot bot, boolean inputEnabled) { if (bot == getSelectedBot()) { if (bot.getInputEnabled() != inputEnabled) { userInputToggle.doClick(); // fake a click so the model also changes. } } else { bot.setInputEnabled(inputEnabled); } } private void userInputToggleActionPerformed() { boolean enabled = userInputToggle.isSelected(); Bot selectedBot = getSelectedBot(); selectedBot.setInputEnabled(enabled); if(!selectedBot.hasFocus()) selectedBot.requestFocus(); if (enabled) { userInputToggle.setText("User input[ON]"); } else { userInputToggle.setText("User input[OFF]"); } } private void tabbedPaneStateChanged() { final Bot selectedBot = getSelectedBot(); if (selectedBot != null) { screenshotButton.setEnabled(true); userInputToggle.setEnabled(true); botSpecificSettingsButton.setEnabled(true); pauseToggleButton.setEnabled(true); setInputEnabled(selectedBot, selectedBot.isInputEnabled()); final boolean paused = selectedBot.getScriptHandler().pausedAll; pauseToggleButton.setSelected(paused); if(paused){ pauseToggleButton.setText("Resume"); } else{ pauseToggleButton.setText("Pause"); } if(!selectedBot.hasFocus()) selectedBot.requestFocus(); } else { screenshotButton.setEnabled(false); pauseToggleButton.setEnabled(false); userInputToggle.setEnabled(false); botSpecificSettingsButton.setEnabled(false); } } private void startScriptMenuItemActionPerformed() { Bot selected = getSelectedBot(); if (selected == null) { JOptionPane.showMessageDialog(this, "Please select a bot first.", "Error", JOptionPane.ERROR_MESSAGE); return; } new ScriptSelector(selected.getScriptHandler()); } private void stopScriptMenuItemActionPerformed() { Bot selected = getSelectedBot(); if (selected == null) { JOptionPane.showMessageDialog(this, "Please select a bot first.", "Error", JOptionPane.ERROR_MESSAGE); return; } ScriptHandler scriptHandler = selected.getScriptHandler(); if (scriptHandler.runningScripts.size() == 1) { scriptHandler.stopAllScripts(); } else if (!scriptHandler.runningScripts.isEmpty()) { new StopScript(scriptHandler); } else { JOptionPane.showMessageDialog(this, "There is no running scripts.", "Error", JOptionPane.ERROR_MESSAGE); } } private void closeBotMenuItemActionPerformed() { Bot selected = getSelectedBot(); if (selected == null) { JOptionPane.showMessageDialog(this, "Please select a bot first.", "Error", JOptionPane.ERROR_MESSAGE); return; } if (JOptionPane.showConfirmDialog(this, "Are you sure you want to close this bot?", "Confirm", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { selected.close(); tabbedPane.remove(selected); System.gc(); } } private void botConfigMenuItemActionPerformed() { new BotConfigUI(); } private void accountManagerMenuItemActionPerformed() { new AccountManagerUI(); } private void accontSelectorMenuItemActionPerformed() { Bot bot = getSelectedBot(); if (bot != null) { new SelectAccount(bot); } } public static void renameBot(Bot bot, String newName) { if (bot == null) return; int index = tabbedPane.indexOfComponent(bot); Console.writeLine(null, "HaXD Title. ToshiXZ R Pr0"); if (index != -1) { tabbedPane.setTitleAt(index, "RuneScape - the massive online adventure game by Jagex Ltd. - Minefield"); } tabbedPane.updateUI(); } private void botSpecificSettingsButtonActionPerformed() { Bot bot = getSelectedBot(); if (bot == null) { JOptionPane.showMessageDialog(this, "Please select a bot first", "Error", JOptionPane.ERROR_MESSAGE); return; } new BotSpecificSettingUI(bot).setVisible(true); } private void reloadScriptsButtonActionPerformed() { ScriptLoader.loadScripts(); } private void botSpecStartMenuItemActionPerformed() { if(botSpectate == null){ botSpectate = new BotSpectateUI(); } botSpectate.start(); if(tabbedPane.indexOfComponent(botSpectate) == -1){ tabbedPane.insertTab("Spectate", null, botSpectate, "Bot Spectator", 1); //tabbedPane.addTab("Spectate", botSpectate); } tabbedPane.setSelectedComponent(botSpectate); tabbedPane.updateUI(); } private void stopSpectateMenuItemActionPerformed() { if(botSpectate == null) return; botSpectate.stop(); tabbedPane.remove(botSpectate); } private void randomConfigMenuItemActionPerformed() { Bot selected = getSelectedBot(); if (selected == null) { JOptionPane.showMessageDialog(this, "Please select a bot first.", "Error", JOptionPane.ERROR_MESSAGE); return; } new RandomSelector(selected.botEnv).setVisible(true); } private void pauseToggleButtonActionPerformed() { boolean enabled = pauseToggleButton.isSelected(); Bot selectedBot = getSelectedBot(); if(!selectedBot.hasFocus()) selectedBot.requestFocus(); if (enabled) { pauseToggleButton.setText("Resume"); selectedBot.getScriptHandler().pauseAllScripts(); } else { pauseToggleButton.setText("Pause"); selectedBot.getScriptHandler().resumeAllScripts(); } } private void screenshotButtonActionPerformed() { Bot bot = getSelectedBot(); if(bot == null){ return; } new ScreenshotForm(bot).setVisible(true); } private void stopAllItemsMenuItemActionPerformed() { Bot bot = getSelectedBot(); if(bot == null){ JOptionPane.showMessageDialog(this, "Please select a bot first.", "Error", JOptionPane.ERROR_MESSAGE); return; } List