Advertisement
gamax92

tnmm_-_source

Apr 28th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.14 KB | None | 0 0
  1. -- If the Developers add or remove a button on the right toolbar, please change
  2. -- numOfButtons to reflect how many buttons are natural TPT buttons.
  3. -- You can also change buttonIcon to change the button label.
  4. local numOfButtons = 6
  5. local buttonIcon = "V"
  6.  
  7. -- Code below.
  8. -- Check if code already loaded.
  9. if mods_tnmm == true then
  10.     tpt.message_box("MovieMaker", "MovieMaker is already loaded.")
  11.     return false
  12. end
  13.  
  14. mods_tnmm = true
  15.  
  16. -- LuaSrcDiet purposes
  17. local tpt,math,Button,tostring = tpt,math,Button,tostring
  18.  
  19. -- Check if the Videos folder exists, create it if it doesn't, show a mesagebox if creating it failed
  20. if not fs.exists("Videos") then
  21.     if not fs.makeDirectory("Videos") then
  22.         tpt.message_box("MovieMaker - Creating folder failed","Please create folder 'Videos' in\nTPTs directory manually.")
  23.     end
  24. end
  25.  
  26. --[[
  27. -- Need to fix this code so it checks for windows.
  28. -- Check if ffmpeg.exe exists.
  29. if not fs.exists("ffmpeg.exe") then
  30.     tpt.message_box("MovieMaker - FFMPEG not found","Please copy an version of FFMPEG in\nTPTs directory and name it 'ffmpeg.exe'.")
  31. end
  32. --]]
  33.  
  34. local recording = 0
  35. local recordfps = 30
  36. local recordqual = 3
  37. local recordframes = 0
  38. local recordft = ".avi"
  39. local recorduiseen = 0
  40. local recorduitype = 0
  41. local recordhud = 0
  42. local recordcf = 0
  43. local recordds
  44.  
  45. if package.config:sub(1,1) == "\\" then
  46.     recordds = "\\"
  47. else
  48.     recordds = "/"
  49. end
  50.  
  51. local uix = math.floor((612 / 2) - (149 / 2))
  52. local uiy = math.floor((384 / 2) - (59 / 2))
  53.  
  54. local function load_last()
  55.     local f = io.open("MovieMakerSettings.txt", "rb")
  56.     if f ~= nil then
  57.         while true do
  58.             line = f:read("*l")
  59.             if line == nil then
  60.                 break
  61.             elseif line ~= "" then
  62.                 local key, val = line:match("(.-) (.*)")
  63.                 if key == "FPS" then
  64.                     recordfps = tonumber(val)
  65.                 elseif key == "COD" then
  66.                     recordft = val
  67.                 elseif key == "FUL" then
  68.                     recordhud = tonumber(val)
  69.                 elseif key == "QUA" then
  70.                     recordqual = tonumber(val)
  71.                 elseif key == "FOC" then
  72.                     recordcf = tonumber(val)
  73.                 end
  74.             end
  75.         end
  76.         f:close()
  77.     end
  78. end
  79.  
  80. -- Save settings to a file
  81. local function save_last()
  82.     local savetable = {
  83.         FPS = recordfps,
  84.         COD = recordft,
  85.         FUL = recordhud,
  86.         QUA = recordqual,
  87.         FOC = recordcf,
  88.     }
  89.     local savestring = ""
  90.     for k,v in pairs(savetable) do
  91.         savestring = savestring .. k .. " " .. tostring(v) .. "\n"
  92.     end
  93.     savestring = savestring:sub(1,-2)
  94.     local f = io.open("MovieMakerSettings.txt", "wb")
  95.     if f ~= nil then
  96.         f:write(savestring)
  97.         f:close()
  98.     end
  99. end
  100.  
  101. -- Show S button, recorded seconds, and recorded frames when recording.
  102. local function recordsui()
  103.     recordframes = recordframes + 1
  104.     tpt.drawtext(5,385,tostring(math.floor(recordframes / 60)) .. " s")
  105.     tpt.drawtext(5,395,tostring(math.floor(recordframes)) .. " F")
  106. end
  107.  
  108. -- Screenshot wrapper for HUD mode.
  109. local function screensh()
  110.     tpt.screenshot(recordhud, recordcf == 1 and 0 or 2)
  111. end
  112.  
  113. load_last()
  114.  
  115. -- Create Buttons and stuff.
  116.  
  117. local toolButton = Button:new(613, (numOfButtons * 16) + 1, 15, 15, buttonIcon)
  118. local toolButton2 = Button:new(613 - uix, ((numOfButtons * 16) + 1) - uiy, 15, 15, buttonIcon)
  119.  
  120. local testWindowMenu = Button:new(109, 2, 38, 13, "Options")
  121.  
  122. local testWindow = Window:new(uix, uiy, 149, 59)
  123. local testWindowAVI = Checkbox:new(0, 0, 13, 13, "AVI Out")
  124. testWindowAVI:checked(recordft == ".avi")
  125. local testWindowGIF = Checkbox:new(53, 0, 13, 13, "GIF Out")
  126. testWindowGIF:checked(recordft ~= ".avi")
  127. local testWindowClean = Button:new(2, 30, 72, 13, "Clean folder")
  128. local testWindowRec = Button:new(75, 30, 72, 13, "Record")
  129. local testWindowCONV = Button:new(2, 44, 145, 13, "Convert")
  130.  
  131. local testWindowFOC = Checkbox:new(0, 0, 13, 13, "Frame Compress")
  132. testWindowFOC:checked(recordcf == 1)
  133. local testWindowHUD = Checkbox:new(0, 13, 13, 13, "Record HUD")
  134. testWindowHUD:checked(recordhud == 1)
  135. local testWindowFPSL = Label:new(2, 30, tpt.textwidth("Video framerate:"), 13, "Video framerate:")
  136. local testWindowFPSM = Button:new(99, 30, 13, 13, "-")
  137. local testWindowFPST = Textbox:new(113, 30, 20, 13, tostring(recordfps), "FPS")
  138. testWindowFPST:readonly(true)
  139. local testWindowFPSP = Button:new(134, 30, 13, 13, "+")
  140. local testWindowCOMPL = Label:new(2, 44, tpt.textwidth("Compression:"), 13, "Compression:")
  141. local testWindowCOMPM = Button:new(99, 44, 13, 13, "-")
  142. local testWindowCOMPT = Textbox:new(113, 44, 20, 13, tostring(recordqual), "FPS")
  143. testWindowCOMPT:readonly(true)
  144. local testWindowCOMPP = Button:new(134, 44, 13, 13, "+")
  145.  
  146. -- Add controls to windows.
  147. interface.addComponent(toolButton)
  148. testWindow:addComponent(toolButton2)
  149. testWindow:addComponent(testWindowMenu)
  150.  
  151. -- Main Controls
  152. testWindow:addComponent(testWindowAVI)
  153. testWindow:addComponent(testWindowGIF)
  154. testWindow:addComponent(testWindowClean)
  155. testWindow:addComponent(testWindowRec)
  156. testWindow:addComponent(testWindowCONV)
  157.  
  158. -- Options Controls
  159. testWindow:addComponent(testWindowFOC)
  160. testWindow:addComponent(testWindowHUD)
  161. testWindow:addComponent(testWindowFPSL)
  162. testWindow:addComponent(testWindowFPSM)
  163. testWindow:addComponent(testWindowFPST)
  164. testWindow:addComponent(testWindowFPSP)
  165. testWindow:addComponent(testWindowCOMPL)
  166. testWindow:addComponent(testWindowCOMPM)
  167. testWindow:addComponent(testWindowCOMPT)
  168. testWindow:addComponent(testWindowCOMPP)
  169.  
  170. -- Add control handlers.
  171. local function showMain()
  172.     testWindowAVI:visible(true)
  173.     testWindowGIF:visible(true)
  174.     testWindowClean:visible(true)
  175.     testWindowRec:visible(true)
  176.     testWindowCONV:visible(true)
  177.    
  178.     testWindowFOC:visible(false)
  179.     testWindowHUD:visible(false)
  180.     testWindowFPSL:visible(false)
  181.     testWindowFPSM:visible(false)
  182.     testWindowFPST:visible(false)
  183.     testWindowFPSP:visible(false)
  184.     testWindowCOMPL:visible(false)
  185.     testWindowCOMPM:visible(false)
  186.     testWindowCOMPT:visible(false)
  187.     testWindowCOMPP:visible(false)
  188. end
  189.  
  190. local function showOptions()
  191.     testWindowAVI:visible(false)
  192.     testWindowGIF:visible(false)
  193.     testWindowClean:visible(false)
  194.     testWindowRec:visible(false)
  195.     testWindowCONV:visible(false)
  196.    
  197.     testWindowFOC:visible(true)
  198.     testWindowHUD:visible(true)
  199.     testWindowFPSL:visible(true)
  200.     testWindowFPSM:visible(true)
  201.     testWindowFPST:visible(true)
  202.     testWindowFPSP:visible(true)
  203.     testWindowCOMPL:visible(true)
  204.     testWindowCOMPM:visible(true)
  205.     testWindowCOMPT:visible(true)
  206.     testWindowCOMPP:visible(true)
  207. end
  208.  
  209. local function showWindow()
  210.     interface.showWindow(testWindow)
  211.     if recorduitype == 0 then
  212.         showMain()
  213.     else
  214.         showOptions()
  215.     end
  216. end
  217.  
  218. local function toolButtonHDR(self)
  219.     if recording == 0 then
  220.         recorduiseen = 1 - recorduiseen
  221.         if recorduiseen == 1 then
  222.             showWindow()
  223.         else
  224.             interface.closeWindow(testWindow)
  225.         end
  226.         tpt.set_pause(recorduiseen)
  227.         save_last()
  228.     else
  229.         tpt.unregister_step(recordsui)
  230.         tpt.unregister_step(screensh)
  231.         showWindow()
  232.         tpt.set_pause(1)
  233.         recording = 0
  234.         recorduiseen = 1
  235.         tpt.hud(1)
  236.         tpt.message_box("MovieMaker - Recording finished", "Recorded " .. tostring(math.floor(recordframes / 60)) .. " seconds.\nDon't forget to Convert frames to a video!")
  237.     end
  238. end
  239.  
  240.  
  241. local function testWindowSwitch(self)
  242.     recorduitype = 1 - recorduitype
  243.     if recorduitype == 0 then
  244.         showMain()
  245.     else
  246.         showOptions()
  247.     end
  248.     save_last()
  249. end
  250.  
  251. local function testWindowAVIHdr(self)
  252.     testWindowAVI:checked(true)
  253.     testWindowGIF:checked(false)
  254.     recordft = ".avi"
  255. end
  256.  
  257. local function testWindowGIFHdr(self)
  258.     testWindowAVI:checked(false)
  259.     testWindowGIF:checked(true)
  260.     recordft = ".gif"
  261. end
  262.  
  263. local function cleanFolder(self)
  264.     local list = fs.list(".")
  265.     local delcount = 0
  266.     for k,v in pairs(list) do
  267.         if v:sub(1,11) == "screenshot_" then
  268.             fs.removeFile(v)
  269.             delcount = delcount + 1
  270.         end
  271.     end
  272.     tpt.message_box("MovieMaker - Cleanup finished", "Deleted " .. tostring(math.floor(delcount / 60)) .. " seconds of video.")
  273.     recordframes = 0
  274. end
  275.  
  276. local function record(self)
  277.     -- Start recording.
  278.     save_last()
  279.     tpt.hud(recordhud)
  280.     interface.closeWindow(testWindow)
  281.     tpt.register_step(recordsui)
  282.     tpt.register_step(screensh)
  283.     tpt.set_pause(0)
  284.     recording = 1
  285. end
  286.  
  287. local function convertVid(self)
  288.     -- Slide frame indexes down.
  289.     local list = fs.list(".")
  290.     table.sort(list)
  291.     local index = 0
  292.     for k, v in ipairs(list) do
  293.         if v:sub(1,11) == "screenshot_" then
  294.             fs.move(v, "screenshot_" .. string.format("%06d", index) .. "." .. (recordcf == 1 and "png" or "ppm"))
  295.             index = index + 1
  296.         end
  297.     end
  298.    
  299.     -- Convert frames to video
  300.     local videocount = 1
  301.     while fs.exists("Videos" .. recordds .. "tptvideo" .. string.format("%04d",videocount) .. recordft) == true do
  302.         videocount = videocount + 1
  303.     end
  304.     os.execute("ffmpeg -r 60 -i screenshot_%06d." .. (recordcf == 1 and "png" or "ppm") .. " -q:v " .. recordqual .. " -r " .. recordfps .. " Videos" .. recordds .. "tptvideo" .. string.format("%04d",videocount) .. recordft)
  305.     tpt.message_box("MovieMaker - Converting finished", "Converted " .. math.floor( index / 60 ) .. " seconds of video.\nDon't forget to clean your folder!")
  306. end
  307.  
  308. local function testWindowSPD(self)
  309.     recordcf = self:checked() == true and 1 or 0
  310. end
  311.  
  312. local function testWindowFUL(self)
  313.     recordhud = self:checked() == true and 1 or 0
  314. end
  315.  
  316. local function testWindowFPSS(self)
  317.     if recordfps > 1 then recordfps = recordfps - 1 end
  318.     testWindowFPST:text(tostring(recordfps))
  319. end
  320.  
  321. local function testWindowFPSA(self)
  322.     if recordfps < 60 then recordfps = recordfps + 1 end
  323.     testWindowFPST:text(tostring(recordfps))
  324. end
  325.  
  326. local function testWindowCOMPS(self)
  327.     if recordqual > 1 then recordqual = recordqual - 1 end
  328.     testWindowCOMPT:text(tostring(recordqual))
  329. end
  330.  
  331. local function testWindowCOMPA(self)
  332.     if recordqual < 31 then recordqual = recordqual + 1 end
  333.     testWindowCOMPT:text(tostring(recordqual))
  334. end
  335.  
  336. toolButton:action(toolButtonHDR)
  337. toolButton2:action(toolButtonHDR)
  338. testWindowMenu:action(testWindowSwitch)
  339.  
  340. testWindowAVI:action(testWindowAVIHdr)
  341. testWindowGIF:action(testWindowGIFHdr)
  342. testWindowClean:action(cleanFolder)
  343. testWindowRec:action(record)
  344. testWindowCONV:action(convertVid)
  345.  
  346. testWindowFOC:action(testWindowSPD)
  347. testWindowHUD:action(testWindowFUL)
  348. testWindowFPSM:action(testWindowFPSS)
  349. testWindowFPSP:action(testWindowFPSA)
  350. testWindowCOMPM:action(testWindowCOMPS)
  351. testWindowCOMPP:action(testWindowCOMPA)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement