mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-05 08:39:30 +00:00
Merge pull request #84 from Parabot/feature/keybindings
[FEATURE] Added base for keybindings
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
package org.parabot.core.ui;
|
package org.parabot.core.ui;
|
||||||
|
|
||||||
|
import org.parabot.core.Configuration;
|
||||||
import org.parabot.core.Context;
|
import org.parabot.core.Context;
|
||||||
import org.parabot.core.Directories;
|
import org.parabot.core.Directories;
|
||||||
import org.parabot.core.ui.components.GamePanel;
|
import org.parabot.core.ui.components.GamePanel;
|
||||||
import org.parabot.core.ui.components.VerboseLoader;
|
import org.parabot.core.ui.components.VerboseLoader;
|
||||||
import org.parabot.core.ui.images.Images;
|
import org.parabot.core.ui.images.Images;
|
||||||
|
import org.parabot.core.ui.listeners.PBKeyListener;
|
||||||
import org.parabot.core.ui.utils.SwingUtil;
|
import org.parabot.core.ui.utils.SwingUtil;
|
||||||
import org.parabot.environment.OperatingSystem;
|
import org.parabot.environment.OperatingSystem;
|
||||||
import org.parabot.environment.api.utils.StringUtils;
|
import org.parabot.environment.api.utils.StringUtils;
|
||||||
@@ -23,7 +25,7 @@ import java.util.ArrayList;
|
|||||||
/**
|
/**
|
||||||
* The bot user interface
|
* The bot user interface
|
||||||
*
|
*
|
||||||
* @author Dane, Everel, Paradox
|
* @author Dane, Everel, JKetelaar
|
||||||
*/
|
*/
|
||||||
public class BotUI extends JFrame implements ActionListener, ComponentListener, WindowListener {
|
public class BotUI extends JFrame implements ActionListener, ComponentListener, WindowListener {
|
||||||
|
|
||||||
@@ -31,18 +33,19 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
|||||||
private static BotUI instance;
|
private static BotUI instance;
|
||||||
private static JDialog dialog;
|
private static JDialog dialog;
|
||||||
|
|
||||||
private JMenuItem run, pause, stop, cacheClear;
|
private JMenuItem run, pause, stop;
|
||||||
private boolean runScript, pauseScript;
|
private boolean runScript, pauseScript;
|
||||||
|
|
||||||
|
private PBKeyListener keyListener;
|
||||||
|
|
||||||
public BotUI(String username, String password) {
|
public BotUI(String username, String password) {
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
throw new IllegalStateException("BotUI already created");
|
throw new IllegalStateException("BotUI already created");
|
||||||
}
|
}
|
||||||
instance = this;
|
instance = this;
|
||||||
//WebLookAndFeel.install();
|
|
||||||
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
|
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
|
||||||
|
|
||||||
setTitle("Parabot");
|
setTitle(Configuration.BOT_TITLE);
|
||||||
setResizable(false);
|
setResizable(false);
|
||||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
createMenu();
|
createMenu();
|
||||||
@@ -51,6 +54,9 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
|||||||
addComponentListener(this);
|
addComponentListener(this);
|
||||||
addWindowListener(this);
|
addWindowListener(this);
|
||||||
|
|
||||||
|
this.keyListener = new PBKeyListener();
|
||||||
|
addKeyListener(keyListener);
|
||||||
|
|
||||||
add(GamePanel.getInstance());
|
add(GamePanel.getInstance());
|
||||||
GamePanel.getInstance().add(VerboseLoader.get(username, password), BorderLayout.CENTER);
|
GamePanel.getInstance().add(VerboseLoader.get(username, password), BorderLayout.CENTER);
|
||||||
add(Logger.getInstance(), BorderLayout.SOUTH);
|
add(Logger.getInstance(), BorderLayout.SOUTH);
|
||||||
@@ -102,7 +108,7 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
|||||||
stop.setEnabled(false);
|
stop.setEnabled(false);
|
||||||
stop.setIcon(new ImageIcon(Images.getResource("/storage/images/stop.png")));
|
stop.setIcon(new ImageIcon(Images.getResource("/storage/images/stop.png")));
|
||||||
|
|
||||||
cacheClear = new JMenuItem("Clear cache");
|
JMenuItem cacheClear = new JMenuItem("Clear cache");
|
||||||
cacheClear.setIcon(new ImageIcon(Images.getResource("/storage/images/trash.png")));
|
cacheClear.setIcon(new ImageIcon(Images.getResource("/storage/images/trash.png")));
|
||||||
|
|
||||||
screenshot.addActionListener(this);
|
screenshot.addActionListener(this);
|
||||||
@@ -142,8 +148,10 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
String command = e.getActionCommand();
|
this.performCommand(e.getActionCommand());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void performCommand(String command){
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "Create screenshot":
|
case "Create screenshot":
|
||||||
try {
|
try {
|
||||||
@@ -153,11 +161,14 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
|||||||
String randString = StringUtils.randomString(10);
|
String randString = StringUtils.randomString(10);
|
||||||
boolean search = true;
|
boolean search = true;
|
||||||
boolean duplicate = false;
|
boolean duplicate = false;
|
||||||
while (search == true) {
|
while (search) {
|
||||||
for (File f : Directories.getScreenshotDir().listFiles()) {
|
File[] files;
|
||||||
if (f.getAbsoluteFile().getName().contains(randString)) {
|
if ((files = Directories.getScreenshotDir().listFiles()) != null) {
|
||||||
duplicate = true;
|
for (File f : files) {
|
||||||
break;
|
if (f.getAbsoluteFile().getName().contains(randString)) {
|
||||||
|
duplicate = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!duplicate) {
|
if (!duplicate) {
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package org.parabot.core.ui.listeners;
|
||||||
|
|
||||||
|
import org.parabot.core.ui.listeners.key.ActionEventBinding;
|
||||||
|
import org.parabot.core.ui.listeners.key.Binding;
|
||||||
|
import org.parabot.environment.OperatingSystem;
|
||||||
|
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.KeyListener;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author JKetelaar
|
||||||
|
*/
|
||||||
|
public class PBKeyListener implements KeyListener {
|
||||||
|
|
||||||
|
private int mainKey;
|
||||||
|
|
||||||
|
private List<Binding> bindings;
|
||||||
|
|
||||||
|
public PBKeyListener() {
|
||||||
|
this.bindings = new ArrayList<>();
|
||||||
|
this.mainKey = (OperatingSystem.getOS() == OperatingSystem.MAC ? KeyEvent.VK_META : KeyEvent.VK_CONTROL);
|
||||||
|
this.fillBindings();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PBKeyListener(int mainKey) {
|
||||||
|
this.bindings = new ArrayList<>();
|
||||||
|
this.mainKey = mainKey;
|
||||||
|
this.fillBindings();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillBindings() {
|
||||||
|
this.bindings.add(new ActionEventBinding(KeyEvent.VK_R, "Run"));
|
||||||
|
this.bindings.add(new ActionEventBinding(KeyEvent.VK_R, "Stop"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMainKey() {
|
||||||
|
return mainKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMainKey(int mainKey) {
|
||||||
|
this.mainKey = mainKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Binding> getBindings() {
|
||||||
|
return bindings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addBinding(Binding binding) {
|
||||||
|
this.bindings.add(binding);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyTyped(KeyEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
if (e.getKeyCode() == mainKey) {
|
||||||
|
for (Binding binding : bindings) {
|
||||||
|
if (binding.getKey() == e.getKeyCode()) {
|
||||||
|
binding.perform();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package org.parabot.core.ui.listeners.key;
|
||||||
|
|
||||||
|
import org.parabot.core.ui.BotUI;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author JKetelaar
|
||||||
|
*/
|
||||||
|
public class ActionEventBinding extends Binding {
|
||||||
|
|
||||||
|
private String actionString;
|
||||||
|
|
||||||
|
public ActionEventBinding(int key, String actionString) {
|
||||||
|
super(key);
|
||||||
|
this.actionString = actionString;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void perform() {
|
||||||
|
BotUI.getInstance().performCommand(actionString);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package org.parabot.core.ui.listeners.key;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author JKetelaar
|
||||||
|
*/
|
||||||
|
public abstract class Binding {
|
||||||
|
|
||||||
|
private int key;
|
||||||
|
|
||||||
|
public Binding(int key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(int key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void perform();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user