Guest User

Untitled

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