diff --git a/src/engine/client/bind.lua b/src/engine/client/bind.lua index cf3faaee..18aada01 100644 --- a/src/engine/client/bind.lua +++ b/src/engine/client/bind.lua @@ -57,6 +57,21 @@ function readBinds() end end +function readDefaultBinds() + local config = "cfg/binds_default.cfg" + if ( not filesystem.exists( config ) ) then + return + end + + local binds = {} + for line in filesystem.lines( config ) do + for k, v in string.gmatch( line, "(.+)%s(.+)" ) do + binds[ string.trim( k ) ] = string.trim( v ) + end + end + return binds +end + function saveBinds() local config = {} for k, v in pairs( binds ) do diff --git a/src/engine/client/gui/optionsmenu/bindlistpanel.lua b/src/engine/client/gui/optionsmenu/bindlistpanel.lua index 3d56f7e2..bb7b6185 100644 --- a/src/engine/client/gui/optionsmenu/bindlistpanel.lua +++ b/src/engine/client/gui/optionsmenu/bindlistpanel.lua @@ -81,7 +81,7 @@ function bindlistpanel:onBindChange( item, key, oldKey, concommand ) } end -function bindlistpanel:readBinds() +function bindlistpanel:readBinds( binds ) if ( not filesystem.exists( "cfg/binds.lst" ) ) then return end @@ -99,8 +99,14 @@ function bindlistpanel:readBinds() self:addHeader( line ) elseif ( string.find( line, "[^=]" ) ) then local name, concommand = string.match( line, "\"(.+)\"%s(.+)" ) - concommand = string.trim( concommand ) - local key = bind.getKeyForBind( concommand ) + concommand = string.trim( concommand ) + + local key + if ( binds ) then + key = table.hasvalue( binds, concommand ) + else + key = bind.getKeyForBind( concommand ) + end self:addBinding( name, key or '', concommand ) end end @@ -108,12 +114,31 @@ function bindlistpanel:readBinds() end function bindlistpanel:saveBinds() + local i = 0 for concommand, keys in pairs( self.changedBinds ) do + i = i + 1 bind.setBind( keys.oldKey, nil ) bind.setBind( keys.key, concommand ) end - bind.saveBinds() + if ( i >= 1 ) then + bind.saveBinds() + self.changedBinds = {} + end +end + +function bindlistpanel:useDefaults() + local defaultBinds = bind.readDefaultBinds() + for key, concommand in pairs( defaultBinds ) do + self.changedBinds[ concommand ] = { + key = key, + oldKey = bind.getKeyForBind( concommand ) + } + end + + local innerPanel = self:getInnerPanel() + innerPanel:removeChildren() + self:readBinds( defaultBinds ) end gui.register( bindlistpanel, "bindlistpanel" ) diff --git a/src/engine/client/gui/optionsmenu/keyboardoptionspanel.lua b/src/engine/client/gui/optionsmenu/keyboardoptionspanel.lua index cf5dae83..80bf71d4 100644 --- a/src/engine/client/gui/optionsmenu/keyboardoptionspanel.lua +++ b/src/engine/client/gui/optionsmenu/keyboardoptionspanel.lua @@ -26,7 +26,7 @@ function keyboardoptionspanel:keyboardoptionspanel() local buttonName = name .. " Use Defaults Button" self.useDefaultsButton = gui.commandbutton( group, buttonName, "Use Defaults" ) self.useDefaultsButton.onClick = function( commandbutton ) - print( "TODO: Default bindings." ) + self.bindList:useDefaults() end buttonName = name .. " Advanced Button" self.advancedButton = gui.commandbutton( group, buttonName, "Advanced" ) diff --git a/src/engine/shared/require.lua b/src/engine/shared/require.lua index ca347c43..84a2b9c3 100644 --- a/src/engine/shared/require.lua +++ b/src/engine/shared/require.lua @@ -8,7 +8,6 @@ require( "engine.shared.filesystem" ) local error = error local filesystem = filesystem -local hook = hook local ipairs = ipairs local pairs = pairs local pcall = pcall