From 78f8b46e7dd9fa82f5fe48b189204d1ce642441a Mon Sep 17 00:00:00 2001 From: Casey Date: Sun, 19 Jun 2022 07:07:51 -0400 Subject: [PATCH] readd_quotes() for TGUI Text Input --- code/_helpers/text.dm | 17 ++- code/modules/mob/typing_indicator.dm | 36 ++++- code/modules/tgui/tgui_input_list.dm | 217 +++++++++++++++++++++++++++ code/modules/tgui_input/text.dm | 1 + 4 files changed, 260 insertions(+), 11 deletions(-) create mode 100644 code/modules/tgui/tgui_input_list.dm diff --git a/code/_helpers/text.dm b/code/_helpers/text.dm index 6bf02bf77f..333f2edc15 100644 --- a/code/_helpers/text.dm +++ b/code/_helpers/text.dm @@ -481,20 +481,23 @@ /// Used to get a properly sanitized input, of max_length /// no_trim is self explanatory but it prevents the input from being trimed if you intend to parse newlines or whitespace. /proc/stripped_input(mob/user, message = "", title = "", default = "", max_length=MAX_MESSAGE_LEN, no_trim=FALSE) - var/name = input(user, message, title, default) as text|null - + var/user_input = input(user, message, title, default) as text|null + if(isnull(user_input)) + return if(no_trim) - return copytext(html_encode(name), 1, max_length) + return copytext(html_encode(user_input), 1, max_length) else - return trim(html_encode(name), max_length) //trim is "outside" because html_encode can expand single symbols into multiple symbols (such as turning < into <) + return trim(html_encode(user_input), max_length) //trim is "outside" because html_encode can expand single symbols into multiple symbols (such as turning < into <) // Used to get a properly sanitized multiline input, of max_length /proc/stripped_multiline_input(mob/user, message = "", title = "", default = "", max_length=MAX_MESSAGE_LEN, no_trim=FALSE) - var/name = input(user, message, title, default) as message|null + var/user_input = input(user, message, title, default) as message|null + if(isnull(user_input)) + return if(no_trim) - return copytext(html_encode(name), 1, max_length) + return copytext(html_encode(user_input), 1, max_length) else - return trim(html_encode(name), max_length) + return trim(html_encode(user_input), max_length) //Adds 'char' ahead of 'text' until there are 'count' characters total /proc/add_leading(text, count, char = " ") diff --git a/code/modules/mob/typing_indicator.dm b/code/modules/mob/typing_indicator.dm index 08547e3146..a5efb5a7c6 100644 --- a/code/modules/mob/typing_indicator.dm +++ b/code/modules/mob/typing_indicator.dm @@ -40,8 +40,15 @@ set hidden = 1 set_typing_indicator(TRUE) +<<<<<<< HEAD + var/message + if(usr.client.prefs.tgui_input_mode) + message = tgui_input_text(usr, "Type your message:", "Say") + else + message = input(usr, "Type your message:", "Say") as text +======= var/message = tgui_input_text(usr, "Type your message:", "Say") - message = readd_quotes(message) +>>>>>>> 170cc46a9a... Merge pull request #13120 from ItsSelis/tgui-input-framework-hotfix set_typing_indicator(FALSE) if(message) @@ -52,8 +59,15 @@ set hidden = 1 set_typing_indicator(TRUE) +<<<<<<< HEAD + var/message + if(usr.client.prefs.tgui_input_mode) + message = tgui_input_message(usr, "Type your message:", "Emote") + else + message = input(usr, "Type your message:", "Emote") as message +======= var/message = tgui_input_text(usr, "Type your message:", "Emote", multiline = TRUE) - message = readd_quotes(message) +>>>>>>> 170cc46a9a... Merge pull request #13120 from ItsSelis/tgui-input-framework-hotfix set_typing_indicator(FALSE) if(message) @@ -64,8 +78,15 @@ set name = ".Whisper" set hidden = 1 +<<<<<<< HEAD + var/message + if(usr.client.prefs.tgui_input_mode) + message = tgui_input_text(usr, "Type your message:", "Whisper") + else + message = input(usr, "Type your message:", "Whisper") as text +======= var/message = tgui_input_text(usr, "Type your message:", "Whisper") - message = readd_quotes(message) +>>>>>>> 170cc46a9a... Merge pull request #13120 from ItsSelis/tgui-input-framework-hotfix if(message) whisper(message) @@ -74,8 +95,15 @@ set name = ".Subtle" set hidden = 1 +<<<<<<< HEAD + var/message + if(usr.client.prefs.tgui_input_mode) + message = tgui_input_message(usr, "Type your message:", "Subtle") + else + message = input(usr, "Type your message:", "Subtle") as message +======= var/message = tgui_input_text(usr, "Type your message:", "Subtle", multiline = TRUE) - message = readd_quotes(message) +>>>>>>> 170cc46a9a... Merge pull request #13120 from ItsSelis/tgui-input-framework-hotfix if(message) me_verb_subtle(message) \ No newline at end of file diff --git a/code/modules/tgui/tgui_input_list.dm b/code/modules/tgui/tgui_input_list.dm new file mode 100644 index 0000000000..231b527b12 --- /dev/null +++ b/code/modules/tgui/tgui_input_list.dm @@ -0,0 +1,217 @@ +/** + * Creates a TGUI input list window and returns the user's response. + * + * This proc should be used to create alerts that the caller will wait for a response from. + * Arguments: + * * user - The user to show the input box to. + * * message - The content of the input box, shown in the body of the TGUI window. + * * title - The title of the input box, shown on the top of the TGUI window. + * * buttons - The options that can be chosen by the user, each string is assigned a button on the UI. + * * default - The option with this value will be selected on first paint of the TGUI window. + * * timeout - The timeout of the input box, after which the input box will close and qdel itself. Set to zero for no timeout. + * * strict_modern - Disabled the preference check of the input box, only allowing the TGUI window to show. + */ +<<<<<<< HEAD:code/modules/tgui/tgui_input_list.dm +/proc/tgui_input_list(mob/user, message, title, list/buttons, default, timeout = 0) +======= +/proc/tgui_input_list(mob/user, message, title = "Select", list/items, default, timeout = 0, strict_modern = FALSE) +>>>>>>> 170cc46a9a... Merge pull request #13120 from ItsSelis/tgui-input-framework-hotfix:code/modules/tgui_input/list.dm + if (istext(user)) + stack_trace("tgui_alert() received text for user instead of mob") + return + if (!user) + user = usr + if(!length(buttons)) + return + if (!istype(user)) + if (istype(user, /client)) + var/client/client = user + user = client.mob + else + return +<<<<<<< HEAD:code/modules/tgui/tgui_input_list.dm + var/datum/tgui_list_input/input = new(user, message, title, buttons, default, timeout) +======= + /// Client does NOT have tgui_input on: Returns regular input + if(!usr.client.prefs.tgui_input_mode && !strict_modern) + return input(user, message, title, default) as null|anything in items + var/datum/tgui_list_input/input = new(user, message, title, items, default, timeout) +>>>>>>> 170cc46a9a... Merge pull request #13120 from ItsSelis/tgui-input-framework-hotfix:code/modules/tgui_input/list.dm + input.tgui_interact(user) + input.wait() + if (input) + . = input.choice + qdel(input) + +/** + * Creates an asynchronous TGUI input list window with an associated callback. + * + * This proc should be used to create inputs that invoke a callback with the user's chosen option. + * Arguments: + * * user - The user to show the input box to. + * * message - The content of the input box, shown in the body of the TGUI window. + * * title - The title of the input box, shown on the top of the TGUI window. + * * buttons - The options that can be chosen by the user, each string is assigned a button on the UI. + * * default - The option with this value will be selected on first paint of the TGUI window. + * * callback - The callback to be invoked when a choice is made. + * * timeout - The timeout of the input box, after which the menu will close and qdel itself. Set to zero for no timeout. + */ +/proc/tgui_input_list_async(mob/user, message, title, list/buttons, default, datum/callback/callback, timeout = 60 SECONDS) + if (istext(user)) + stack_trace("tgui_alert() received text for user instead of mob") + return + if (!user) + user = usr + if(!length(buttons)) + return + if (!istype(user)) + if (istype(user, /client)) + var/client/client = user + user = client.mob + else + return + var/datum/tgui_list_input/async/input = new(user, message, title, buttons, default, callback, timeout) + input.tgui_interact(user) + +/** + * # tgui_list_input + * + * Datum used for instantiating and using a TGUI-controlled list input that prompts the user with + * a message and shows a list of selectable options + */ +/datum/tgui_list_input + /// The title of the TGUI window + var/title + /// The textual body of the TGUI window + var/message + /// The list of buttons (responses) provided on the TGUI window + var/list/buttons + /// Buttons (strings specifically) mapped to the actual value (e.g. a mob or a verb) + var/list/buttons_map + /// Value of the button that should be pre-selected on first paint. + var/initial + /// The button that the user has pressed, null if no selection has been made + var/choice + /// The time at which the tgui_list_input was created, for displaying timeout progress. + var/start_time + /// The lifespan of the tgui_list_input, after which the window will close and delete itself. + var/timeout + /// Boolean field describing if the tgui_list_input was closed by the user. + var/closed + +/datum/tgui_list_input/New(mob/user, message, title, list/buttons, default, timeout) + src.title = title + src.message = message + src.buttons = list() + src.buttons_map = list() + src.initial = default + var/list/repeat_buttons = list() + + // Gets rid of illegal characters + var/static/regex/whitelistedWords = regex(@{"([^\u0020-\u8000]+)"}) + + for(var/i in buttons) + if(isnull(i)) + stack_trace("Null in a tgui_input_list() buttons") + continue + + var/string_key = whitelistedWords.Replace("[i]", "") + + //avoids duplicated keys E.g: when areas have the same name + string_key = avoid_assoc_duplicate_keys(string_key, repeat_buttons) + + src.buttons += string_key + src.buttons_map[string_key] = i + + + if (timeout) + src.timeout = timeout + start_time = world.time + QDEL_IN(src, timeout) + +/datum/tgui_list_input/Destroy(force, ...) + SStgui.close_uis(src) + QDEL_NULL(buttons) + . = ..() + +/** + * Waits for a user's response to the tgui_list_input's prompt before returning. Returns early if + * the window was closed by the user. + */ +/datum/tgui_list_input/proc/wait() + while (!choice && !closed) + stoplag(1) + +/datum/tgui_list_input/tgui_interact(mob/user, datum/tgui/ui) + ui = SStgui.try_update_ui(user, src, ui) + if(!ui) + ui = new(user, src, "ListInput") + ui.open() + +/datum/tgui_list_input/tgui_close(mob/user) + . = ..() + closed = TRUE + +/datum/tgui_list_input/tgui_state(mob/user) + return GLOB.tgui_always_state + +/datum/tgui_list_input/tgui_static_data(mob/user) + . = list( + "title" = title, + "message" = message, + "buttons" = buttons, + "initial" = initial + ) + +/datum/tgui_list_input/tgui_data(mob/user) + . = list() + if(timeout) + .["timeout"] = clamp((timeout - (world.time - start_time) - 1 SECONDS) / (timeout - 1 SECONDS), 0, 1) + +/datum/tgui_list_input/tgui_act(action, list/params) + . = ..() + if (.) + return + switch(action) + if("choose") + if (!(params["choice"] in buttons)) + return + set_choice(buttons_map[params["choice"]]) + SStgui.close_uis(src) + return TRUE + if("cancel") + SStgui.close_uis(src) + closed = TRUE + return TRUE + +/datum/tgui_list_input/proc/set_choice(choice) + src.choice = choice + +/** + * # async tgui_list_input + * + * An asynchronous version of tgui_list_input to be used with callbacks instead of waiting on user responses. + */ +/datum/tgui_list_input/async + /// The callback to be invoked by the tgui_list_input upon having a choice made. + var/datum/callback/callback + +/datum/tgui_list_input/async/New(mob/user, message, title, list/buttons, default, callback, timeout) + ..(user, title, message, buttons, default, timeout) + src.callback = callback + +/datum/tgui_list_input/async/Destroy(force, ...) + QDEL_NULL(callback) + . = ..() + +/datum/tgui_list_input/async/tgui_close(mob/user) + . = ..() + qdel(src) + +/datum/tgui_list_input/async/set_choice(choice) + . = ..() + if(!isnull(src.choice)) + callback?.InvokeAsync(src.choice) + +/datum/tgui_list_input/async/wait() + return diff --git a/code/modules/tgui_input/text.dm b/code/modules/tgui_input/text.dm index 474f8e7c3e..42c50d377a 100644 --- a/code/modules/tgui_input/text.dm +++ b/code/modules/tgui_input/text.dm @@ -150,6 +150,7 @@ /datum/tgui_input_text/proc/set_entry(entry) if(!isnull(entry)) var/converted_entry = encode ? html_encode(entry) : entry + converted_entry = readd_quotes(converted_entry) src.entry = trim(converted_entry, max_length) /**