Guest User

Untitled

a guest
Feb 15th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. function widget:GetInfo()
  2. return {
  3. name = "Red Tooltip", --version 4
  4. desc = "Requires Red UI Framework",
  5. author = "Regret",
  6. date = "August 11, 2009", --last change September 10,2009
  7. license = "GNU GPL, v2 or later",
  8. layer = -100,
  9. enabled = true, --enabled by default
  10. handler = true, --can use widgetHandler:x()
  11. }
  12. end
  13. local NeededFrameworkVersion = 8
  14. local CanvasX,CanvasY = 1272,734 --resolution in which the widget was made (for 1:1 size)
  15. --1272,734 == 1280,768 windowed
  16.  
  17. --todo: sy adjustment
  18.  
  19. local Config = {
  20. tooltip = {
  21. px = 0,py = CanvasY-(12*6+5*2), --default start position
  22. sx = 300,sy = 12*6+5*2, --background size
  23.  
  24. fontsize = 12,
  25.  
  26. margin = 5, --distance from background border
  27.  
  28. cbackground = {0,0,0,0.5}, --color {r,g,b,alpha}
  29. cborder = {0,0,0,1},
  30.  
  31. dragbutton = {2}, --middle mouse button
  32. tooltip = {
  33. background = "Hold \255\255\255\1middle mouse button\255\255\255\255 to drag the tooltip display around.",
  34. },
  35. },
  36. }
  37.  
  38. local sGetCurrentTooltip = Spring.GetCurrentTooltip
  39. local sGetSelectedUnitsCount = Spring.GetSelectedUnitsCount
  40.  
  41. local function IncludeRedUIFrameworkFunctions()
  42. New = WG.Red.New(widget)
  43. Copy = WG.Red.Copytable
  44. SetTooltip = WG.Red.SetTooltip
  45. GetSetTooltip = WG.Red.GetSetTooltip
  46. Screen = WG.Red.Screen
  47. GetWidgetObjects = WG.Red.GetWidgetObjects
  48. end
  49.  
  50. local function RedUIchecks()
  51. local color = "\255\255\255\1"
  52. local passed = true
  53. if (type(WG.Red)~="table") then
  54. Spring.Echo(color..widget:GetInfo().name.." requires Red UI Framework.")
  55. passed = false
  56. elseif (type(WG.Red.Screen)~="table") then
  57. Spring.Echo(color..widget:GetInfo().name..">> strange error.")
  58. passed = false
  59. elseif (WG.Red.Version < NeededFrameworkVersion) then
  60. Spring.Echo(color..widget:GetInfo().name..">> update your Red UI Framework.")
  61. passed = false
  62. end
  63. if (not passed) then
  64. widgetHandler:ToggleWidget(widget:GetInfo().name)
  65. return false
  66. end
  67. IncludeRedUIFrameworkFunctions()
  68. return true
  69. end
  70.  
  71. local function AutoResizeObjects() --autoresize v2
  72. if (LastAutoResizeX==nil) then
  73. LastAutoResizeX = CanvasX
  74. LastAutoResizeY = CanvasY
  75. end
  76. local lx,ly = LastAutoResizeX,LastAutoResizeY
  77. local vsx,vsy = Screen.vsx,Screen.vsy
  78. if ((lx ~= vsx) or (ly ~= vsy)) then
  79. local objects = GetWidgetObjects(widget)
  80. local scale = vsy/ly
  81. local skippedobjects = {}
  82. Spring.Echo("AUTORESIZEOBJECTS", lx, ly, vsx, vsy, scale)
  83. for i=1,#objects do
  84. local o = objects[i]
  85. local adjust = 0
  86. if ((o.movableslaves) and (#o.movableslaves > 0)) then
  87. adjust = (o.px*scale+o.sx*scale)-vsx
  88. if (((o.px+o.sx)-lx) == 0) then
  89. o._moveduetoresize = true
  90. end
  91. end
  92. Spring.Echo("adjust", adjust)
  93. if (o.px) then o.px = o.px * scale end
  94. if (o.py) then o.py = o.py * scale end
  95. if (o.sx) then o.sx = o.sx * scale end
  96. if (o.sy) then o.sy = o.sy * scale end
  97. if (o.fontsize) then o.fontsize = o.fontsize * scale end
  98. if (adjust > 0) then
  99. o._moveduetoresize = true
  100. o.px = o.px - adjust
  101. for j=1,#o.movableslaves do
  102. local s = o.movableslaves[j]
  103. Spring.Echo("s", s.px,s.py)
  104. s.px = s.px - adjust/scale
  105. Spring.Echo("s", s.px,s.py)
  106. end
  107. elseif ((adjust < 0) and o._moveduetoresize) then
  108. o._moveduetoresize = nil
  109. o.px = o.px - adjust
  110. for j=1,#o.movableslaves do
  111. local s = o.movableslaves[j]
  112. Spring.Echo("s", s.px,s.py)
  113. s.px = s.px - adjust/scale
  114. Spring.Echo("s", s.px,s.py)
  115. end
  116. end
  117. end
  118. LastAutoResizeX,LastAutoResizeY = vsx,vsy
  119. end
  120. end
  121.  
  122. local function createtooltip(r)
  123. local text = {"text",
  124. px=r.px+r.margin,py=r.py+r.margin,
  125. fontsize=r.fontsize,
  126. caption="",
  127. options="o",
  128.  
  129. onupdate=function(self)
  130. local unitcount = sGetSelectedUnitsCount()
  131. if (unitcount ~= 0) then
  132. self.caption = "Selected units: "..unitcount.."\n"
  133. else
  134. self.caption = "\n"
  135. end
  136.  
  137. if (self._mouseoverself) then
  138. self.caption = self.caption..r.tooltip.background
  139. else
  140. self.caption = self.caption..(GetSetTooltip() or sGetCurrentTooltip())
  141. end
  142. end
  143. }
  144.  
  145. local background = {"rectangle",
  146. px=r.px,py=r.py,
  147. sx=r.sx,sy=r.sy,
  148. color=r.cbackground,
  149. border=r.cborder,
  150.  
  151. movable=r.dragbutton,
  152. movableslaves={text},
  153.  
  154. obeyscreenedge = true,
  155. --overridecursor = true,
  156. overrideclick = {2},
  157.  
  158. onupdate=function(self)
  159. if (self.px < (Screen.vsx/2)) then --left side of screen
  160. if ((self.sx-r.margin*2) <= text.getwidth()) then
  161. self.sx = (text.getwidth()+r.margin*2) -1
  162. else
  163. self.sx = r.sx * Screen.vsy/CanvasY
  164. end
  165. text.px = self.px + r.margin
  166. else --right side of screen
  167. if ((self.sx-r.margin*2 -1) <= text.getwidth()) then
  168. self.px = self.px - ((text.getwidth() + r.margin*2) - self.sx)
  169. self.sx = (text.getwidth() + r.margin*2)
  170. else
  171. self.px = self.px - ((r.sx * Screen.vsy/CanvasY) - self.sx)
  172. self.sx = r.sx * Screen.vsy/CanvasY
  173. end
  174. text.px = self.px + r.margin
  175. end
  176. end,
  177.  
  178. mouseover=function(mx,my,self)
  179. text._mouseoverself = true
  180. end,
  181. mousenotover=function(mx,my,self)
  182. text._mouseoverself = nil
  183. end,
  184. }
  185.  
  186. New(background)
  187. New(text)
  188.  
  189. return {
  190. ["background"] = background,
  191. ["text"] = text,
  192.  
  193. margin = r.margin,
  194. }
  195. end
  196.  
  197. function widget:Initialize()
  198. PassedStartupCheck = RedUIchecks()
  199. if (not PassedStartupCheck) then return end
  200.  
  201. tooltip = createtooltip(Config.tooltip)
  202.  
  203. Spring.SetDrawSelectionInfo(false) --disables springs default display of selected units count
  204. Spring.SendCommands("tooltip 0")
  205. AutoResizeObjects()
  206. end
  207.  
  208. function widget:Shutdown()
  209. Spring.SendCommands("tooltip 1")
  210. end
  211.  
  212. function widget:Update()
  213. AutoResizeObjects()
  214. end
  215.  
  216. --save/load stuff
  217. --currently only position
  218. function widget:GetConfigData() --save config
  219. if (PassedStartupCheck) then
  220. local vsy = Screen.vsy
  221. local unscale = CanvasY/vsy --needed due to autoresize, stores unresized variables
  222. Config.tooltip.px = tooltip.background.px * unscale
  223. Config.tooltip.py = tooltip.background.py * unscale
  224. return {Config=Config}
  225. end
  226. end
  227. function widget:SetConfigData(data) --load config
  228. if (data.Config ~= nil) then
  229. Config.tooltip.px = data.Config.tooltip.px
  230. Config.tooltip.py = data.Config.tooltip.py
  231. end
  232. end
Advertisement
Add Comment
Please, Sign In to add comment