function MouseCam_GlobalVariables takes nothing returns nothing
// Constants variables
set udg_MouseCam_BoundOffset = 1
set udg_MouseCam_Interval = 0.05
set udg_MouseCam_RotationSpeed = 1
set udg_MouseCam_AoaSpeed = 1
set udg_MouseCam_Precision = 0.1
set udg_MouseCam_Key_None = 0
set udg_MouseCam_Key_Left = 1
set udg_MouseCam_Key_Up = 2
set udg_MouseCam_Key_Right = 3
set udg_MouseCam_Key_Down = 4
// Non-constant variables
set udg_MouseCam_Key = 0
set udg_MouseCam_U = null
set udg_MouseCam_toggle = false
set udg_MouseCam_StoredX = 0
set udg_MouseCam_StoredY = 0
set udg_MouseCam_initialized = false
endfunction
function MouseCam_ApplyCameraForPlayer takes unit u, player p returns nothing
if GetLocalPlayer() == p then
set udg_MouseCam_U = u
endif
endfunction
function MouseCam_CamMovementToArrowKey takes real dx, real dy returns integer
local real AngleCamera = GetCameraField(CAMERA_FIELD_ROTATION) * bj_RADTODEG
local real AngleDirection = Atan2(dy, dx) * bj_RADTODEG
local real Delta = AngleCamera - AngleDirection
if (RAbsBJ(dx) < udg_MouseCam_Precision) and (RAbsBJ(dy) < udg_MouseCam_Precision) then //within precision margin, assume no camera movement
return udg_MouseCam_Key_None
endif
set Delta = Delta + 45
if Delta > 360 then
set Delta = Delta - 360
elseif Delta < 0 then
set Delta = Delta + 360
endif
return R2I(Delta/90.0)
endfunction
function MouseCam_Periodic takes nothing returns nothing
local real x = GetUnitX(udg_MouseCam_U)
local real y = GetUnitY(udg_MouseCam_U)
if GetUnitTypeId(udg_MouseCam_U) != 0 then
set udg_MouseCam_toggle = not udg_MouseCam_toggle
if udg_MouseCam_toggle then
set udg_MouseCam_Key = MouseCam_CamMovementToArrowKey(GetCameraTargetPositionX()-udg_MouseCam_StoredX, GetCameraTargetPositionY()-udg_MouseCam_StoredY)
call SetCameraBounds(x, y, x, y, x, y, x, y)
set udg_MouseCam_StoredX = x
set udg_MouseCam_StoredY = y
else
call SetCameraBounds(udg_MouseCam_StoredX-udg_MouseCam_BoundOffset, udg_MouseCam_StoredY-udg_MouseCam_BoundOffset, udg_MouseCam_StoredX-udg_MouseCam_BoundOffset, udg_MouseCam_StoredY+udg_MouseCam_BoundOffset, udg_MouseCam_StoredX+udg_MouseCam_BoundOffset, udg_MouseCam_StoredY+udg_MouseCam_BoundOffset, udg_MouseCam_StoredX+udg_MouseCam_BoundOffset, udg_MouseCam_StoredY-udg_MouseCam_BoundOffset)
endif
else
set udg_MouseCam_Key = udg_MouseCam_Key_None
endif
if udg_MouseCam_Key == udg_MouseCam_Key_Left then
call BJDebugMsg("Left")
elseif udg_MouseCam_Key == udg_MouseCam_Key_Up then
call BJDebugMsg("Up")
elseif udg_MouseCam_Key == udg_MouseCam_Key_Right then
call BJDebugMsg("Right")
elseif udg_MouseCam_Key == udg_MouseCam_Key_Down then
call BJDebugMsg("Down")
endif
endfunction
function MouseCam_init takes nothing returns nothing
local timer t = CreateTimer()
set udg_MouseCam_U = CreateUnit(GetLocalPlayer(), 'hfoo', 0, 0, 0)
call TimerStart(t, udg_MouseCam_Interval, true, function MouseCam_Periodic)
set t = null
endfunction
function InitTrig_MouseCam takes nothing returns nothing
call MouseCam_GlobalVariables()
call MouseCam_init()
endfunction