mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 00:37:55 +00:00
Merge pull request #116 from Parabot/feature/keybindings
[FEATURE] Finished key bindings
This commit is contained in:
@@ -9,6 +9,7 @@ import org.parabot.core.parsers.hooks.HookParser;
|
||||
import org.parabot.core.ui.BotDialog;
|
||||
import org.parabot.core.ui.BotUI;
|
||||
import org.parabot.core.ui.components.GamePanel;
|
||||
import org.parabot.core.ui.listeners.PBKeyListener;
|
||||
import org.parabot.environment.api.interfaces.Paintable;
|
||||
import org.parabot.environment.input.Keyboard;
|
||||
import org.parabot.environment.input.Mouse;
|
||||
@@ -47,6 +48,7 @@ public class Context {
|
||||
private PaintDebugger paintDebugger;
|
||||
private Mouse mouse;
|
||||
private Keyboard keyboard;
|
||||
private PBKeyListener pbKeyListener;
|
||||
private ServerProviderInfo providerInfo;
|
||||
private JSONParser jsonParser;
|
||||
|
||||
@@ -225,6 +227,10 @@ public class Context {
|
||||
Core.verbose("Initializing keyboard...");
|
||||
serverProvider.initKeyboard();
|
||||
Core.verbose("Done.");
|
||||
|
||||
Core.verbose("Initializing key listener...");
|
||||
this.pbKeyListener = new PBKeyListener();
|
||||
applet.addKeyListener(this.pbKeyListener);
|
||||
|
||||
BotDialog.getInstance().validate();
|
||||
System.setOut(this.defaultOut);
|
||||
@@ -359,4 +365,8 @@ public class Context {
|
||||
public JSONParser getJsonParser() {
|
||||
return jsonParser;
|
||||
}
|
||||
|
||||
public PBKeyListener getPbKeyListener() {
|
||||
return pbKeyListener;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,6 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
|
||||
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");
|
||||
@@ -54,9 +52,6 @@ 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);
|
||||
|
||||
@@ -14,19 +14,10 @@ import java.util.List;
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -35,22 +26,24 @@ public class PBKeyListener implements KeyListener {
|
||||
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) {
|
||||
for (Binding bind : this.bindings){
|
||||
if (bind.getKey() == binding.getKey()){
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.bindings.add(binding);
|
||||
}
|
||||
|
||||
public void resetBindings(){
|
||||
this.bindings = new ArrayList<>();
|
||||
this.fillBindings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
@@ -58,12 +51,7 @@ public class PBKeyListener implements KeyListener {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
if (e.getKeyCode() == mainKey) {
|
||||
if (e.isControlDown()) {
|
||||
for (Binding binding : bindings) {
|
||||
if (binding.getKey() == e.getKeyCode()) {
|
||||
binding.perform();
|
||||
@@ -71,4 +59,9 @@ public class PBKeyListener implements KeyListener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +123,9 @@ public class Script implements Runnable {
|
||||
this.state = STATE_STOPPED;
|
||||
context.setRunningScript(null);
|
||||
|
||||
Core.verbose("Resetting key bindings...");
|
||||
Context.getInstance().getPbKeyListener().resetBindings();
|
||||
|
||||
BotUI.getInstance().toggleRun();
|
||||
Core.verbose("Done.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user