Snippet #154030

TTL: forever — WordwrapView raw

on 2022/12/08 7:31:55 (UTC) by Anonymous as Lua

  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 = false
  35. local DebugUp
  36. local last_doubleClickTime
  37. local textLabel
  38. local secondClick
  39. local f = debugging and VFS.Include("LuaUI\\Widgets\\UtilsFunc.lua")
  40. --
  41.  
  42. function widget:KeyPress(key,mods,isRepeat)
  43.     if isRepeat then
  44.         return
  45.     end
  46.     catchDrawing = hotkey[key]
  47. end
  48.  
  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 
  116.             -- text label should have been triggered there
  117.         else
  118.             waitDblClick = not lmb and doubleClickDelay
  119.             if not waitDblClick then
  120.                 spMarkerAddPoint(pos[1],pos[2],pos[3])
  121.                 if debugging then
  122.                     measureSecondClick = false
  123.                 end
  124.             else
  125.                 return
  126.             end
  127.         end
  128.     end
  129.     if not drawing then
  130.         if lmb  then
  131.             last_mx,last_my = mx,my
  132.             drawing = true
  133.             doubleClickDelay = DOUBLECLICK_TIME
  134.             dontCatchThisOne = false
  135.             if debugging then
  136.                 measureSecondClick = true
  137.                 secondClick = 0
  138.                 last_doubleClickTime = doubleClickDelay
  139.             end
  140.         end
  141.     else
  142.         if not lmb then
  143.             drawing = false
  144.         end
  145.         if dontCatchThisOne then
  146.             return
  147.         end
  148.         local delta = ((last_mx-mx)^2 + (last_my-my)^2)^0.5
  149.         if delta>MOVE_THRESHOLD then
  150.             dontCatchThisOne = true
  151.             return
  152.         end
  153.         if not (drawing or dontCatchThisOne) then
  154.             -- we're half way there, get the pos for our marker
  155.             _,pos = spTraceScreenRay(mx, my, true, true, false,false)
  156.             if not pos then
  157.                 return
  158.             end
  159.             waitDblClick = true
  160.             waitRelease = true
  161.         end
  162.     end
  163. end
  164.  
  165. function widget:Initialize()
  166.     if debugging then
  167.         DebugUp = f.DebugWinInit(widget)
  168.     end
  169.     for _,key in pairs(WG.crude.GetHotkeys("drawinmap") or {}) do
  170.         hotkey[key:byte(-1)] = true
  171.     end
  172.     if not next(hotkey) then
  173.         Echo(widget:GetInfo().name .. ' disabled, no hotkey for drawing on map')
  174.         widgetHandler:RemoveWidget(widget)
  175.     end
  176. end

Recent Snippets