Fix a bunch of small bugs due to mainmenu cleanup

Fix doubleclick not working in singleplayer
Fix of by one issue on accessing raw list
Fix this->self
Fix copy&paste error for scroll button
master
sapier 2014-05-18 19:40:02 +02:00
parent 18fe277d94
commit f969a91c0a
4 changed files with 100 additions and 93 deletions

View File

@ -37,9 +37,9 @@
filterlist = {} filterlist = {}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.refresh(this) function filterlist.refresh(self)
this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param) self.m_raw_list = self.m_raw_list_fct(self.m_fetch_param)
filterlist.process(this) filterlist.process(self)
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -48,105 +48,105 @@ function filterlist.create(raw_fct,compare_fct,uid_match_fct,filter_fct,fetch_pa
assert((raw_fct ~= nil) and (type(raw_fct) == "function")) assert((raw_fct ~= nil) and (type(raw_fct) == "function"))
assert((compare_fct ~= nil) and (type(compare_fct) == "function")) assert((compare_fct ~= nil) and (type(compare_fct) == "function"))
local this = {} local self = {}
this.m_raw_list_fct = raw_fct self.m_raw_list_fct = raw_fct
this.m_compare_fct = compare_fct self.m_compare_fct = compare_fct
this.m_filter_fct = filter_fct self.m_filter_fct = filter_fct
this.m_uid_match_fct = uid_match_fct self.m_uid_match_fct = uid_match_fct
this.m_filtercriteria = nil self.m_filtercriteria = nil
this.m_fetch_param = fetch_param self.m_fetch_param = fetch_param
this.m_sortmode = "none" self.m_sortmode = "none"
this.m_sort_list = {} self.m_sort_list = {}
this.m_processed_list = nil self.m_processed_list = nil
this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param) self.m_raw_list = self.m_raw_list_fct(self.m_fetch_param)
this.add_sort_mechanism = filterlist.add_sort_mechanism self.add_sort_mechanism = filterlist.add_sort_mechanism
this.set_filtercriteria = filterlist.set_filtercriteria self.set_filtercriteria = filterlist.set_filtercriteria
this.get_filtercriteria = filterlist.get_filtercriteria self.get_filtercriteria = filterlist.get_filtercriteria
this.set_sortmode = filterlist.set_sortmode self.set_sortmode = filterlist.set_sortmode
this.get_list = filterlist.get_list self.get_list = filterlist.get_list
this.get_raw_list = filterlist.get_raw_list self.get_raw_list = filterlist.get_raw_list
this.get_raw_element = filterlist.get_raw_element self.get_raw_element = filterlist.get_raw_element
this.get_raw_index = filterlist.get_raw_index self.get_raw_index = filterlist.get_raw_index
this.get_current_index = filterlist.get_current_index self.get_current_index = filterlist.get_current_index
this.size = filterlist.size self.size = filterlist.size
this.uid_exists_raw = filterlist.uid_exists_raw self.uid_exists_raw = filterlist.uid_exists_raw
this.raw_index_by_uid = filterlist.raw_index_by_uid self.raw_index_by_uid = filterlist.raw_index_by_uid
this.refresh = filterlist.refresh self.refresh = filterlist.refresh
filterlist.process(this) filterlist.process(self)
return this return self
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.add_sort_mechanism(this,name,fct) function filterlist.add_sort_mechanism(self,name,fct)
this.m_sort_list[name] = fct self.m_sort_list[name] = fct
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.set_filtercriteria(this,criteria) function filterlist.set_filtercriteria(self,criteria)
if criteria == this.m_filtercriteria and if criteria == self.m_filtercriteria and
type(criteria) ~= "table" then type(criteria) ~= "table" then
return return
end end
this.m_filtercriteria = criteria self.m_filtercriteria = criteria
filterlist.process(this) filterlist.process(self)
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.get_filtercriteria(this) function filterlist.get_filtercriteria(self)
return this.m_filtercriteria return self.m_filtercriteria
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
--supported sort mode "alphabetic|none" --supported sort mode "alphabetic|none"
function filterlist.set_sortmode(this,mode) function filterlist.set_sortmode(self,mode)
if (mode == this.m_sortmode) then if (mode == self.m_sortmode) then
return return
end end
this.m_sortmode = mode self.m_sortmode = mode
filterlist.process(this) filterlist.process(self)
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.get_list(this) function filterlist.get_list(self)
return this.m_processed_list return self.m_processed_list
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.get_raw_list(this) function filterlist.get_raw_list(self)
return this.m_raw_list return self.m_raw_list
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.get_raw_element(this,idx) function filterlist.get_raw_element(self,idx)
if type(idx) ~= "number" then if type(idx) ~= "number" then
idx = tonumber(idx) idx = tonumber(idx)
end end
if idx ~= nil and idx > 0 and idx < #this.m_raw_list then if idx ~= nil and idx > 0 and idx <= #self.m_raw_list then
return this.m_raw_list[idx] return self.m_raw_list[idx]
end end
return nil return nil
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.get_raw_index(this,listindex) function filterlist.get_raw_index(self,listindex)
assert(this.m_processed_list ~= nil) assert(self.m_processed_list ~= nil)
if listindex ~= nil and listindex > 0 and if listindex ~= nil and listindex > 0 and
listindex <= #this.m_processed_list then listindex <= #self.m_processed_list then
local entry = this.m_processed_list[listindex] local entry = self.m_processed_list[listindex]
for i,v in ipairs(this.m_raw_list) do for i,v in ipairs(self.m_raw_list) do
if this.m_compare_fct(v,entry) then if self.m_compare_fct(v,entry) then
return i return i
end end
end end
@ -156,16 +156,16 @@ function filterlist.get_raw_index(this,listindex)
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.get_current_index(this,listindex) function filterlist.get_current_index(self,listindex)
assert(this.m_processed_list ~= nil) assert(self.m_processed_list ~= nil)
if listindex ~= nil and listindex > 0 and if listindex ~= nil and listindex > 0 and
listindex <= #this.m_raw_list then listindex <= #self.m_raw_list then
local entry = this.m_raw_list[listindex] local entry = self.m_raw_list[listindex]
for i,v in ipairs(this.m_processed_list) do for i,v in ipairs(self.m_processed_list) do
if this.m_compare_fct(v,entry) then if self.m_compare_fct(v,entry) then
return i return i
end end
end end
@ -175,48 +175,48 @@ function filterlist.get_current_index(this,listindex)
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.process(this) function filterlist.process(self)
assert(this.m_raw_list ~= nil) assert(self.m_raw_list ~= nil)
if this.m_sortmode == "none" and if self.m_sortmode == "none" and
this.m_filtercriteria == nil then self.m_filtercriteria == nil then
this.m_processed_list = this.m_raw_list self.m_processed_list = self.m_raw_list
return return
end end
this.m_processed_list = {} self.m_processed_list = {}
for k,v in pairs(this.m_raw_list) do for k,v in pairs(self.m_raw_list) do
if this.m_filtercriteria == nil or if self.m_filtercriteria == nil or
this.m_filter_fct(v,this.m_filtercriteria) then self.m_filter_fct(v,self.m_filtercriteria) then
table.insert(this.m_processed_list,v) table.insert(self.m_processed_list,v)
end end
end end
if this.m_sortmode == "none" then if self.m_sortmode == "none" then
return return
end end
if this.m_sort_list[this.m_sortmode] ~= nil and if self.m_sort_list[self.m_sortmode] ~= nil and
type(this.m_sort_list[this.m_sortmode]) == "function" then type(self.m_sort_list[self.m_sortmode]) == "function" then
this.m_sort_list[this.m_sortmode](this) self.m_sort_list[self.m_sortmode](self)
end end
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.size(this) function filterlist.size(self)
if this.m_processed_list == nil then if self.m_processed_list == nil then
return 0 return 0
end end
return #this.m_processed_list return #self.m_processed_list
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.uid_exists_raw(this,uid) function filterlist.uid_exists_raw(self,uid)
for i,v in ipairs(this.m_raw_list) do for i,v in ipairs(self.m_raw_list) do
if this.m_uid_match_fct(v,uid) then if self.m_uid_match_fct(v,uid) then
return true return true
end end
end end
@ -224,11 +224,11 @@ function filterlist.uid_exists_raw(this,uid)
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function filterlist.raw_index_by_uid(this, uid) function filterlist.raw_index_by_uid(self, uid)
local elementcount = 0 local elementcount = 0
local elementidx = 0 local elementidx = 0
for i,v in ipairs(this.m_raw_list) do for i,v in ipairs(self.m_raw_list) do
if this.m_uid_match_fct(v,uid) then if self.m_uid_match_fct(v,uid) then
elementcount = elementcount +1 elementcount = elementcount +1
elementidx = i elementidx = i
end end
@ -236,7 +236,7 @@ function filterlist.raw_index_by_uid(this, uid)
-- If there are more elements than one with same name uid can't decide which -- If there are more elements than one with same name uid can't decide which
-- one is meant. This shouldn't be possible but just for sure. -- one is meant. self shouldn't be possible but just for sure.
if elementcount > 1 then if elementcount > 1 then
elementidx=0 elementidx=0
end end
@ -267,9 +267,9 @@ function compare_worlds(world1,world2)
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function sort_worlds_alphabetic(this) function sort_worlds_alphabetic(self)
table.sort(this.m_processed_list, function(a, b) table.sort(self.m_processed_list, function(a, b)
--fixes issue #857 (crash due to sorting nil in worldlist) --fixes issue #857 (crash due to sorting nil in worldlist)
if a == nil or b == nil then if a == nil or b == nil then
if a == nil and b ~= nil then return false end if a == nil and b ~= nil then return false end
@ -284,9 +284,9 @@ function sort_worlds_alphabetic(this)
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function sort_mod_list(this) function sort_mod_list(self)
table.sort(this.m_processed_list, function(a, b) table.sort(self.m_processed_list, function(a, b)
-- Show game mods at bottom -- Show game mods at bottom
if a.typ ~= b.typ then if a.typ ~= b.typ then
return b.typ == "game_mod" return b.typ == "game_mod"

View File

@ -101,7 +101,7 @@ local function buttonbar_formspec(self)
self.name, text_dec) self.name, text_dec)
formspec = formspec .. formspec = formspec ..
string.format("image_button[%f,%f;%f,%f;;btnbar_dec_%s;%s;true;true]", string.format("image_button[%f,%f;%f,%f;;btnbar_inc_%s;%s;true;true]",
btn_inc_pos.x, btn_inc_pos.y, btn_size.x, btn_size.y, btn_inc_pos.x, btn_inc_pos.y, btn_size.x, btn_size.y,
self.name, text_inc) self.name, text_inc)
end end

View File

@ -74,6 +74,7 @@ local function main_button_handler(this, fields, name, tabdata)
if event.type == "CHG" then if event.type == "CHG" then
core.setting_set("mainmenu_last_selected_world", core.setting_set("mainmenu_last_selected_world",
menudata.worldlist:get_raw_index(core.get_textlist_index("srv_worlds"))) menudata.worldlist:get_raw_index(core.get_textlist_index("srv_worlds")))
return true
end end
end end
@ -83,14 +84,17 @@ local function main_button_handler(this, fields, name, tabdata)
if fields["cb_creative_mode"] then if fields["cb_creative_mode"] then
core.setting_set("creative_mode", fields["cb_creative_mode"]) core.setting_set("creative_mode", fields["cb_creative_mode"])
return true
end end
if fields["cb_enable_damage"] then if fields["cb_enable_damage"] then
core.setting_set("enable_damage", fields["cb_enable_damage"]) core.setting_set("enable_damage", fields["cb_enable_damage"])
return true
end end
if fields["cb_server_announce"] then if fields["cb_server_announce"] then
core.setting_set("server_announce", fields["cb_server_announce"]) core.setting_set("server_announce", fields["cb_server_announce"])
return true
end end
if fields["start_server"] ~= nil or if fields["start_server"] ~= nil or
@ -111,9 +115,11 @@ local function main_button_handler(this, fields, name, tabdata)
--update last game --update last game
local world = menudata.worldlist:get_raw_element(gamedata.selected_world) local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
local game,index = gamemgr.find_by_gameid(world.gameid) local game,index = gamemgr.find_by_gameid(world.gameid)
core.setting_set("menu_last_game",game.id) core.setting_set("menu_last_game",game.id)
core.start() core.start()
return true
end end
end end

View File

@ -112,9 +112,8 @@ local function main_button_handler(this, fields, name, tabdata)
if event.type == "CHG" then if event.type == "CHG" then
core.setting_set("mainmenu_last_selected_world", core.setting_set("mainmenu_last_selected_world",
menudata.worldlist:get_raw_index(core.get_textlist_index("sp_worlds"))) menudata.worldlist:get_raw_index(core.get_textlist_index("sp_worlds")))
return true
end end
return true
end end
if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
@ -135,9 +134,11 @@ local function main_button_handler(this, fields, name, tabdata)
world_doubleclick or world_doubleclick or
fields["key_enter"] then fields["key_enter"] then
local selected = core.get_textlist_index("sp_worlds") local selected = core.get_textlist_index("sp_worlds")
if selected ~= nil then if selected ~= nil then
gamedata.selected_world = menudata.worldlist:get_raw_index(selected) gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
gamedata.singleplayer = true gamedata.singleplayer = true
core.start() core.start()
end end
return true return true