Skip to content

Commit

Permalink
camera console now uses camera manager (#5310)
Browse files Browse the repository at this point in the history
# About the pull request

Continuing the work of cmss13-devs/cmss13#5309 -
camera code now uses camera manager component.

<!-- Remove this text and explain what the purpose of your PR is.

Mention if you have tested your changes. If you changed a map, make sure
you used the mapmerge tool.
If this is an Issue Correction, you can type "Fixes Issue #169420" to
link the PR to the corresponding Issue number #169420.

Remember: something that is self-evident to you might not be to others.
Explain your rationale fully, even if you feel it goes without saying.
-->

# Explain why it's good for the game

Reduction of duplicated code.
# Testing Photographs and Procedure
<details>
<summary>Screenshots & Videos</summary>

Put screenshots and videos here with an empty line between the
screenshots and the `<details>` tags.

</details>


# Changelog
:cl:
refactor: camera consoles now use camera manager component
/:cl:

---------

Co-authored-by: Drathek <[email protected]>
  • Loading branch information
2 people authored and Doubleumc committed Jul 25, 2024
1 parent 78860b5 commit 214dce9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 86 deletions.
1 change: 1 addition & 0 deletions code/__DEFINES/dcs/signals/atom/signals_item.dm
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@
#define COMSIG_CAMERA_SET_TARGET "camera_manager_set_target"
#define COMSIG_CAMERA_SET_AREA "camera_manager_set_area"
#define COMSIG_CAMERA_CLEAR "camera_manager_clear_target"
#define COMSIG_CAMERA_REFRESH "camera_manager_refresh"
9 changes: 9 additions & 0 deletions code/game/camera_manager/camera_manager.dm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
RegisterSignal(parent, COMSIG_CAMERA_SET_AREA, PROC_REF(set_camera_rect))
RegisterSignal(parent, COMSIG_CAMERA_SET_TARGET, PROC_REF(set_camera))
RegisterSignal(parent, COMSIG_CAMERA_CLEAR, PROC_REF(clear_camera))
RegisterSignal(parent, COMSIG_CAMERA_REFRESH, PROC_REF(refresh_camera))

/datum/component/camera_manager/UnregisterFromParent()
. = ..()
Expand All @@ -99,6 +100,7 @@
UnregisterSignal(parent, COMSIG_CAMERA_SET_AREA)
UnregisterSignal(parent, COMSIG_CAMERA_SET_TARGET)
UnregisterSignal(parent, COMSIG_CAMERA_CLEAR)
UnregisterSignal(parent, COMSIG_CAMERA_REFRESH)

/datum/component/camera_manager/proc/clear_camera()
SIGNAL_HANDLER
Expand All @@ -113,6 +115,13 @@
target_height = null
show_camera_static()

/datum/component/camera_manager/proc/refresh_camera()
SIGNAL_HANDLER
if(render_mode == RENDER_MODE_AREA)
update_area_camera()
return
update_target_camera()

/datum/component/camera_manager/proc/set_camera(source, atom/target, w, h)
SIGNAL_HANDLER
render_mode = RENDER_MODE_TARGET
Expand Down
105 changes: 19 additions & 86 deletions code/game/machinery/computer/camera_console.dm
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,35 @@
var/list/concurrent_users = list()

// Stuff needed to render the map
var/map_name
var/atom/movable/screen/map_view/cam_screen
var/atom/movable/screen/background/cam_background
var/camera_map_name

var/colony_camera_mapload = TRUE
var/admin_console = FALSE

/// All the plane masters that need to be applied.
var/list/cam_plane_masters

/obj/structure/machinery/computer/cameras/Initialize(mapload)
. = ..()
// Map name has to start and end with an A-Z character,
// and definitely NOT with a square bracket or even a number.
// I wasted 6 hours on this. :agony:
map_name = "camera_console_[REF(src)]_map"

RegisterSignal(src, COMSIG_CAMERA_MAPNAME_ASSIGNED, PROC_REF(camera_mapname_update))

// camera setup
AddComponent(/datum/component/camera_manager)
SEND_SIGNAL(src, COMSIG_CAMERA_CLEAR)

if(colony_camera_mapload && mapload && is_ground_level(z))
network = list(CAMERA_NET_COLONY)

cam_plane_masters = list()
for(var/plane in subtypesof(/atom/movable/screen/plane_master) - /atom/movable/screen/plane_master/blackness)
var/atom/movable/screen/plane_master/instance = new plane()
instance.assigned_map = map_name
instance.del_on_map_removal = FALSE
if(instance.blend_mode_override)
instance.blend_mode = instance.blend_mode_override
instance.screen_loc = "[map_name]:CENTER"
cam_plane_masters += instance

// Initialize map objects
cam_screen = new
cam_screen.icon = null
cam_screen.name = "screen"
cam_screen.assigned_map = map_name
cam_screen.del_on_map_removal = FALSE
cam_screen.screen_loc = "[map_name]:1,1"
cam_background = new
cam_background.assigned_map = map_name
cam_background.del_on_map_removal = FALSE

/obj/structure/machinery/computer/cameras/Destroy()
SStgui.close_uis(src)
QDEL_NULL(current)
QDEL_NULL(cam_screen)
QDEL_NULL(cam_background)
QDEL_NULL_LIST(cam_plane_masters)
UnregisterSignal(src, COMSIG_CAMERA_MAPNAME_ASSIGNED)
last_camera_turf = null
concurrent_users = null
return ..()

/obj/structure/machinery/computer/cameras/proc/camera_mapname_update(source, value)
camera_map_name = value

/obj/structure/machinery/computer/cameras/attack_remote(mob/user as mob)
return attack_hand(user)

Expand All @@ -90,8 +68,7 @@
// Update UI
ui = SStgui.try_update_ui(user, src, ui)

// Update the camera, showing static if necessary and updating data if the location has moved.
update_active_camera_screen()
SEND_SIGNAL(src, COMSIG_CAMERA_REFRESH)

if(!ui)
var/user_ref = WEAKREF(user)
Expand All @@ -103,11 +80,9 @@
// Turn on the console
if(length(concurrent_users) == 1 && is_living)
update_use_power(USE_POWER_ACTIVE)
// Register map objects
user.client.register_map_obj(cam_screen)
user.client.register_map_obj(cam_background)
for(var/plane in cam_plane_masters)
user.client.register_map_obj(plane)

SEND_SIGNAL(src, COMSIG_CAMERA_REGISTER_UI, user)

// Open UI
ui = new(user, src, "CameraConsole", name)
ui.open()
Expand All @@ -125,7 +100,7 @@

/obj/structure/machinery/computer/cameras/ui_static_data()
var/list/data = list()
data["mapRef"] = map_name
data["mapRef"] = camera_map_name
var/list/cameras = get_available_cameras()
data["cameras"] = list()
for(var/i in cameras)
Expand Down Expand Up @@ -159,69 +134,27 @@
if(!selected_camera)
return TRUE

update_active_camera_screen()
SEND_SIGNAL(src, COMSIG_CAMERA_SET_TARGET, selected_camera, selected_camera.view_range, selected_camera.view_range)

return TRUE

/obj/structure/machinery/computer/cameras/proc/update_active_camera_screen()
// Show static if can't use the camera
if(!current?.can_use())
show_camera_static()
return

// Is this camera located in or attached to a living thing, Vehicle or helmet? If so, assume the camera's loc is the living (or non) thing.
var/cam_location = current
if(isliving(current.loc) || isVehicle(current.loc))
cam_location = current.loc
else if(istype(current.loc, /obj/item/clothing/head/helmet/marine))
var/obj/item/clothing/head/helmet/marine/helmet = current.loc
cam_location = helmet.loc

// If we're not forcing an update for some reason and the cameras are in the same location,
// we don't need to update anything.
// Most security cameras will end here as they're not moving.
var/newturf = get_turf(cam_location)
if(last_camera_turf == newturf)
return

// Cameras that get here are moving, and are likely attached to some moving atom such as cyborgs.
last_camera_turf = get_turf(cam_location)

var/list/visible_things = current.isXRay() ? range(current.view_range, cam_location) : view(current.view_range, cam_location)

var/list/visible_turfs = list()
for(var/turf/visible_turf in visible_things)
visible_turfs += visible_turf

var/list/bbox = get_bbox_of_atoms(visible_turfs)
var/size_x = bbox[3] - bbox[1] + 1
var/size_y = bbox[4] - bbox[2] + 1

cam_screen.vis_contents = visible_turfs
cam_background.icon_state = "clear"
cam_background.fill_rect(1, 1, size_x, size_y)

/obj/structure/machinery/computer/cameras/ui_close(mob/user)
var/user_ref = WEAKREF(user)
var/is_living = isliving(user)
// Living creature or not, we remove you anyway.
concurrent_users -= user_ref
// Unregister map objects
user.client.clear_map(map_name)
SEND_SIGNAL(src, COMSIG_CAMERA_UNREGISTER_UI, user)
// Turn off the console
if(length(concurrent_users) == 0 && is_living)
current = null
SEND_SIGNAL(src, COMSIG_CAMERA_CLEAR)
last_camera_turf = null
if(use_power)
update_use_power(USE_POWER_IDLE)
user.unset_interaction()

/obj/structure/machinery/computer/cameras/proc/show_camera_static()
cam_screen.vis_contents.Cut()
last_camera_turf = null
cam_background.icon_state = "scanline2"
cam_background.fill_rect(1, 1, DEFAULT_MAP_SIZE, DEFAULT_MAP_SIZE)

// Returns the list of cameras accessible from this computer
/obj/structure/machinery/computer/cameras/proc/get_available_cameras()
var/list/D = list()
Expand Down

0 comments on commit 214dce9

Please sign in to comment.