1 --------------------------------------------------------------------------------
2 -- Generic implementation of a filter/sortable list --
3 --------------------------------------------------------------------------------
6 --------------------------------------------------------------------------------
7 function filterlist.refresh(this)
8 this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param)
9 filterlist.process(this)
12 --------------------------------------------------------------------------------
13 function filterlist.create(raw_fct,compare_fct,uid_match_fct,filter_fct,fetch_param)
15 assert((raw_fct ~= nil) and (type(raw_fct) == "function"))
16 assert((compare_fct ~= nil) and (type(compare_fct) == "function"))
20 this.m_raw_list_fct = raw_fct
21 this.m_compare_fct = compare_fct
22 this.m_filter_fct = filter_fct
23 this.m_uid_match_fct = uid_match_fct
25 this.m_filtercriteria = nil
26 this.m_fetch_param = fetch_param
28 this.m_sortmode = "none"
31 this.m_processed_list = nil
32 this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param)
34 filterlist.process(this)
39 --------------------------------------------------------------------------------
40 function filterlist.add_sort_mechanism(this,name,fct)
41 this.m_sort_list[name] = fct
44 --------------------------------------------------------------------------------
45 function filterlist.set_filtercriteria(this,criteria)
46 if criteria == this.m_filtercriteria and
47 type(criteria) ~= "table" then
50 this.m_filtercriteria = criteria
51 filterlist.process(this)
54 --------------------------------------------------------------------------------
55 function filterlist.get_filtercriteria(this)
56 return this.m_filtercriteria
59 --------------------------------------------------------------------------------
60 --supported sort mode "alphabetic|none"
61 function filterlist.set_sortmode(this,mode)
62 if (mode == this.m_sortmode) then
65 this.m_sortmode = mode
66 filterlist.process(this)
69 --------------------------------------------------------------------------------
70 function filterlist.get_list(this)
71 return this.m_processed_list
74 --------------------------------------------------------------------------------
75 function filterlist.get_raw_list(this)
76 return this.m_raw_list
79 --------------------------------------------------------------------------------
80 function filterlist.get_raw_element(this,idx)
81 if type(idx) ~= "number" then
85 if idx ~= nil and idx > 0 and idx < #this.m_raw_list then
86 return this.m_raw_list[idx]
92 --------------------------------------------------------------------------------
93 function filterlist.get_raw_index(this,listindex)
94 assert(this.m_processed_list ~= nil)
96 if listindex ~= nil and listindex > 0 and
97 listindex <= #this.m_processed_list then
98 local entry = this.m_processed_list[listindex]
100 for i,v in ipairs(this.m_raw_list) do
102 if this.m_compare_fct(v,entry) then
111 --------------------------------------------------------------------------------
112 function filterlist.get_current_index(this,listindex)
113 assert(this.m_processed_list ~= nil)
115 if listindex ~= nil and listindex > 0 and
116 listindex <= #this.m_raw_list then
117 local entry = this.m_raw_list[listindex]
119 for i,v in ipairs(this.m_processed_list) do
121 if this.m_compare_fct(v,entry) then
130 --------------------------------------------------------------------------------
131 function filterlist.process(this)
132 assert(this.m_raw_list ~= nil)
134 if this.m_sortmode == "none" and
135 this.m_filtercriteria == nil then
136 this.m_processed_list = this.m_raw_list
140 this.m_processed_list = {}
142 for k,v in pairs(this.m_raw_list) do
143 if this.m_filtercriteria == nil or
144 this.m_filter_fct(v,this.m_filtercriteria) then
145 table.insert(this.m_processed_list,v)
149 if this.m_sortmode == "none" then
153 if this.m_sort_list[this.m_sortmode] ~= nil and
154 type(this.m_sort_list[this.m_sortmode]) == "function" then
156 this.m_sort_list[this.m_sortmode](this)
160 --------------------------------------------------------------------------------
161 function filterlist.size(this)
162 if this.m_processed_list == nil then
166 return #this.m_processed_list
169 --------------------------------------------------------------------------------
170 function filterlist.uid_exists_raw(this,uid)
171 for i,v in ipairs(this.m_raw_list) do
172 if this.m_uid_match_fct(v,uid) then
179 --------------------------------------------------------------------------------
180 function filterlist.raw_index_by_uid(this, uid)
181 local elementcount = 0
183 for i,v in ipairs(this.m_raw_list) do
184 if this.m_uid_match_fct(v,uid) then
185 elementcount = elementcount +1
191 -- If there are more elements than one with same name uid can't decide which
192 -- one is meant. This shouldn't be possible but just for sure.
193 if elementcount > 1 then
200 --------------------------------------------------------------------------------
201 -- COMMON helper functions --
202 --------------------------------------------------------------------------------
204 --------------------------------------------------------------------------------
205 function compare_worlds(world1,world2)
207 if world1.path ~= world2.path then
211 if world1.name ~= world2.name then
215 if world1.gameid ~= world2.gameid then
222 --------------------------------------------------------------------------------
223 function sort_worlds_alphabetic(this)
225 table.sort(this.m_processed_list, function(a, b)
228 local count = math.min(#n1, #n2)
231 if n1:sub(i, i):lower() < n2:sub(i, i):lower() then
233 elseif n1:sub(i, i):lower() > n2:sub(i, i):lower() then