--------------------------- --------------------------- -- Extension Example. -- Loads the AVI/ZIP/COM extensions. -- Display an Excel Spreadsheet. -- Plays an AVI file. -- Opens a Zip file. -- Copyright (c) 2007-09 Capricorn 76 Pty. Ltd. --------------------------- --------------------------- local subGui = nil; local extCOM = nil; local extAVI = nil; local aviReader = nil; local extZip = nil; -- Load modules used by this demo require("luacom"); require("luaavi"); require("luazip"); -- Check that everything loaded ok assert(luacom, 'Lua COM module failed to load'); assert(luaavi, 'Lua AVI module failed to load'); assert(luazip, 'Lua ZIP module failed to load'); --------------------------- -- This function is called by the main program when the world is loaded --------------------------- function OnWorldLoad(worldName) -- Feel free to use the CAVIReader class in your scripts. -- Be sure to copy the code into your program folders in case it changes in future ScriptENGINE versions. IApp:loadScript('CAVIReader'); -- Load GUI IGraphics:loadGui('extension'); subGui = IEvents:subscribeGui('OnGui'); return true; end --------------------------- -- This function is called by the main program when the world is unloaded --------------------------- function OnWorldUnload(worldName) if (subGui) then IEvents:unsubscribe(subGui); end if (aviReader) then aviReader:destroy(); end end local function doCompanySpreadsheet() local appExcel = luacom.CreateObject("Excel.Application") if not appExcel then IApp:showMessageBoxOk('Microsoft Excel is not installed on your computer. Cannot open spreadsheet.', 'Error'); else IMouse:setType('WAIT'); local filename = '[Extensions] Using AVIs, ZIPs and COM via extensions\\Resource\\CompanyInfo.xls'; if (IApp:fileExists(filename)) then filename = IApp:getBasePath() .. '\\' .. filename; else filename = IApp:getFilenameFromFileSystem(filename); end filename = IApp:pathClean(filename, '\\'); appExcel.Workbooks:Open(tostring(filename)); appExcel.Visible = true; -- appExcel.ActiveSheet:PrintPreview(); IMouse:setType('ARROW'); end end local function doZipExtension() local filename = IApp:showOpenFileDialog(nil, 'Zip files (*.zip):*.zip', 0, 'Select a zip file', false, false); if (filename) then IMouse:setType('WAIT'); -- Open the zip local zipId = luazip.open(filename, true); if (zipId) then -- Display the filenames IGraphics:loadGui('extensionZip'); local i = 1; for i = 1, luazip.getNumberOfItems(zipId) do local itemData = luazip.getItemInfo(zipId, i); local item; if (itemData.isDirectory) then item = string.format('%20s', itemData.name); else item = string.format('%20s (compress ratio %.1f%%)', itemData.name, 100*itemData.compressedSize/itemData.uncompressedSize); end IGraphics:addListboxItem('listZipItems', item); end luazip.close(zipId); IMouse:setType('ARROW'); else IMouse:setType('ARROW'); IApp:showMessageBoxOk('Failed to open zip file', 'Open zip'); end end end local function closeZipExtension() IGraphics:removeControl('tabZip'); end local function doAVIExtension() -- Play the intro avi if (aviReader) then aviReader:destroy(); aviReader = nil; end -- Create a new instance of the CAVIReader class local filename = IApp:getFilenameFromFileSystem('./Resource/demo.avi'); aviReader = IMisc:classCreateInstance(CAVIReader, filename); if (aviReader and aviReader:isOpen()) then -- Create a texture to send AVI image data to IGraphics:createTexture('aviTexture', '256 256', 3); local left = (IGraphics:getCurrentVideoModeWidth()-256)/2/IGraphics:getCurrentVideoModeWidth(); local top = (IGraphics:getCurrentVideoModeHeight()-256)/2/IGraphics:getCurrentVideoModeHeight(); -- Create a background tab to dull the current gui IGraphics:createTab('tabAVI', '0 0 1 1', true, '0 0 0 0.7'); -- Create an image to assign the texture to IGraphics:createImage('aviImage', 'aviTexture', IMisc:rectCreate(left, top, 1, 1), false, false); -- Play the AVI, wait for it to finish aviReader:play('aviTexture', false, 'extAVIComplete'); elseif (aviReader) then IApp:showMessageBoxOk('Your computer does not have the required AVI decompression library installed. This demo AVI cannot be played.', 'AVI Information'); end end function extAVIComplete(aviReaderInstance) aviReader:destroy(); aviReader = nil; IGraphics:removeControl('aviImage'); IGraphics:removeControl('tabAVI'); end --------------------------- -- GUI handler --------------------------- function OnGui(callerId, eventType, userData) if (IGraphics:buttonClicked('btnBack', callerId, eventType)) then if (soundMenuClick) then ISound:play(soundMenuClick); end IApp:stop(); elseif (IGraphics:buttonClicked('btnCreateCompanySpreadsheet', callerId, eventType)) then doCompanySpreadsheet(); elseif (IGraphics:buttonClicked('btnCreateTransactionSpreadsheet', callerId, eventType)) then elseif (IGraphics:buttonClicked('btnOpenZip', callerId, eventType)) then doZipExtension(); elseif (IGraphics:buttonClicked('btnZipClose', callerId, eventType)) then closeZipExtension(); elseif (IGraphics:buttonClicked('btnPlayAVI', callerId, eventType)) then doAVIExtension(); end end
Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:12 2009)