Advertisement
Guest User

ruler

a guest
Apr 19th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.19 KB | None | 0 0
  1. /*
  2. Name:RulerPP
  3. Version 1.1 (Mon March 02, 2015)
  4. Created: Sat January 31, 2015
  5. Author: tidbit
  6. Credit:
  7.  
  8. Hotkeys:
  9. esc --- Quit
  10.  
  11. Description:
  12. A multi-use ruler program. Designed using the worst code ever written.
  13.  
  14. TO DO:
  15. get Vertical fully working.
  16. */
  17.  
  18. ; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
  19. #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
  20. ; #Warn ; Enable warnings to assist with detecting common errors.
  21. SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
  22. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
  23. #SingleInstance, force
  24. #maxThreadsPerHotkey, 2
  25.  
  26. _name_=
  27. _version_=1.0 (date)
  28. _created_=date
  29. _author_=tidbit ([email protected])
  30. _credit_=
  31. _info_=
  32.  
  33. onexit, DIE
  34.  
  35.  
  36.  
  37. Width:=A_ScreenWidth
  38. Height:=A_ScreenHeight
  39. clickthrough:=0 ; not used right now
  40. snapping:=0 ; set to 0 for no snapping, otherwise the number you want it to snap to.
  41. ; wholeScreenTicks:=0 ; [NOT IMPLEMENTED. TOO LAZY TO FIX]
  42.  
  43. global vert:=0 ; sue me.
  44. global coordsRuler ; sue me again.
  45.  
  46. ; used for ruler mode
  47. ; [x, y, w, h, color, border color]
  48. ; leave border color blank for no border.
  49. coordsRuler:=[[7,250,250,50,0x77ff0000,0x77ffffff] ; main window
  50. ,[0,0,8,50,0x55ffffff,""] ; move bar
  51. ,[100,0,3,50,0xff00ffff,""] ; slider
  52. ,[242,0,8,50,0x55ffffff,""] ; resize bar
  53. ,[0,0,15,10,0x770000ff,""]] ; settings
  54.  
  55. ; coordsRuler:=[[270,250,5,100, 0x9900ffff]
  56. ; ,[250,250,width//2,100,0x33ff0000]]
  57.  
  58.  
  59.  
  60. ; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
  61. Gui, +hwndMYHWND -Caption +E0x80000 +AlwaysOnTop +ToolWindow +OwnDialogs
  62. ; Gui, +hwndMYHWND -Caption +E0x20 +E0x80000 +AlwaysOnTop +ToolWindow +OwnDialogs
  63.  
  64.  
  65. pToken:=Gdip_Startup()
  66. hbm :=CreateDIBSection(Width, Height)
  67. hdc :=CreateCompatibleDC()
  68. obm :=SelectObject(hdc, hbm)
  69. G :=Gdip_GraphicsFromHDC(hdc)
  70. Gdip_SetSmoothingMode(G, 4)
  71.  
  72.  
  73. ; WinSet_Click_Through(MYHWND)
  74. Gui, Show, NA
  75.  
  76. gosub, mainStuff
  77.  
  78.  
  79. return
  80.  
  81.  
  82. tooltipOff:
  83. toolTip
  84. return
  85.  
  86. /*
  87. ~lbutton up::
  88. mouseGetPos, x, y
  89. if ((x>=coordsRuler[5,1]+crx
  90. && x<=coordsRuler[5,1]+crx+coordsRuler[5,3])
  91. && (y>=coordsRuler[5,2]+cry
  92. && y<=coordsRuler[5,2]+cry+coordsRuler[5,4]))
  93. {
  94. clickthrough:=!clickthrough
  95. msgbox hi %clickthrough%
  96. ; coordsRuler[3,5]:=((clickthrough=1) ? 0xff0000ff ? 0xffff4400)
  97. ; Gui, % ((clickthrough=1) ? "+E0x20" : "-E0x20")
  98. ; drawControls(coordsRuler, G)
  99. ; drawTicks(crx, cry, 2, ((wholeScreenTicks=1) ? -1 : coordsRuler[2,4]), abs(arr[1]), tickCount, G)
  100. ; UpdateLayeredWindow(MYHWND, hdc, 0, 0, Width, Height)
  101. }
  102. return
  103. */
  104.  
  105. $~lbutton::
  106. mainStuff:
  107. ; critical
  108. ; needed for the first showing.
  109. tickCount:=coordsRuler[1,3]//coordsRuler[3,1]
  110. ; calculate the offset of where you clicked inside the control.
  111. ; that way it doesn't jump when you go to drag it.
  112. mouseGetPos, x, y
  113. crx:=coordsRuler[1,1]
  114. cry:=coordsRuler[1,2]
  115. offx:=x-crx ; calculate the offset of where you clicked inside the control
  116. offy:=y-cry ; calculate the offset of where you clicked inside the control
  117.  
  118. ; UIObj 1 = main window
  119. ; UIObj 2 = move
  120. ; UIObj 3 = slider
  121. ; UIObj 4 = resize
  122. ; UIObj 5 = button 1
  123. UIObj:=0 ; nothing
  124. for k, arr in coordsRuler
  125. {
  126. if (a_index=1)
  127. continue
  128.  
  129. if (vert=0)
  130. xoff:=arr[1], yoff:=arr[2], w:=arr[3], h:=arr[4]
  131. else
  132. xoff:=arr[2], yoff:=arr[1], w:=arr[4], h:=arr[3]
  133.  
  134.  
  135. if ((x>=xoff+crx && x<=xoff+w+crx)
  136. && (y>=yoff+cry && y<=yoff+h+cry))
  137. {
  138. UIObj:=k ; return the item clicked.
  139. ; break
  140. }
  141. }
  142. ; if (clickthrough=1)
  143. ; continue
  144. if (UIObj=5)
  145. vert:=!vert
  146.  
  147.  
  148. if (UIObj=2) ; move bar
  149. {
  150. while (getKeyState("Lbutton", "P"))
  151. {
  152. mouseGetPos, x, y
  153. coordsRuler[1,1]:=x-offx
  154. coordsRuler[1,2]:=y-offy
  155.  
  156. drawControls(coordsRuler, G)
  157. if (snapping>0)
  158. drawTicks(coordsRuler[1,1], coordsRuler[1,2], 1, coordsRuler[3,4], snapping, coordsRuler[1,3]//snapping, G, 0x66ffffff, 0)
  159. drawTicks(coordsRuler[1,1], coordsRuler[1,2], 2, ((wholeScreenTicks=1) ? -1 : coordsRuler[3,4]), abs(coordsRuler[3,1]), tickCount, G)
  160. UpdateLayeredWindow(MYHWND, hdc, 0, 0, Width, Height)
  161.  
  162. ; toolTip, % "x: " coordsRuler[1,1] "`n---`n" st_printArr(coordsRuler)
  163. }
  164. settimer, tooltipOff, -2000
  165. ; return
  166. }
  167.  
  168. if (UIObj=3) ; slider bar
  169. {
  170. if (vert=0)
  171. offx:=x-coordsRuler[3,1]
  172. , banana:=crx
  173. else
  174. offx:=x-coordsRuler[3,2]
  175. , banana:=cry
  176. while (getKeyState("Lbutton", "P"))
  177. {
  178. tickCount:=coordsRuler[1,3]//abs(coordsRuler[3,1])
  179. mouseGetPos, x, y
  180.  
  181. if (vert=1)
  182. {
  183. x:=y
  184. toolTip % abs(coordsRuler[3,1]) " (" round(coordsRuler[3,1]/coordsRuler[1,3]*100,1) "%)", % crx+coordsRuler[1,4], % coordsRuler[3,1]+cry
  185. }
  186. else
  187. toolTip % abs(coordsRuler[3,1]) " (" round(coordsRuler[3,1]/coordsRuler[1,3]*100,1) "%)", % coordsRuler[3,1]+crx, % cry-22
  188.  
  189. if (snapping>0)
  190. {
  191. ; toolTip, % crx " - " x-crx " - " mod(x-crx, snapping),,,2
  192. if (mod(x-banana, snapping)=0)
  193. coordsRuler[3,1]:=x-banana
  194. }
  195. else
  196. coordsRuler[3,1]:=x-banana
  197. ; tooltip, % x ", " tickCount "`n---`n" st_printArr(coordsRuler),,,2
  198.  
  199. if (coordsRuler[3,1]<=1)
  200. coordsRuler[3,1]:=1
  201. if (coordsRuler[3,1]>=coordsRuler[1,3])
  202. coordsRuler[3,1]:=coordsRuler[1,3]
  203.  
  204. drawControls(coordsRuler, G)
  205. if (snapping>0)
  206. drawTicks(crx, cry, 1, coordsRuler[3,4], snapping, coordsRuler[1,3]//snapping, G,0x66ffffff, 0)
  207. drawTicks(crx, cry, 2, ((wholeScreenTicks=1) ? -1 : coordsRuler[3,4]), abs(coordsRuler[3,1]), tickCount, G)
  208. UpdateLayeredWindow(MYHWND, hdc, 0, 0, Width, Height)
  209. }
  210. settimer, tooltipOff, -2000
  211. ; return
  212. }
  213.  
  214. if (UIObj=4) ; resize bar
  215. {
  216. ; if (vert=0)
  217. offw:=coordsRuler[1,3]
  218. offh:=coordsRuler[1,4]
  219. ; else
  220. ; offw:=coordsRuler[1,4]
  221. while (getKeyState("Lbutton", "P"))
  222. {
  223. mouseGetPos, x, y
  224. tickCount:=coordsRuler[1,3]//abs(coordsRuler[3,1])
  225.  
  226. if (vert=0)
  227. coordsRuler[1,3]:=x-offx+offw-crx
  228. else
  229. coordsRuler[1,3]:=y-cry
  230.  
  231. ; the border is 2px. 2*2=4. but then we need 1 more for some reason.
  232. coordsRuler[4,1]:=coordsRuler[1,3]-coordsRuler[3,3]-5
  233.  
  234. drawControls(coordsRuler, G)
  235. if (snapping>0)
  236. drawTicks(coordsRuler[1,1], coordsRuler[1,2], 1, coordsRuler[3,4], snapping, coordsRuler[1,3]//snapping, G, 0x66ffffff, 0)
  237. drawTicks(crx, cry, 2, ((wholeScreenTicks=1) ? -1 : coordsRuler[3,4]), abs(coordsRuler[3,1]), tickCount, G)
  238. UpdateLayeredWindow(MYHWND, hdc, 0, 0, Width, Height)
  239. }
  240. }
  241.  
  242. drawControls(coordsRuler, G)
  243.  
  244. if (snapping>0)
  245. drawTicks(coordsRuler[1,1], coordsRuler[1,2], 1, coordsRuler[2,4], snapping, coordsRuler[1,3]//snapping, G, 0x66ffffff, 0)
  246. ; msgbox % coordsRuler[1,2]
  247. drawTicks(coordsRuler[1,1], coordsRuler[1,2], 2, ((wholeScreenTicks=1) ? -1 : coordsRuler[3,4]), abs(coordsRuler[3,1]), tickCount, G)
  248.  
  249.  
  250. ; drawTicks(crx
  251. ; , cry
  252. ; , 2
  253. ; , ((wholeScreenTicks=1) ? -1 : coordsRuler[3,4])
  254. ; , abs(coordsRuler[3,1])
  255. ; , tickCount, G)
  256.  
  257.  
  258. UpdateLayeredWindow(MYHWND, hdc, 0, 0, Width, Height)
  259. Gdip_DeleteBrush(pBrush)
  260. return
  261.  
  262. /*
  263. j::
  264. ; random, r, 40,250
  265. ; coordsRuler[2,1]:=r
  266. ; drawControls(coordsRuler, G)
  267. ; drawTicks(coordsRuler[1,1], coordsRuler[1,2], 2, ((wholeScreenTicks=1) ? -1 : coordsRuler[2,4]), abs(coordsRuler[2,1]), coordsRuler[1,3]//coordsRuler[2,1], G)
  268. ; UpdateLayeredWindow(MYHWND, hdc, 0, 0, Width, Height)
  269. tooltip % "aaaaaa" (st_printArr(coordsRuler))
  270. ; Gui, % ((clickthrough:=!clickthrough) ? "+E0x20" : "-E0x20")
  271. return
  272. */
  273.  
  274. drawControls(byref contArr, G)
  275. {
  276. Gdip_GraphicsClear(G)
  277. basex:=contArr[1,1]
  278. basey:=contArr[1,2]
  279. offx:=0
  280. offy:=0
  281.  
  282. ; ypos:=0
  283. for k,arr in contArr
  284. {
  285. if (a_index>1)
  286. {
  287. if (vert=0)
  288. offx:=arr[1], offy:=0
  289. else
  290. offx:=0, offy:=arr[1]
  291. ; msgbox % arr[2]
  292. }
  293. pBrush:=Gdip_BrushCreateSolid(arr[5])
  294.  
  295. Gdip_FillRectangle(G
  296. , pBrush
  297. , abs(basex+offx)
  298. , abs(basey+offy)
  299. , (vert=0) ? arr[3] : arr[4]
  300. , (vert=0) ? arr[4] : arr[3])
  301.  
  302. Gdip_DeleteBrush(pBrush)
  303. if (arr[6]!="")
  304. {
  305. pPen:=Gdip_CreatePen(arr[6], 2)
  306. Gdip_DrawRectangle(G
  307. , pPen
  308. , abs(basex+offx)
  309. , abs(basey+offy)
  310. , (vert=0) ? arr[3] : arr[4]
  311. , (vert=0) ? arr[4] : arr[3])
  312. Gdip_DeletePen(pPen)
  313. }
  314. }
  315. }
  316.  
  317. drawTicks(x, y, w, h, spacing, tickCount, G, color="0xccffffff", text=1)
  318. {
  319. ; if (spacing<w)
  320. ; return 0
  321. if (vert=0)
  322. banana:=w
  323. else
  324. banana:=w, w:=h, h:=banana
  325.  
  326. step:=spacing
  327. fname=Verdana ; Font name, one that's on your OS.
  328. fsize=14 ; Font size, like in MS Word or whatever.
  329. font:=Gdip_FontCreate(ff, fsize, 1)
  330. ff:=Gdip_FontFamilyCreate(fname)
  331. j:=0
  332. ; y:=((h=-1) ? 0 : y)
  333. ; h:=((h=-1) ? A_ScreenHeight : h)
  334. loop, %tickCount%
  335. {
  336. if (spacing<w)
  337. break
  338.  
  339. pBrush:=Gdip_BrushCreateSolid(color)
  340. Gdip_FillRectangle(G, pBrush, x+((vert=0) ? spacing : 0), y+((vert=0) ? 0 : spacing), w, h)
  341. Gdip_DeleteBrush(pBrush)
  342.  
  343. options:=(vert=0)
  344. ? "x" x+spacing " y" ((mod(a_index, 2)=1) ? (y, j:=0) : (y+15, j:=1)) ; why "j"? no idea.
  345. : "x" x " y" y+spacing
  346.  
  347. if (step>=20 && text=1)
  348. Gdip_TextToGraphics(G, spacing, options " c" substr(color, 3) " s" fsize, fname)
  349. spacing+=step
  350. }
  351. options:=(vert=0)
  352. ? "x" coordsRuler[1,1]+coordsRuler[1,3] " y" ((j=0) ? y+15 : y)
  353. : "x" x " y" coordsRuler[1,2]+coordsRuler[1,3]
  354.  
  355. Gdip_TextToGraphics(G, coordsRuler[1,3], options " c" substr(color, 3) " s" fsize, fname)
  356. Gdip_DeleteFont(font)
  357. Gdip_DeleteFontFamily(ff)
  358. }
  359.  
  360.  
  361.  
  362. st_printArr(array, depth=5, indentLevel="")
  363. {
  364. for k,v in Array
  365. {
  366. list.= indentLevel "[" k "]"
  367. if (IsObject(v) && depth>1)
  368. list.="`n" st_printArr(v, depth-1, indentLevel . " ")
  369. Else
  370. list.=" => " v
  371. list.="`n"
  372. }
  373. return rtrim(list)
  374. }
  375.  
  376.  
  377. esc::
  378. DIE:
  379. critical
  380. SelectObject(hdc, obm)
  381. DeleteObject(hbm)
  382. DeleteDC(hdc)
  383. Gdip_DeleteGraphics(G)
  384. Gdip_DeleteBrush(pBrush)
  385. Gdip_DisposeImage(pB)
  386. Gdip_Shutdown(pToken)
  387. exitapp
  388. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement