Snippet #140524

TTL: forever — WordwrapView raw

on 2022/11/19 1:56:07 (UTC) by Anonymous as Text

  1. local version = '0.3'
  2. function widget:GetInfo()
  3.     return {
  4.         name      = "Marker On Click",
  5.         desc      = "Place marker on single click while using hotkey for drawing, ver "..version,
  6.         author    = "Helwor",
  7.         date      = "October 2022", 
  8.         license   = "GNU GPL v2",
  9.         layer     = 0,
  10.         enabled   = true,
  11.         handler   = true,
  12.     }
  13. end
  14. -- Config
  15. local MOVE_THRESHOLD = 4 -- can place a marker until the mouse moved this many pixel from the click position
  16. local DOUBLECLICK_TIME = 0.30 -- how long shall we wait for a double click to happen before authorizing to place a marker
  17.                               -- , value try to be as close as possible of the text label trigger
  18. --
  19. -- NOTE: since we use Update CallIn, the delay may vary depending on lag, the catching of click is not reliable, so user have to press until the next Update round
  20. -- the widget is disabled at start of game if no hotkey is found for drawing on map
  21.  
  22. local spMarkerAddPoint = Spring.MarkerAddPoint
  23. local spTraceScreenRay = Spring.TraceScreenRay
  24. local spGetMouseState = Spring.GetMouseState
  25. local spIsUserWriting = Spring.IsUserWriting
  26. local Echo = Spring.Echo
  27.  
  28. local hotkey = {}
  29.  
  30. local catchDrawing, dontCatchThisOne, drawing, _, pos
  31. local doubleClickDelay, waitDblClick, waitRelease
  32.  
  33.  
  34. -- debugging
  35. local debugging = true
  36. local DebugUp
  37. local last_doubleClickTime
  38. local textLabel
  39. local secondClick
  40. local f = debugging and VFS.Include("LuaUI\\Widgets\\UtilsFunc.lua")
  41. --
  42.  
  43. function widget:KeyPress(key,mods,isRepeat)
  44.     if isRepeat then
  45.         return
  46.     end
  47.     catchDrawing = hotkey[key]
  48. end
  49. function widget:KeyRelease(key)
  50.     if hotkey[key] then
  51.         catchDrawing = false
  52.         doubleClickDelay = false
  53.         drawing = false
  54.         waitRelease = false
  55.         waitDblClick = false
  56.         if debugging then
  57.             measureSecondClick = false
  58.             secondClick = 0
  59.         end
  60.         
  61.  
  62.     end
  63. end
  64.  
  65. function widget:Update(dt)
  66.     if debugging then
  67.         DebugUp("catchDrawing", catchDrawing)
  68.         DebugUp("dontCatchThisOne", dontCatchThisOne)
  69.         DebugUp("drawing", drawing)
  70.         DebugUp("pos", pos)
  71.         DebugUp("doubleClickDelay", doubleClickDelay)
  72.         DebugUp("waitDblClick", waitDblClick)
  73.         DebugUp("waitRelease", waitRelease)
  74.         DebugUp("last_doubleClickTime", last_doubleClickTime)
  75.         DebugUp("textLabel", textLabel)
  76.     end
  77.     if not catchDrawing then
  78.         return
  79.     end
  80.     if spIsUserWriting() then
  81.         doubleClickDelay = false
  82.         waitDblClick = false
  83.         drawing = false
  84.         catchDrawing = false
  85.         dontCatchThisOne = false
  86.         if debugging then
  87.             textLabel = true
  88.             measureSecondClick = false
  89.         end
  90.         return
  91.     end
  92.  
  93.     textLabel = false
  94.     if doubleClickDelay then
  95.         doubleClickDelay = doubleClickDelay - dt
  96.         if doubleClickDelay <= -dt then
  97.             doubleClickDelay = false
  98.         end
  99.     end
  100.     local mx,my,lmb = spGetMouseState()
  101.     if debugging then
  102.         if measureSecondClick then 
  103.             secondClick = secondClick+dt
  104.             DebugUp("secondClick", secondClick)
  105.             DebugUp('lastDT',dt)
  106.         end
  107.     end
  108.     if waitRelease then
  109.         if lmb then 
  110.             return
  111.         end
  112.         waitRelease = false
  113.     end
  114.     if waitDblClick then
  115.         if lmb and spIsUserWriting() then -- text label should have been triggered there
  116.         else
  117.             waitDblClick = not lmb and doubleClickDelay
  118.             if not waitDblClick then
  119.                 spMarkerAddPoint(pos[1],pos[2],pos[3])
  120.                 if debugging then
  121.                     measureSecondClick = false
  122.                 end
  123.             else
  124.                 return
  125.             end
  126.         end
  127.     end
  128.     if not drawing then
  129.         if lmb  then
  130.             last_mx,last_my = mx,my
  131.             drawing = true
  132.             doubleClickDelay = DOUBLECLICK_TIME
  133.             dontCatchThisOne = false
  134.             if debugging then
  135.                 measureSecondClick = true
  136.                 secondClick = 0
  137.                 last_doubleClickTime = doubleClickDelay
  138.             end
  139.         end
  140.     else
  141.         if not lmb then
  142.             drawing = false
  143.         end
  144.         if dontCatchThisOne then
  145.             return
  146.         end
  147.         local delta = ((last_mx-mx)^2 + (last_my-my)^2)^0.5
  148.         if delta>MOVE_THRESHOLD then
  149.             dontCatchThisOne = true
  150.             return
  151.         end
  152.         if not (drawing or dontCatchThisOne) then
  153.             -- we're half way there, get the pos for our marker
  154.             _,pos = spTraceScreenRay(mx, my, true, true, false,false)
  155.             if not pos then
  156.                 return
  157.             end
  158.             waitDblClick = true
  159.             waitRelease = true
  160.         end
  161.     end
  162. end
  163.  
  164. function widget:Initialize()
  165.     if debugging then
  166.         DebugUp = f.DebugWinInit(widget)
  167.     end
  168.     for _,key in pairs(WG.crude.GetHotkeys("drawinmap") or {}) do
  169.         hotkey[key:byte(-1)] = true
  170.     end
  171.     if not next(hotkey) then
  172.         Echo(widget:GetInfo().name .. ' disabled, no hotkey for drawing on map')
  173.         widgetHandler:RemoveWidget(widget)
  174.     end
  175. end

Recent Snippets