Raipin

Lockdown

Apr 19th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ; MouselockRebind
  3. ;
  4. ; NO EDITS OF THIS FILE ARE NECESSARY ANY MORE.
  5. ; CONFIGURE THIS SCRIPT VIA LOCKDOWN OPTIONS IN GAME
  6. ;
  7. ; Checks visibility of mouse cursor to determine lock status
  8. ; Provides cursor re-centering and locking of cursor to screen, otherwise it would be much simpler.
  9.  
  10. #NoEnv
  11. SendMode Input
  12. #InstallKeybdHook
  13. #UseHook
  14. #SingleInstance force
  15.  
  16. GroupAdd, wildstar, ahk_exe Wildstar.exe
  17. GroupAdd, wildstar, ahk_exe Wildstar64.exe
  18.  
  19.  
  20. DEBUG := false ;;;; Change this   false   to   true   if told.
  21.  
  22.  
  23. LockdownConfigFile := A_AppData . "\NCSoft\WildStar\AddonSaveData\Lockdown_0_Gen.xml"
  24.  
  25. ; Configuration defaults (Used if Lockdown isn't in play)
  26. free_with_shift := false
  27. free_with_alt := false
  28. free_with_ctrl := false
  29. reticle_offset_x := 0
  30. reticle_offset_y := 0
  31. ahk_lmb := "-"
  32. ahk_rmb := "="
  33. ahk_mmb := ""
  34. ahk_cursor_center := true
  35. ahk_update_interval := 100
  36.  
  37. if (DEBUG)
  38.   FileDelete, %A_Desktop%\MouselockRebind_debug.txt
  39.  
  40. print( str, tag="", prefix="" )
  41. {
  42.   global DEBUG
  43.   if (DEBUG) {
  44.     if (prefix)
  45.       str := prefix . ": " . str
  46.     if (tag)
  47.       str := "[" . tag . "] " . str
  48.     FileAppend, %A_Now%  %str%`n, %A_Desktop%\MouselockRebind_debug.txt
  49.     if (ErrorLevel == 1)
  50.       MsgBox Could not write to %A_Desktop%\MouselockRebind_debug.txt    
  51.   }
  52. }
  53.  
  54. XMLDoc := ComObjCreate("MSXML2.DOMDocument.6.0")
  55. XMLDoc.async := false ; UPGRADE AUTOHOTKEY FROM WWW.AHKSCRIPT.ORG
  56.  
  57. ReadConfig()
  58. {
  59.   global
  60.   ; Read file
  61.   local Contents
  62.   FileRead, Contents, %LockdownConfigFile%
  63.   if (Contents == 0)
  64.     return
  65.  
  66.   ; Load document
  67.   XMLDoc.loadXML(Contents)
  68.  
  69.   ; Read options
  70.   local node
  71.   for node in XMLDoc.selectSingleNode("/Document").childNodes
  72.   {
  73.     local key := node.GetAttribute("K")
  74.     ; Ugh.
  75.     if ( key == "free_with_shift"
  76.       or key == "free_with_alt"
  77.       or key == "free_with_ctrl"
  78.       or key == "reticle_offset_x"
  79.       or key == "reticle_offset_y"
  80.       or key == "ahk_lmb"
  81.       or key == "ahk_rmb"
  82.       or key == "ahk_mmb"
  83.       or key == "ahk_cursor_center"
  84.       or key == "ahk_update_interval" ) {
  85.  
  86.       local vtype := node.GetAttribute("T")
  87.       local value := node.GetAttribute("V")
  88.  
  89.       if (vtype == "n")
  90.         %key% := value + 0
  91.       else if (vtype == "b")
  92.         %key% := (value == "+")
  93.       else
  94.         %key% := value
  95.     }
  96.   }
  97.  
  98.   SetTimer, UpdateState, %ahk_update_interval%
  99.  
  100.   if (DEBUG) {
  101.     print(free_with_shift, "OPTION", "free_with_shift")
  102.     print(free_with_ctrl, "OPTION", "free_with_ctrl")
  103.     print(free_with_alt, "OPTION", "free_with_alt")
  104.     print(reticle_offset_x, "OPTION", "reticle_offset_x")
  105.     print(reticle_offset_y, "OPTION", "reticle_offset_y")
  106.     print(ahk_lmb, "OPTION", "ahk_lmb")
  107.     print(ahk_rmb, "OPTION", "ahk_rmb")
  108.     print(ahk_mmb, "OPTION", "ahk_mmb")
  109.     print(ahk_cursor_center, "OPTION", "ahk_cursor_center")
  110.     print(ahk_update_interval, "OPTION", "ahk_update_interval")
  111.   }
  112. }
  113.  
  114. IsCursorVisible()
  115. {
  116.   NumPut(VarSetCapacity(CurrentCursorStruct, A_PtrSize + 16), CurrentCursorStruct, "uInt")
  117.   DllCall("GetCursorInfo", "ptr", &CurrentCursorStruct)
  118.   if (NumGet(CurrentCursorStruct, 8) <> 0)
  119.     return true
  120.   return false
  121. }
  122.  
  123. LockCursor( activate=false, offset=5 )
  124. {
  125.   global reticle_offset_y
  126.   global reticle_offset_x
  127.   if (activate) {
  128.     WinGetPos, x, y, w, h, ahk_group wildstar
  129.     x1 := x + round(w/2 + reticle_offset_x)
  130.     y1 := y + round(h/2 + reticle_offset_y)
  131.     VarSetCapacity(R,16,0),  NumPut(x1-offset,&R+0),NumPut(y1-offset,&R+4),NumPut(x1+offset,&R+8),NumPut(y1+offset,&R+12)
  132.     DllCall( "ClipCursor", UInt, &R )
  133.   } else
  134.     DllCall( "ClipCursor", UInt, 0 )
  135. }
  136.  
  137. if FileExist(A_ScriptDir . "\wildstar_icon.ico") {
  138.   Menu, Tray, Icon, %A_ScriptDir%\wildstar_icon.ico
  139. }
  140.  
  141. Menu, Tray, NoStandard
  142. Menu, Tray, Add, Reload, ReloadScript
  143. Menu, Tray, Add, Edit Script, EditScript
  144. Menu, Tray, Add, Pause Script, PauseScript
  145. Menu, Tray, Add, Exit, ExitScript
  146. Menu, Tray, Default, Reload
  147.  
  148. print("Starting up", "LOG")
  149.  
  150. ReadConfig()
  151.  
  152. ; State is the current reading of the in-game indicator pixel
  153. state := false
  154. ; Intent is the assumed state the game is in while tabbed out
  155. intent := false
  156. ; UID of Wildstar window for us to check against when clicking outside of wildstar
  157. uid := false
  158.  
  159. ; State update timer
  160. SetTimer, UpdateState, Off
  161.  
  162. ; Config refresh timer
  163. SetTimer, GetConfig, 10000
  164. SetTimer, GetConfig, Off
  165.  
  166. ; Timer control and alt-tab locking/unlocking
  167. Loop {
  168.   WinWaitActive, ahk_group wildstar
  169.   {
  170.     ; Resume lock when refocused after automatically unlocking
  171.     if (state == false && intent == true) {
  172.       ControlSend, , {vkF1}, ahk_group wildstar
  173.       print("Relocking", "ALT-TAB")
  174.     }
  175.  
  176.     ; Update UID for click checks
  177.     uid := WinExist()
  178.    
  179.     ; Activate polling
  180.     SetTimer, UpdateState, On
  181.     ; SetTimer, GetConfig, On
  182.  
  183.     ; Reload settings
  184.     ReadConfig()
  185.  
  186.     print("Active", "WINDOW")
  187.    
  188.     ; Wait for unfocus
  189.     WinWaitNotActive, ahk_group wildstar
  190.     {
  191.       ; SetTimer, GetConfig, Off
  192.     }
  193.   }
  194. }
  195.  
  196. return
  197.  
  198. ;;;;;;;;;;;;;;;;
  199. ; Labels
  200.  
  201. GetConfig:
  202.  ReadConfig()
  203. return
  204.  
  205. ; Because we're not perfect.
  206. ClickDelay:
  207.  SetTimer, UpdateState, On
  208.   SetTimer, ClickDelay, Off
  209.   if (state) {
  210.     ControlSend, , {vkF2}, ahk_group wildstar
  211.     state := false
  212.     intent := false
  213.   }
  214. return
  215.  
  216. ; Cursor state polling
  217. hidden_ticks := 0 ; As of Drop 4, the cursor flickers invisible occasionally. Tolerate this
  218. UpdateState:
  219.  ; Release and disable if not focused
  220.   if not WinActive("ahk_group wildstar") {
  221.     if (state) {
  222.       ControlSend, , {vkF2}, ahk_group wildstar
  223.       print("Unlocking", "ALT-TAB")
  224.       hidden_ticks := 0
  225.       state := false
  226.       SetTimer, UpdateState, Off
  227.       LockCursor()
  228.     }
  229.     print("Inactive", "WINDOW")
  230.     return
  231.   }
  232.  
  233.   ; Cursor visible, unlock
  234.   if (IsCursorVisible()) {
  235.     if (state) {
  236.       print("Change: Off", "STATE")
  237.       hidden_ticks := 0
  238.       state := false
  239.       LockCursor()
  240.     }
  241.     intent := false
  242.  
  243.   ; Cursor not visible, determine if we should lock
  244.   } else {
  245.     if (state == false and not GetKeyState("LButton") and not GetKeyState("RButton")) {
  246.       MouseGetPos, , , over_uid
  247.       if (over_uid == uid) {
  248.         hidden_ticks++
  249.         if (hidden_ticks >= 2) {
  250.           hidden_ticks := 0
  251.           print("Change: On", "STATE")
  252.           if (ahk_cursor_center) {
  253.             ; Send release signal
  254.             ControlSend, , {vkF2}, ahk_group wildstar
  255.             Sleep, 10
  256.             ; Forcefully recenter cursor, possibly redundant
  257.             WinGetPos, x, y, w, h
  258.             DllCall("SetCursorPos", int, w/2 - 5 + reticle_offset_x, int, h/2 + reticle_offset_y)
  259.             LockCursor(true, 5)
  260.             ; Re-lock mouse
  261.             ControlSend, , {vkF1}, ahk_group wildstar
  262.             ; Lock loosely to prevent it leaving the screen
  263.             ; but allowing it to feel responsive while unlocking
  264.             LockCursor(true, 300)
  265.           }
  266.           state := true
  267.           intent := true
  268.         }
  269.       }
  270.     }
  271.   }
  272. return
  273.  
  274. ReloadScript:
  275.  Reload
  276. return
  277.  
  278. EditScript:
  279.  RunWait, notepad %A_ScriptFullPath%
  280.   Reload
  281. return
  282.  
  283. PauseScript:
  284.  Pause, Toggle
  285. return
  286.  
  287. ExitScript:
  288.  ExitApp
  289. return
  290.  
  291. ; Mouse remaps
  292. #IfWinActive, ahk_group wildstar
  293.  
  294. $r::
  295.   If (state) {
  296.     Send, 9
  297.     Send, r
  298.   }
  299.   else {
  300.     Send, r
  301.   }
  302. return
  303.  
  304. *LButton::
  305.   If (state and ahk_lmb != "") {
  306.     Send, {blind}{%ahk_lmb% Down}
  307.     KeyWait, LButton
  308.     Send, {blind}{%ahk_lmb% Up}
  309.   }
  310.   else {
  311.     SetTimer, UpdateState, Off
  312.     Send, {blind}{LButton Down}
  313.     KeyWait, LButton
  314.     Send, {blind}{LButton Up}
  315.     state := false
  316.     intent := false
  317.     SetTimer, ClickDelay, 50
  318.   }
  319. return
  320.  
  321. *RButton::
  322.   If (state and ahk_rmb != "") {
  323.     PriorDownKey := DownKey := ""
  324.     while GetKeyState("RButton", "P")                                       ;Update this line with macro bind as well
  325.       {
  326.         PixelGetColor, AMColor, 3, 3
  327.           if (AMColor = 0x0000FF)
  328.             DownKey := "="                              ;Rampage Bind
  329.           else if (AMColor = 0x0050FF)
  330.             DownKey := "4"                              ;Smackdown
  331.           else if (AMColor = 0x00FFFF)
  332.             DownKey := "3"                              ;Powerlink Bind
  333.           else if (AMColor = 0x00FF00)
  334.             DownKey := "1"                              ;Breaching Strikes Bind
  335.           else if (AMColor = 0xFF00FF)
  336.             DownKey := "2"                              ;Tremor
  337.           else if (AMColor = 0xFFFFFF)
  338.             DownKey := "5"                          ;Savage Strikes
  339.           else
  340.             DownKey := "-"                              ;Relentless Strikes Bind
  341.           if (DownKey != PriorDownKey)
  342.             {
  343.               for each, Key in [1,2,3,4,5,"-","="]        ;Update these with all your binds
  344.               if (Key != DownKey && GetKeyState(Key))
  345.                 Send, {%Key% up}
  346.                 Send, {%DownKey% down}
  347.                 PriorDownKey := DownKey
  348.             }
  349.         }
  350.         ;Update these with all your binds as well
  351.         if (GetKeyState("1"))
  352.           Send, {1 up}
  353.         if (GetKeyState("2"))
  354.           Send, {2 up}
  355.         if (GetKeyState("3"))
  356.           Send, {3 up}
  357.         if (GetKeyState("4"))
  358.           Send, {4 up}
  359.         if (GetKeyState("5"))
  360.           Send, {5 up}
  361.         if (GetKeyState("-"))
  362.           Send, {- up}
  363.         if (GetKeyState("="))
  364.           Send, {= up}
  365.         return
  366.   }
  367.   else {
  368.     SetTimer, UpdateState, Off
  369.     Send, {blind}{RButton Down}
  370.     KeyWait, RButton
  371.     Send, {blind}{RButton Up}
  372.     state := false
  373.     intent := false
  374.     SetTimer, ClickDelay, 50
  375.   }
  376. return
  377.  
  378. *MButton::
  379.   If (state and ahk_mmb != "") {
  380.     Send, {blind}{%ahk_mmb% Down}
  381.     KeyWait, MButton
  382.     Send, {blind}{%ahk_mmb% Up}
  383.   }
  384.   else {
  385.     Send, {blind}{MButton Down}
  386.     KeyWait, MButton
  387.     Send, {blind}{MButton Up}
  388.   }
  389. return
Advertisement
Add Comment
Please, Sign In to add comment