Code: Select all
;Create Image DataSection from folder of images
;IdeasVacuum 2012
;Tested PB4.60 Win32
Enumeration
#WinMain
#Path
#Folder
#List
#TabPanel
#BtnBrowse
#BtnCreate
#BtnCopy
#Opt1
#Opt2
#SpinLabel
#SpinEnum
#Code
EndEnumeration
Global FontID1 = LoadFont(1, "CourierNew", 10, #PB_Font_HighQuality)
Global igJpgPluginRequired.i = #False
Global igPngPluginRequired.i = #False
Global NewList sgFileFull.s()
Global igLongestName.i = 0
Global igLocalCatch.i = #True
Procedure ListImageFiles()
;------------------------
Shared igJpgPluginRequired
Shared igPngPluginRequired
Shared igLongestName
Protected iLen.i = 0
Protected sName.s = ""
Protected sFileFull.s = ""
Protected sFile.s = ""
Protected sExt.s = ""
Protected sPath.s = PathRequester("Browse to and select Folder containing images", "")
SetGadgetText(#Folder,sPath)
ClearGadgetItems(#List)
ClearGadgetItems(#Code)
If ExamineDirectory(#Path, sPath, "*.*")
While NextDirectoryEntry(#Path)
If DirectoryEntryType(#Path) = #PB_DirectoryEntry_File
sFile = DirectoryEntryName(#Path)
sExt = GetExtensionPart(sFile)
sName = GetFilePart(sFile)
If( (sExt = "jpg") Or (sExt = "png") Or (sExt = "bmp") )
AddElement(sgFileFull())
sgFileFull() = sPath + sFile
AddGadgetItem(#List, -1, sgFileFull())
iLen = Len(sName)
If(iLen > igLongestName) : igLongestName = iLen : EndIf
EndIf
If(LCase(sExt) = "jpg") : igJpgPluginRequired = #True : EndIf
If(LCase(sExt) = "png") : igPngPluginRequired = #True : EndIf
EndIf
Wend
FinishDirectory(#Path)
EndIf
EndProcedure
Procedure MakeCode()
;-------------------
Protected iTotalFiles.i = 0
Protected iRow.i = 0
Protected iLen.i = 0
Protected iPad.i = 0
Protected sName.s = ""
Protected sRow.s = ""
Protected Dim sLabel.s(1)
Protected Dim sEnum.s(1)
ClearGadgetItems(#Code)
If(igJpgPluginRequired = #True)
AddGadgetItem(#Code, -1, "UseJPEGImageDecoder()")
AddGadgetItem(#Code, -1, "UseJPEGImageEncoder()")
EndIf
If(igPngPluginRequired = #True)
AddGadgetItem(#Code, -1, "UsePNGImageDecoder()")
AddGadgetItem(#Code, -1, "UsePNGImageEncoder()")
EndIf
iEnum = GetGadgetState(#SpinEnum)
AddGadgetItem(#Code, -1, "")
If(igLocalCatch = #True) : AddGadgetItem(#Code, -1, "Enumeration " + Str(iEnum) + " Step 1") : EndIf
iTotalFiles = ListSize(sgFileFull())
Dim sLabel.s(iTotalFiles)
Dim sEnum.s(iTotalFiles)
FirstElement(sgFileFull())
For iRow = 0 To (iTotalFiles - 1)
sName = GetFilePart(sgFileFull())
ReplaceString(sName, " ", "_", #PB_String_InPlace , 1) ;Replace Space chars
iLen = Len(sName)
If(igLocalCatch = #True)
sEnum(iRow) = "#Image" + Left(sName,(iLen - 4))
sLabel(iRow) = Left(sName,(iLen - 4)) + ":"
AddGadgetItem(#Code, -1, sEnum(iRow))
Else
sLabel(iRow) = Left(sName,(iLen - 4))
iPad = (igLongestName - iLen) + 1
sRow = "Global" + Space(iPad) + "igImg" + sLabel(iRow) + ".i = CatchImage(" + Str(iRow) + ", ?img" + sLabel(iRow) + ")"
AddGadgetItem(#Code, -1, sRow)
sLabel(iRow) = Left(sName,(iLen - 4)) + ":"
EndIf
NextElement(sgFileFull())
Next iRow
If(igLocalCatch = #True) : AddGadgetItem(#Code, -1, "EndEnumeration") : EndIf
AddGadgetItem(#Code, -1, "")
AddGadgetItem(#Code, -1, "DataSection")
FirstElement(sgFileFull())
For iRow = 0 To (iTotalFiles - 1)
AddGadgetItem(#Code, -1, "img" + sLabel(iRow))
sRow = "IncludeBinary " + Chr(34) + sgFileFull() + Chr(34)
AddGadgetItem(#Code, -1, sRow)
NextElement(sgFileFull())
Next iRow
AddGadgetItem(#Code, -1, "EndDataSection")
AddGadgetItem(#Code, -1, "")
AddGadgetItem(#Code, -1, ";Image Gadgets (manual edit!)")
AddGadgetItem(#Code, -1, "")
sCatch.s = ""
If(igLocalCatch = #True)
For iRow = 0 To (iTotalFiles - 1)
sCatch = RTrim(sLabel(iRow),":")
sRow = "ImageGadget(#PB_Any, 0, 0, 0, 0, CatchImage(" + sEnum(iRow) + ", ?img" + sCatch + "))"
AddGadgetItem(#Code, -1, sRow)
Next iRow
Else
For iRow = 0 To (iTotalFiles - 1)
sCatch = RTrim(sLabel(iRow),":")
sRow = "ImageGadget(#PB_Any, 0, 0, 0, 0, igImg" + sCatch + ")"
AddGadgetItem(#Code, -1, sRow)
Next iRow
EndIf
EndProcedure
Procedure CopyCode()
;-------------------
iCnt.i = 0
iRowTotal.i = CountGadgetItems(#Code)
sRow.s = ""
sAll.s = ""
For iCnt = 0 To iRowTotal
sRow = GetGadgetItemText(#Code,iCnt,0)
sAll = sAll + sRow + #CRLF$
Next
SetClipboardText(sAll)
EndProcedure
Procedure OpenWinMain()
;----------------------
If OpenWindow(#WinMain,0,0,700,450,"Data Section Maker",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
StringGadget(#Folder, 5, 10, 610, 25, "File Path")
ButtonGadget(#BtnBrowse, 625, 10, 70, 25, "Browse")
PanelGadget(#TabPanel, 5, 40, 690, 400)
;TAB 0: Files List
;---------------------------------------------------------------------------
AddGadgetItem(#TabPanel, -1, "Files")
ListIconGadget(#List, 10, 10, 660, 355, "Files Found",630,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)
CloseGadgetList()
;TAB 1: Data Section
;---------------------------------------------------------------------------
OpenGadgetList(#TabPanel)
AddGadgetItem(#TabPanel, -1, "DataSection")
OptionGadget(#Opt1, 10, 10, 100, 25, "Local Cache")
OptionGadget(#Opt2, 120, 10, 100, 25, "Global Cache")
TextGadget(#SpinLabel, 265, 16, 80, 20, "Enum Start")
SpinGadget(#SpinEnum, 325, 13, 50, 20, 0, 5000, #PB_Spin_Numeric)
SetGadgetText(#SpinEnum,"0")
ButtonGadget(#BtnCreate, 475, 10, 80, 25, "Create")
ButtonGadget(#BtnCopy, 555, 10, 120, 25, "Copy to Clipboard")
ListIconGadget(#Code, 10, 40, 665, 325, "Code",630,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)
SetGadgetFont(#Code,FontID1)
CloseGadgetList()
EndIf
EndProcedure
OpenWinMain()
;MAIN LOOP
iGdgID.i = 0
iEvent.i = 0
Repeat
iEvent = WaitWindowEvent(1)
Select iEvent
Case #PB_Event_Gadget
iGdgID = EventGadget()
Select iGdgID
Case #BtnBrowse: ListImageFiles()
Case #BtnCreate: MakeCode()
Case #BtnCopy: CopyCode()
Case #Opt1: igLocalCatch = #True
Case #Opt2: igLocalCatch = #False
EndSelect
EndSelect
Until iEvent = #PB_Event_CloseWindow
CloseWindow(#WinMain)
End