From f4987714290092d4975d5fe116a7a27487b1e8e7 Mon Sep 17 00:00:00 2001 From: Markus Ast Date: Tue, 13 Aug 2019 20:26:50 +0200 Subject: [PATCH] save Scratchpad content into Scratchpad\0000.txt --- README.md | 10 ++++--- Scripts/Hooks/scratchpad-hook.lua | 45 ++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4fb2c2c..987527f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -9,14 +9,18 @@ Copy the `Scripts` folder into your DCS Saved games folder. ## Usage - Toggle the scratchpad with `CTRL+Shift+X` -- Use `Esc` to remove the textfield focus, but keep the scratchpad open +- Use `Esc` to remove the text field focus, but keep the scratchpad open ## Settings 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 diff --git a/Scripts/Hooks/scratchpad-hook.lua b/Scripts/Hooks/scratchpad-hook.lua index ff8c786..b95091d 100644 --- a/Scripts/Hooks/scratchpad-hook.lua +++ b/Scripts/Hooks/scratchpad-hook.lua @@ -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