From ac5a15bf5434c1a134e2ad19b676193f138e0619 Mon Sep 17 00:00:00 2001 From: Markus Ast Date: Sat, 1 Jul 2023 11:48:16 +0200 Subject: [PATCH] add hotkey to reload pages Resolves #55 --- README.md | 1 + Scripts/Hooks/scratchpad-hook.lua | 69 ++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ef5c60..d9a1322 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Some settings can be changed in your DCS saved games folder under `Config/Scratc - `hotkeyNextPage` hotkey to switch to the next page (not set by default) ¹ - `hotkeyPrevPage` hotkey to switch to the next page (not set by default) ¹ - `hotkeyInsertCoordinates` hotkey to add coordinates from the F10 map (not set by default) ¹ +- `hotkeyReloadPages` hotkey to reload all pages from disk (useful if they were modified by other tools; not set by default) ¹ - `fontSize` increase or decrease the font size of the Scratchpads textarea (`14` by default) _¹ check `DCS World\dxgui\bind\KeyNames.txt` to find how to reference a key; only the keys can be used, that work without having to press `Shift` - so `(` cannot be used, but `Shift+9` can_ diff --git a/Scripts/Hooks/scratchpad-hook.lua b/Scripts/Hooks/scratchpad-hook.lua index f18aaa6..adb4272 100644 --- a/Scripts/Hooks/scratchpad-hook.lua +++ b/Scripts/Hooks/scratchpad-hook.lua @@ -487,6 +487,50 @@ local function loadScratchpad() U.saveInFile(config, "config", lfs.writedir() .. "Config/ScratchpadConfig.lua") end + + local function loadPages() + log("Loading pages ...") + + pages = {} + local dirPath = lfs.writedir() .. [[Scratchpad\]] + + -- scan scratchpad dir for pages + for name in lfs.dir(dirPath) do + local path = dirPath .. name + if lfs.attributes(path, "mode") == "file" then + if name:sub(-4) ~= ".txt" then + log("Ignoring file " .. name .. ", because of it doesn't seem to be a text file (.txt)") + elseif lfs.attributes(path, "size") > 1024 * 1024 then + log("Ignoring file " .. name .. ", because of its file size of more than 1MB") + else + log("found page " .. path) + table.insert( + pages, + { + name = name:sub(1, -5), + path = path + } + ) + pagesCount = pagesCount + 1 + end + end + end + + -- there are no pages yet, create one + if pagesCount == 0 then + path = dirPath .. [[0000.txt]] + log("creating page " .. path) + table.insert( + pages, + { + name = "0000", + path = path + } + ) + pagesCount = pagesCount + 1 + end + end + local function unlockKeyboardInput() if keyboardLocked then DCS.unlockKeyboardInput(true) @@ -979,7 +1023,7 @@ local function loadScratchpad() ) -- add insert coordinates hotkey - if config.hotkeyPrevPage then + if config.hotkeyInsertCoordinates then window:addHotKeyCallback( config.hotkeyInsertCoordinates, function() @@ -990,6 +1034,28 @@ local function loadScratchpad() ) end + -- add reload pages hotkey + if config.hotkeyReloadPages then + window:addHotKeyCallback( + config.hotkeyReloadPages, + function() + loadPages() + if currentPage ~= nil then + for _, page in pairs(pages) do + if page.path == currentPage then + loadPage(page) + return + end + end + end + + -- file not found anymore restart at the beginning + currentPage = nil + nextPage() + end + ) + end + window:addSizeCallback(handleResize) window:addPositionCallback(handleMove) @@ -1023,6 +1089,7 @@ local function loadScratchpad() function handler.onSimulationFrame() if config == nil then loadConfiguration() + loadPages() loadExtensions() end