dialogs.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. --[[------------------------------------
  2. LoveFS Dialogs v0.8
  3. Pure Lua FileSystem Access - Loveframes interface
  4. Under the MIT license.
  5. copyright(c) 2012 Caldas Lopes aka linux-man
  6. --]]------------------------------------
  7. require 'loveframes'
  8. local lovefs_dir = 'lovefs'
  9. local function normalize(str)
  10. local str2 = ''
  11. for n = 1, #str do
  12. if str:byte(n) < 128 then str2 = str2..str:sub(n, n) end
  13. end
  14. return str2
  15. end
  16. dialog = {}
  17. dialog.__index = dialog
  18. function dialog:refresh()
  19. self.current:SetText(self.window_fs.current)
  20. self.list:Clear()
  21. local i = loveframes.Create('button')
  22. i:SetSize(405, 25)
  23. i.image = up
  24. i:SetText('..')
  25. i.groupIndex = 1
  26. i.OnClick = function(object)
  27. self.window.selectedFile = ''
  28. self.window_fs:cd(object:GetText())
  29. self:refresh()
  30. end
  31. self.list:AddItem(i)
  32. for _, d in ipairs(self.window_fs.dirs) do
  33. local i = loveframes.Create('button')
  34. i:SetSize(405, 25)
  35. i.image = folder
  36. i:SetText(d)
  37. i.groupIndex = 1
  38. i.OnClick = function(object)
  39. self.window.selectedFile = ''
  40. if self.fileinput then self.fileinput.text ='' end
  41. self.window_fs:cd(object:GetText())
  42. self:refresh()
  43. end
  44. self.list:AddItem(i)
  45. end
  46. for _, f in ipairs(self.window_fs.files) do
  47. local i = loveframes.Create('button')
  48. i:SetSize(405, 25)
  49. i.image = file
  50. i:SetText(f)
  51. i.groupIndex = 1
  52. i.OnClick = function(object)
  53. if self.window_fs:isFile(object:GetText()) then
  54. self.window.selectedFile = object:GetText()
  55. if self.fileinput then self.fileinput.text = normalize(self.window.selectedFile) end
  56. end
  57. end
  58. self.list:AddItem(i)
  59. end
  60. end
  61. function dialog:default()
  62. folder = love.graphics.newImage(lovefs_dir..'/folder.png')
  63. file = love.graphics.newImage(lovefs_dir..'/file.png')
  64. up = love.graphics.newImage(lovefs_dir..'/up.png')
  65. self.window = loveframes.Create('frame')
  66. self.window:SetSize(415, 395)
  67. self.window:Center()
  68. self.window.selectedFile = ''
  69. --self.window:SetModal(true) --multichoice and tooltip don't play well with SetModal
  70. local drives = loveframes.Create('multichoice', self.window)
  71. local tooltip = loveframes.Create('tooltip')
  72. tooltip:SetObject(drives)
  73. tooltip:SetPadding(5)
  74. tooltip:SetOffsets(5, -5)
  75. tooltip:SetText('Drives')
  76. drives:SetPos(5, 25+5)
  77. drives:SetSize(100, 25)
  78. drives:SetListHeight(100)
  79. drives:SetPadding(0)
  80. drives:SetSpacing(0)
  81. drives.OnChoiceSelected = function(object, choice)
  82. self.window.selectedFile = ''
  83. self.window_fs:cd(choice)
  84. self:refresh()
  85. end
  86. local _, drive
  87. for _, drive in ipairs(self.window_fs.drives) do
  88. drives:AddChoice(drive)
  89. end
  90. drives:SetChoice(self.window_fs.drives[1])
  91. self.current = loveframes.Create('button', self.window)
  92. local tooltip = loveframes.Create('tooltip')
  93. tooltip:SetObject(self.current)
  94. tooltip:SetPadding(5)
  95. tooltip:SetOffsets(5, -5)
  96. tooltip:SetText('Current Directory')
  97. self.current:SetPos(100+10, 25+5)
  98. self.current:SetSize(300, 25)
  99. self.current.image = folder
  100. self.current.checked = true
  101. self.current.enabled = false
  102. self.list = loveframes.Create('list', self.window)
  103. self.list:SetPos(5, 60)
  104. self.list:SetSize(405, 300)
  105. self.list:SetDisplayType('vertical')
  106. self.list:SetPadding(0)
  107. self.list:SetSpacing(0)
  108. local cancel = loveframes.Create('button', self.window)
  109. cancel:SetPos(410-75-80, 360+5)
  110. cancel:SetSize(75, 25)
  111. cancel:SetText('Cancel')
  112. cancel.OnClick = function(object)
  113. self.window:Remove()
  114. self = nil
  115. end
  116. local ok = loveframes.Create('button', self.window)
  117. ok:SetPos(410-75, 360+5)
  118. ok:SetSize(75, 25)
  119. ok:SetText('OK')
  120. ok.OnClick = function(object)
  121. if self.window.selectedFile ~= '' then
  122. if self.window_fs.os == 'Windows' then self.window_fs.selectedFile = self.window_fs.current..'\\'..self.window.selectedFile
  123. else self.window_fs.selectedFile = self.window_fs.current..'/'..self.window.selectedFile end
  124. self.window:Remove()
  125. self = nil
  126. end
  127. end
  128. end
  129. function loadDialog(window_fs, filters)
  130. local temp = {}
  131. setmetatable(temp, dialog)
  132. temp.window_fs = window_fs
  133. temp:default()
  134. local tb_filters = {}
  135. local _, v
  136. if filters then for _,v in ipairs(filters) do table.insert(tb_filters, v) end end
  137. temp.window:SetName('Load File')
  138. local filter = loveframes.Create('multichoice', temp.window)
  139. local tooltip = loveframes.Create('tooltip')
  140. tooltip:SetObject(filter)
  141. tooltip:SetPadding(5)
  142. tooltip:SetOffsets(5, -5)
  143. tooltip:SetText('Filter')
  144. filter:SetPos(5, 360+5)
  145. filter:SetSize(245, 25)
  146. filter:SetListHeight(100)
  147. filter:SetPadding(0)
  148. filter:SetSpacing(0)
  149. filter.OnChoiceSelected = function(object, choice)
  150. if choice:find('|') then window_fs:setParam(choice:sub(choice:find('|') + 1))
  151. else window_fs:setParam(choice) end
  152. temp:refresh()
  153. end
  154. local _, f
  155. for _, f in ipairs(tb_filters) do
  156. filter:AddChoice(f)
  157. end
  158. filter:SetChoice(tb_filters[1])
  159. if tb_filters[1]:find('|') then window_fs:setParam(tb_filters[1]:sub(tb_filters[1]:find('|') + 1))
  160. else window_fs:setParam(tb_filters[1]) end
  161. temp:refresh()
  162. return temp
  163. end
  164. function saveDialog(window_fs)
  165. local temp = {}
  166. setmetatable(temp, dialog)
  167. temp.window_fs = window_fs
  168. temp:default()
  169. temp.window:SetName('Save File')
  170. temp.fileinput = loveframes.Create('textinput', temp.window)
  171. local tooltip = loveframes.Create('tooltip')
  172. tooltip:SetObject(temp.fileinput)
  173. tooltip:SetPadding(5)
  174. tooltip:SetOffsets(5, -5)
  175. tooltip:SetText('Filename')
  176. temp.fileinput:SetPos(5, 360+5)
  177. temp.fileinput:SetSize(245, 25)
  178. temp.fileinput.OnTextChanged = function(object, text)
  179. temp.window.selectedFile = object:GetText()
  180. end
  181. temp:refresh()
  182. return temp
  183. end