PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/games/CT/luaui/widgets/chili/Controls/imagelistview.lua

http://conflictterra.googlecode.com/
Lua | 278 lines | 214 code | 51 blank | 13 comment | 30 complexity | 0eea2f7ab6b46b150407556911e9a503 MD5 | raw file
  1. --//=============================================================================
  2. ImageListView = LayoutPanel:Inherit{
  3. classname = "imagelistview",
  4. autosize = true,
  5. autoArrangeH = false,
  6. autoArrangeV = false,
  7. centerItems = false,
  8. iconX = 64,
  9. iconY = 64,
  10. itemMargin = {1, 1, 1, 1},
  11. selectable = true,
  12. multiSelect = true,
  13. items = {},
  14. dir = '',
  15. }
  16. local this = ImageListView
  17. local inherited = this.inherited
  18. --//=============================================================================
  19. local image_exts = {'.jpg','.bmp','.png','.tga','.dds','.ico','.gif','.psd','.tif'} --'.psp'
  20. --//=============================================================================
  21. function ImageListView:New(obj)
  22. obj = inherited.New(self,obj)
  23. obj:SetDir(obj.dir)
  24. return obj
  25. end
  26. --//=============================================================================
  27. local function GetParentDir(dir)
  28. dir = dir:gsub("\\", "/")
  29. local lastChar = dir:sub(-1)
  30. if (lastChar == "/") then
  31. dir = dir:sub(1,-2)
  32. end
  33. local pos,b,e,match,init,n = 1,1,1,1,0,0
  34. repeat
  35. pos,init,n = b,init+1,n+1
  36. b,init,match = dir:find("/",init,true)
  37. until (not b)
  38. if (n==1) then
  39. return ''
  40. else
  41. return dir:sub(1,pos)
  42. end
  43. end
  44. local function ExtractFileName(filepath)
  45. filepath = filepath:gsub("\\", "/")
  46. local lastChar = filepath:sub(-1)
  47. if (lastChar == "/") then
  48. filepath = filepath:sub(1,-2)
  49. end
  50. local pos,b,e,match,init,n = 1,1,1,1,0,0
  51. repeat
  52. pos,init,n = b,init+1,n+1
  53. b,init,match = filepath:find("/",init,true)
  54. until (not b)
  55. if (n==1) then
  56. return filepath
  57. else
  58. return filepath:sub(pos+1)
  59. end
  60. end
  61. local function ExtractDir(filepath)
  62. filepath = filepath:gsub("\\", "/")
  63. local lastChar = filepath:sub(-1)
  64. if (lastChar == "/") then
  65. filepath = filepath:sub(1,-2)
  66. end
  67. local pos,b,e,match,init,n = 1,1,1,1,0,0
  68. repeat
  69. pos,init,n = b,init+1,n+1
  70. b,init,match = filepath:find("/",init,true)
  71. until (not b)
  72. if (n==1) then
  73. return filepath
  74. else
  75. return filepath:sub(1,pos)
  76. end
  77. end
  78. --//=============================================================================
  79. function ImageListView:_AddFile(name,imagefile)
  80. self:AddChild( LayoutPanel:New{
  81. width = self.iconX+10,
  82. height = self.iconY+20,
  83. padding = {0,0,0,0},
  84. itemPadding = {0,0,0,0},
  85. itemMargin = {0,0,0,0},
  86. rows = 2,
  87. columns = 1,
  88. children = {
  89. Image:New{
  90. width = self.iconX,
  91. height = self.iconY,
  92. passive = true,
  93. file = ':clr' .. self.iconX .. ',' .. self.iconY .. ':' .. imagefile,
  94. },
  95. Label:New{
  96. width = self.iconX+10,
  97. height = 20,
  98. align = 'center',
  99. autosize = false,
  100. caption = name,
  101. },
  102. },
  103. })
  104. end
  105. function ImageListView:ScanDir()
  106. local files = VFS.DirList(self.dir)
  107. local dirs = VFS.SubDirs(self.dir)
  108. local imageFiles = {}
  109. for i=1,#files do
  110. local f = files[i]
  111. local ext = (f:GetExt() or ""):lower()
  112. if (table.ifind(image_exts,ext))then
  113. imageFiles[#imageFiles+1]=f
  114. end
  115. end
  116. self._dirsNum = #dirs
  117. self._dirList = dirs
  118. local n = 1
  119. local items = self.items
  120. items[n] = '..'
  121. n = n+1
  122. for i=1,#dirs do
  123. items[n],n=dirs[i],n+1
  124. end
  125. for i=1,#imageFiles do
  126. items[n],n=imageFiles[i],n+1
  127. end
  128. if (#items>n-1) then
  129. for i=n,#items do
  130. items[i] = nil
  131. end
  132. end
  133. self:DisableRealign()
  134. --// clear old
  135. for i=#self.children,1,-1 do
  136. self:RemoveChild(self.children[i])
  137. end
  138. --// add ".."
  139. self:_AddFile('..',self.imageFolderUp)
  140. --// add dirs at top
  141. for i=1,#dirs do
  142. self:_AddFile(ExtractFileName(dirs[i]),self.imageFolder)
  143. end
  144. --// add files
  145. for i=1,#imageFiles do
  146. self:_AddFile(ExtractFileName(imageFiles[i]),imageFiles[i])
  147. end
  148. self:EnableRealign()
  149. end
  150. function ImageListView:SetDir(directory)
  151. self:DeselectAll()
  152. self.dir = directory
  153. self:ScanDir()
  154. if (self.parent) then
  155. self.parent:RequestRealign()
  156. else
  157. self:UpdateLayout()
  158. self:Invalidate()
  159. end
  160. end
  161. function ImageListView:GotoFile(filepath)
  162. local dir = ExtractDir(filepath)
  163. local file = ExtractFileName(filepath)
  164. self.dir = dir
  165. self:ScanDir()
  166. self:Select(file)
  167. if (self.parent) then
  168. if (self.parent.classname == "scrollpanel") then
  169. local x,y,w,h = self:GetItemXY(next(self.selectedItems))
  170. self.parent:SetScrollPos(x+w*0.5, y+h*0.5, true)
  171. end
  172. self.parent:RequestRealign()
  173. else
  174. self:UpdateLayout()
  175. self:Invalidate()
  176. end
  177. end
  178. --//=============================================================================
  179. function ImageListView:Select(item)
  180. if (type(item)=="number") then
  181. self:SelectItem(item)
  182. else
  183. local items = self.items
  184. for i=1,#items do
  185. if (ExtractFileName(items[i])==item) then
  186. self:SelectItem(i)
  187. return
  188. end
  189. end
  190. self:SelectItem(1)
  191. end
  192. end
  193. --//=============================================================================
  194. function ImageListView:DrawItemBkGnd(index)
  195. local cell = self._cells[index]
  196. local itemPadding = self.itemPadding
  197. if (self.selectedItems[index]) then
  198. self:DrawItemBackground(cell[1] - itemPadding[1],cell[2] - itemPadding[2],cell[3] + itemPadding[1] + itemPadding[3],cell[4] + itemPadding[2] + itemPadding[4],"selected")
  199. else
  200. self:DrawItemBackground(cell[1] - itemPadding[1],cell[2] - itemPadding[2],cell[3] + itemPadding[1] + itemPadding[3],cell[4] + itemPadding[2] + itemPadding[4],"normal")
  201. end
  202. end
  203. --//=============================================================================
  204. function ImageListView:HitTest(x,y)
  205. local cx,cy = self:LocalToClient(x,y)
  206. local obj = inherited.HitTest(self,cx,cy)
  207. if (obj) then return obj end
  208. local itemIdx = self:GetItemIndexAt(cx,cy)
  209. return (itemIdx>=0) and self
  210. end
  211. function ImageListView:MouseDblClick(x,y)
  212. local cx,cy = self:LocalToClient(x,y)
  213. local itemIdx = self:GetItemIndexAt(cx,cy)
  214. if (itemIdx<0) then return end
  215. if (itemIdx==1) then
  216. self:SetDir(GetParentDir(self.dir))
  217. return self
  218. end
  219. if (itemIdx<=self._dirsNum+1) then
  220. self:SetDir(self._dirList[itemIdx-1])
  221. return self
  222. else
  223. self:CallListeners(self.OnDblClickItem, self.items[itemIdx], itemIdx)
  224. return self
  225. end
  226. end
  227. --//=============================================================================