mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 00:37:55 +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;
|
||||
|
||||
import org.parabot.core.Configuration;
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.Directories;
|
||||
import org.parabot.core.ui.components.GamePanel;
|
||||
import org.parabot.core.ui.components.VerboseLoader;
|
||||
import org.parabot.core.ui.images.Images;
|
||||
import org.parabot.core.ui.listeners.PBKeyListener;
|
||||
import org.parabot.core.ui.utils.SwingUtil;
|
||||
import org.parabot.environment.OperatingSystem;
|
||||
import org.parabot.environment.api.utils.StringUtils;
|
||||
@@ -23,7 +25,7 @@ import java.util.ArrayList;
|
||||
/**
|
||||
* The bot user interface
|
||||
*
|
||||
* @author Dane, Everel, Paradox
|
||||
* @author Dane, Everel, JKetelaar
|
||||
*/
|
||||
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 JDialog dialog;
|
||||
|
||||
private JMenuItem run, pause, stop, cacheClear;
|
||||
private JMenuItem run, pause, stop;
|
||||
private boolean runScript, pauseScript;
|
||||
|
||||
private PBKeyListener keyListener;
|
||||
|
||||
public BotUI(String username, String password) {
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("BotUI already created");
|
||||
}
|
||||
instance = this;
|
||||
//WebLookAndFeel.install();
|
||||
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
|
||||
|
||||
setTitle("Parabot");
|
||||
setTitle(Configuration.BOT_TITLE);
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
createMenu();
|
||||
@@ -51,6 +54,9 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
addComponentListener(this);
|
||||
addWindowListener(this);
|
||||
|
||||
this.keyListener = new PBKeyListener();
|
||||
addKeyListener(keyListener);
|
||||
|
||||
add(GamePanel.getInstance());
|
||||
GamePanel.getInstance().add(VerboseLoader.get(username, password), BorderLayout.CENTER);
|
||||
add(Logger.getInstance(), BorderLayout.SOUTH);
|
||||
@@ -102,7 +108,7 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
stop.setEnabled(false);
|
||||
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")));
|
||||
|
||||
screenshot.addActionListener(this);
|
||||
@@ -142,8 +148,10 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String command = e.getActionCommand();
|
||||
this.performCommand(e.getActionCommand());
|
||||
}
|
||||
|
||||
public void performCommand(String command){
|
||||
switch (command) {
|
||||
case "Create screenshot":
|
||||
try {
|
||||
@@ -153,11 +161,14 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
String randString = StringUtils.randomString(10);
|
||||
boolean search = true;
|
||||
boolean duplicate = false;
|
||||
while (search == true) {
|
||||
for (File f : Directories.getScreenshotDir().listFiles()) {
|
||||
if (f.getAbsoluteFile().getName().contains(randString)) {
|
||||
duplicate = true;
|
||||
break;
|
||||
while (search) {
|
||||
File[] files;
|
||||
if ((files = Directories.getScreenshotDir().listFiles()) != null) {
|
||||
for (File f : files) {
|
||||
if (f.getAbsoluteFile().getName().contains(randString)) {
|
||||
duplicate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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