-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New checkbox widget #73
Open
ZookaOnGit
wants to merge
5
commits into
demonnic:main
Choose a base branch
from
ZookaOnGit:checkbox-widget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
|
||
checkbox = { | ||
parent = Geyser.Container, | ||
name = 'CheckboxClass', | ||
checkboxLocation = "https://demonnic.github.io/image-assets/checkbox-25px.png", | ||
uncheckedLocation = "https://demonnic.github.io/image-assets/unchecked-25px.png" | ||
} | ||
|
||
checkbox.__index = checkbox | ||
setmetatable(checkbox, checkbox.parent) | ||
|
||
|
||
--local gss = Geyser.StyleSheet | ||
local directory = getMudletHomeDir() .. "/checkbox/" | ||
local saveFile = directory .. "fileLocations.lua" | ||
if not io.exists(directory) then | ||
lfs.mkdir(directory) | ||
end | ||
|
||
|
||
function checkbox:new(cons, container) | ||
cons = cons or {} | ||
local consType = type(cons) | ||
if consType ~= "table" then | ||
printError(f"checkbox:new(cons, container): cons as table of options expected, got {consType}!", true, true) | ||
end | ||
cons.name = cons.name or Geyser.nameGen("checkbox") | ||
local me = self.parent:new(cons, container) | ||
setmetatable(me, self) | ||
me:createComponents() | ||
return me | ||
end | ||
|
||
|
||
function checkbox:createDisplay() | ||
|
||
self:set_constraints({x = "30%", y = "60%", width = 200, height = 25}) | ||
|
||
self.checkboxLabel = Geyser.Label:new({ | ||
message="A checkbox label", | ||
x = 0, y = 0, | ||
width = "100%-25px", | ||
height = "100%", | ||
color = "white", | ||
fgColor = "black" | ||
}, self) | ||
|
||
self.checkboxButton = Geyser.Button:new({ | ||
name = "Checkbox", | ||
x = "-25r", | ||
y = 0, | ||
width = 25, | ||
height = 25, | ||
color = "white", | ||
downColor = "white", | ||
downStyle = [[ background-color: white; border-image: url(/home/kirk/mudlet-data/profiles/Development/checkbox-25px.png); ]], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should use self.checkboxFile and self.uncheckedFile instead of hardcoding them |
||
style = [[ background-color: white; border-image: url(/home/kirk/mudlet-data/profiles/Development/unchecked-25px.png); ]], | ||
downCommand = "unticked", | ||
clickCommand = "ticked", | ||
msg = "", | ||
downMsg = "", | ||
tooltip = "", | ||
downTooltip = "", | ||
twoState = true, | ||
state = "up", | ||
toolTipDuration = 0 | ||
}, self) | ||
|
||
end | ||
|
||
|
||
function checkbox:createComponents() | ||
|
||
self:obtainImages() | ||
self:createDisplay() | ||
|
||
end | ||
|
||
|
||
function checkbox:obtainImages() | ||
local locations = self:getFileLocs() | ||
local checkboxURL = self.checkboxLocation | ||
local uncheckedURL = self.uncheckedLocation | ||
local checkboxFile = locations[checkboxURL] | ||
local uncheckedFile = locations[uncheckedURL] | ||
local locationsChanged = false | ||
if not (checkboxFile and io.exists(checkboxFile)) then | ||
if not checkboxFile then | ||
checkboxFile = directory .. self.name .. "/checkbox-25px.png" | ||
locations[checkboxURL] = checkboxFile | ||
locationsChanged = true | ||
end | ||
if checkboxURL:match("^http") then | ||
self:downloadFile(checkboxURL, checkboxFile) | ||
elseif io.exists(checkboxURL) then | ||
checkboxFile = checkboxURL | ||
locations[checkboxURL] = checkboxFile | ||
locationsChanged = true | ||
end | ||
end | ||
if not (uncheckedFile and io.exists(uncheckedFile)) then | ||
if not uncheckedFile then | ||
uncheckedFile = directory .. self.name .. "/unchecked-25px.png" | ||
locations[uncheckedURL] = uncheckedFile | ||
locationsChanged = true | ||
end | ||
if uncheckedURL:match("^http") then | ||
self:downloadFile(uncheckedURL, uncheckedFile) | ||
elseif io.exists(uncheckedURL) then | ||
uncheckedFile = uncheckedURL | ||
locations[uncheckedURL] = uncheckedFile | ||
locationsChanged = true | ||
end | ||
end | ||
self.checkboxFile = checkboxFile | ||
self.uncheckedFile = uncheckedFile | ||
if locationsChanged then | ||
table.save(saveFile, locations) | ||
end | ||
end | ||
|
||
|
||
function checkbox:downloadFile(url, fileName) | ||
local parts = fileName:split("/") | ||
parts[#parts] = nil | ||
local dirName = table.concat(parts, "/") .. "/" | ||
if not io.exists(dirName) then | ||
lfs.mkdir(dirName) | ||
end | ||
local uname = "checkbox" | ||
local handlerName = self.name .. url | ||
local handler = function(event, ...) | ||
local args = {...} | ||
local file = #args == 1 and args[1] or args[2] | ||
if file ~= fileName then | ||
return true | ||
end | ||
if event == "sysDownloadDone" then | ||
debugc(f"INFO:Checkbox successfully downloaded {file}") | ||
stopNamedEventHandler(uname, handlerName .. "error") | ||
return false | ||
end | ||
cecho(f"\n<red>ERROR:<reset>Checkbox had an issue downloading an image file to {file}: {args[1]}\n") | ||
stopNamedEventHandler(uname, handlerName .. "done") | ||
end | ||
registerNamedEventHandler(uname, handlerName .. "done", "sysDownloadDone", handler, true) | ||
registerNamedEventHandler(uname, handlerName .. "error", "sysDownloadError", handler, true) | ||
downloadFile(fileName, url) | ||
end | ||
|
||
|
||
function checkbox:getFileLocs() | ||
local locations = {} | ||
if io.exists(saveFile) then | ||
table.load(saveFile, locations) | ||
end | ||
return locations | ||
end | ||
|
||
|
||
function checkbox:isChecked() | ||
|
||
if self.checkboxButton.state == "down" then | ||
return true | ||
end | ||
|
||
return false | ||
|
||
end | ||
|
||
return checkbox |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should be set via the cons values in the checkbox:new function. We just then let inheritance handle the rest.