From c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 12 Feb 2026 22:33:32 -0800 Subject: Initial commit --- NET/worlds/console/FocusPreservingTextField.java | 89 ++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 NET/worlds/console/FocusPreservingTextField.java (limited to 'NET/worlds/console/FocusPreservingTextField.java') diff --git a/NET/worlds/console/FocusPreservingTextField.java b/NET/worlds/console/FocusPreservingTextField.java new file mode 100644 index 0000000..57b656b --- /dev/null +++ b/NET/worlds/console/FocusPreservingTextField.java @@ -0,0 +1,89 @@ +package NET.worlds.console; + +import NET.worlds.scape.EventQueue; +import NET.worlds.scape.Pilot; +import java.awt.Event; +import java.awt.Font; +import java.awt.TextField; + +class FocusPreservingTextField extends TextField { + private static final long serialVersionUID = 7475622191515920214L; + private static FocusPreservingTextField lostFocus; + private static FocusPreservingTextField hasFocus; + private static FocusPreservingTextField chatLine; + private static Object hasFocusMutex = new Object(); + private static Font font = new Font(Console.message("GammaTextFont"), 0, 12); + boolean preserveFocus; + boolean takeNextFocus; + + public FocusPreservingTextField() { + super(30); + this.setFocusable(true); + this.setFont(font); + } + + public void isChatLine() { + chatLine = this; + } + + @Override + public void requestFocus() { + if (!this.takeNextFocus) { + synchronized (hasFocusMutex) { + this.preserveFocus = hasFocus != null && hasFocus.getText().length() != 0 || chatLine != null && chatLine.getText().length() != 0; + } + } else { + this.takeNextFocus = false; + } + + if (!this.preserveFocus) { + super.requestFocus(); + } + } + + public void takeNextFocus() { + this.preserveFocus = false; + this.takeNextFocus = true; + } + + @Override + public boolean handleEvent(Event e) { + synchronized (hasFocusMutex) { + if (e.id == 1005) { + if (hasFocus == this) { + lostFocus = this; + hasFocus = null; + this.preserveFocus = false; + } + } else if (e.id == 1004) { + hasFocus = this; + if (this.preserveFocus && lostFocus != null) { + lostFocus.takeNextFocus(); + lostFocus.requestFocus(); + } + + lostFocus = null; + this.preserveFocus = false; + } + } + + if (e.id == 401) { + Console.wake(); + if (e.key == 27) { + this.setText(""); + return true; + } + + if ((e.modifiers & 2) != 0 && e.key >= 1 && e.key <= 26 && e.key != 22) { + Pilot pilot = Pilot.getActive(); + if (pilot != null) { + pilot.animate("abcdefghijklmnopqrstuvwxyz".substring(e.key - 1, e.key)); + } + + return true; + } + } + + return EventQueue.redirectDrivingKeys(e) ? true : super.handleEvent(e); + } +} -- cgit v1.2.3