Advertisement
wwww

ww AHK Tiling

Dec 22nd, 2012
2,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. [DESCRIPTION]
  4.  
  5. Mouse-heavy tiling script. A default grid is included, but I recommend modifying it to your needs.
  6.  
  7. [HOTKEYS]
  8.  
  9. ralt + mbutton : to drag windows according to best-guess
  10. ralt + rbutton on edges of window : to resize and snap according to grid or other windows
  11. ralt + rbutton on middle of window : to move and snap according to grid or other windows
  12. ralt + lbutton : for quick colour-picker/pixel ruler
  13. ctrl + alt + a : minimize active window
  14. ctrl + alt + w : close active window
  15.  
  16.  
  17.  
  18. [PREPARATION]
  19.  
  20. The script needs an image called 000000.png in the same directory as the script for the grid to work.
  21. The image I'm using is a 1x1 pixel with the colour of #000, but any image can theoretically be used.
  22. If you do not wish to prepare an image, change UseGrid (below) to 0.
  23.  
  24.  
  25. [CONFIGURATION]
  26.  
  27. Imagine the tiling grid that you wish to create, and the lines that make it up.
  28. For each vertical line starting from the left-most of your screen (IMPORTANT to be in ASCENDING ORDER),
  29. type the following on one line in the variable VerticalLines below:
  30.  
  31. x-pos of line,
  32. index of horizontal line representing top edge (ignore first if you don't understand),
  33. index of horizontal line representing bottom edge (ignore first if you don't understand),
  34. corner?, (1 if the line is at the edge of the screen, 0 otherwise (i.e. you want double the amount of margin border))
  35. resize-only?, (1 if you want the line to be of lesser priority when selecting best-fit-tile, 0 otherwise)
  36.  
  37. After that, repeat the steps for each horizontal line:
  38.  
  39. y-pos of line,
  40. index of vertical line representing left edge (refer to VerticalLines, the first line is index 1 and the nth line is index n)
  41. index of vertical line representing right edge (refer to VerticalLines, the first line is index 1 and the nth line is index n)
  42. corner?, (1 if the line is at the edge of the screen, 0 otherwise (i.e. you want double the amount of margin border))
  43. resize-only?, (1 if you want the line to be of lesser priority when selecting best-fit-tile, 0 otherwise)
  44.  
  45. The syntax is the same as in VerticalLines above.
  46. Remember to finish up VerticalLines with the horizontal line indices after completing this step.
  47.  
  48. An example grid would be this:
  49.  
  50. V[1] := [ 0,    1, 3, 1, 0 ]
  51. V[2] := [ 1440, 1, 3, 0, 0 ]
  52. V[3] := [ 1920, 1, 3, 1, 0 ]
  53.  
  54. H[1] := [ 0,     1, 3, 1, 0 ]
  55. H[2] := [ 540,   2, 3, 0, 0 ]
  56. H[3] := [ 1080,  1, 3, 1, 0 ]
  57.  
  58. And this would represent a grid that looks something like this
  59.  
  60. +-----+-+
  61. |     | |
  62. |     +-+
  63. |     | |
  64. +-----+-+
  65.  
  66. */
  67.  
  68. SysGet, ScreenX, 76 ; SM_XVIRTUALSCREEN
  69. SysGet, ScreenY, 77 ; SM_YVIRTUALSCREEN
  70. SysGet, ScreenW, 78 ; SM_CXVIRTUALSCREEN
  71. SysGet, ScreenH, 79 ; SM_CYVIRTUALSCREEN
  72. MarginWidth := 16
  73. MarginWidthHalf := MarginWidth//2
  74. SnapDistance := 32
  75. MinimumMovement := 5
  76. GridColor := "FFFFFF"
  77. UseGrid := 0
  78. UseColourPicker := 0
  79. UseExceptions := 0
  80.  
  81. ; V[n]:= [ x-coord,   y0-index, y1-index, corner?, resize-only?]
  82. ; you can use numbers as well, this is just an example.
  83. V := []
  84. V[1] := [ ScreenX,                  1,  5,  1, 0 ]
  85. V[2] := [ ScreenX+ScreenW/4,        2,  5,  0, 0 ]
  86. V[3] := [ ScreenX+ScreenW/2,        2,  5,  0, 0 ]
  87. V[4] := [ ScreenX+ScreenW/4*3,      1,  5,  0, 0 ]
  88. V[5] := [ ScreenX+ScreenW,          1,  5,  1, 0 ]
  89.  
  90. H := []
  91. H[1] := [ ScreenY,                                                         1,  5,  1, 0 ]
  92. H[2] := [ ScreenY+(ScreenW/4*3-MarginWidthHalf*2)/16*9+MarginWidthHalf*2,  1,  4,  0, 1 ]
  93. H[3] := [ ScreenY+ScreenW/4,                                               4,  5,  0, 1 ]
  94. H[4] := [ ScreenY+ScreenW/4+(ScreenH-ScreenW/4)/2,                         4,  5,  0, 1 ]
  95. H[5] := [ ScreenY+ScreenH,                                                 1,  5,  1, 0 ]
  96.  
  97. GoSub, Initialise
  98.  
  99. ; Hotkeys~
  100.  
  101. !+c::WinClose, A
  102. !+a::WinMinimize, A
  103. !+s::
  104.     WinGet, MinMax, MinMax, A
  105.     If (MinMax = 0)
  106.         WinMaximize, A
  107.     Else
  108.         WinRestore, A
  109. Return
  110.  
  111. NumpadDiv::ShowGrid()
  112. NumpadDiv Up::HideGrid()
  113.  
  114. NumpadClear::MoveWindowToTile("A", 4, 7, 1, 5)
  115. NumpadUp::
  116. NumpadDown::
  117.     Left := 3, Right := 4
  118.     If (GetKeyState("NumpadUp", "P"))
  119.         Left := 2, Bottom := 3
  120.     If (GetKeyState("NumpadDown", "P"))
  121.         Right := 4
  122.     MoveWindowToTile("A", Left, Right, 6, 10)
  123. Return
  124.  
  125. NumpadPgUp::
  126. NumpadRight::
  127. NumpadPgDn::
  128.     Top := 3, Bottom := 5
  129.     If (GetKeyState("NumpadPgUp", "P"))
  130.         Top := 1, Bottom := 2
  131.     If (GetKeyState("NumpadRight", "P"))
  132.         Top := Top = 1 ? 1 : 2, Bottom := 3
  133.     If (GetKeyState("NumpadPgDn", "P"))
  134.         Top := Top = 1 ? 1 : Top = 2 ? 2 : 3, Bottom := 5
  135.     MoveWindowToTile("A", 7, 8, Top, Bottom)
  136. Return
  137.  
  138. NumpadHome::
  139. NumpadLeft::
  140. NumpadEnd::
  141.     Top := 9, Bottom := 10
  142.     If (GetKeyState("NumpadHome", "P"))
  143.         Top := 6, Bottom := 7
  144.     If (GetKeyState("NumpadLeft", "P"))
  145.         Top := Top = 6 ? 6 : 7, Bottom := 9
  146.     If (GetKeyState("NumpadEnd", "P"))
  147.         Top := Top = 6 ? 6 : Top = 7 ? 7 : 9, Bottom := 10
  148.     MoveWindowToTile("A", 1, 2, Top, Bottom)
  149. Return
  150.  
  151. ; DO NOT CHANGE ANYTHING BELOW THIS LINE
  152. Initialise:
  153. CoordMode, Mouse, Screen
  154. CoordMode, Pixel, Screen
  155. #SingleInstance, Force
  156. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  157. ;#Warn  ; Recommended for catching common errors.
  158. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  159. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  160. SetBatchLines, -1
  161. SetWinDelay, -1
  162.  
  163. Grid := CreateGrid()
  164. Return
  165.  
  166.  
  167. ; Color Picker + Ruler
  168. #If, UseColourPicker
  169. >!LButton::
  170.     If (GetKeyState("RButton", "P") or GetKeyState("MButton", "P"))
  171.         Return
  172.     MouseGetPos, X, Y
  173.     If (!OldX)
  174.     {
  175.         OldX := X, OldY := Y
  176.     }
  177.     Length := X - OldX
  178.     Height := Y - OldY
  179.     D := round(sqrt(Length*Length + Height*Height), 1)
  180.     OldX := X
  181.     OldY := Y
  182.     PixelGetColor, Color, X, Y, Slow|RGB
  183.     Color := SubStr(Color, 3)
  184.     Clipboard := Color
  185.     R := "0x" . SubStr(Color, 1, 2)
  186.     R += 0
  187.     G := "0x" . SubStr(Color, 3, 2)
  188.     G += 0
  189.     B := "0x" . SubStr(Color, 5, 2)
  190.     B += 0
  191.     ToolTip(Color . "`tX: " . X . "`nR: " . R . "`tY: " . Y . "`nG: " . G . "`tL: " . Length . "`nB: " . B . "`tH: " . Height . "`n`tD: " . D, 5000)
  192. Return
  193. #If
  194.  
  195. ToolTip(text, duration = 2000)
  196. {
  197.     ToolTip, % text
  198.     SetTimer, RemoveToolTip, % -duration
  199. }
  200. RemoveToolTip:
  201.    ToolTip
  202. Return
  203.  
  204. GetLinePos(type, index)
  205. {
  206.     Global
  207.     Corner := %type%[index][4]
  208.     Resize := %type%[index][5]
  209.     If (type = "V")
  210.     {
  211.         Y0 := H[V[index][2]][1]
  212.         Y1 := H[V[index][3]][1]
  213.         Return V[index][1]
  214.     }
  215.     Else If (type = "H")
  216.     {
  217.         X0 := V[H[index][2]][1]
  218.         X1 := V[H[index][3]][1]
  219.         Return H[index][1]
  220.     }
  221. }
  222.  
  223. GetLeftLine(MouseX, MouseY, Best=0)
  224. {
  225.     Global
  226.     Loop % V.MaxIndex()
  227.     {
  228.         Index := V.MaxIndex() - A_Index + 1
  229.         X := GetLinePos("V", Index)
  230.         If (Best and Resize)
  231.             Continue
  232.         If (MouseY >= Y0 and MouseY <= Y1)
  233.         {
  234.             If (MouseX > X)
  235.             {
  236.                 Return Index
  237.             }
  238.         }
  239.     }
  240.     Return 0
  241. }
  242.  
  243. GetRightLine(MouseX, MouseY, Best=0)
  244. {
  245.     Global
  246.     Loop % V.MaxIndex()
  247.     {
  248.         X := GetLinePos("V", A_Index)
  249.         If (Best and Resize)
  250.             Continue
  251.         If (MouseY >= Y0 and MouseY <= Y1)
  252.         {
  253.             If (MouseX < X)
  254.             {
  255.                 Return A_Index
  256.             }
  257.         }
  258.     }
  259.     Return 0
  260. }
  261.  
  262. GetTopLine(MouseX, MouseY, Best=0)
  263. {
  264.     Global
  265.     Loop % H.MaxIndex()
  266.     {
  267.         Index := H.MaxIndex() - A_Index + 1
  268.         Y := GetLinePos("H", Index)
  269.         If (Best and Resize)
  270.             Continue
  271.         If (MouseX >= X0 and MouseX <= X1)
  272.         {
  273.             If (MouseY > Y)
  274.             {
  275.                 Return Index
  276.             }
  277.         }
  278.     }
  279.     Return 0
  280. }
  281.  
  282. GetBottomLine(MouseX, MouseY, Best=0)
  283. {
  284.     Global
  285.     Loop % H.MaxIndex()
  286.     {
  287.         Y := GetLinePos("H", A_Index)
  288.         If (Best and Resize)
  289.             Continue
  290.         If (MouseX >= X0 and MouseX <= X1)
  291.         {
  292.             If (MouseY < Y)
  293.             {
  294.                 Return A_Index
  295.             }
  296.         }
  297.     }
  298.     Return 0
  299. }
  300.  
  301. GetLeftEdge(MouseX, MouseY, CurrentWindow)
  302. {
  303.     Global SnapDistance, ScreenX, MarginWidth
  304.     If (abs(ScreenX + MarginWidth - MouseX) < SnapDistance)
  305.         Return ScreenX + MarginWidth
  306.  
  307.     WinGetPos, CurrWinX, CurrWinY, CurrWinW, CurrWinH, % "ahk_id" . CurrentWindow
  308.     CurrWinB := CurrWinY + CurrWinH
  309.     Found := 0
  310.     BestDistance := SnapDistance
  311.     WinGet, id, list,,, Program Manager
  312.     Loop, %id%
  313.     {
  314.         If (id%A_Index% = CurrentWindow)
  315.             Continue
  316.         WinGetPos, WinX, WinY, WinW, WinH, % "ahk_id" . id%A_Index%
  317.         If (CurrWinY > WinY + WinH + SnapDistance or CurrWinB + SnapDistance < WinY)
  318.             Continue
  319.         NewDistance := abs(WinX - MouseX)
  320.         If (NewDistance < BestDistance)
  321.         {
  322.             Found := 1
  323.             Pos := WinX
  324.             BestDistance := NewDistance
  325.         }
  326.         WinRight := WinX + WinW
  327.         NewDistance := abs(WinRight + MarginWidth - MouseX)
  328.         If (NewDistance < BestDistance)
  329.         {
  330.             Found := 1
  331.             Pos := WinRight + MarginWidth
  332.             BestDistance := NewDistance
  333.         }
  334.     }
  335.     Return Found ? Pos : MouseX
  336. }
  337.  
  338. GetRightEdge(MouseX, MouseY, CurrentWindow)
  339. {
  340.     Global SnapDistance, ScreenX, ScreenW, MarginWidth
  341.     If (abs(ScreenX + ScreenW - MarginWidth - MouseX) < SnapDistance)
  342.         Return ScreenX + ScreenW - MarginWidth
  343.  
  344.     WinGetPos, CurrWinX, CurrWinY, CurrWinW, CurrWinH, % "ahk_id" . CurrentWindow
  345.     CurrWinB := CurrWinY + CurrWinH
  346.     Found := 0
  347.     BestDistance := SnapDistance
  348.     WinGet, id, list,,, Program Manager
  349.     Loop, %id%
  350.     {
  351.         If (id%A_Index% = CurrentWindow)
  352.             Continue
  353.         WinGetPos, WinX, WinY, WinW, WinH, % "ahk_id" . id%A_Index%
  354.         If (CurrWinY > WinY + WinH + SnapDistance or CurrWinB + SnapDistance < WinY)
  355.             Continue
  356.         NewDistance := abs(WinX - MarginWidth - MouseX)
  357.         If (NewDistance < BestDistance)
  358.         {
  359.             Found := 1
  360.             Pos := WinX - MarginWidth
  361.             BestDistance := NewDistance
  362.         }
  363.         WinRight := WinX + WinW
  364.         NewDistance := abs(WinRight  - MouseX)
  365.         If (NewDistance < BestDistance)
  366.         {
  367.             Found := 1
  368.             Pos := WinRight
  369.             BestDistance := NewDistance
  370.         }
  371.     }
  372.     Return Found ? Pos : MouseX
  373. }
  374.  
  375. GetTopEdge(MouseX, MouseY, CurrentWindow)
  376. {
  377.     Global SnapDistance, ScreenY, MarginWidth
  378.     If (abs(ScreenY + MarginWidth - MouseY) < SnapDistance)
  379.         Return ScreenY + MarginWidth
  380.  
  381.     WinGetPos, CurrWinX, CurrWinY, CurrWinW, CurrWinH, % "ahk_id" . CurrentWindow
  382.     CurrWinR := CurrWinX + CurrWinW
  383.     Found := 0
  384.     BestDistance := SnapDistance
  385.     WinGet, id, list,,, Program Manager
  386.     Loop, %id%
  387.     {
  388.         If (id%A_Index% = CurrentWindow)
  389.             Continue
  390.         WinGetPos, WinX, WinY, WinW, WinH, % "ahk_id" . id%A_Index%
  391.         If (CurrWinX > WinX + WinW + SnapDistance or CurrWinR + SnapDistance < WinX)
  392.             Continue
  393.         NewDistance := abs(WinY - MouseY)
  394.         If (NewDistance < BestDistance)
  395.         {
  396.             Found := 1
  397.             Pos := WinY
  398.             BestDistance := NewDistance
  399.         }
  400.         WinBottom := WinY + WinH
  401.         NewDistance := abs(WinBottom + MarginWidth - MouseY)
  402.         If (NewDistance < BestDistance)
  403.         {
  404.             Found := 1
  405.             Pos := WinBottom + MarginWidth
  406.             BestDistance := NewDistance
  407.         }
  408.     }
  409.     Return Found ? Pos : MouseY
  410. }
  411.  
  412. GetBottomEdge(MouseX, MouseY, CurrentWindow)
  413. {
  414.     Global SnapDistance, ScreenY, ScreenH, MarginWidth
  415.     If (abs(ScreenY + ScreenH - MarginWidth - MouseY) < SnapDistance)
  416.         Return ScreenY + ScreenH - MarginWidth
  417.  
  418.     WinGetPos, CurrWinX, CurrWinY, CurrWinW, CurrWinH, % "ahk_id" . CurrentWindow
  419.     CurrWinR := CurrWinX + CurrWinW
  420.     Found := 0
  421.     BestDistance := SnapDistance
  422.     WinGet, id, list,,, Program Manager
  423.     Loop, %id%
  424.     {
  425.         If (id%A_Index% = CurrentWindow)
  426.             Continue
  427.         WinGetPos, WinX, WinY, WinW, WinH, % "ahk_id" . id%A_Index%
  428.         If (CurrWinX > WinX + WinW + SnapDistance or CurrWinR + SnapDistance < WinX)
  429.             Continue
  430.         NewDistance := abs(WinY - MarginWidth - MouseY)
  431.         If (NewDistance < BestDistance)
  432.         {
  433.             Found := 1
  434.             Pos := WinY - MarginWidth
  435.             BestDistance := NewDistance
  436.         }
  437.         WinBottom := WinY + WinH
  438.         NewDistance := abs(WinBottom  - MouseY)
  439.         If (NewDistance < BestDistance)
  440.         {
  441.             Found := 1
  442.             Pos := WinBottom
  443.             BestDistance := NewDistance
  444.         }
  445.     }
  446.     Return Found ? Pos : MouseY
  447. }
  448.  
  449. GetBestTile(MouseX0, MouseY0)
  450. {
  451.     Global Left, Right, Top, Bottom
  452.     Left := GetLeftLine(MouseX0, MouseY0, 1)
  453.     Right := GetRightLine(MouseX0, MouseY0, 1)
  454.     Top := GetTopLine(MouseX0, MouseY0, 1)
  455.     Bottom := GetBottomLine(MouseX0, MouseY0, 1)
  456.  
  457.     If (Left and Right and Top and Bottom)
  458.         Return 1
  459.     Else
  460.         Return 0
  461. }
  462.  
  463. GetSmallestTile(MouseX0, MouseY0, MouseX1="", MouseY1="")
  464. {
  465.     If (!MouseX1)
  466.     {
  467.         MouseX1 := MouseX0, MouseY1 := MouseY0
  468.     }
  469.     Else
  470.     {
  471.         ; Flip the positions if X0 > X1
  472.         If (MouseX0 > MouseX1)
  473.         {
  474.             Temp := MouseX0
  475.             MouseX0 := MouseX1
  476.             MouseX1 := Temp
  477.             Temp := MouseY0
  478.             MouseY0 := MouseY1
  479.             MouseY1 := Temp
  480.         }
  481.         ; Determine if Y is decreasing (1) or increasing (0)
  482.         Inverted := 0
  483.         If (MouseY0 > MouseY1)
  484.         {
  485.             Inverted = 1
  486.         }
  487.     }
  488.  
  489.     Global Left, Right, Top, Bottom
  490.     Left := GetLeftLine(MouseX0, MouseY0)
  491.     Right := GetRightLine(MouseX1, MouseY1)
  492.     Top := GetTopLine(Inverted ? MouseX1 : MouseX0, Inverted ? MouseY1 : MouseY0)
  493.     Bottom := GetBottomLine(Inverted ? MouseX0 : MouseX1, Inverted ? MouseY0 : MouseY1)
  494.  
  495.     If (Left and Right and Top and Bottom)
  496.         Return 1
  497.     Else
  498.         Return 0
  499. }
  500.  
  501. GetNearbyLine(type, MouseX, MouseY, distance = 0, Best = 0)
  502. {
  503.     Global
  504.     distance := distance ? distance : SnapDistance
  505.     If (type = "V")
  506.     {
  507.         Loop % V.MaxIndex()
  508.         {
  509.             X := GetLinePos("V", A_Index)
  510.             If (Best and Resize)
  511.                 Continue
  512.             If (MouseY >= Y0 and MouseY <= Y1)
  513.             {
  514.                 If (abs(MouseX - X) <= distance)
  515.                 {
  516.                     Return A_Index
  517.                 }
  518.             }
  519.         }
  520.     }
  521.     Else
  522.     {
  523.         Loop % H.MaxIndex()
  524.         {
  525.             Y := GetLinePos("H", A_Index)
  526.             If (Best and Resize)
  527.                 Continue
  528.             If (MouseX >= X0 and MouseX <= X1)
  529.             {
  530.                 If (abs(MouseY - Y) <= distance)
  531.                 {
  532.                     Return A_Index
  533.                 }
  534.             }
  535.         }
  536.     }
  537. }
  538.  
  539. ; 0 1 2
  540. ; 3 4 5
  541. ; 6 7 8
  542. GetOctant(X, Y, WinX, WinY, WinW, WinH)
  543. {
  544.     ScaledX := (X - WinX) * 3 // WinW
  545.     ScaledY := (Y - WinY) * 3 // WinH
  546.     Return ScaledX + ScaledY * 3
  547. }
  548.  
  549. MoveWindowRelative(Title, X=0, Y=0, W=0, H=0)
  550. {
  551.     WinGetPos, WinX, WinY, WinW, WinH, % Title
  552.     WinMove, % Title,, % WinX + X, % WinY + Y, % WinW + W, % WinH + H
  553. }
  554.  
  555. MoveWindowToTile(Title, Left, Right, Top, Bottom)
  556. {
  557.     Global
  558.     If (UseExceptions and Left = 4 and Right = 7 and Top = 1 and Bottom = 5)
  559.     {
  560.         Local X := V[Left][1]
  561.         Local Y := H[Top][1]
  562.         WinMove, % Title,
  563.             , % X
  564.             , % Y
  565.             , % V[Right][1] - X - MarginWidthHalf
  566.             , % H[Bottom][1] - Y - 2
  567.     }
  568.     Else
  569.     {
  570.         Local X := V[Left][1] + (V[Left][4] ? MarginWidth : MarginWidthHalf)
  571.         Local Y := H[Top][1] + (H[Top][4] ? MarginWidth : MarginWidthHalf)
  572.         WinMove, % Title,
  573.             , % X
  574.             , % Y
  575.             , % V[Right][1] - X - (V[Right][4] ? MarginWidth : MarginWidthHalf)
  576.             , % H[Bottom][1] - Y  - (H[Bottom][4] ? MarginWidth : MarginWidthHalf)
  577.     }
  578. }
  579.  
  580. CreateGrid()
  581. {
  582.     Global
  583.     If (!UseGrid)
  584.         Return
  585.     Local LineWidth := MarginWidth = 0 ? 2 : MarginWidth
  586.     Gui, Color, % GridColor
  587.     Gui, +LastFound
  588.     WinSet, TransColor, % GridColor
  589.     WinSet, Transparent, 64
  590.     Gui, +Owner +AlwaysOnTop -Resize -SysMenu -MinimizeBox -MaximizeBox -Disabled -Caption -Border -ToolWindow
  591.     Loop, % V.MaxIndex()
  592.     {
  593.         Local X := GetLinePos("V", A_Index) - ScreenX   ; to convert the position to positive
  594.         Local Y := Y0 - ScreenY                         ; to convert the position to positive
  595.         Local Height := Y1 - Y0
  596.         If (Corner)
  597.             Gui, Add, Picture, % "x" . X-LineWidth . " y" . Y . " w" . LineWidth*2 . " h" . Height, 000000.png
  598.         Else
  599.             Gui, Add, Picture, % "x" . X-LineWidth//2 . " y" . Y . " w" . LineWidth . " h" . Height, 000000.png
  600.     }
  601.     Loop, % H.MaxIndex()
  602.     {
  603.         Local Y := GetLinePos("H", A_Index) - ScreenY   ; to convert the position to positive
  604.         Local X := X0 - ScreenX                         ; to convert the position to positive
  605.         Local Width := X1 - X0
  606.         If (Corner)
  607.             Gui, Add, Picture, % "x" . X . " y" . Y-LineWidth . " w" . Width . " h" . LineWidth*2, 000000.png
  608.         Else
  609.             Gui, Add, Picture, % "x" . X . " y" . Y-LineWidth//2 . " w" . Width . " h" . LineWidth, 000000.png
  610.     }
  611. }
  612.  
  613. ShowGrid()
  614. {
  615.     Global UseGrid
  616.     If (!UseGrid)
  617.         Return
  618.     Global ScreenX, ScreenY, ScreenW, ScreenH
  619.     Gui, Show, % "x" . ScreenX . " y" . ScreenY . " w" . ScreenW . " h" . ScreenH
  620. }
  621.  
  622. HideGrid()
  623. {
  624.     Global UseGrid
  625.     If (!UseGrid)
  626.         Return
  627.     Gui, Hide
  628. }
  629.  
  630. ; Move Window (Pixel Perfect)
  631. ; [Shift [Contro]] RAlt + Up/Down/Left/Right
  632. >!Up::MoveWindowRelative("A", 0, -1),Return
  633. >!Down::MoveWindowRelative("A", 0, 1),Return
  634. >!Left::MoveWindowRelative("A", -1, 0),Return
  635. >!Right::MoveWindowRelative("A", 1, 0),Return
  636. +>!Up::MoveWindowRelative("A", 0, -1, 0, 1),Return
  637. +>!Down::MoveWindowRelative("A", 0, 0, 0, 1),Return
  638. +>!Left::MoveWindowRelative("A", -1, 0, 1, 0),Return
  639. +>!Right::MoveWindowRelative("A", 0, 0, 1, 0),Return
  640. ^+>!Up::MoveWindowRelative("A", 0, 0, 0, -1),Return
  641. ^+>!Down::MoveWindowRelative("A", 0, 1, 0, -1),Return
  642. ^+>!Left::MoveWindowRelative("A", 0, 0, -1, 0),Return
  643. ^+>!Right::MoveWindowRelative("A", 1, 0, -1, 0),Return
  644.  
  645. ; Move Window
  646. +>!MButton::
  647. >!MButton::
  648.     MouseGetPos, X, Y, WinId
  649.     OldX := X, OldY := Y
  650.     OrigX := X, OrigY := Y
  651.  
  652.     ; Don't do anything until button is released OR mouse moves
  653.     While 1
  654.     {
  655.         Sleep, 10
  656.         MouseGetPos, X, Y
  657.         If (abs(OrigX - X) > MinimumMovement or abs(OrigY - Y) > MinimumMovement)
  658.             Break
  659.         If (!GetKeyState("MButton", "P"))
  660.             Return
  661.     }
  662.  
  663.     WinTitle := "ahk_id " . WinId
  664.     WinGet, MinMax, MinMax, % WinTitle
  665.     If (MinMax = 1)
  666.         WinRestore, % WinTitle
  667.     WinGetPos, WinX, WinY, WinW, WinH, % WinTitle
  668.     OffsetX := X - WinX, OffsetY := Y - WinY
  669.  
  670.     WinSet, AlwaysOnTop, On, % WinTitle
  671.     WinSet, Transparent, 212, % WinTitle
  672.     ShowGrid()
  673.  
  674.     MultipleTiles := 0
  675.     While (GetKeyState("MButton", "P"))
  676.     {
  677.         If (!MultipleTiles)
  678.         {
  679.             If (GetKeyState("LButton", "P"))
  680.             {
  681.                 MultipleTiles := 1
  682.             }
  683.             Else
  684.             {
  685.                 MouseGetPos, X, Y
  686.                 If (GetBestTile(X, Y))
  687.                 {
  688.                     MoveWindowToTile(WinTitle, Left, Right, Top, Bottom)
  689.                 }
  690.             }
  691.         }
  692.         Else If (GetKeyState("LButton", "P"))
  693.         {
  694.             MouseGetPos, MouseX0, MouseY0
  695.             While (GetKeyState("LButton", "P"))
  696.             {
  697.                 MouseGetPos, MouseX1, MouseY1
  698.                 If (GetSmallestTile(MouseX0, MouseY0, MouseX1, MouseY1))
  699.                 {
  700.                     MoveWindowToTile(WinTitle, Left, Right, Top, Bottom)
  701.                 }
  702.             }
  703.         }
  704.         Sleep, 10
  705.     }
  706.  
  707.     If (MinMax = 1)
  708.         WinMaximize, % WinTitle
  709.     WinSet, AlwaysOnTop, Off, % WinTitle
  710.     WinSet, Transparent, Off, % WinTitle
  711.     WinActivate, % WinTitle
  712.     HideGrid()
  713. Return
  714.  
  715. >!RButton::
  716.     MouseGetPos, X, Y, WinId
  717.     OrigX := X, OrigY := Y
  718.  
  719.     ; Don't do anything until button is released OR mouse moves
  720.     While 1
  721.     {
  722.         Sleep, 10
  723.         MouseGetPos, X, Y
  724.         If (abs(OrigX - X) > MinimumMovement or abs(OrigY - Y) > MinimumMovement)
  725.         {
  726.             Break
  727.         }
  728.         If (!GetKeyState("RButton", "P"))
  729.         {
  730.             Return
  731.         }
  732.     }
  733.  
  734.     WinTitle := "ahk_id " . WinId
  735.     WinGet, MinMax, MinMax, % WinTitle
  736.     If (MinMax = 1)
  737.         WinRestore, % WinTitle
  738.     WinGetPos, WinX, WinY, WinW, WinH, % WinTitle
  739.     Octant := GetOctant(X, Y, WinX, WinY, WinW, WinH)
  740.  
  741.     WinSet, AlwaysOnTop, On, % WinTitle
  742.     WinSet, Transparent, 212, % WinTitle
  743.     ShowGrid()
  744.  
  745.     MultipleTiles := 0
  746.     Snap := 0
  747.     While (GetKeyState("RButton", "P"))
  748.     {
  749.         MouseGetPos, MouseX, MouseY
  750.         If (!MultipleTiles)
  751.         {
  752.             If (Octant = 4)
  753.             {
  754.                 If (GetKeyState("LButton", "P"))
  755.                 {
  756.                     MultipleTiles := 1
  757.                     Continue
  758.                 }
  759.                 Else
  760.                 {
  761.                     NewX := MouseX - OrigX + WinX
  762.                     NewY := MouseY - OrigY + WinY
  763.                     If (!GetKeyState("Shift", "P"))
  764.                     {
  765.                         NewNewX := GetLeftEdge(NewX, NewY, WinId)
  766.                         If (NewNewX = NewX)
  767.                         {
  768.                             NewNewX := GetRightEdge(NewX + WinW, NewY, WinId) - WinW
  769.                             If (NewNewX = NewX)
  770.                             {
  771.                                 NearbyLine := GetNearbyLine("V", NewX, NewY + WinH//2)
  772.                                 If (NearbyLine)
  773.                                     NewX := GetLinePos("V", NearbyLine) + (Corner ? MarginWidth : MarginWidthHalf)
  774.                                 Else
  775.                                 {
  776.                                     NearbyLine := GetNearbyLine("V", NewX + WinW, MouseY - OrigY + WinY + WinH//2)
  777.                                     If (NearbyLine)
  778.                                         NewX := GetLinePos("V", NearbyLine) - (Corner ? MarginWidth : MarginWidthHalf) - WinW
  779.                                 }
  780.                             }
  781.                             Else
  782.                                 NewX := NewNewX
  783.                         }
  784.                         Else
  785.                             NewX := NewNewX
  786.                         NewNewY := GetTopEdge(NewX, NewY, WinId)
  787.                         If (NewNewY = NewY)
  788.                         {
  789.                             NewNewY := GetBottomEdge(NewX, NewY + WinH, WinId) - WinH
  790.                             If (NewNewY = NewY)
  791.                             {
  792.                                 NearbyLine := GetNearbyLine("H", NewX + WinW//2, NewY)
  793.                                 If (NearbyLine)
  794.                                     NewY := GetLinePos("H", NearbyLine) + (Corner ? MarginWidth : MarginWidthHalf)
  795.                                 Else
  796.                                 {
  797.                                     NearbyLine := GetNearbyLine("H", NewX + WinW//2, NewY + WinH)
  798.                                     If (NearbyLine)
  799.                                         NewY := GetLinePos("H", NearbyLine) - (Corner ? MarginWidth : MarginWidthHalf) - WinH
  800.                                 }
  801.                             }
  802.                             Else
  803.                                 NewY := NewNewY
  804.                         }
  805.                         Else
  806.                             NewY := NewNewY
  807.                     }
  808.                     WinMove, % WinTitle,, % NewX, % NewY
  809.                 }
  810.             }
  811.             Else
  812.             {
  813.                 If (mod(Octant, 3) = 0)
  814.                 {
  815.                     If (GetKeyState("LButton", "P") or Snap)
  816.                     {
  817.                         Snap := 1
  818.                         Edge := GetLeftLine(MouseX, MouseY)
  819.                         If (Edge)
  820.                         {
  821.                             LinePos := GetLinePos("V", Edge)
  822.                             NewX := LinePos + (Corner ? MarginWidth : MarginWidthHalf)
  823.                             WinMove, % WinTitle,, % NewX,, % WinX - NewX + WinW
  824.                         }
  825.                     }
  826.                     Else
  827.                     {
  828.                         NewX := MouseX - OrigX + WinX
  829.                         If (!GetKeyState("Shift", "P"))
  830.                         {
  831.                             NewNewX := GetLeftEdge(NewX, MouseY, WinId)
  832.                             If (NewNewX = NewX)
  833.                             {
  834.                                 NearbyLine := GetNearbyLine("V", NewX - (Corner ? MarginWidth : MarginWidthHalf), MouseY)
  835.                                 If (NearbyLine)
  836.                                     NewX := GetLinePos("V", NearbyLine) + (Corner ? MarginWidth : MarginWidthHalf)
  837.                             }
  838.                             Else
  839.                                 NewX := NewNewX
  840.                         }
  841.                         WinMove, % WinTitle,, % NewX,, % WinX - NewX + WinW
  842.                     }
  843.                 }
  844.                 Else If (mod(Octant, 3) = 2)
  845.                 {
  846.                     If (GetKeyState("LButton", "P") or Snap)
  847.                     {
  848.                         Snap := 1
  849.                         Edge := GetRightLine(MouseX, MouseY)
  850.                         If (Edge)
  851.                         {
  852.                             LinePos := GetLinePos("V", Edge)
  853.                             NewX := LinePos - (Corner ? MarginWidth : MarginWidthHalf)
  854.                             WinMove, % WinTitle,,,, % NewX - WinX
  855.                         }
  856.                     }
  857.                     Else
  858.                     {
  859.                         NewW := MouseX - OrigX + WinW
  860.                         If (!GetKeyState("Shift", "P"))
  861.                         {
  862.                             NewNewW := GetRightEdge(WinX + NewW, MouseY, WinId) - WinX
  863.                             If (NewNewW = NewW)
  864.                             {
  865.                                 NearbyLine := GetNearbyLine("V", NewW + WinX + (Corner ? MarginWidth : MarginWidthHalf), MouseY)
  866.                                 If (NearbyLine)
  867.                                     NewW := GetLinePos("V", NearbyLine) - (Corner ? MarginWidth : MarginWidthHalf) - WinX
  868.                             }
  869.                             Else
  870.                                 NewW := NewNewW
  871.                         }
  872.                         WinMove, % WinTitle,,,, % NewW
  873.                     }
  874.                 }
  875.                 If (Octant // 3 = 0)
  876.                 {
  877.                     If (GetKeyState("LButton", "P") or Snap)
  878.                     {
  879.                         Snap := 1
  880.                         Edge := GetTopLine(MouseX, MouseY)
  881.                         If (Edge)
  882.                         {
  883.                             LinePos := GetLinePos("H", Edge)
  884.                             NewY := LinePos + (Corner ? MarginWidth : MarginWidthHalf)
  885.                             WinMove, % WinTitle,,, % NewY,, % WinY - NewY + WinH
  886.                         }
  887.                     }
  888.                     Else
  889.                     {
  890.                         NewY := MouseY - OrigY + WinY
  891.                         If (!GetKeyState("Shift", "P"))
  892.                         {
  893.                             NewNewY := GetTopEdge(NewX, MouseY, WinId)
  894.                             If (NewNewY = NewY)
  895.                             {
  896.                                 NearbyLine := GetNearbyLine("H", MouseX, NewY - (Corner ? MarginWidth : MarginWidthHalf))
  897.                                 If (NearbyLine)
  898.                                     NewY := GetLinePos("H", NearbyLine) + (Corner ? MarginWidth : MarginWidthHalf)
  899.                             }
  900.                             Else
  901.                                 NewY := NewNewY
  902.                         }
  903.                         WinMove, % WinTitle,,, % NewY,, % WinY - NewY + WinH
  904.                     }
  905.                 }
  906.                 Else If (Octant // 3 = 2)
  907.                 {
  908.                     If (GetKeyState("LButton", "P") or Snap)
  909.                     {
  910.                         Snap := 1
  911.                         Edge := GetBottomLine(MouseX, MouseY)
  912.                         If (Edge)
  913.                         {
  914.                             LinePos := GetLinePos("H", Edge)
  915.                             NewY := LinePos - (Corner ? MarginWidth : MarginWidthHalf)
  916.                             WinMove, % WinTitle,,,,, % NewY - WinY
  917.                         }
  918.                     }
  919.                     Else
  920.                     {
  921.                         NewH := MouseY - OrigY + WinH
  922.                         If (!GetKeyState("Shift", "P"))
  923.                         {
  924.                             NewNewH := GetBottomEdge(MouseX, WinY + NewH, WinId) - WinY
  925.                             If (NewNewH = NewH)
  926.                             {
  927.                                 NearbyLine := GetNearbyLine("H", MouseX, NewH + WinY + (Corner ? MarginWidth : MarginWidthHalf))
  928.                                 If (NearbyLine)
  929.                                     NewH := GetLinePos("H", NearbyLine) - (Corner ? MarginWidth : MarginWidthHalf) - WinY
  930.                             }
  931.                             Else
  932.                                 NewH := NewNewH
  933.                         }
  934.                         WinMove, % WinTitle,,,,, % NewH
  935.                     }
  936.                 }
  937.             }
  938.         }
  939.         Else
  940.         {
  941.             If (GetKeyState("LButton", "P"))
  942.             {
  943.                 MouseGetPos, MouseX0, MouseY0
  944.                 While (GetKeyState("LButton", "P"))
  945.                 {
  946.                     MouseGetPos, MouseX1, MouseY1
  947.                     If (GetSmallestTile(MouseX0, MouseY0, MouseX1, MouseY1))
  948.                     {
  949.                         MoveWindowToTile(WinTitle, Left, Right, Top, Bottom)
  950.                     }
  951.                 }
  952.             }
  953.         }
  954.         Sleep, 10
  955.     }
  956.  
  957.     WinSet, AlwaysOnTop, Off, % WinTitle
  958.     WinSet, Transparent, Off, % WinTitle
  959.     WinActivate, % WinTitle
  960.     HideGrid()
  961. Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement