PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/bkit_main_form.ahk

https://bitbucket.org/willbr/bookit_tweaks
AutoHotKey | 864 lines | 746 code | 99 blank | 19 comment | 100 complexity | 0ae406b08b163819698806b03f67dcc2 MD5 | raw file
  1. ; Header
  2. #SingleInstance Force
  3. #Persistent
  4. #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
  5. SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
  6. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
  7. main()
  8. return
  9. main()
  10. {
  11. global
  12. form = ahk_class TMainForm
  13. curFocus = none
  14. last_key := ""
  15. visual = 0
  16. Hotkey,IfWinActive, %form%
  17. Hotkey, ~LButton, StartVisualQuery
  18. Hotkey, ~LButton Up, EndVisualQuery
  19. Hotkey, Esc, CancelCmd
  20. Hotkey, ^[, CancelCmd
  21. Hotkey, /, GridKeysHighJack
  22. Hotkey, h, GridKeysHighJack
  23. Hotkey, j, GridKeysHighJack
  24. Hotkey, k, GridKeysHighJack
  25. Hotkey, l, GridKeysHighJack
  26. Hotkey, +j, GridKeysHighJack
  27. Hotkey, +k, GridKeysHighJack
  28. Hotkey, w, GridKeysHighJack
  29. Hotkey, e, GridKeysHighJack
  30. Hotkey, b, GridKeysHighJack
  31. Hotkey, v, GridKeysHighJack
  32. Hotkey, ^f, GridKeysHighJack
  33. Hotkey, ^b, GridKeysHighJack
  34. Hotkey, g, GridKeysHighJack
  35. Hotkey, +g, GridKeysHighJack
  36. Hotkey, d, GridKeysHighJack
  37. Hotkey, i, GridKeysHighJack
  38. Hotkey, o, GridKeysHighJack
  39. Loop
  40. {
  41. WinWaitActive, %form%
  42. CreateTimeTable()
  43. SetControlFocus("grid")
  44. WinWaitNotActive, %form%
  45. Tooltip
  46. }
  47. return
  48. }
  49. ; ############################################################################################
  50. StartVisualQuery:
  51. SetTimer, VisualQuery, 500
  52. Visual("on")
  53. return
  54. EndVisualQuery:
  55. SetTimer, VisualQuery, off
  56. Visual("off")
  57. return
  58. VisualQuery:
  59. Visual("update")
  60. return
  61. ; ############################################################################################
  62. CancelCmd:
  63. Visual("off")
  64. return
  65. ; ############################################################################################
  66. GridKeysHighJack:
  67. GetControlFocus()
  68. if (curFocus = "grid")
  69. {
  70. GridHotkey(A_ThisHotkey)
  71. } else
  72. {
  73. send, %A_ThisHotkey%
  74. }
  75. return
  76. ; ############################################################################################
  77. Visual(cmd)
  78. {
  79. global visual, sel_x, sel_y
  80. msg := "-- Visual --"
  81. if (cmd = "toggle")
  82. {
  83. if (visual = "+")
  84. {
  85. Visual("off")
  86. }
  87. else
  88. {
  89. Visual("on")
  90. }
  91. }
  92. else if (cmd = "on")
  93. {
  94. visual := "+"
  95. }
  96. else if (cmd = "off")
  97. {
  98. visual := ""
  99. CursorMessage("")
  100. }
  101. else if (cmd = "update")
  102. {
  103. if (visual = "+")
  104. {
  105. CursorMessage(msg)
  106. }
  107. }
  108. else
  109. {
  110. msgbox error: %A_LineNumber%
  111. }
  112. return
  113. }
  114. CursorMessage(msg)
  115. {
  116. global sel_x, sel_y, sel_end_x, sel_end_y
  117. if (msg = "")
  118. {
  119. ToolTip
  120. return
  121. }
  122. GetSelection()
  123. GetSelectionEnd()
  124. msg_x := sel_x - 70
  125. msg_y := sel_y
  126. start_time := GetTime(sel_x, sel_y, "start")
  127. end_time := GetTime(sel_end_x, sel_end_y, "end")
  128. duration := GetDuration(start_time, end_time)
  129. ToolTip %msg%`n%start_time%`n%duration%`n%end_time%, %msg_x%, %msg_y%
  130. return
  131. }
  132. ; ############################################################################################
  133. GetControlFocus()
  134. {
  135. global curFocus
  136. ControlGetFocus, curConFocus, %form%
  137. curFocus := CtrlDictionary("",curConFocus)
  138. return
  139. }
  140. ; ############################################################################################
  141. SetControlFocus(ctrl)
  142. {
  143. global form
  144. ctrl_name := CtrlDictionary(ctrl,"")
  145. ControlFocus, %ctrl_name%, %form%
  146. return
  147. }
  148. ; ############################################################################################
  149. CtrlDictionary(key,value)
  150. {
  151. dict=
  152. (
  153. grid|TStringGrid1
  154. )
  155. if (key = "")
  156. {
  157. getValue = 0
  158. }
  159. else if (value = "")
  160. {
  161. getValue = 1
  162. }
  163. else
  164. {
  165. msgbox, Err: Too many arguments`nk: %key%`nv: %value%
  166. }
  167. Loop, parse, dict, `n
  168. {
  169. Loop, parse, A_LoopField, |
  170. {
  171. if ( A_Index = 1) ; key
  172. {
  173. a_key = %A_LoopField%
  174. }
  175. else if ( A_Index = 2) ; value
  176. {
  177. a_value = %A_LoopField%
  178. }
  179. else
  180. {
  181. msgbox, Err: Dictionary index = 3
  182. }
  183. }
  184. if (getValue AND key = a_key)
  185. {
  186. return a_value
  187. }
  188. else if (!getValue AND value = a_value)
  189. {
  190. return a_key
  191. }
  192. }
  193. return
  194. }
  195. GridHotkey(key)
  196. {
  197. global visual, last_key, sel_x, sel_y
  198. if (key = "j")
  199. {
  200. Send, %visual%{Down}
  201. }
  202. else if (key = "k")
  203. {
  204. Send, %visual%{Up}
  205. }
  206. else if (key = "h")
  207. {
  208. Send, {Left}
  209. Visual("off")
  210. }
  211. else if (key = "l")
  212. {
  213. Send, {Right}
  214. Visual("off")
  215. }
  216. else if (key = "+j")
  217. {
  218. Send, +{Down}
  219. }
  220. else if (key = "+k")
  221. {
  222. Send, +{Up}
  223. }
  224. else if (key = "w")
  225. {
  226. GotoNext("start")
  227. }
  228. else if (key = "e")
  229. {
  230. if (last_key = "g")
  231. {
  232. GotoPrevious("end")
  233. }
  234. else
  235. {
  236. GotoNext("end")
  237. }
  238. }
  239. else if (key = "b")
  240. {
  241. GotoPrevious("start")
  242. }
  243. else if (key = "v")
  244. {
  245. Visual("toggle")
  246. }
  247. else if (key = "^f")
  248. {
  249. Send, {Down 7}
  250. }
  251. else if (key = "^b")
  252. {
  253. Send, {Up 7}
  254. }
  255. else if (key = "g")
  256. {
  257. if (DoubleClick(key, 500))
  258. {
  259. Send, {Up 20}
  260. reset_last_key = 1
  261. }
  262. }
  263. else if (key = "+g")
  264. {
  265. Send, {Down 20}
  266. }
  267. else if (key = "d")
  268. {
  269. Send, {Delete}
  270. Visual("off")
  271. }
  272. else if (key = "i")
  273. {
  274. ClickSelection()
  275. Visual("off")
  276. }
  277. else if (key = "o")
  278. {
  279. Send, ^v
  280. Visual("off")
  281. }
  282. else if (key = "/")
  283. {
  284. Send, ^s
  285. }
  286. else
  287. {
  288. msgbox, unknown search hot key
  289. }
  290. if (reset_last_key)
  291. {
  292. last_key := ""
  293. }
  294. else
  295. {
  296. last_key := key
  297. }
  298. Visual("update")
  299. return
  300. }
  301. ; ############################################################################################
  302. DoubleClick(key, time)
  303. {
  304. static
  305. now := A_TickCount
  306. diff := now - last_%key%
  307. last_%key% := now
  308. if (diff < time and diff > 0)
  309. {
  310. return 1
  311. }
  312. return 0
  313. }
  314. ; ############################################################################################
  315. GetSelection()
  316. {
  317. global form, sel_x, sel_y, sel_end_x, sel_end_y
  318. grid := CtrlDictionary("grid","")
  319. ControlGetPos, g_x, g_y, g_w, g_h, %grid%, %form%
  320. x1 := g_x
  321. y1 := g_y
  322. x2 := g_x + g_w
  323. y2 := g_y + g_h
  324. ImageSearch, sel_x, sel_y, %x1%, %y1%, %x2%, %y2%, data\selection.bmp
  325. return
  326. }
  327. ; ############################################################################################
  328. SelectionDebug()
  329. {
  330. global
  331. GetSelectionEnd()
  332. tooltip, sel, %sel_x%, %sel_y%
  333. sleep, 1000
  334. tooltip, end, %sel_end_x%, %sel_end_y%
  335. sleep, 1000
  336. tooltip
  337. diff_y := sel_end_y - sel_y
  338. diff_x := sel_end_x - sel_x
  339. msgbox, %diff_x%, %diff_y%
  340. }
  341. ; ############################################################################################
  342. GetIncTime(time, increment)
  343. {
  344. times =
  345. (
  346. 09:30
  347. 10:00
  348. 10:30
  349. 11:00
  350. 11:30
  351. 12:00
  352. 12:30
  353. 13:00
  354. 13:30
  355. 14:00
  356. 14:30
  357. 15:00
  358. 15:30
  359. 16:00
  360. 16:30
  361. 17:00
  362. 17:30
  363. 18:00
  364. 18:30
  365. 19:00
  366. 19:30
  367. 20:00
  368. )
  369. loop, parse, times, `n
  370. {
  371. a_%A_Index% := A_LoopField
  372. len := A_Index
  373. if (time = A_LoopField)
  374. {
  375. found_i := A_Index
  376. }
  377. }
  378. next_i := found_i + increment
  379. if (next_i > len)
  380. {
  381. next_i := len
  382. } else if (next_i < 1)
  383. {
  384. next_i := 1
  385. }
  386. next := a_%next_i%
  387. return next
  388. }
  389. ; ############################################################################################
  390. GetSelectionEnd()
  391. {
  392. global form, sel_x, sel_y, sel_end_x, sel_end_y
  393. grid := CtrlDictionary("grid","")
  394. ControlGetPos, g_x, g_y, g_w, g_h, %grid%, %form%
  395. x1 := g_x
  396. y1 := g_y
  397. x2 := g_x + g_w
  398. y2 := g_y + g_h
  399. img_list =
  400. (
  401. box
  402. mar
  403. )
  404. Loop, Parse, img_list, `n
  405. {
  406. i = %A_LoopField%
  407. ImageSearch, %i%_x, %i%_y, %x1%, %y1%, %x2%, %y2%, data\sel_end\%i%.bmp
  408. i_x := %i%_x
  409. i_y := %i%_y
  410. found := found . i_x . "|" . i_y . "`n"
  411. if ( i_x <> "")
  412. {
  413. found_x := i_x
  414. }
  415. }
  416. solid_img_list =
  417. (
  418. solid_b
  419. solid_y
  420. solid_w
  421. )
  422. Loop, Parse, solid_img_list, `n
  423. {
  424. i = %A_LoopField%
  425. ImageSearch, %i%_x, %i%_y, %x1%, %y1%, %x2%, %y2%, data\sel_end\%i%.bmp
  426. i_x := %i%_x
  427. i_y := %i%_y
  428. i_y += 6
  429. solid_found := solid_found . i_x . "|" . i_y . "`n"
  430. if ( i_x <> "")
  431. {
  432. solid_found_x := i_x
  433. }
  434. }
  435. found := found . solid_found
  436. sel_end := ListGet(found, "max")
  437. sel_end_x := GetX(sel_end)
  438. sel_end_y := GetY(sel_end)
  439. return
  440. }
  441. ; ############################################################################################
  442. ClickSelection()
  443. {
  444. global sel_x, sel_y
  445. GetSelection()
  446. sel_x += 2
  447. sel_y += 2
  448. if (sel_x > 0 and sel_y > 0)
  449. {
  450. click %sel_x%, %sel_y%, 2
  451. }
  452. return
  453. }
  454. ; ############################################################################################
  455. GotoNext(location)
  456. {
  457. MouseGetPos, m_x, m_y
  458. found := FindLocations(location, "after")
  459. ListClick(found,"Min")
  460. MouseMove, %m_x%, %m_y%, 0
  461. }
  462. ; ############################################################################################
  463. GotoPrevious(location)
  464. {
  465. MouseGetPos, m_x, m_y
  466. found := FindLocations(location, "before")
  467. ListClick(found,"Max")
  468. MouseMove, %m_x%, %m_y%, 0
  469. }
  470. FindLocations(location, direction)
  471. {
  472. global form, sel_x, sel_y
  473. grid := CtrlDictionary("grid","")
  474. ControlGetPos, g_x, g_y, g_w, g_h, %grid%, %form%
  475. GetSelection()
  476. x1 := sel_x - 5
  477. x2 := sel_x + 15
  478. if (direction = "before")
  479. {
  480. y1 := g_y
  481. y2 := sel_y
  482. }
  483. else if (direction = "after")
  484. {
  485. y1 := sel_y
  486. y2 := g_y + g_h
  487. }
  488. fl1 := FindColourLocations("b", location, x1, y1, x2, y2)
  489. fl2 := FindColourLocations("y", location, x1, y1, x2, y2)
  490. found := fl1 . "___`n" . fl2
  491. return found
  492. }
  493. FindColourLocations(colour, location, x1, y1, x2, y2)
  494. {
  495. global form, sel_x, sel_y
  496. found_list := ""
  497. c := 0
  498. Loop
  499. {
  500. ImageSearch, cx, cy, %x1%, %y1%, %x2%, %y2%, data\%colour%_%location%.bmp
  501. if (cx = "")
  502. {
  503. break
  504. }
  505. found_list = %found_list%%cx%|%cy%`n
  506. if c >= 32
  507. {
  508. break
  509. }
  510. y1 := cy + 5
  511. c += 1
  512. }
  513. return found_list
  514. }
  515. ; ############################################################################################
  516. GetX(point)
  517. {
  518. Loop, parse, point, |
  519. {
  520. if (A_Index = 1)
  521. {
  522. return A_LoopField
  523. }
  524. }
  525. }
  526. GetY(point)
  527. {
  528. Loop, parse, point, |
  529. {
  530. if (A_Index = 2)
  531. {
  532. return A_LoopField
  533. }
  534. }
  535. }
  536. ListClick(points_list, operator)
  537. {
  538. found := ListGet(points_list, operator)
  539. lx := GetX(found)
  540. ly := GetY(found)
  541. WClick(lx, ly, 1)
  542. }
  543. ListGet(points_list, operator)
  544. {
  545. if (points_list = "")
  546. {
  547. return
  548. }
  549. Loop, parse, points_list, `n
  550. {
  551. cx := GetX(A_LoopField)
  552. cy := GetY(A_LoopField)
  553. comp := Get(operator, cy, my)
  554. if (cy = comp)
  555. {
  556. mx := cx
  557. my := cy
  558. }
  559. }
  560. rval = %mx%|%my%
  561. return rval
  562. }
  563. ; ############################################################################################
  564. Get(cmd, a, b)
  565. {
  566. if ( a = "" and b = "")
  567. {
  568. return
  569. }
  570. else if ( a = "" or b = "")
  571. {
  572. return a . b
  573. }
  574. if (cmd = "min")
  575. {
  576. if (a < b)
  577. {
  578. return a
  579. } else {
  580. return b
  581. }
  582. }
  583. else if (cmd = "max")
  584. {
  585. if (a > b)
  586. {
  587. return a
  588. } else {
  589. return b
  590. }
  591. } else {
  592. msgbox error: %A_LineNumber%
  593. }
  594. }
  595. ; ############################################################################################
  596. CreateTimeTable()
  597. {
  598. global time_table, sel_x, sel_y
  599. grid := CtrlDictionary("grid","")
  600. ControlGetPos, g_x, g_y, g_w, g_h, %grid%, %form%
  601. GetSelection()
  602. offset := GetTimeStep()
  603. x1 := g_x
  604. y1 := gy
  605. x2 := g_x + 70
  606. y2 := g_y + g_h
  607. times=
  608. (
  609. 09
  610. 10
  611. 10
  612. 11
  613. 11
  614. 12
  615. 12
  616. 13
  617. 13
  618. 14
  619. 14
  620. 15
  621. 15
  622. 16
  623. 16
  624. 17
  625. 17
  626. 18
  627. 18
  628. 19
  629. 19
  630. )
  631. cur_min := "30"
  632. Loop, parse, times,`n
  633. {
  634. i := A_LoopField
  635. ImageSearch, %i%_x, %i%_y, %x1%, %y1%, %x2%, %y2%, data\times\%i%.bmp
  636. top := %i%_y +10
  637. c := GetNextMin()
  638. time_table := time_table . i . ":" . c . "|" . %i%_y . "`n"
  639. y1 := %i%_y +offset
  640. }
  641. }
  642. GetTimeStep()
  643. {
  644. grid := CtrlDictionary("grid","")
  645. ControlGetPos, g_x, g_y, g_w, g_h, %grid%, %form%
  646. x1 := g_x
  647. y1 := gy
  648. x2 := g_x + 70
  649. y2 := g_y + g_h
  650. ImageSearch, 09_x, 09_y, %x1%, %y1%, %x2%, %y2%, data\times\09.bmp
  651. ImageSearch, 10_x, 10_y, %x1%, %y1%, %x2%, %y2%, data\times\10.bmp
  652. diff := 10_y - 09_y
  653. return diff
  654. }
  655. GetNextMin()
  656. {
  657. global cur_min
  658. if (cur_min = "30")
  659. {
  660. cur_min := "00"
  661. } else {
  662. cur_min := "30"
  663. }
  664. return cur_min
  665. }
  666. GetTime(x, y, when)
  667. {
  668. global time_table
  669. rval := "20:00"
  670. Loop, parse, time_table, `n
  671. {
  672. Loop, parse, A_LoopField, `|
  673. {
  674. if (A_Index = 1)
  675. {
  676. time := A_LoopField
  677. } else if (A_Index =2)
  678. {
  679. iy := A_LoopField
  680. }
  681. }
  682. if (y < iy)
  683. {
  684. if (when = "start")
  685. {
  686. rval := GetIncTime(time, 0)
  687. break
  688. } else {
  689. rval := GetIncTime(time, 0)
  690. break
  691. }
  692. break
  693. }
  694. }
  695. return rval
  696. }
  697. WClick(x, y, count)
  698. {
  699. if (x = "" or y = "")
  700. {
  701. return
  702. }
  703. Click, %x%, %y%, %count%
  704. }
  705. GetDuration(t1, t2)
  706. {
  707. times =
  708. (
  709. 09:30
  710. 10:00
  711. 10:30
  712. 11:00
  713. 11:30
  714. 12:00
  715. 12:30
  716. 13:00
  717. 13:30
  718. 14:00
  719. 14:30
  720. 15:00
  721. 15:30
  722. 16:00
  723. 16:30
  724. 17:00
  725. 17:30
  726. 18:00
  727. 18:30
  728. 19:00
  729. 19:30
  730. 20:00
  731. )
  732. Loop, parse, times, `n
  733. {
  734. if (A_LoopField = t1)
  735. {
  736. t1_i := A_Index
  737. }
  738. if (A_LoopField = t2)
  739. {
  740. t2_i := A_Index
  741. }
  742. if (t1_i <> "" AND t2_i <> "")
  743. {
  744. break
  745. }
  746. }
  747. diff := t1_i - t2_i
  748. diff := abs(diff)
  749. h := Floor(diff / 2)
  750. m := mod(diff,2)
  751. a := strlen(h)
  752. if (a = 1)
  753. {
  754. h := "0" . h
  755. }
  756. if ( m = 0)
  757. {
  758. m := "00"
  759. } else
  760. {
  761. m := "30"
  762. }
  763. duration = %h%:%m%
  764. return duration
  765. }