save Scratchpad content into Scratchpad\0000.txt

This commit is contained in:
Markus Ast 2019-08-13 20:26:50 +02:00
parent cc777d8aff
commit f498771429
2 changed files with 48 additions and 7 deletions

View File

@ -1,6 +1,6 @@
# DCS Scratchpad
Resizable and movable DCS World ingame Scratchpad for quick persistant notes - especially useful in VR.
Resizable and movable DCS World in-game Scratchpad for quick persistent notes - especially useful in VR.
## Installation
@ -16,7 +16,11 @@ Copy the `Scripts` folder into your DCS Saved games folder.
Some settings can be changed in your DCS saved games folder under `Config/ScratchpadConfig.lua` (if the file does not exist, start DCS once after mod installation):
- `hotkey` change the hotkey used to toggle the scratchpad
- `fontSize` increase or decrease the font size of the scratchpad's textarea
- `fontSize` increase or decrease the font size of the Scratchpads textarea
## Scratchpad Content
The Scratchpads content is persisted into `Scratchpad\0000.txt` (in your saved games folder; if the file does not exist, start DCS once after mod installation/upgrade). You can also change the file in your favorite text editor before starting DCS.
## Kudos

View File

@ -29,10 +29,19 @@ function scratchpad_load()
scratchpad.config = tbl.config
-- config migration
-- add default fontSize config
if scratchpad.config.fontSize == nil then
scratchpad.config.fontSize = 14
scratchpad.saveConfiguration()
end
-- move content into text file
if scratchpad.config.content ~= nil then
scratchpad.saveContent("0000", scratchpad.config.content, false)
scratchpad.config.content = nil
scratchpad.saveConfiguration()
end
else
scratchpad.log("Configuration not found, creating defaults...")
scratchpad.config = {
@ -40,12 +49,41 @@ function scratchpad_load()
windowPosition = { x = 200, y = 200 },
windowSize = { w = 350, h = 150 },
fontSize = 14,
content = "Start writing here ...",
}
scratchpad.saveConfiguration()
end
end
function scratchpad.getContent(name)
local path = lfs.writedir()..[[Scratchpad\]]..name..[[.txt]]
file, err = io.open(path, "r")
if err then
scratchpad.log("Error reading file: "..path)
return ""
else
local content = file:read("*all")
file:close()
return content
end
end
function scratchpad.saveContent(name, content, override)
lfs.mkdir(lfs.writedir()..[[Scratchpad\]])
local path = lfs.writedir()..[[Scratchpad\]]..name..[[.txt]]
local mode = "a"
if override then
mode = "w"
end
file, err = io.open(path, mode)
if err then
scratchpad.log("Error writing file: "..path)
else
file:write(content)
file:flush()
file:close()
end
end
function scratchpad.saveConfiguration()
U.saveInFile(scratchpad.config, "config", lfs.writedir() .. "Config/ScratchpadConfig.lua")
end
@ -89,10 +127,9 @@ function scratchpad_load()
skin.skinData.states.released[1].text.fontSize = scratchpad.config.fontSize
textarea:setSkin(skin)
textarea:setText(scratchpad.config.content)
textarea:setText(scratchpad.getContent("0000"))
textarea:addChangeCallback(function(self)
scratchpad.config.content = self:getText()
scratchpad.saveConfiguration()
scratchpad.saveContent("0000", self:getText(), true)
end)
textarea:addFocusCallback(function(self)
if self:getFocused() then