filterlist.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. --Minetest
  2. --Copyright (C) 2013 sapier
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. --------------------------------------------------------------------------------
  18. -- Generic implementation of a filter/sortable list --
  19. --------------------------------------------------------------------------------
  20. filterlist = {}
  21. --------------------------------------------------------------------------------
  22. function filterlist.refresh(this)
  23. this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param)
  24. filterlist.process(this)
  25. end
  26. --------------------------------------------------------------------------------
  27. function filterlist.create(raw_fct,compare_fct,uid_match_fct,filter_fct,fetch_param)
  28. assert((raw_fct ~= nil) and (type(raw_fct) == "function"))
  29. assert((compare_fct ~= nil) and (type(compare_fct) == "function"))
  30. local this = {}
  31. this.m_raw_list_fct = raw_fct
  32. this.m_compare_fct = compare_fct
  33. this.m_filter_fct = filter_fct
  34. this.m_uid_match_fct = uid_match_fct
  35. this.m_filtercriteria = nil
  36. this.m_fetch_param = fetch_param
  37. this.m_sortmode = "none"
  38. this.m_sort_list = {}
  39. this.m_processed_list = nil
  40. this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param)
  41. filterlist.process(this)
  42. return this
  43. end
  44. --------------------------------------------------------------------------------
  45. function filterlist.add_sort_mechanism(this,name,fct)
  46. this.m_sort_list[name] = fct
  47. end
  48. --------------------------------------------------------------------------------
  49. function filterlist.set_filtercriteria(this,criteria)
  50. if criteria == this.m_filtercriteria and
  51. type(criteria) ~= "table" then
  52. return
  53. end
  54. this.m_filtercriteria = criteria
  55. filterlist.process(this)
  56. end
  57. --------------------------------------------------------------------------------
  58. function filterlist.get_filtercriteria(this)
  59. return this.m_filtercriteria
  60. end
  61. --------------------------------------------------------------------------------
  62. --supported sort mode "alphabetic|none"
  63. function filterlist.set_sortmode(this,mode)
  64. if (mode == this.m_sortmode) then
  65. return
  66. end
  67. this.m_sortmode = mode
  68. filterlist.process(this)
  69. end
  70. --------------------------------------------------------------------------------
  71. function filterlist.get_list(this)
  72. return this.m_processed_list
  73. end
  74. --------------------------------------------------------------------------------
  75. function filterlist.get_raw_list(this)
  76. return this.m_raw_list
  77. end
  78. --------------------------------------------------------------------------------
  79. function filterlist.get_raw_element(this,idx)
  80. if type(idx) ~= "number" then
  81. idx = tonumber(idx)
  82. end
  83. if idx ~= nil and idx > 0 and idx < #this.m_raw_list then
  84. return this.m_raw_list[idx]
  85. end
  86. return nil
  87. end
  88. --------------------------------------------------------------------------------
  89. function filterlist.get_raw_index(this,listindex)
  90. assert(this.m_processed_list ~= nil)
  91. if listindex ~= nil and listindex > 0 and
  92. listindex <= #this.m_processed_list then
  93. local entry = this.m_processed_list[listindex]
  94. for i,v in ipairs(this.m_raw_list) do
  95. if this.m_compare_fct(v,entry) then
  96. return i
  97. end
  98. end
  99. end
  100. return 0
  101. end
  102. --------------------------------------------------------------------------------
  103. function filterlist.get_current_index(this,listindex)
  104. assert(this.m_processed_list ~= nil)
  105. if listindex ~= nil and listindex > 0 and
  106. listindex <= #this.m_raw_list then
  107. local entry = this.m_raw_list[listindex]
  108. for i,v in ipairs(this.m_processed_list) do
  109. if this.m_compare_fct(v,entry) then
  110. return i
  111. end
  112. end
  113. end
  114. return 0
  115. end
  116. --------------------------------------------------------------------------------
  117. function filterlist.process(this)
  118. assert(this.m_raw_list ~= nil)
  119. if this.m_sortmode == "none" and
  120. this.m_filtercriteria == nil then
  121. this.m_processed_list = this.m_raw_list
  122. return
  123. end
  124. this.m_processed_list = {}
  125. for k,v in pairs(this.m_raw_list) do
  126. if this.m_filtercriteria == nil or
  127. this.m_filter_fct(v,this.m_filtercriteria) then
  128. table.insert(this.m_processed_list,v)
  129. end
  130. end
  131. if this.m_sortmode == "none" then
  132. return
  133. end
  134. if this.m_sort_list[this.m_sortmode] ~= nil and
  135. type(this.m_sort_list[this.m_sortmode]) == "function" then
  136. this.m_sort_list[this.m_sortmode](this)
  137. end
  138. end
  139. --------------------------------------------------------------------------------
  140. function filterlist.size(this)
  141. if this.m_processed_list == nil then
  142. return 0
  143. end
  144. return #this.m_processed_list
  145. end
  146. --------------------------------------------------------------------------------
  147. function filterlist.uid_exists_raw(this,uid)
  148. for i,v in ipairs(this.m_raw_list) do
  149. if this.m_uid_match_fct(v,uid) then
  150. return true
  151. end
  152. end
  153. return false
  154. end
  155. --------------------------------------------------------------------------------
  156. function filterlist.raw_index_by_uid(this, uid)
  157. local elementcount = 0
  158. local elementidx = 0
  159. for i,v in ipairs(this.m_raw_list) do
  160. if this.m_uid_match_fct(v,uid) then
  161. elementcount = elementcount +1
  162. elementidx = i
  163. end
  164. end
  165. -- If there are more elements than one with same name uid can't decide which
  166. -- one is meant. This shouldn't be possible but just for sure.
  167. if elementcount > 1 then
  168. elementidx=0
  169. end
  170. return elementidx
  171. end
  172. --------------------------------------------------------------------------------
  173. -- COMMON helper functions --
  174. --------------------------------------------------------------------------------
  175. --------------------------------------------------------------------------------
  176. function compare_worlds(world1,world2)
  177. if world1.path ~= world2.path then
  178. return false
  179. end
  180. if world1.name ~= world2.name then
  181. return false
  182. end
  183. if world1.gameid ~= world2.gameid then
  184. return false
  185. end
  186. return true
  187. end
  188. --------------------------------------------------------------------------------
  189. function sort_worlds_alphabetic(this)
  190. table.sort(this.m_processed_list, function(a, b)
  191. --fixes issue #857 (crash due to sorting nil in worldlist)
  192. if a == nil or b == nil then
  193. if a == nil and b ~= nil then return false end
  194. if b == nil and a ~= nil then return true end
  195. return false
  196. end
  197. if a.name:lower() == b.name:lower() then
  198. return a.name < b.name
  199. end
  200. return a.name:lower() < b.name:lower()
  201. end)
  202. end
  203. --------------------------------------------------------------------------------
  204. function sort_mod_list(this)
  205. table.sort(this.m_processed_list, function(a, b)
  206. -- Show game mods at bottom
  207. if a.typ ~= b.typ then
  208. return b.typ == "game_mod"
  209. end
  210. -- If in same or no modpack, sort by name
  211. if a.modpack == b.modpack then
  212. if a.name:lower() == b.name:lower() then
  213. return a.name < b.name
  214. end
  215. return a.name:lower() < b.name:lower()
  216. -- Else compare name to modpack name
  217. else
  218. -- Always show modpack pseudo-mod on top of modpack mod list
  219. if a.name == b.modpack then
  220. return true
  221. elseif b.name == a.modpack then
  222. return false
  223. end
  224. local name_a = a.modpack or a.name
  225. local name_b = b.modpack or b.name
  226. if name_a:lower() == name_b:lower() then
  227. return name_a < name_b
  228. end
  229. return name_a:lower() < name_b:lower()
  230. end
  231. end)
  232. end