Guest User

Untitled

a guest
Sep 8th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. function widget:GetInfo()
  2. return {
  3. name = "replay control buttons",
  4. desc = "GUI for votes",
  5. author = "knorke",
  6. date = "August 2012",
  7. license = "stackable",
  8. layer = 1,
  9. enabled = true -- loaded by my horse?
  10. }
  11. end
  12.  
  13. local Chili
  14. local Button
  15. local Label
  16. local Window
  17. local Panel
  18. local TextBox
  19. local Image
  20. local Progressbar
  21. local Control
  22. local Font
  23.  
  24. -- elements
  25. local window
  26. local button_setspeed = {}
  27. local button_skipPreGame
  28. local button_startStop
  29.  
  30. local speeds = {0.5, 1, 2, 3, 4, 5,10}
  31.  
  32. local isPaused = false
  33. local wantedSpeed = nil
  34.  
  35.  
  36. function widget:Initialize()
  37. if (not Spring.IsReplay()) then
  38. Spring.Echo ("replaycontrol: Not a replay, removing myself.")
  39. widgetHandler:RemoveWidget(self)
  40. Spring.Echo ("replaycontrol: LOL did not work")
  41. return
  42. end
  43. -- setup Chili
  44. Chili = WG.Chili
  45. Button = Chili.Button
  46. Label = Chili.Label
  47. Colorbars = Chili.Colorbars
  48. Window = Chili.Window
  49. StackPanel = Chili.StackPanel
  50. Image = Chili.Image
  51. Progressbar = Chili.Progressbar
  52. Control = Chili.Control
  53. screen0 = Chili.Screen0
  54.  
  55. --create main Chili elements
  56. local screenWidth,screenHeight = Spring.GetWindowGeometry()
  57. local height = tostring(math.floor(screenWidth/screenHeight*0.35*0.35*100)) .. "%"
  58. local y = tostring(math.floor((1-screenWidth/screenHeight*0.35*0.35)*100)) .. "%"
  59.  
  60. local labelHeight = 24
  61. local fontSize = 16
  62.  
  63. window = Window:New{
  64. --parent = screen0,
  65. name = 'replaycontroller';
  66. color = {255, 255, 255, 255},
  67. width = 300;
  68. height = 85;
  69. right = 10;
  70. y = "20%";
  71. dockable = false;
  72. draggable = true,
  73. resizable = false,
  74. tweakDraggable = true,
  75. tweakResizable = false,
  76. minWidth = MIN_WIDTH,
  77. minHeight = MIN_HEIGHT,
  78. padding = {0, 0, 0, 0},
  79. --itemMargin = {0, 0, 0, 0},
  80. --caption = "replay control"
  81. }
  82.  
  83. for i = 1, #speeds do
  84. button_setspeed[i] = Button:New {
  85. width = 40,
  86. height = 20,
  87. y = 30,
  88. x = 10+(i-1)*40,
  89. parent=window;
  90. padding = {0, 0, 0,0},
  91. margin = {0, 0, 0, 0},
  92. backgroundColor = {1, 1, 0, 255},
  93. caption=speeds[i] .."x",
  94. tooltip = "play at " .. speeds[i] .. "x speed";
  95. OnMouseDown = {function()
  96. setReplaySpeed (speeds[i], i)
  97. button_setspeed.backgroundColor = {0, 0, 1, 1}
  98. end}
  99. }
  100. end
  101.  
  102. button_skipPreGame = Button:New {
  103. width = 180,
  104. height = 20,
  105. y = 55,
  106. x = 100,
  107. parent=window;
  108. padding = {0, 0, 0,0},
  109. margin = {0, 0, 0, 0},
  110. backgroundColor = {1, 1, 1, 1},
  111. caption="skip pregame chatter",
  112. tooltip = "Skip the pregame chat and startposition chosing, directly to the action!";
  113. OnMouseDown = {function()
  114. skipPreGameChatter ()
  115. end}
  116. }
  117.  
  118. button_startStop = Button:New {
  119. width = 80,
  120. height = 20,
  121. y = 55,
  122. x = 10,
  123. parent=window;
  124. padding = {0, 0, 0,0},
  125. margin = {0, 0, 0, 0},
  126. backgroundColor = {1, 1, 1, 1},
  127. caption="pause", --pause/continue
  128. tooltip = "pause or continue playback";
  129. OnMouseDown = {function()
  130. if (isPaused) then
  131. unpause ()
  132. else
  133. pause ()
  134. end
  135. end}
  136. }
  137.  
  138. progress_speed = Progressbar:New{
  139. parent = window,
  140. y = 8,
  141. x = 10,
  142. width = 280,
  143. height = 20,
  144. max = #speeds;
  145. caption = "replay speed";
  146. color = (i == 1 and {0.2,0.9,0.3,1}) or {0.9,0.15,0.2,1};
  147. value = 1 +1,
  148. }
  149.  
  150. screen0:AddChild(window)
  151.  
  152. end
  153.  
  154. function pause ()
  155. Spring.Echo ("playback paused")
  156. Spring.SendCommands ("pause 1")
  157. isPaused = true
  158. button_startStop:SetCaption ("play")
  159. --window:SetColor ({1,0,0, 1})
  160. --window:SetCaption ("trololo")--button stays pressed down and game lags ANY INVALID CODE MAKES IT LAG, REASON WHY COM MORPH LAGS?
  161. end
  162.  
  163. function unpause ()
  164. Spring.Echo ("playback continued")
  165. Spring.SendCommands ("pause 0")
  166. isPaused = false
  167. button_startStop:SetCaption ("pause")
  168. end
  169.  
  170.  
  171. function setReplaySpeed (speed, i)
  172. --put something invalid in these button function like:
  173. --doesNotExist[3] = 5
  174. --and there will be no error message. However, the game will stutter for a second
  175.  
  176. local s = Spring.GetGameSpeed()
  177. --Spring.Echo ("setting speed to: " .. speed .. " current is " .. s)
  178. if (speed > s) then --speedup
  179. Spring.SendCommands ("setminspeed " .. speed)
  180. Spring.SendCommands ("setminspeed " ..0.1)
  181. else --slowdown
  182. wantedSpeed = speed
  183. --[[
  184. --does not work:
  185. Spring.SendCommands ("slowdown")
  186. Spring.SendCommands ("slowdown")
  187. Spring.SendCommands ("slowdown")
  188. Spring.SendCommands ("slowdown")
  189. Spring.SendCommands ("slowdown")
  190. --does not work:
  191. local i = 0
  192. while (Spring.GetGameSpeed() > speed and i < 50) do
  193. --Spring.SendCommands ("setminspeed " ..0.4)
  194. --Spring.SendCommands ("setmaxpeed " .. speed)
  195. Spring.SendCommands ("slowdown")
  196. i=i+1
  197. end
  198. --]]
  199. end
  200. --Spring.SendCommands ("setmaxpeed " .. speed)
  201. progress_speed:SetValue(i)
  202. end
  203.  
  204. function widget:Update()
  205. if (wantedSpeed) then
  206. if (Spring.GetGameSpeed() > wantedSpeed) then
  207. Spring.SendCommands ("slowdown")
  208. else
  209. wantedSpeed = nil
  210. end
  211. end
  212. end
  213.  
  214. function widget:GameFrame (f)
  215. Spring.Echo (f)
  216. if (f==1) then
  217. window:RemoveChild(button_skipPreGame)
  218. end
  219. end
  220.  
  221. function skipPreGameChatter ()
  222. Spring.Echo ("skipping pre game chatter")
  223. Spring.SendCommands ("skip 1")
  224. window:RemoveChild(button_skipPreGame)
  225. end
Advertisement
Add Comment
Please, Sign In to add comment