Skip to content

Commit 6ad0daf

Browse files
authored
Merge pull request #44 from UCSDOalads/addCommandMode
Add command mode
2 parents 001a581 + 23ade5e commit 6ad0daf

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/ui/KeyHandler.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package ui;
2+
3+
import java.awt.event.KeyEvent;
4+
5+
import paintcomponents.TextPaintComponent;
6+
7+
public class KeyHandler {
8+
9+
private PaintPanel paintPanel;
10+
private String pendingCommand;
11+
private boolean inCommandMode;
12+
private TextPaintComponent component;
13+
14+
public KeyHandler(PaintPanel paintPanel) {
15+
pendingCommand = "";
16+
this.paintPanel = paintPanel;
17+
this.component = new TextPaintComponent("", 0, 0);
18+
this.paintPanel.addPaintComponent(this.component);
19+
inCommandMode = false;
20+
}
21+
22+
public void keyPressed(KeyEvent e) {
23+
24+
// backspace
25+
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && inCommandMode) {
26+
if (pendingCommand.length() != 0) {
27+
pendingCommand = pendingCommand.substring(0,
28+
pendingCommand.length() - 1);
29+
} else {
30+
inCommandMode = false;
31+
}
32+
33+
} else if (e.getKeyCode() == KeyEvent.VK_ENTER && inCommandMode) {
34+
35+
executeCommand(pendingCommand);
36+
pendingCommand = "";
37+
inCommandMode = false;
38+
} else {
39+
40+
char keyChar = e.getKeyChar();
41+
42+
if (keyChar == ':') {
43+
pendingCommand = "";
44+
inCommandMode = true;
45+
} else if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED
46+
&& inCommandMode)/* if(e.isActionKey()) */{
47+
pendingCommand += e.getKeyChar();
48+
}
49+
50+
}
51+
52+
update();
53+
54+
}
55+
56+
private void executeCommand(String pendingCommand2) {
57+
58+
}
59+
60+
private void update() {
61+
if (inCommandMode) {
62+
component.setDisplayingText(": " + pendingCommand);
63+
64+
} else {
65+
component.setDisplayingText(pendingCommand);
66+
}
67+
int height = paintPanel.getHeight();
68+
int rowHeight = component.getRowHeight();
69+
component.setX(0);
70+
component.setY(height - rowHeight);
71+
paintPanel.repaint();
72+
}
73+
74+
}

src/ui/PaintPanel.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum State {
3232
private PaintComponent tempComponent;
3333

3434
private SelectTool selectTool;
35+
private KeyHandler keyHandler;
3536

3637
/**
3738
* @return the tempComponent
@@ -79,7 +80,9 @@ private void resetTool() {
7980

8081
public PaintPanel() {
8182
requestFocusInWindow();
83+
8284
this.components = new ArrayList<>();
85+
this.keyHandler = new KeyHandler(this);
8386
this.addMouseListener(new MouseListener() {
8487

8588
@Override
@@ -121,6 +124,8 @@ public void mouseDragged(MouseEvent e) {
121124

122125
this.addKeyListener(new KeyListener() {
123126

127+
128+
124129
@Override
125130
public void keyTyped(KeyEvent e) {
126131

@@ -132,8 +137,10 @@ public void keyReleased(KeyEvent e) {
132137
case KeyEvent.VK_ESCAPE:
133138
resetTool();
134139
break;
140+
135141

136142
default:
143+
keyHandler.keyPressed(e);
137144
break;
138145
}
139146
}

0 commit comments

Comments
 (0)