Create UtilityFunction class, enable displaying of no keybind, allow escaping from keybind menu
parent
15ceff8c92
commit
04e8cb3453
|
@ -304,13 +304,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
|
|
||||||
//if a key is pressed, we'll send it over to the Player class for processing
|
//if a key is pressed, we'll send it over to the Player class for processing
|
||||||
public void keyPressed(KeyEvent e){
|
public void keyPressed(KeyEvent e){
|
||||||
for (Middleware m: middlewareArray) {
|
e = UtilityFunction.intercept(e, middlewareArray);
|
||||||
if (m.canIntercept(e)) {
|
|
||||||
e = m.interceptKey(e);
|
|
||||||
System.out.println("intercept");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||||||
isPaused = !isPaused;
|
isPaused = !isPaused;
|
||||||
} else {
|
} else {
|
||||||
|
@ -320,12 +314,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
|
|
||||||
//if a key is released, we'll send it over to the Player class for processing
|
//if a key is released, we'll send it over to the Player class for processing
|
||||||
public void keyReleased(KeyEvent e){
|
public void keyReleased(KeyEvent e){
|
||||||
for (Middleware m: middlewareArray) {
|
e = UtilityFunction.intercept(e, middlewareArray);
|
||||||
if (m.canIntercept(e)) {
|
|
||||||
e = m.interceptKey(e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
player.keyReleased(e);
|
player.keyReleased(e);
|
||||||
if(e.getKeyChar() == 'p'){
|
if(e.getKeyChar() == 'p'){
|
||||||
LevelManager.nextLevel();
|
LevelManager.nextLevel();
|
||||||
|
|
|
@ -21,7 +21,6 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
|
||||||
//dimensions of window
|
//dimensions of window
|
||||||
public static final int GAME_WIDTH = 1225;
|
public static final int GAME_WIDTH = 1225;
|
||||||
public static final int GAME_HEIGHT = 630;
|
public static final int GAME_HEIGHT = 630;
|
||||||
public final static int TOTAL_BOXES = 2;
|
|
||||||
|
|
||||||
public CameraPanel gameFrame;
|
public CameraPanel gameFrame;
|
||||||
|
|
||||||
|
@ -44,7 +43,7 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
|
||||||
this.gameFrame = gameFrame;
|
this.gameFrame = gameFrame;
|
||||||
camera = gameFrame.camera;
|
camera = gameFrame.camera;
|
||||||
|
|
||||||
title = new TextBox(100, 400, 100, GAME_WIDTH, standardFont, "Detroit", null);
|
title = new TextBox(100, 400, 100, GAME_WIDTH, standardFont, "Placeholder", null);
|
||||||
enter = new TextBox(300, 600, 100, GAME_WIDTH, standardFont, "Start Game", "game");
|
enter = new TextBox(300, 600, 100, GAME_WIDTH, standardFont, "Start Game", "game");
|
||||||
settings = new TextBox(400, 600, 100, GAME_WIDTH, standardFont, "Settings", "settings");
|
settings = new TextBox(400, 600, 100, GAME_WIDTH, standardFont, "Settings", "settings");
|
||||||
textBoxArray.add(enter);
|
textBoxArray.add(enter);
|
||||||
|
@ -146,18 +145,13 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
|
||||||
|
|
||||||
//if a key is pressed, we'll send it over to the Player class for processing
|
//if a key is pressed, we'll send it over to the Player class for processing
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
|
e = UtilityFunction.intercept(e, GamePanel.middlewareArray);
|
||||||
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||||
// logic for different screens starts here
|
// logic for different screens starts here
|
||||||
CardLayout cardLayout = (CardLayout) gameFrame.getLayout();
|
CardLayout cardLayout = (CardLayout) gameFrame.getLayout();
|
||||||
cardLayout.show(gameFrame, textBoxArray.get(currentBox).id);
|
cardLayout.show(gameFrame, textBoxArray.get(currentBox).id);
|
||||||
} else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
|
} else {
|
||||||
// if currentBox > 0, subtract one
|
currentBox = UtilityFunction.processBox(e, currentBox, textBoxArray);
|
||||||
// else, set to TOTAL_BOXES-1
|
|
||||||
currentBox = currentBox > 0 ? currentBox - 1:textBoxArray.size() - 1;
|
|
||||||
} else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
|
|
||||||
// if currentBox > total box amount - 1, set to 0
|
|
||||||
// else, set to TOTAL_BOXES-1
|
|
||||||
currentBox = currentBox < textBoxArray.size() - 1 ? currentBox + 1:0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.security.Key;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class SettingPanel extends JPanel implements Runnable, KeyListener{
|
public class SettingPanel extends JPanel implements Runnable, KeyListener{
|
||||||
|
@ -112,13 +113,19 @@ public class SettingPanel extends JPanel implements Runnable, KeyListener{
|
||||||
for (TextBox t: textBoxArray) {
|
for (TextBox t: textBoxArray) {
|
||||||
oldText = t.text;
|
oldText = t.text;
|
||||||
middlewareIndex = GamePanel.middlewareArray.indexOf(new Middleware(Integer.parseInt(t.id), -1));
|
middlewareIndex = GamePanel.middlewareArray.indexOf(new Middleware(Integer.parseInt(t.id), -1));
|
||||||
t.text += "(" + (middlewareIndex > -1 ? (char)GamePanel.middlewareArray.get(middlewareIndex).newCode:(char)Integer.parseInt(t.id)) + ")";
|
t.text += "(" + (middlewareIndex > -1 ? (char)GamePanel.middlewareArray.get(middlewareIndex).newCode:
|
||||||
|
GamePanel.middlewareArray.contains(new Middleware(-2, Integer.parseInt(t.id))) ?
|
||||||
|
"None":(char)Integer.parseInt(t.id)) + ")";
|
||||||
t.draw(g, null, Color.cyan);
|
t.draw(g, null, Color.cyan);
|
||||||
t.text = oldText;
|
t.text = oldText;
|
||||||
}
|
}
|
||||||
oldText = textBoxArray.get(currentBox).text;
|
oldText = textBoxArray.get(currentBox).text;
|
||||||
|
// TODO: clean up
|
||||||
middlewareIndex = GamePanel.middlewareArray.indexOf(new Middleware(Integer.parseInt(textBoxArray.get(currentBox).id), -1));
|
middlewareIndex = GamePanel.middlewareArray.indexOf(new Middleware(Integer.parseInt(textBoxArray.get(currentBox).id), -1));
|
||||||
textBoxArray.get(currentBox).text += "(" + (middlewareIndex > -1 ? (char)GamePanel.middlewareArray.get(middlewareIndex).newCode:(char)Integer.parseInt(textBoxArray.get(currentBox).id)) + ")";
|
// -2 was chosen as oldCode instead of -1 to prevent conflicts
|
||||||
|
textBoxArray.get(currentBox).text += "(" + (middlewareIndex > -1 ? (char)GamePanel.middlewareArray.get(middlewareIndex).newCode:
|
||||||
|
GamePanel.middlewareArray.contains(new Middleware(-2, Integer.parseInt(textBoxArray.get(currentBox).id))) ?
|
||||||
|
"None":(char)Integer.parseInt(textBoxArray.get(currentBox).id)) + ")";
|
||||||
textBoxArray.get(currentBox).draw(g, Color.gray, Color.blue);
|
textBoxArray.get(currentBox).draw(g, Color.gray, Color.blue);
|
||||||
textBoxArray.get(currentBox).text = oldText;
|
textBoxArray.get(currentBox).text = oldText;
|
||||||
if (waitForKey) {
|
if (waitForKey) {
|
||||||
|
@ -169,7 +176,9 @@ public class SettingPanel extends JPanel implements Runnable, KeyListener{
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeKeyBind() {
|
public void changeKeyBind() {
|
||||||
if (lastKeyCode != -1) {
|
if (lastKeyCode == KeyEvent.VK_ESCAPE) {
|
||||||
|
lastKeyCode = -1;
|
||||||
|
} else if (lastKeyCode != -1) {
|
||||||
boolean canRemove = true;
|
boolean canRemove = true;
|
||||||
while (canRemove) {
|
while (canRemove) {
|
||||||
// newCode is -1 as it does not matter
|
// newCode is -1 as it does not matter
|
||||||
|
@ -186,22 +195,21 @@ public class SettingPanel extends JPanel implements Runnable, KeyListener{
|
||||||
|
|
||||||
//if a key is pressed, we'll send it over to the Player class for processing
|
//if a key is pressed, we'll send it over to the Player class for processing
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
|
if (!waitForKey) {
|
||||||
|
e = UtilityFunction.intercept(e, GamePanel.middlewareArray);
|
||||||
|
}
|
||||||
if (waitForKey) {
|
if (waitForKey) {
|
||||||
|
if (e.getKeyCode() != KeyEvent.VK_ENTER) {
|
||||||
lastKeyCode = e.getKeyCode();
|
lastKeyCode = e.getKeyCode();
|
||||||
waitForKey = false;
|
waitForKey = false;
|
||||||
|
}
|
||||||
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||||||
((CardLayout)gameFrame.getLayout()).show(gameFrame, "menu");
|
((CardLayout)gameFrame.getLayout()).show(gameFrame, "menu");
|
||||||
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||||
// logic for changing keys starts here
|
// logic for changing keys starts here
|
||||||
waitForKey = true;
|
waitForKey = true;
|
||||||
} else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
|
} else {
|
||||||
// if currentBox > 0, subtract one
|
currentBox = UtilityFunction.processBox(e, currentBox, textBoxArray);
|
||||||
// else, set to TOTAL_BOXES-1
|
|
||||||
currentBox = currentBox > 0 ? currentBox - 1:textBoxArray.size() - 1;
|
|
||||||
} else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
|
|
||||||
// if currentBox > total box amount - 1, set to 0
|
|
||||||
// else, set to TOTAL_BOXES-1
|
|
||||||
currentBox = currentBox < textBoxArray.size() - 1 ? currentBox + 1:0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public final class UtilityFunction {
|
||||||
|
private UtilityFunction(){}
|
||||||
|
|
||||||
|
public static KeyEvent intercept(KeyEvent e, ArrayList<Middleware> middlewareArray) {
|
||||||
|
for (Middleware m: middlewareArray) {
|
||||||
|
if (m.canIntercept(e)) {
|
||||||
|
e = m.interceptKey(e);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int processBox(KeyEvent e, int currentBox, ArrayList<TextBox> textBoxArray) {
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
|
||||||
|
// if currentBox > 0, subtract one
|
||||||
|
// else, set to TOTAL_BOXES-1
|
||||||
|
return currentBox > 0 ? currentBox - 1:textBoxArray.size() - 1;
|
||||||
|
} else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
|
||||||
|
// if currentBox > total box amount - 1, set to 0
|
||||||
|
// else, set to TOTAL_BOXES-1
|
||||||
|
return currentBox < textBoxArray.size() - 1 ? currentBox + 1:0;
|
||||||
|
}
|
||||||
|
return currentBox;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue