From 49086d25f058d2fef7e62c8e82168e9b0795ebb3 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 12 Jan 2010 23:18:34 +0000
Subject: [PATCH] resolution switcher

git-svn-id: http://svn.net-core.org/repos/t-engine4@238 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Game.lua                      |  7 +++
 game/engine/dialogs/DisplayResolution.lua | 62 +++++++++++++++++++++++
 game/engine/dialogs/GameMenu.lua          |  5 ++
 game/engine/dialogs/KeyBinder.lua         |  4 +-
 game/engine/init.lua                      | 10 ++--
 game/modules/tome/class/Game.lua          |  2 +-
 6 files changed, 84 insertions(+), 6 deletions(-)
 create mode 100644 game/engine/dialogs/DisplayResolution.lua

diff --git a/game/engine/Game.lua b/game/engine/Game.lua
index fcd9bb7119..90fb58e9dc 100644
--- a/game/engine/Game.lua
+++ b/game/engine/Game.lua
@@ -138,6 +138,13 @@ function _M:setResolution(res)
 
 	if self.w ~= old_w or self.h ~= old_h then
 		self:onResolutionChange()
+
+		local restore = fs.getWritePath()
+		fs.setWritePath(engine.homepath)
+		local f = fs.open("/settings/resolution.cfg", "w")
+		f:write(("window.size = %q\n"):format(res))
+		f:close()
+		if restore then fs.setWritePath(restore) end
 	end
 end
 
diff --git a/game/engine/dialogs/DisplayResolution.lua b/game/engine/dialogs/DisplayResolution.lua
new file mode 100644
index 0000000000..985109798f
--- /dev/null
+++ b/game/engine/dialogs/DisplayResolution.lua
@@ -0,0 +1,62 @@
+require "engine.class"
+require "engine.Dialog"
+
+module(..., package.seeall, class.inherit(engine.Dialog))
+
+function _M:init()
+	self:generateList()
+
+	engine.Dialog.init(self, "Switch Resolution", 300, #self.list * 30 + 20)
+
+	self.sel = 1
+	self.scroll = 1
+	self.max = math.floor((self.ih - 5) / self.font_h) - 1
+
+	self:keyCommands({
+		__TEXTINPUT = function(c)
+			if c:find("^[a-z]$") then
+				self.sel = util.bound(1 + string.byte(c) - string.byte('a'), 1, #self.list)
+				self:use()
+			end
+		end,
+	},{
+		MOVE_UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end,
+		MOVE_DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end,
+		ACCEPT = function() self:use() end,
+		EXIT = function() game:unregisterDialog(self) end,
+	})
+	self:mouseZones{
+		{ x=2, y=5, w=350, h=self.font_h*self.max, fct=function(button, x, y, xrel, yrel, tx, ty)
+			self.sel = util.bound(self.scroll + math.floor(ty / self.font_h), 1, #self.list)
+			if button == "left" then self:use()
+			elseif button == "right" then
+			end
+		end },
+	}
+end
+
+function _M:use()
+	game:setResolution(self.list[self.sel].r)
+	game:unregisterDialog(self)
+end
+
+function _M:generateList()
+	local l = {}
+	for r, _ in pairs(game.available_resolutions) do
+		l[#l+1] = r
+	end
+	table.sort(l, function(a,b) return game.available_resolutions[a][1] < game.available_resolutions[b][1] end)
+
+	-- Makes up the list
+	local list = {}
+	local i = 0
+	for _, r in ipairs(l) do
+		list[#list+1] = { name=string.char(string.byte('a') + i)..")  "..r, r=r }
+		i = i + 1
+	end
+	self.list = list
+end
+
+function _M:drawDialog(s)
+	self:drawSelectionList(s, 2, 5, self.font_h, self.list, self.sel, "name", self.scroll, self.max)
+end
diff --git a/game/engine/dialogs/GameMenu.lua b/game/engine/dialogs/GameMenu.lua
index b2afd3a15a..654409e247 100644
--- a/game/engine/dialogs/GameMenu.lua
+++ b/game/engine/dialogs/GameMenu.lua
@@ -47,6 +47,11 @@ function _M:generateList(actions)
 			local menu = require("engine.dialogs.KeyBinder").new(game.normal_key)
 			game:registerDialog(menu)
 		end },
+		resolution = { "Display Resolution", function()
+			game:unregisterDialog(self)
+			local menu = require("engine.dialogs.DisplayResolution").new()
+			game:registerDialog(menu)
+		end },
 		save = { "Save Game", function() game:saveGame() end },
 		quit = { "Save and Exit", function() game:onQuit() end },
 	}
diff --git a/game/engine/dialogs/KeyBinder.lua b/game/engine/dialogs/KeyBinder.lua
index fb90a741b5..5dc4016763 100644
--- a/game/engine/dialogs/KeyBinder.lua
+++ b/game/engine/dialogs/KeyBinder.lua
@@ -144,6 +144,6 @@ function _M:drawDialog(s)
 	local selcol = {255,255,0}
 
 	self:drawSelectionList(s, 2,   5, self.font_h, self.list, self.sel, "name", self.scroll, self.max)
-	self:drawSelectionList(s, 200, 5, self.font_h, self.list, self.sel, "b1", self.scroll, self.max, col, self.selcol == 1 and selcol or col)
-	self:drawSelectionList(s, 400, 5, self.font_h, self.list, self.sel, "b2", self.scroll, self.max, col, self.selcol == 2 and selcol or col)
+	self:drawSelectionList(s, 230, 5, self.font_h, self.list, self.sel, "b1", self.scroll, self.max, col, self.selcol == 1 and selcol or col)
+	self:drawSelectionList(s, 430, 5, self.font_h, self.list, self.sel, "b2", self.scroll, self.max, col, self.selcol == 2 and selcol or col)
 end
diff --git a/game/engine/init.lua b/game/engine/init.lua
index 2774e8cacb..75dc47835e 100644
--- a/game/engine/init.lua
+++ b/game/engine/init.lua
@@ -17,15 +17,19 @@ engine.homepath = fs.getUserPath()..fs.getPathSeparator()..fs.getHomePath()..fs.
 fs.setWritePath(fs.getUserPath())
 fs.mkdir(fs.getHomePath())
 fs.mkdir(fs.getHomePath().."/4.0/")
+fs.mkdir(fs.getHomePath().."/4.0/settings/")
 fs.setWritePath(fs.getHomePath())
 
 -- Loads default config & user config
 fs.mount(engine.homepath, "/")
 config.loadString[[
-keyboard.locale = "en_US"
-window.size = "1024x768"
+window.size = "800x600"
 ]]
-config.load("/settings.cfg")
+for i, file in ipairs(fs.list("/settings/")) do
+	if file:find(".cfg$") then
+		config.load("/settings/"..file)
+	end
+end
 
 -- Load default keys
 engine.KeyBind:load("move,actions")
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index 5a41c79485..d9ab9a8734 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -452,7 +452,7 @@ function _M:setupCommands()
 		end,
 
 		EXIT = function()
-			local menu = require("engine.dialogs.GameMenu").new{"resume", "keybinds", "save", "quit"}
+			local menu = require("engine.dialogs.GameMenu").new{"resume", "keybinds", "resolution", "save", "quit"}
 			self:registerDialog(menu)
 		end,
 	}
-- 
GitLab