Advertisement
zoom377

MouseSteering_V1.6.3

Oct 14th, 2015
1,394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;----------------------------------------
  2. ; Mouse Steering program made by Zoom377 | 27/9/2015
  3. ;----------------------------------------
  4.  
  5. #Persistent
  6. #SingleInstance, Force
  7. #NoEnv
  8. SetFormat, float, 0.20
  9. SetBatchLines, -1
  10. #Include <Gdip>
  11. #Include <CvJoyInterface>
  12. #Include <AHKHID>
  13.  
  14.  
  15. Gui InputGUI:New, +LastFound +ToolWindow
  16. Gui InputGUI:Show, NA
  17.  
  18. InputGUIHandle := WinExist()
  19.  
  20. AHKHID_UseConstants()
  21.  
  22. OnMessage(0x00FF, "InputMsg")
  23.  
  24. gosub Register
  25.  
  26. global vJoyInterface := new CvJoyInterface()
  27.  
  28. if(!vJoyInterface.vJoyEnabled()){
  29.     MsgBox, % "Error: " . vJoyInterface.LoadLibraryLog
  30. }
  31. else
  32. {
  33.     TrayTip, Mouse Steering, vJoy found.
  34. }
  35.  
  36. If !pToken := Gdip_Startup()
  37. {
  38.     MsgBox, w, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
  39.     ExitApp
  40. }
  41.  
  42. ;- Quadratic curves:
  43. ;- ax^2 + bx + c
  44. ;- c is unnecessary as the curve must intercept 0 so:
  45. ;- ax^2 + bx
  46.  
  47. ;-User defined variables
  48. global sensitivity := "" ;The amount mouse movements affect joyStickX
  49. global pushBack := "" ;The speed at which joyStickX is pushed to 0
  50. global a := "" ;Nonlinearity for the joyStickX pushback
  51.  
  52. ;-Program constants
  53. global SETTINGS_FOLDER_PATH := A_MyDocuments . "\Mouse Steering"
  54. global SETTINGS_PATH := A_MyDocuments . "\Mouse Steering\Settings.ini"
  55.  
  56. global SLIDER_MIN_SENSITIVITY := 1
  57. global SLIDER_MAX_SENSITIVITY := 1500
  58. global SLIDER_INTERVAL_SENSITIVITY := 0.1
  59.  
  60. global SLIDER_MIN_PUSHBACK := 0
  61. global SLIDER_MAX_PUSHBACK := 100
  62. global SLIDER_INTERVAL_PUSHBACK := 0.01
  63.  
  64. global SLIDER_MIN_NONLINEARITY := 100
  65. global SLIDER_MAX_NONLINEARITY := 300
  66. global SLIDER_INTERVAL_NONLINEARITY := 0.01
  67.  
  68. global MARKER_WIDTH := 30
  69. global MARKER_WIDTH_WITH_BORDER := 40
  70. global MARKER_HEIGHT := 60
  71. global MARKER_HEIGHT_WITH_BORDER := 70
  72.  
  73.  
  74. global MARKER_COLORS := [0xffff0000, 0xff00ff00, 0xff0000ff]
  75.  
  76. global MOUSE_RESET_INTERVAL := 500
  77.  
  78. global SCREEN_CENTER_X := A_ScreenWidth/2
  79. global SCREEN_CENTER_Y := A_ScreenHeight/2
  80.  
  81. ;-Program variables
  82. global scale := 1
  83. global previousMouseX := 0
  84. global b := "" ;
  85. global joyStickX := 0
  86. global lastMouseReset := 0
  87. global mouseHideEnabled := false
  88. global markerEnabled := false
  89. global markerColorIndex := 1
  90.  
  91. ;-Setup
  92. Menu, Tray, NoStandard
  93. Menu, Tray, Add, Settings
  94. Menu, Tray, Add, Exit
  95. gosub LoadSettings
  96. gosub CalculateScale
  97.  
  98.  
  99.  
  100. ;-Gdip stuff!!!
  101. ; Get the dimensions of the primary monitor
  102. SysGet, MonitorPrimary, MonitorPrimary
  103. SysGet, WA, Monitor, %MonitorPrimary%
  104. SysGet, screenPixelWidth, 16
  105. WAWidth := WARight-WALeft
  106. WAHeight := WABottom-WATop
  107.  
  108. ; Create a layered window (+E0x80000) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
  109. Gui, 2: +LastFound -Caption +E0x80000 +ToolWindow +OwnDialogs +AlwaysOnTop
  110.  
  111. ; Show the window
  112. Gui, 2: Show, NA
  113.  
  114. ; Get a handle to this window we have created in order to update it later
  115. global hwnd1 := WinExist()
  116.  
  117. WinSet, ExStyle, +0x20, ahk_id %hwnd1%
  118.  
  119. ; Create a gdi bitmap with width and height of the work area
  120. global hbm := CreateDIBSection(WAWidth, WAHeight)
  121.  
  122. ; Get a device context compatible with the screen
  123. global hdc := CreateCompatibleDC()
  124.  
  125. ; Select the bitmap into the device context
  126. global obm := SelectObject(hdc, hbm)
  127.  
  128. ; Get a pointer to the graphics of the bitmap, for use with drawing functions
  129. global G := Gdip_GraphicsFromHDC(hdc)
  130.  
  131. ; Set the smoothing mode to antialias = 4 to make shapes appear smother (only used for vector drawing and filling)
  132. Gdip_SetSmoothingMode(G, 4)
  133.  
  134.  
  135.  
  136. SetTimer, TimerTick, 15
  137. return ;- Finish initialisation
  138.  
  139. TimerTick:
  140.    
  141.     difference := 16384 - joyStickX ;- Calculate difference between 16384 (desired) and joyStickX position
  142.     ;- ax^2 + bx
  143.     ;- a * difference^2 + b * difference   
  144.    
  145.  
  146.    
  147.     if (a >= 1 && a < 2)
  148.     {
  149.         if (difference >= 0) ;- If positive
  150.         {
  151.             quadraticOutput := (difference**a) ;- Quadratic stuff
  152.             quadraticOutput *= pushBack * scale
  153.         }
  154.         else if (difference < 0) ;- If negative
  155.         {
  156.             difference *= -1
  157.             quadraticOutput := -(difference**a) ;- Quadratic stuff
  158.             quadraticOutput *= pushBack * scale
  159.         }
  160.     }
  161.     else if (a >= 2 && a < 3)
  162.     {
  163.         if (difference >= 0) ;- If positive
  164.         {
  165.             quadraticOutput := (difference**a) ;- Quadratic stuff
  166.             quadraticOutput *= pushBack * scale
  167.         }
  168.         else if (difference < 0) ;- If negative
  169.         {
  170.             difference *= -1
  171.             quadraticOutput := -(difference**a) ;- Quadratic stuff
  172.             quadraticOutput *= pushBack * scale
  173.         }
  174.     }
  175.     else if (a >= 3)
  176.     {
  177.         if (difference >= 0) ;- If positive
  178.         {
  179.             quadraticOutput := (difference**a) ;- Quadratic stuff
  180.             quadraticOutput *= pushBack * scale
  181.         }
  182.         else if (difference < 0) ;- If negative
  183.         {
  184.             quadraticOutput := (difference**a) ;- Quadratic stuff
  185.             quadraticOutput *= pushBack * scale
  186.         }
  187.     }
  188.    
  189.     GetKeyState, state, RButton
  190.     if (state = "U") ;- If right mouse button not held, push joystickX towards 0
  191.     {
  192.         joyStickX += quadraticOutput
  193.     }
  194.     ;~ ToolTip, %joyStickX%, 10, 10
  195.    
  196.     if (joyStickX > 32767)
  197.         joyStickX := 32767
  198.    
  199.     if (joyStickX < 0)
  200.         joyStickX := 0 
  201.    
  202.     vJoyInterface.Devices[1].SetAxisByIndex(joyStickX, 1) ;- Set virtual joystick value
  203.    
  204.    
  205.    
  206.     if (GetKeyState("LButton") = 1)
  207.     {
  208.         vJoyInterface.Devices[1].SetBtn(1, 7)
  209.     }
  210.     else
  211.     {
  212.         vJoyInterface.Devices[1].SetBtn(0, 7)
  213.     }
  214.    
  215.     if (GetKeyState("RButton") = 1)
  216.     {
  217.         vJoyInterface.Devices[1].SetBtn(1, 8)
  218.     }
  219.     else
  220.     {
  221.         vJoyInterface.Devices[1].SetBtn(0, 8)
  222.     }
  223.    
  224.    
  225.    
  226.     xScale := screenPixelWidth / 32767
  227.     markerXPos := (joystickX * xScale) - (MARKER_WIDTH/2)
  228.     markerXPosWithBorder := (joystickX * xScale) - (MARKER_WIDTH_WITH_BORDER/2)
  229.    
  230.     Gdip_GraphicsClear(G)
  231.    
  232.     if (markerEnabled = true)
  233.     {
  234.         markerColorElement := MARKER_COLORS[markerColorIndex]
  235.         pBrush := Gdip_BrushCreateSolid(markerColorElement)
  236.         pBrushBlack := Gdip_BrushCreateSolid(0xff000000)
  237.        
  238.         Gdip_FillRectangle(G, pBrushBlack, markerXPosWithBorder, WAHeight - MARKER_HEIGHT_WITH_BORDER - 30 + 5, MARKER_WIDTH_WITH_BORDER, MARKER_HEIGHT_WITH_BORDER)
  239.         Gdip_FillRectangle(G, pBrush, markerXPos, WAHeight-MARKER_HEIGHT - 30, MARKER_WIDTH, MARKER_HEIGHT)
  240.            
  241.         Gdip_DeleteBrush(pBrush)
  242.         Gdip_DeleteBrush(pBrushBlack)
  243.     }
  244.     ; Update the specified window
  245.     UpdateLayeredWindow(hwnd1, hdc, WALeft, WATop, WAWidth, WAHeight)  
  246. return
  247.  
  248. PresentSettings:
  249.     Gui, 1:Add, Text, x12 y10 w140 h20 +Center, Sensitivity
  250.     Gui, 1:Add, Slider, vSliderSensitivity gSubSliderSensitivity x12 y30 w140 h30 ToolTip Range%SLIDER_MIN_SENSITIVITY%-%SLIDER_MAX_SENSITIVITY%, % sensitivity / SLIDER_INTERVAL_SENSITIVITY
  251.     Gui, 1:Add, Text, vLabelSensitivity x152 y30 w50 h30 , % Format("{1:0.1f}", sensitivity)
  252.    
  253.     Gui, 1:Add, Text, x12 y70 w140 h20 +Center, Push back
  254.     Gui, 1:Add, Slider, vSliderPushBack gSubSliderPushBack x12 y90 w140 h30 ToolTip Range%SLIDER_MIN_PUSHBACK%-%SLIDER_MAX_PUSHBACK%, % pushBack / SLIDER_INTERVAL_PUSHBACK
  255.     Gui, 1:Add, Text, vLabelPushBack x152 y90 w70 h30 , % Format("{1:0.2f}", pushBack)
  256.    
  257.     Gui, 1:Add, Text, x12 y130 w140 h20 +Center, Non-linearity
  258.     Gui, 1:Add, Slider, vSliderNonLinearity gSubSliderNonLinearity x12 y150 w140 h30 ToolTip Range%SLIDER_MIN_NONLINEARITY%-%SLIDER_MAX_NONLINEARITY%, % a / SLIDER_INTERVAL_NONLINEARITY
  259.     Gui, 1:Add, Text, vLabelNonLinearity x152 y150 w50 h30 , % Format("{1:0.2f}", a)
  260.    
  261.     Gui, 1:Add, Button, gBtnSave x32 y190 w140 h30 , Save
  262.     Gui, 1:Show, w205 h236, Settings
  263.     WinWaitClose, Settings
  264. return
  265.  
  266. SubSliderSensitivity:
  267.     GuiControl, , LabelSensitivity, % Format("{1:0.1f}", SliderSensitivity * SLIDER_INTERVAL_SENSITIVITY)
  268. return
  269.  
  270. SubSliderPushBack:
  271.     GuiControl, , LabelPushBack, % Format("{1:0.2f}", SliderPushBack * SLIDER_INTERVAL_PUSHBACK)
  272. return
  273.  
  274. SubSliderNonLinearity:
  275.     GuiControl, , LabelNonLinearity, % Format("{1:0.2f}", SliderNonLinearity * SLIDER_INTERVAL_NONLINEARITY)
  276. return
  277.  
  278. LoadSettings:
  279.     IfExist, %SETTINGS_FOLDER_PATH%
  280.     {
  281.         IniRead, sensitivity, %SETTINGS_PATH%, UserVariables, key_sensitivity, 1
  282.         IniRead, pushBack, %SETTINGS_PATH%, UserVariables, key_pushBack, 1
  283.         IniRead, a, %SETTINGS_PATH%, UserVariables, key_a, 1
  284.         b := 1 - a
  285.         gosub CalculateScale
  286.     }
  287.     else
  288.     {      
  289.         FileCreateDir, %SETTINGS_FOLDER_PATH%
  290.         FileAppend,, SETTINGS_PATH
  291.         gosub PresentSettings
  292.         gosub CalculateScale
  293.     }
  294. return
  295.  
  296. SaveSettings:
  297.     IniWrite, %sensitivity%, %SETTINGS_PATH%, UserVariables, key_sensitivity
  298.     IniWrite, %pushBack%, %SETTINGS_PATH%, UserVariables, key_pushBack
  299.     IniWrite, %a%, %SETTINGS_PATH%, UserVariables, key_a
  300. return
  301.  
  302. BtnSave:
  303.     Gui, 1:Submit
  304.     sensitivity := SliderSensitivity * SLIDER_INTERVAL_SENSITIVITY
  305.     pushBack := SliderPushBack * SLIDER_INTERVAL_PUSHBACK
  306.     a := SliderNonLinearity * SLIDER_INTERVAL_NONLINEARITY
  307.     b := 1 - a
  308.     Gui, 1:Destroy
  309.     gosub SaveSettings
  310.     gosub CalculateScale
  311. return
  312.  
  313. Settings:
  314.     gosub PresentSettings
  315. return
  316.  
  317. Exit:
  318.     ExitApp
  319. return
  320.  
  321. OnExit:
  322.     ; Select the object back into the hdc
  323.     SelectObject(hdc, obm)
  324.     ; Now the bitmap may be deleted
  325.     DeleteObject(hbm)
  326.     ; Also the device context related to the bitmap may be deleted
  327.     DeleteDC(hdc)
  328.     ; The graphics may now be deleted
  329.     Gdip_DeleteGraphics(G)
  330.     ; ...and gdi+ may now be shutdown
  331.     Gdip_Shutdown(pToken)  
  332.    
  333.     gosub SaveSettings
  334. return
  335.  
  336. StopTimer:
  337.     SetTimer, TimerTick, Off
  338. return
  339.  
  340. StartTimer:
  341.     SetTimer, TimerTick, 15
  342. return
  343.  
  344. GuiEscape:
  345. GuiClose:
  346. ButtonCancel:
  347.     Gui, 1:Destroy
  348. return
  349.  
  350.  
  351. InputMsg(wParam, lParam) {
  352.     Local xRawDelta, yRawDelta
  353.     Critical
  354.    
  355.     xRawDelta += AHKHID_GetInputInfo(lParam, II_MSE_LASTX) + 0.0
  356.     yRawDelta += AHKHID_GetInputInfo(lParam, II_MSE_LASTY) + 0.0
  357.    
  358.     joystickX += xRawDelta * sensitivity
  359. }
  360.  
  361. Register:
  362.    AHKHID_Register(1,2,InputGUIHandle, 0x00000100)
  363. Return
  364.  
  365.  
  366. CalculateScale:
  367.     scale := 32767 / (32767**a)
  368. return
  369.  
  370. ToggleFakeFullscreen()
  371. {
  372.     CoordMode Screen, Window
  373.     static WINDOW_STYLE_UNDECORATED := -0xC40000
  374.     static savedInfo := Object() ;; Associative array!
  375.     WinGet, id, ID, A
  376.     if (savedInfo[id])
  377.     {
  378.         inf := savedInfo[id]
  379.         WinSet, Style, % inf["style"], ahk_id %id%
  380.         WinMove, ahk_id %id%,, % inf["x"], % inf["y"], % inf["width"], % inf["height"]
  381.         savedInfo[id] := ""
  382.     }
  383.     else
  384.     {
  385.         savedInfo[id] := inf := Object()
  386.         WinGet, ltmp, Style, A
  387.         inf["style"] := ltmp
  388.         WinGetPos, ltmpX, ltmpY, ltmpWidth, ltmpHeight, ahk_id %id%
  389.         inf["x"] := ltmpX
  390.         inf["y"] := ltmpY
  391.         inf["width"] := ltmpWidth
  392.         inf["height"] := ltmpHeight
  393.         WinSet, Style, %WINDOW_STYLE_UNDECORATED%, ahk_id %id%
  394.         mon := GetMonitorActiveWindow()
  395.         SysGet, mon, Monitor, %mon%
  396.         WinMove, A,, %monLeft%, %monTop%, % monRight-monLeft, % monBottom-monTop
  397.     }
  398. }
  399.  
  400. GetMonitorAtPos(x,y)
  401. {
  402.     ;; Monitor number at position x,y or -1 if x,y outside monitors.
  403.     SysGet monitorCount, MonitorCount
  404.     i := 0
  405.     while(i < monitorCount)
  406.     {
  407.         SysGet area, Monitor, %i%
  408.         if ( areaLeft <= x && x <= areaRight && areaTop <= y && y <= areaBottom )
  409.         {
  410.             return i
  411.         }
  412.         i := i+1
  413.     }
  414.     return -1
  415. }
  416.  
  417. GetMonitorActiveWindow(){
  418.     ;; Get Monitor number at the center position of the Active window.
  419.     WinGetPos x,y,width,height, A
  420.     return GetMonitorAtPos(x+width/2, y+height/2)
  421. }
  422.  
  423. ^F7::
  424.     markerColorIndex++
  425.     if (markerColorIndex > MARKER_COLORS.Length())
  426.     {
  427.         markerColorIndex -= MARKER_COLORS.Length()
  428.     }
  429. return
  430.  
  431. ^F8::
  432.     mouseHideEnabled := !mouseHideEnabled
  433.     if (mouseHideEnabled)
  434.     {
  435.         MouseMove, 9999, A_ScreenHeight/2, 0
  436.         BlockInput, MouseMove
  437.     }
  438.     else
  439.     {
  440.         BlockInput, MouseMoveOff
  441.     }  
  442. return
  443.  
  444. ^F9::ToggleFakeFullscreen()
  445. ^F10::joyStickX := 16384
  446. ^F11::
  447.     markerEnabled := !markerEnabled
  448.     if (markerEnabled = true)
  449.     {
  450.         WinSet, Top, , ahk_id %hwnd1%
  451.         WinSet, AlwaysOnTop, On, ahk_id %hwnd1%
  452.     }
  453. return
  454.  
  455. ^F12::ExitApp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement