mirror of
https://github.com/asherao/dcs-scratchpad.git
synced 2025-10-29 16:56:22 +00:00
remove unnecessary scratchpad. prefix in code
This commit is contained in:
parent
3db79d821a
commit
8fa31a56b7
@ -1,4 +1,4 @@
|
|||||||
function scratchpad_load()
|
local function loadScratchpad()
|
||||||
package.path = package.path .. ";.\\Scripts\\?.lua;.\\Scripts\\UI\\?.lua;"
|
package.path = package.path .. ";.\\Scripts\\?.lua;.\\Scripts\\UI\\?.lua;"
|
||||||
|
|
||||||
local lfs = require("lfs")
|
local lfs = require("lfs")
|
||||||
@ -71,20 +71,30 @@ function scratchpad_load()
|
|||||||
return result
|
return result
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local scratchpad = {
|
local logFile = io.open(lfs.writedir() .. [[Logs\Scratchpad.log]], "w")
|
||||||
logFile = io.open(lfs.writedir() .. [[Logs\Scratchpad.log]], "w")
|
local config = nil
|
||||||
}
|
|
||||||
|
|
||||||
local dirPath = lfs.writedir() .. [[Scratchpad\]]
|
local dirPath = lfs.writedir() .. [[Scratchpad\]]
|
||||||
local currentPage = nil
|
local currentPage = nil
|
||||||
local pagesCount = 0
|
local pagesCount = 0
|
||||||
local pages = {}
|
local pages = {}
|
||||||
|
|
||||||
|
local function log(str)
|
||||||
|
if not str then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if logFile then
|
||||||
|
logFile:write("[" .. os.date("%H:%M:%S") .. "] " .. str .. "\r\n")
|
||||||
|
logFile:flush()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function loadPage(page)
|
local function loadPage(page)
|
||||||
scratchpad.log("loading page " .. page.path)
|
log("loading page " .. page.path)
|
||||||
file, err = io.open(page.path, "r")
|
file, err = io.open(page.path, "r")
|
||||||
if err then
|
if err then
|
||||||
scratchpad.log("Error reading file: " .. page.path)
|
log("Error reading file: " .. page.path)
|
||||||
return ""
|
return ""
|
||||||
else
|
else
|
||||||
local content = file:read("*all")
|
local content = file:read("*all")
|
||||||
@ -97,7 +107,7 @@ function scratchpad_load()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function savePage(path, content, override)
|
local function savePage(path, content, override)
|
||||||
scratchpad.log("saving page " .. path)
|
log("saving page " .. path)
|
||||||
lfs.mkdir(lfs.writedir() .. [[Scratchpad\]])
|
lfs.mkdir(lfs.writedir() .. [[Scratchpad\]])
|
||||||
local mode = "a"
|
local mode = "a"
|
||||||
if override then
|
if override then
|
||||||
@ -105,7 +115,7 @@ function scratchpad_load()
|
|||||||
end
|
end
|
||||||
file, err = io.open(path, mode)
|
file, err = io.open(path, mode)
|
||||||
if err then
|
if err then
|
||||||
scratchpad.log("Error writing file: " .. path)
|
log("Error writing file: " .. path)
|
||||||
else
|
else
|
||||||
file:write(content)
|
file:write(content)
|
||||||
file:flush()
|
file:flush()
|
||||||
@ -149,49 +159,49 @@ function scratchpad_load()
|
|||||||
currentPage = pages[pagesCount].path
|
currentPage = pages[pagesCount].path
|
||||||
end
|
end
|
||||||
|
|
||||||
function scratchpad.loadConfiguration()
|
local function loadConfiguration()
|
||||||
scratchpad.log("Loading config file...")
|
log("Loading config file...")
|
||||||
local tbl = Tools.safeDoFile(lfs.writedir() .. "Config/ScratchpadConfig.lua", false)
|
local tbl = Tools.safeDoFile(lfs.writedir() .. "Config/ScratchpadConfig.lua", false)
|
||||||
if (tbl and tbl.config) then
|
if (tbl and tbl.config) then
|
||||||
scratchpad.log("Configuration exists...")
|
log("Configuration exists...")
|
||||||
scratchpad.config = tbl.config
|
config = tbl.config
|
||||||
|
|
||||||
-- config migration
|
-- config migration
|
||||||
|
|
||||||
-- add default fontSize config
|
-- add default fontSize config
|
||||||
if scratchpad.config.fontSize == nil then
|
if config.fontSize == nil then
|
||||||
scratchpad.config.fontSize = 14
|
config.fontSize = 14
|
||||||
scratchpad.saveConfiguration()
|
saveConfiguration()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- move content into text file
|
-- move content into text file
|
||||||
if scratchpad.config.content ~= nil then
|
if config.content ~= nil then
|
||||||
savePage(dirPath .. [[0000.txt]], scratchpad.config.content, false)
|
savePage(dirPath .. [[0000.txt]], config.content, false)
|
||||||
scratchpad.config.content = nil
|
config.content = nil
|
||||||
scratchpad.saveConfiguration()
|
saveConfiguration()
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
scratchpad.log("Configuration not found, creating defaults...")
|
log("Configuration not found, creating defaults...")
|
||||||
scratchpad.config = {
|
config = {
|
||||||
hotkey = "Ctrl+Shift+x",
|
hotkey = "Ctrl+Shift+x",
|
||||||
windowPosition = {x = 200, y = 200},
|
windowPosition = {x = 200, y = 200},
|
||||||
windowSize = {w = 350, h = 150},
|
windowSize = {w = 350, h = 150},
|
||||||
fontSize = 14
|
fontSize = 14
|
||||||
}
|
}
|
||||||
scratchpad.saveConfiguration()
|
saveConfiguration()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- scan scratchpad dir for pages
|
-- scan scratchpad dir for pages
|
||||||
for name in lfs.dir(dirPath) do
|
for name in lfs.dir(dirPath) do
|
||||||
local path = dirPath .. name
|
local path = dirPath .. name
|
||||||
scratchpad.log(path)
|
log(path)
|
||||||
if lfs.attributes(path, "mode") == "file" then
|
if lfs.attributes(path, "mode") == "file" then
|
||||||
if name:sub(-4) ~= ".txt" then
|
if name:sub(-4) ~= ".txt" then
|
||||||
scratchpad.log("Ignoring file " .. name .. ", because of it doesn't seem to be a text file (.txt)")
|
log("Ignoring file " .. name .. ", because of it doesn't seem to be a text file (.txt)")
|
||||||
elseif lfs.attributes(path, "size") > 1024 * 1024 then
|
elseif lfs.attributes(path, "size") > 1024 * 1024 then
|
||||||
scratchpad.log("Ignoring file " .. name .. ", because of its file size of more than 1MB")
|
log("Ignoring file " .. name .. ", because of its file size of more than 1MB")
|
||||||
else
|
else
|
||||||
scratchpad.log("found page " .. path)
|
log("found page " .. path)
|
||||||
table.insert(
|
table.insert(
|
||||||
pages,
|
pages,
|
||||||
{
|
{
|
||||||
@ -207,7 +217,7 @@ function scratchpad_load()
|
|||||||
-- there are no pages yet, create one
|
-- there are no pages yet, create one
|
||||||
if pagesCount == 0 then
|
if pagesCount == 0 then
|
||||||
path = dirPath .. [[0000.txt]]
|
path = dirPath .. [[0000.txt]]
|
||||||
scratchpad.log("creating page " .. path)
|
log("creating page " .. path)
|
||||||
table.insert(
|
table.insert(
|
||||||
pages,
|
pages,
|
||||||
{
|
{
|
||||||
@ -219,19 +229,8 @@ function scratchpad_load()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function scratchpad.saveConfiguration()
|
local function saveConfiguration()
|
||||||
U.saveInFile(scratchpad.config, "config", lfs.writedir() .. "Config/ScratchpadConfig.lua")
|
U.saveInFile(config, "config", lfs.writedir() .. "Config/ScratchpadConfig.lua")
|
||||||
end
|
|
||||||
|
|
||||||
function scratchpad.log(str)
|
|
||||||
if not str then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if scratchpad.logFile then
|
|
||||||
scratchpad.logFile:write("[" .. os.date("%H:%M:%S") .. "] " .. str .. "\r\n")
|
|
||||||
scratchpad.logFile:flush()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function unlockKeyboardInput(releaseKeyboardKeys)
|
local function unlockKeyboardInput(releaseKeyboardKeys)
|
||||||
@ -268,7 +267,78 @@ function scratchpad_load()
|
|||||||
savePage(currentPage, textarea:getText(), true)
|
savePage(currentPage, textarea:getText(), true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function scratchpad.createWindow()
|
local function setVisible(b)
|
||||||
|
window:setVisible(b)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function handleResize(self)
|
||||||
|
local w, h = self:getSize()
|
||||||
|
|
||||||
|
panel:setBounds(0, 0, w, h - 20)
|
||||||
|
textarea:setBounds(0, 0, w, h - 20 - 20)
|
||||||
|
prevButton:setBounds(0, h - 40, 50, 20)
|
||||||
|
nextButton:setBounds(55, h - 40, 50, 20)
|
||||||
|
|
||||||
|
if pagesCount > 1 then
|
||||||
|
insertCoordsBtn:setBounds(120, h - 40, 50, 20)
|
||||||
|
else
|
||||||
|
insertCoordsBtn:setBounds(0, h - 40, 50, 20)
|
||||||
|
end
|
||||||
|
|
||||||
|
config.windowSize = {w = w, h = h}
|
||||||
|
saveConfiguration()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function handleMove(self)
|
||||||
|
local x, y = self:getPosition()
|
||||||
|
config.windowPosition = {x = x, y = y}
|
||||||
|
saveConfiguration()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function show()
|
||||||
|
if window == nil then
|
||||||
|
local status, err = pcall(createWindow)
|
||||||
|
if not status then
|
||||||
|
net.log("[Scratchpad] Error creating window: " .. tostring(err))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
window:setVisible(true)
|
||||||
|
window:setSkin(windowDefaultSkin)
|
||||||
|
panel:setVisible(true)
|
||||||
|
window:setHasCursor(true)
|
||||||
|
|
||||||
|
-- insert coords only works if the client is the server, so hide the button otherwise
|
||||||
|
if DCS.isServer() then
|
||||||
|
insertCoordsBtn:setVisible(true)
|
||||||
|
else
|
||||||
|
insertCoordsBtn:setVisible(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- show prev/next buttons only if we have more than one page
|
||||||
|
if pagesCount > 1 then
|
||||||
|
prevButton:setVisible(true)
|
||||||
|
nextButton:setVisible(true)
|
||||||
|
else
|
||||||
|
prevButton:setVisible(false)
|
||||||
|
nextButton:setVisible(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
isHidden = false
|
||||||
|
end
|
||||||
|
|
||||||
|
local function hide()
|
||||||
|
window:setSkin(windowSkinHidden)
|
||||||
|
panel:setVisible(false)
|
||||||
|
textarea:setFocused(false)
|
||||||
|
window:setHasCursor(false)
|
||||||
|
-- window.setVisible(false) -- if you make the window invisible, its destroyed
|
||||||
|
unlockKeyboardInput(true)
|
||||||
|
|
||||||
|
isHidden = true
|
||||||
|
end
|
||||||
|
|
||||||
|
local function createWindow()
|
||||||
window = DialogLoader.spawnDialogFromFile(lfs.writedir() .. "Scripts\\Scratchpad\\ScratchpadWindow.dlg", cdata)
|
window = DialogLoader.spawnDialogFromFile(lfs.writedir() .. "Scripts\\Scratchpad\\ScratchpadWindow.dlg", cdata)
|
||||||
windowDefaultSkin = window:getSkin()
|
windowDefaultSkin = window:getSkin()
|
||||||
panel = window.Box
|
panel = window.Box
|
||||||
@ -279,7 +349,7 @@ function scratchpad_load()
|
|||||||
|
|
||||||
-- setup textarea
|
-- setup textarea
|
||||||
local skin = textarea:getSkin()
|
local skin = textarea:getSkin()
|
||||||
skin.skinData.states.released[1].text.fontSize = scratchpad.config.fontSize
|
skin.skinData.states.released[1].text.fontSize = config.fontSize
|
||||||
textarea:setSkin(skin)
|
textarea:setSkin(skin)
|
||||||
|
|
||||||
textarea:addChangeCallback(
|
textarea:addChangeCallback(
|
||||||
@ -324,121 +394,50 @@ function scratchpad_load()
|
|||||||
|
|
||||||
-- setup window
|
-- setup window
|
||||||
window:setBounds(
|
window:setBounds(
|
||||||
scratchpad.config.windowPosition.x,
|
config.windowPosition.x,
|
||||||
scratchpad.config.windowPosition.y,
|
config.windowPosition.y,
|
||||||
scratchpad.config.windowSize.w,
|
config.windowSize.w,
|
||||||
scratchpad.config.windowSize.h
|
config.windowSize.h
|
||||||
)
|
)
|
||||||
scratchpad.handleResize(window)
|
handleResize(window)
|
||||||
|
|
||||||
window:addHotKeyCallback(
|
window:addHotKeyCallback(
|
||||||
scratchpad.config.hotkey,
|
config.hotkey,
|
||||||
function()
|
function()
|
||||||
if isHidden == true then
|
if isHidden == true then
|
||||||
scratchpad.show()
|
show()
|
||||||
else
|
else
|
||||||
scratchpad.hide()
|
hide()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
window:addSizeCallback(scratchpad.handleResize)
|
window:addSizeCallback(handleResize)
|
||||||
window:addPositionCallback(scratchpad.handleMove)
|
window:addPositionCallback(handleMove)
|
||||||
|
|
||||||
window:setVisible(true)
|
window:setVisible(true)
|
||||||
nextPage()
|
nextPage()
|
||||||
|
|
||||||
scratchpad.hide()
|
hide()
|
||||||
scratchpad.log("Scratchpad Window created")
|
log("Scratchpad Window created")
|
||||||
end
|
end
|
||||||
|
|
||||||
function scratchpad.setVisible(b)
|
local handler = {}
|
||||||
window:setVisible(b)
|
function handler.onSimulationFrame()
|
||||||
end
|
if config == nil then
|
||||||
|
loadConfiguration()
|
||||||
function scratchpad.handleResize(self)
|
|
||||||
local w, h = self:getSize()
|
|
||||||
|
|
||||||
panel:setBounds(0, 0, w, h - 20)
|
|
||||||
textarea:setBounds(0, 0, w, h - 20 - 20)
|
|
||||||
prevButton:setBounds(0, h - 40, 50, 20)
|
|
||||||
nextButton:setBounds(55, h - 40, 50, 20)
|
|
||||||
|
|
||||||
if pagesCount > 1 then
|
|
||||||
insertCoordsBtn:setBounds(120, h - 40, 50, 20)
|
|
||||||
else
|
|
||||||
insertCoordsBtn:setBounds(0, h - 40, 50, 20)
|
|
||||||
end
|
|
||||||
|
|
||||||
scratchpad.config.windowSize = {w = w, h = h}
|
|
||||||
scratchpad.saveConfiguration()
|
|
||||||
end
|
|
||||||
|
|
||||||
function scratchpad.handleMove(self)
|
|
||||||
local x, y = self:getPosition()
|
|
||||||
scratchpad.config.windowPosition = {x = x, y = y}
|
|
||||||
scratchpad.saveConfiguration()
|
|
||||||
end
|
|
||||||
|
|
||||||
function scratchpad.show()
|
|
||||||
if window == nil then
|
|
||||||
local status, err = pcall(scratchpad.createWindow)
|
|
||||||
if not status then
|
|
||||||
net.log("[Scratchpad] Error creating window: " .. tostring(err))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
window:setVisible(true)
|
|
||||||
window:setSkin(windowDefaultSkin)
|
|
||||||
panel:setVisible(true)
|
|
||||||
window:setHasCursor(true)
|
|
||||||
|
|
||||||
-- insert coords only works if the client is the server, so hide the button otherwise
|
|
||||||
if DCS.isServer() then
|
|
||||||
insertCoordsBtn:setVisible(true)
|
|
||||||
else
|
|
||||||
insertCoordsBtn:setVisible(false)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- show prev/next buttons only if we have more than one page
|
|
||||||
if pagesCount > 1 then
|
|
||||||
prevButton:setVisible(true)
|
|
||||||
nextButton:setVisible(true)
|
|
||||||
else
|
|
||||||
prevButton:setVisible(false)
|
|
||||||
nextButton:setVisible(false)
|
|
||||||
end
|
|
||||||
|
|
||||||
isHidden = false
|
|
||||||
end
|
|
||||||
|
|
||||||
function scratchpad.hide()
|
|
||||||
window:setSkin(windowSkinHidden)
|
|
||||||
panel:setVisible(false)
|
|
||||||
textarea:setFocused(false)
|
|
||||||
window:setHasCursor(false)
|
|
||||||
-- window.setVisible(false) -- if you make the window invisible, its destroyed
|
|
||||||
unlockKeyboardInput(true)
|
|
||||||
|
|
||||||
isHidden = true
|
|
||||||
end
|
|
||||||
|
|
||||||
function scratchpad.onSimulationFrame()
|
|
||||||
if scratchpad.config == nil then
|
|
||||||
scratchpad.loadConfiguration()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if not window then
|
if not window then
|
||||||
scratchpad.log("Creating Scratchpad window hidden...")
|
log("Creating Scratchpad window hidden...")
|
||||||
scratchpad.createWindow()
|
createWindow()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
DCS.setUserCallbacks(handler)
|
||||||
DCS.setUserCallbacks(scratchpad)
|
|
||||||
|
|
||||||
net.log("[Scratchpad] Loaded ...")
|
net.log("[Scratchpad] Loaded ...")
|
||||||
end
|
end
|
||||||
|
|
||||||
local status, err = pcall(scratchpad_load)
|
local status, err = pcall(loadScratchpad)
|
||||||
if not status then
|
if not status then
|
||||||
net.log("[Scratchpad] Load Error: " .. tostring(err))
|
net.log("[Scratchpad] Load Error: " .. tostring(err))
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user