Skip to content

Commit

Permalink
Merge pull request #1486 from leiget
Browse files Browse the repository at this point in the history
Added basic hotkey support and scroll speed and zoom speed options in Option menu.
  • Loading branch information
TheCycoONE committed Jan 18, 2019
2 parents eb87957 + 4b0140e commit 138aa5b
Show file tree
Hide file tree
Showing 46 changed files with 3,443 additions and 947 deletions.
5 changes: 4 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ globals = { -- Globals
"lfs", "list_to_set", "loadfile_envcall", "loadstring_envcall",
"permanent", "print_table", "rangeMapLookup", "rnc",
"strict_declare_global", "table_length", "unpermanent", "values",
"serialize","array_join","shallow_clone",

-- Game classes
"AIHospital", "AnimationManager", "App", "Audio",
Expand Down Expand Up @@ -54,7 +55,9 @@ globals = { -- Globals
"UIQueue", "UIQueuePopup", "UIResizable", "UIResearch",
"UIResolution", "UISaveGame", "UISaveMap", "UIStaff",
"UIStaffManagement", "UIStaffRise", "UITipOfTheDay", "UITownMap",
"UIUpdate", "UIWatch",
"UIUpdate", "UIWatch", "UIHotkeyAssign", "UIScrollSpeed",
"UIShiftScrollSpeed", "UIZoomSpeed", "UIHotkeyAssign_Panels",
"UIHotkeyAssign_storeRecallPos",

-- Actions
"AnswerCallAction", "CallCheckPointAction", "CheckWatchAction",
Expand Down
3 changes: 2 additions & 1 deletion CorsixTH/CorsixTH.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ strict_declare_global "TheApp"
TheApp = App()
TheApp:setCommandLine(
"--bitmap-dir=" ..base_dir.. "Bitmap",
"--config-file=" .. corsixth.require("config_finder"),
"--config-file=" .. select(1, corsixth.require("config_finder")),
"--hotkeys-file=" .. select(3, corsixth.require("config_finder")),
-- If a command line option is given twice, the later one is used, hence
-- if the user gave one of the above, that will be used instead.
...
Expand Down
104 changes: 100 additions & 4 deletions CorsixTH/Lua/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local runDebugger = corsixth.require("run_debugger")
-- Increment each time a savegame break would occur
-- and add compatibility code in afterLoad functions

local SAVEGAME_VERSION = 131
local SAVEGAME_VERSION = 132

class "App"

Expand All @@ -38,6 +38,7 @@ local App = _G["App"]
function App:App()
self.command_line = {}
self.config = {}
self.hotkeys = {}
self.runtime_config = {}
self.running = false
self.key_modifiers = {}
Expand Down Expand Up @@ -235,6 +236,16 @@ function App:init()
self:dumpStrings()
end

-- Load/setup hotkeys.
local hotkeys_path = self.command_line["hotkeys-file"] or "hotkeys.txt"
local hotkeys_chunk, hotkeys_err = loadfile_envcall(hotkeys_path)
if not hotkeys_chunk then
error(_S.hotkeys_file_err.file_err_01 .. hotkeys_path .. _S.hotkeys_file_err.file_err_02 .. hotkeys_err )
else
hotkeys_chunk(self.hotkeys)
end
self:fixHotkeys()

-- Load map before world
corsixth.require("map")

Expand Down Expand Up @@ -292,6 +303,7 @@ function App:init()
return true
end


-- Load main menu (which creates UI)
local function callback_after_movie()
self:loadMainMenu()
Expand Down Expand Up @@ -798,7 +810,7 @@ end

function App:fixConfig()
-- Fill in default values for things which don't exist
local _, config_defaults = corsixth.require("config_finder")
local config_defaults = select(2, corsixth.require("config_finder"))
for k, v in pairs(config_defaults) do
if self.config[k] == nil then
self.config[k] = v
Expand Down Expand Up @@ -899,6 +911,91 @@ function App:saveConfig()
fi:close()
end

function App:fixHotkeys()
-- Fill in default values for things which don't exist
local hotkeys_defaults = select(4, corsixth.require("config_finder"))

for k, v in pairs(hotkeys_defaults) do
if self.hotkeys[k] == nil then
self.hotkeys[k] = v
end
end

for key, value in pairs(self.hotkeys) do
-- Trim whitespace from beginning and end string values - it shouldn't be
-- there (at least in any current configuration options).
if type(value) == "string" then
if value:match("^[%s]") or value:match("[%s]$") then
self.hotkeys[key] = value:match("^[%s]*(.-)[%s]*$")
end
end
end
end

function App:saveHotkeys()
-- Load lines from config file
local fi = io.open(self.command_line["hotkeys-file"] or "hotkeys.txt", "r")
local lines = {}
local handled_ids = {}

if fi then
for line in fi:lines() do
lines[#lines + 1] = line
if not (string.find(line, "^%s*$") or string.find(line, "^%s*%-%-")) then -- empty lines or comments
-- Look for identifiers we want to save
local _, _, identifier, value = string.find(line, "^%s*([_%a][_%w]*)%s*=%s*(.-)%s*$")
if identifier then
local _, temp
-- Trim possible trailing comment from value
_, _, temp = string.find(value, "^(.-)%s*%-%-.*")
value = temp or value
-- Remove enclosing [[]], if necessary
_, _, temp = string.find(value, "^%[%[(.*)%]%]$")
value = temp or value

-- If identifier also exists in runtime options, compare their values and
-- replace the line, if needed
handled_ids[identifier] = true

if value ~= serialize(self.hotkeys[identifier]) then
local new_value = self.hotkeys[identifier]
if type(new_value) == "string" then
new_value = string.format("[[%s]]", new_value)
else
new_value = serialize(new_value)
end
lines[#lines] = string.format("%s = %s", identifier, new_value)
end
end
end
end
fi:close()
end

-- Append options that were not found
for identifier, value in pairs(self.hotkeys) do
if not handled_ids[identifier] then
if type(value) == "string" then
value = string.format("[[%s]]", value)
else
value = tostring(value)
end
lines[#lines + 1] = string.format("%s = %s", identifier, value)
end
end
-- Trim trailing newlines
while lines[#lines] == "" do
lines[#lines] = nil
end

fi = io.open(self.command_line["hotkeys-file"] or "hotkeys.txt", "w")
for _, line in ipairs(lines) do
fi:write(line .. "\n")
end

fi:close()
end

function App:run()
-- The application "main loop" is an SDL event loop written in C, which calls
-- a coroutine whenever an event occurs. Initially it may seem odd to involve
Expand Down Expand Up @@ -1420,7 +1517,6 @@ function App:afterLoad()
self.world:gameLog("Savegame version is " .. new .. " (" .. self:getVersion() ..
"), originally it was " .. first .. " (" .. self:getVersion(first) .. ")")
self.world:playLoadedEntitySounds()
return
elseif new > old then
self.world:gameLog("Savegame version changed from " .. old .. " (" .. self:getVersion(old) ..
") to " .. new .. " (" .. self:getVersion() ..
Expand Down Expand Up @@ -1448,8 +1544,8 @@ function App:afterLoad()
end

self.map:afterLoad(old, new)
self.world:afterLoad(old, new)
self.ui:afterLoad(old, new)
self.world:afterLoad(old, new)
end

function App:checkForUpdates()
Expand Down
Loading

0 comments on commit 138aa5b

Please sign in to comment.