chatbox input manager: add support for character limit

This commit is contained in:
Adam
2018-05-07 12:55:40 -04:00
parent e74dfcf13d
commit e60d9ec2c1

View File

@@ -39,10 +39,12 @@ import net.runelite.client.callback.ClientThread;
@Slf4j @Slf4j
public class ChatboxInputManager public class ChatboxInputManager
{ {
private static final int NO_LIMIT = Integer.MAX_VALUE;
private final Client client; private final Client client;
private final ClientThread clientThread; private final ClientThread clientThread;
private Consumer<String> done; private Consumer<String> done;
private int characterLimit = NO_LIMIT;
@Inject @Inject
public ChatboxInputManager(Client client, ClientThread clientThread, EventBus eventBus) public ChatboxInputManager(Client client, ClientThread clientThread, EventBus eventBus)
@@ -60,8 +62,14 @@ public class ChatboxInputManager
* @param done Callback when the text box has been exited, called with "" on esc * @param done Callback when the text box has been exited, called with "" on esc
*/ */
public void openInputWindow(String text, String defaul, Consumer<String> done) public void openInputWindow(String text, String defaul, Consumer<String> done)
{
openInputWindow(text, defaul, NO_LIMIT, done);
}
public void openInputWindow(String text, String defaul, int characterLimit, Consumer<String> done)
{ {
this.done = done; this.done = done;
this.characterLimit = characterLimit;
clientThread.invokeLater(() -> client.runScript( clientThread.invokeLater(() -> client.runScript(
ScriptID.RUNELITE_CHATBOX_INPUT_INIT, ScriptID.RUNELITE_CHATBOX_INPUT_INIT,
text, text,
@@ -97,7 +105,7 @@ public class ChatboxInputManager
} }
default: default:
// If we wanted to do numbers only, we could add a limit here // If we wanted to do numbers only, we could add a limit here
if (typedKey >= 32) if (typedKey >= 32 && (str.length() < characterLimit))
{ {
str += Character.toString((char) typedKey); str += Character.toString((char) typedKey);
} }