Snippet #119543

TTL: forever — WordwrapView raw

on 2022/10/29 11:57:29 (UTC) by Anonymous as Lua

  1. function widget:GetInfo()
  2.     return {
  3.         name      = "Marker On Click",
  4.         desc      = "Place marker on single click",
  5.         author    = "Helwor",
  6.         date      = "October 2022", 
  7.         license   = "GNU GPL v2",
  8.         layer     = 0,
  9.         enabled   = true,
  10.     }
  11. end
  12. -- Config
  13. local MOVE_THRESHOLD = 5 -- can place a marker until the mouse moved this many pixel from the click position
  14. local DOUBLECLICK_TIME = 0.28 -- how long shall we wait for a double click to happen before authorizing to place a marker
  15.                               -- , value try to be as close as possible of the text label trigger
  16. --
  17. -- NOTE: since we use Update CallIn, the delay may vary depending on lag
  18. -- the widget is disabled at start of game if no hotkey is found for drawing on map
  19.  
  20. local spMarkerAddPoint = Spring.MarkerAddPoint
  21. local spTraceScreenRay = Spring.TraceScreenRay
  22. local spGetMouseState = Spring.GetMouseState
  23. local Echo = Spring.Echo
  24.  
  25. local hotkey = {}
  26.  
  27. local catchDrawing, dontCatchThisOne, drawing, _, pos
  28. local doubleClickDelay, waitDblClick, waitRelease
  29.  
  30. function widget:KeyPress(key,mods,isRepeat)
  31.     if isRepeat then
  32.         return
  33.     end
  34.     catchDrawing = hotkey[key]
  35. end
  36. function widget:KeyRelease(key)
  37.     if catchDrawing then
  38.         catchDrawing = not hotkey[key]
  39.     end
  40. end
  41. function widget:Update(dt)
  42.     if not catchDrawing then
  43.         return
  44.     end
  45.     if doubleClickDelay then
  46.         doubleClickDelay = doubleClickDelay - dt
  47.         if doubleClickDelay<=0 then
  48.             doubleClickDelay = false
  49.         end
  50.     end
  51.     local mx,my,lmb = spGetMouseState()
  52.     if waitRelease then
  53.         if lmb then 
  54.             return
  55.         end
  56.         waitRelease = false
  57.     end
  58.     if waitDblClick then
  59.         if lmb then -- text label should have been triggered there
  60.             catchDrawing = false
  61.             waitDblClick = false
  62.             doubleClickDelay = false
  63.         else
  64.             waitDblClick = doubleClickDelay
  65.             if not waitDblClick then
  66.                 spMarkerAddPoint(pos[1],pos[2],pos[3])
  67.             end
  68.         end
  69.         return
  70.     end
  71.  
  72.     if not drawing then
  73.         if lmb then
  74.             last_mx,last_my = mx,my
  75.             drawing = true
  76.             doubleClickDelay = DOUBLECLICK_TIME
  77.             dontCatchThisOne = false
  78.         end
  79.     else
  80.         if not lmb then
  81.             drawing = false
  82.         end
  83.         if dontCatchThisOne then
  84.             return
  85.         end
  86.         local delta = ((last_mx-mx)^2 + (last_my-my)^2)^0.5
  87.         if delta>MOVE_THRESHOLD then
  88.             dontCatchThisOne = true
  89.             return
  90.         end
  91.         if not (drawing or dontCatchThisOne) then
  92.             -- we're half way there, get the pos for our marker
  93.             _,pos = spTraceScreenRay(mx, my, true, true, false,false)
  94.             if not pos then
  95.                 return
  96.             end
  97.             waitDblClick = true
  98.             waitRelease = true
  99.         end
  100.     end
  101. end
  102.  
  103. function widget:Initialize()
  104.     for _,key in pairs(WG.crude.GetHotkeys("drawinmap") or {}) do
  105.         hotkey[key:byte(-1)] = true
  106.     end
  107.     if not next(hotkey) then
  108.         Echo(widget:GetInfo().name .. ' disabled, no hotkey for drawing on map')
  109.         widgetHandler:RemoveWidget(widget)
  110.     end
  111. end

Recent Snippets