Files
Bubberstation/code/modules/modular_computers/computers/item/laptop.dm
SyncIt21 b6369a47b4 Mouse drag & drop refactored attack chain (#83690)
## About The Pull Request
Mouse drag & drop has been refactored into its own attack chain. The
flowchart below summarizes it

![Flowchart](https://github.com/tgstation/tgstation/assets/110812394/d92047ff-d94c-44a6-9e87-354c3d525021)

Brief summary of each proc is as follows

**1. `atom/MouseDrop()`**
- It is now non overridable. No subtype should ever touch this proc
because it performs 2 basic checks
  
a) Measures the time between mouse down & mouse release. If its less
than `LENIENCY_TIME`(0.1 seconds) then the operation is not considered a
drag but a simple click

b) Measures the distance squared between the drag start & end point. If
its less than `LENIENCY_DISTANCE`(16 pixels screen space) then the drag
is considered too small and is discarded

- These 2 sanity checks for drag & drop are applied across all
operations without fail
  
**2. `atom/base_mouse_drop_handler()`**
- This is where atoms handle mouse drag & drop inside the world. Ideally
it is non overridable in most cases because it also performs 2 checks
- Is the dragged object & the drop target adjacent to the player?.
Screen elements always return true for this case
  
- Additional checks can be enforced by `can_perform_action()` done only
on the dragged object. It uses the combined flags of
`interaction_flags_mouse_drop` for both the dragged object & drop target
to determine if the operation is feasible.
     
We do this only on the dragged object because if both the dragged object
& drop target are adjacent to the player then `can_perform_action()`
will return the same results when done on either object so it makes no
difference.

Checks can be bypassed via the `IGNORE_MOUSE_DROP_CHECKS` which is used
by huds & screen elements or in case you want to implement your own
unique checks

**3. `atom/mouse_drop_dragged()`**
- Called on the object that is being dragged, drop target passed here as
well, subtypes do their stuff here
- `COMSIG_MOUSEDROP_ONTO` is sent afterwards. It does not require
subtypes to call their parent proc

**4. `atom/mouse_drop_receive()`**
- Called on the drop target that is receiving the dragged object,
subtypes do their stuff here
- `COMSIG_MOUSEDROPPED_ONTO` is sent afterwards. It does not require
subtypes to call their parent proc

## Why It's Good For The Game
Implements basic sanity checks across all drag & drop operations. Allows
us to reduce code like this


8c8311e624/code/game/machinery/dna_scanner.dm (L144-L145)

Into this

```
if(!iscarbon(target))
	return
```

I'm tired of seeing this code pattern `!Adjacent(user) ||
!user.Adjacent(target)` copy pasted all over the place. Let's just write
that at the atom level & be done with it

## Changelog
🆑
refactor: Mouse drag & drop attack chain has been refactored. Report any
bugs on GitHub
fix: You cannot close the cryo tube on yourself with Alt click like
before
/🆑

---------

Co-authored-by: MrMelbert <51863163+MrMelbert@users.noreply.github.com>
Co-authored-by: Bloop <13398309+vinylspiders@users.noreply.github.com>
2024-06-13 13:28:41 -07:00

122 lines
3.1 KiB
Plaintext

/obj/item/modular_computer/laptop
name = "laptop"
desc = "A portable laptop computer."
icon = 'icons/obj/devices/modular_laptop.dmi'
icon_state = "laptop-closed"
icon_state_powered = "laptop"
icon_state_unpowered = "laptop-off"
icon_state_menu = "menu"
hardware_flag = PROGRAM_LAPTOP
max_idle_programs = 3
w_class = WEIGHT_CLASS_NORMAL
interaction_flags_mouse_drop = NEED_HANDS
// No running around with open laptops in hands.
item_flags = SLOWS_WHILE_IN_HAND
drag_slowdown = 0
screen_on = FALSE // Starts closed
var/start_open = TRUE // unless this var is set to 1
var/icon_state_closed = "laptop-closed"
var/w_class_open = WEIGHT_CLASS_BULKY
var/slowdown_open = 1
/obj/item/modular_computer/laptop/examine(mob/user)
. = ..()
if(screen_on)
. += span_notice("Alt-click to close it.")
/obj/item/modular_computer/laptop/Initialize(mapload)
. = ..()
if(start_open && !screen_on)
toggle_open()
/obj/item/modular_computer/laptop/update_icon_state()
if(!screen_on)
icon_state = icon_state_closed
return
return ..()
/obj/item/modular_computer/laptop/update_overlays()
if(!screen_on)
cut_overlays()
return
return ..()
/obj/item/modular_computer/laptop/attack_self(mob/user)
if(!screen_on)
try_toggle_open(user)
else
return ..()
/obj/item/modular_computer/laptop/verb/open_computer()
set name = "Toggle Open"
set category = "Object"
set src in view(1)
try_toggle_open(usr)
/obj/item/modular_computer/laptop/mouse_drop_dragged(atom/over_object, mob/user, src_location, over_location, params)
if(over_object == user || over_object == src)
try_toggle_open(user)
return
if(istype(over_object, /atom/movable/screen/inventory/hand))
var/atom/movable/screen/inventory/hand/H = over_object
if(!isturf(loc))
return
user.put_in_hand(src, H.held_index)
/obj/item/modular_computer/laptop/attack_hand(mob/user, list/modifiers)
. = ..()
if(.)
return
if(screen_on && isturf(loc))
return attack_self(user)
/obj/item/modular_computer/laptop/proc/try_toggle_open(mob/living/user)
if(issilicon(user))
return
if(!isturf(loc) && !ismob(loc)) // No opening it in backpack.
return
if(!user.can_perform_action(src))
return
toggle_open(user)
/obj/item/modular_computer/laptop/click_alt(mob/user)
if(!screen_on)
return CLICK_ACTION_BLOCKING
try_toggle_open(user) // Close it.
return CLICK_ACTION_SUCCESS
/obj/item/modular_computer/laptop/proc/toggle_open(mob/living/user=null)
if(screen_on)
to_chat(user, span_notice("You close \the [src]."))
slowdown = initial(slowdown)
update_weight_class(initial(w_class))
drag_slowdown = initial(drag_slowdown)
else
to_chat(user, span_notice("You open \the [src]."))
slowdown = slowdown_open
update_weight_class(w_class_open)
drag_slowdown = slowdown_open
if(isliving(loc))
var/mob/living/localmob = loc
localmob.update_equipment_speed_mods()
localmob.update_pull_movespeed()
screen_on = !screen_on
update_appearance()
/obj/item/modular_computer/laptop/get_messenger_ending()
return "Sent from my UNIX Laptop"
// Laptop frame, starts empty and closed.
/obj/item/modular_computer/laptop/buildable
start_open = FALSE