Skip to content

Commit

Permalink
chat but it works
Browse files Browse the repository at this point in the history
  • Loading branch information
Pannoniae committed Aug 4, 2024
1 parent 9ff981e commit 554d737
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/ui/menu/ChatMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,33 @@ public class ChatMenu : Menu {
/// </summary>
public string message = "Test";

public override bool isModal() {
return false;
}

public override bool isBlockingInput() {
return true;
}

public override void onKeyChar(IKeyboard keyboard, char ch) {
if (ch != 't') {
if (ch != 't' &&
(char.IsLetterOrDigit(ch) || char.IsPunctuation(ch) || char.IsWhiteSpace(ch)) &&
!char.IsControl(ch)) {
message += ch;
}
}

public override void onKeyDown(IKeyboard keyboard, Key key, int scancode) {
if (key == Key.Enter) {
message = "";
Game.instance.lockMouse();
Screen.GAME_SCREEN.switchToMenu(Screen.GAME_SCREEN.INGAME_MENU);
} else {
if (key == Key.Backspace && message.Length > 0) {
message = message[..^1];
}
if (key is Key.T or Key.Enter) {
// wait a frame so the key doesn't immediately get pressed again
Game.instance.executeOnMainThread(() => {
message = "";
Game.instance.lockMouse();
Screen.GAME_SCREEN.switchToMenu(Screen.GAME_SCREEN.INGAME_MENU);
});
}
else if (key == Key.Backspace && message.Length > 0) {
message = message[..^1];
}
}

Expand All @@ -37,8 +48,4 @@ public override void draw() {
gui.drawUI(gui.colourTexture, RectangleF.FromLTRB(4, gui.uiHeight - 16, gui.uiWidth - 4, gui.uiHeight - 4), color: new Color4b(0, 0, 0, 128));
gui.drawStringUIThin("> " + message, new Vector2(6, Game.gui.uiHeight - 13));
}

public override bool isModal() {
return false;
}
}
8 changes: 8 additions & 0 deletions src/ui/menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public virtual bool isModal() {
return true;
}

/// <summary>
/// Does this menu block input from the screen?
/// </summary>
/// <returns></returns>
public virtual bool isBlockingInput() {
return isModal();
}

public Menu() {
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/screen/GameScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public override void onKeyDown(IKeyboard keyboard, Key key, int scancode) {
}

// if there is a menu open, don't allow any other keypresses from this handler
if (currentMenu.isModal() && currentMenu != INGAME_MENU) {
if (currentMenu.isBlockingInput() && currentMenu != INGAME_MENU) {
return;
}

Expand Down

0 comments on commit 554d737

Please sign in to comment.