Files
Bubberstation/code/datums/elements/tool_renaming.dm
_0Steven 5e568288f4 Refactor renaming UNIQUE_RENAME items from the pen to an element (#82491)
## About The Pull Request

So a bit ago someone in code_general wanted to make plushies renamable,
but learnt that just adding the `UNIQUE_RENAME` flag wouldn't work as
pens would murder the plushie and only THEN let you rename it. I noted
refactoring both pens and plushies to use the new
`item_interaction(...)` procs would Just Solve This, but, well, they
didn't really have any coding experience.

But, hey, renaming being hardcoded to the pens has annoyed me ever since
I laid my eyes upon the hot mess that is paperwork code.
So here we are!

### We're making it an element.

There's not really much to this, this is mostly the same code but moved
to an element and with some minor cleanups.

First, we move it all from `/obj/item/pen` to a new element we called
`/datum/element/tool_renaming`. With this, instead of having it proc on
`/obj/item/pen/afterattack(...)`, we register it to proc on the
`COMSIG_ITEM_INTERACTING_WITH_ATOM` signal.

6e36ed9840/code/__DEFINES/dcs/signals/signals_atom/signals_atom_x_act.dm (L59-L62)
Secondly, we realize the code is just going through each if statement
regardless of whether the previous was correct.

6e36ed9840/code/modules/paperwork/pen.dm (L225-L258)
And, as we're dealing with text, just make it a switch statement
instead.
```dm
switch(pen_choice)
		if("Rename")
			(...)

		if("Description")
			(...)

		if("Reset")
			(...)
```
Then, we replace all single letter variables with descriptive ones,
replace the if-elses with early returns, and make it actually return
item interaction flags.

Finally, we slap this onto the pen, and we're done.
Now we can slap it onto other fitting renaming tools, and it uses the
proper item interaction system.
## Why It's Good For The Game

I feel it's generally better to not hardcode this to just pens, we have
plenty other writing utensils and possible renaming tools.
It's also a bit cleaner than before.
Apart from that, moves it from using `afterattack(...)` to the proper
item interaction chain by using `COMSIG_ITEM_INTERACTING_WITH_ATOM`,
which should reduce janky interactions.
## Changelog
🆑
refactor: Instead of being hardcoded to the pen, renaming items is now
an element. Currently only pens have this, and functionality should be
the same, but please report it if you find any items that were renamable
but now aren't.
/🆑
2024-04-12 03:30:32 +00:00

79 lines
3.0 KiB
Plaintext

#define OPTION_RENAME "Rename"
#define OPTION_DESCRIPTION "Description"
#define OPTION_RESET "Reset"
/**
* Renaming tool element
*
* When using this tool on an object with UNIQUE_RENAME,
* lets the user rename/redesc it.
*/
/datum/element/tool_renaming
/datum/element/tool_renaming/Attach(datum/target)
. = ..()
if(!isitem(target))
return ELEMENT_INCOMPATIBLE
RegisterSignal(target, COMSIG_ITEM_INTERACTING_WITH_ATOM, PROC_REF(attempt_rename))
/datum/element/tool_renaming/Detach(datum/source)
. = ..()
UnregisterSignal(source, COMSIG_ITEM_INTERACTING_WITH_ATOM)
/datum/element/tool_renaming/proc/attempt_rename(datum/source, mob/living/user, atom/interacting_with, list/modifiers)
SIGNAL_HANDLER
if(!isobj(interacting_with))
return NONE
var/obj/renamed_obj = interacting_with
if(!(renamed_obj.obj_flags & UNIQUE_RENAME))
return NONE
INVOKE_ASYNC(src, PROC_REF(async_rename), user, renamed_obj)
return ITEM_INTERACT_SUCCESS
/datum/element/tool_renaming/proc/async_rename(mob/living/user, obj/renamed_obj)
var/custom_choice = tgui_input_list(user, "What would you like to edit?", "Customization", list(OPTION_RENAME, OPTION_DESCRIPTION, OPTION_RESET))
if(QDELETED(renamed_obj) || !user.can_perform_action(renamed_obj) || isnull(custom_choice))
return
switch(custom_choice)
if(OPTION_RENAME)
var/old_name = renamed_obj.name
var/input = tgui_input_text(user, "What do you want to name [renamed_obj]?", "Object Name", "[old_name]", MAX_NAME_LEN)
if(QDELETED(renamed_obj) || !user.can_perform_action(renamed_obj))
return
if(input == old_name || !input)
to_chat(user, span_notice("You changed [renamed_obj] to... well... [renamed_obj]."))
return
renamed_obj.AddComponent(/datum/component/rename, input, renamed_obj.desc)
to_chat(user, span_notice("You have successfully renamed \the [old_name] to [renamed_obj]."))
ADD_TRAIT(renamed_obj, TRAIT_WAS_RENAMED, RENAMING_TOOL_LABEL_TRAIT)
renamed_obj.update_appearance(UPDATE_NAME)
if(OPTION_DESCRIPTION)
var/old_desc = renamed_obj.desc
var/input = tgui_input_text(user, "Describe [renamed_obj]", "Description", "[old_desc]", MAX_DESC_LEN)
if(QDELETED(renamed_obj) || !user.can_perform_action(renamed_obj))
return
if(input == old_desc || !input)
to_chat(user, span_notice("You decide against changing [renamed_obj]'s description."))
return
renamed_obj.AddComponent(/datum/component/rename, renamed_obj.name, input)
to_chat(user, span_notice("You have successfully changed [renamed_obj]'s description."))
ADD_TRAIT(renamed_obj, TRAIT_WAS_RENAMED, RENAMING_TOOL_LABEL_TRAIT)
renamed_obj.update_appearance(UPDATE_DESC)
if(OPTION_RESET)
qdel(renamed_obj.GetComponent(/datum/component/rename))
to_chat(user, span_notice("You have successfully reset [renamed_obj]'s name and description."))
REMOVE_TRAIT(renamed_obj, TRAIT_WAS_RENAMED, RENAMING_TOOL_LABEL_TRAIT)
renamed_obj.update_appearance(UPDATE_NAME | UPDATE_DESC)
#undef OPTION_RENAME
#undef OPTION_DESCRIPTION
#undef OPTION_RESET