--[[ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * C A M E R A S v 1.09 21/08/2022 (cam size opt, screen cameras, fix active cam limits, removed position tracking) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** Usage: Add this file to mod root, and this to your control.lua events: require "cameras" cam_on_init() ==> on_init / on_changed cam_on_tick(event) ==> on_tick cam_on_gui_click(event) ==> on_gui_click - on the last line of the event ** Create cameras with: CreateCameraForConnectedPlayers(Object,Surface,Text,size,seconds,Zoom) CreateCameraForForce(Force,Object,Surface,Text,size,seconds,Zoom) CreateCameraForPlayer(player,Object,Surface,Text,size,AutoCloseTick,Zoom) Parameters: Object = may be an entity or a fixed position. If entity is a unit, camera will follow its position Surface = optional. If object is entity, gets its surface. If Surface is nil and Object is not entity, gets nauvis Text = nil or {text='Camera', color={r=1,g=1,b=1}} size = nil or camera size AutoCloseTick = nil or number - when it will be closed in game.tick Zoom = nil or Camera Zoon value -- you may add these to load game settings (optional) global.disabled_player_camera[player.name] (bool) -- per player global.enable_drag_camera[player.name] (bool) -- per player global.camera_size[player.name] (int) -- per player global.camera_count_limit (integer) - global ]] --default values. May be changed by your mod mod_name="Big-Monsters" Camera_Default_Zoon = 0.25 Camera_Default_Size = 230 Camera_Default_Text = 'Camera' Camera_Default_Icon = '[img=utility/gps_map_icon]' Camera_Default_Time = nil -- Number - how many ticks it stays on screen. nil => undefined, 60 => 1 second Camera_Count_Limit = 5 local function get_cam_gui(player) local gui if global.enable_drag_camera and global.enable_drag_camera[player.name] then gui = player.gui.screen else gui = player.gui.left if not gui.mf_flow_cameras then gui.add({type="flow", name="mf_flow_cameras", direction="horizontal"}) end gui =player.gui.left.mf_flow_cameras end return gui end --## CAMERAS -- Object may be an entity or a fixed position, if entity, camera will follow it function CreateCameraForPlayer(player,Object,Surface,Text,size,AutoCloseTick,Zoom) cam_player_list_validate_cams(player) local cams = global.active_player_cameras[player.name] local limit = global.camera_count_limit or Camera_Count_Limit if #cams0 then for K,tabdata in pairs (global.mf_frame_cameras) do local frame = tabdata.camframe if frame and frame.valid then frame.destroy() end end global.mf_frame_cameras = {} end global.active_player_cameras = {} end function CloseAllCamerasForPlayer(player) if #global.mf_frame_cameras>0 then for K,tabdata in pairs (global.mf_frame_cameras) do local frame = tabdata.camframe if player==frame.gui.player then if frame and frame.valid then frame.destroy() end end end global.mf_frame_cameras = {} end if global.active_player_cameras[player.name] then global.active_player_cameras[player.name] = {} end end local function get_gps_cam_tag(position,surface) local r = '[gps='..math.floor(position.x)..','..math.floor(position.y) if surface then r=r..','..surface.name end r=r..']' return r end function del_cam_list(list, obj) for i, obj2 in pairs(list) do if obj2 == obj then table.remove( list, i ) return(true) end end return(false) end -- Gui click function CameraClose(player,gui) local frame = gui.parent.parent local name = frame.name del_cam_list(global.active_player_cameras[player.name], name) table.remove(global.active_player_cameras[player.name],c) frame.destroy() end function CameraZoomIn(player,gui,num) local frame = gui.parent.parent local cam = frame["mf_camera"..num] local z = cam.zoom if z>0.1 then cam.zoom = z - 0.05 end end function CameraZoomOut(player,gui,num) local frame = gui.parent.parent local cam = frame["mf_camera"..num] local z = cam.zoom if z<1 then cam.zoom = z + 0.1 end end function ClickCamera(player,gui,num) local frame = gui.parent local cam = frame["mf_camera"..num] local surface = game.surfaces[cam.surface_index] local position = cam.position if cam.entity and cam.entity.valid then position = cam.entity.position end player.print(get_gps_cam_tag(position,surface)) end -- ************ EVENTS ******************************** function cam_on_gui_click(event) if event and event.element and event.element.valid and event.player_index and game.players[event.player_index] then if event.element.get_mod()==mod_name then local player = game.players[event.player_index] local gui = event.element local name= gui.name if string.sub(name,1,17)=="mf_bt_cameraclose" then CameraClose(player,gui) elseif string.sub(name,1,18)=="mf_bt_camerazoomin" then CameraZoomIn(player,gui,string.sub(name,19,string.len(name))) elseif string.sub(name,1,19)=="mf_bt_camerazoomout" then CameraZoomOut(player,gui,string.sub(name,20,string.len(name))) elseif string.sub(name,1,9)=="mf_camera" then ClickCamera(player,gui,string.sub(name,10,string.len(name))) end end end end --#Camera Updates function cam_on_tick(event) if #global.mf_frame_cameras>0 then for K=#global.mf_frame_cameras,1,-1 do local frame = global.mf_frame_cameras[K].camframe local tick = global.mf_frame_cameras[K].tick local autoclosetick = global.mf_frame_cameras[K].autoclosetick if frame and frame.valid then if autoclosetick and autoclosetick