Skip to content

Commit

Permalink
add hotkey to reload pages
Browse files Browse the repository at this point in the history
Resolves #55
  • Loading branch information
rkusa committed Jul 1, 2023
1 parent 95c0443 commit ac5a15b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
69 changes: 68 additions & 1 deletion Scripts/Hooks/scratchpad-hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -979,7 +1023,7 @@ local function loadScratchpad()
)

-- add insert coordinates hotkey
if config.hotkeyPrevPage then
if config.hotkeyInsertCoordinates then
window:addHotKeyCallback(
config.hotkeyInsertCoordinates,
function()
Expand All @@ -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)

Expand Down Expand Up @@ -1023,6 +1089,7 @@ local function loadScratchpad()
function handler.onSimulationFrame()
if config == nil then
loadConfiguration()
loadPages()
loadExtensions()
end

Expand Down

0 comments on commit ac5a15b

Please sign in to comment.