Skip to content

Instantly share code, notes, and snippets.

@FichteFoll
Created May 8, 2013 00:25
Show Gist options
  • Save FichteFoll/5537299 to your computer and use it in GitHub Desktop.
Save FichteFoll/5537299 to your computer and use it in GitHub Desktop.
_G.dlg - Provides functions with basic layouts for aegisub.dialog.display (not sure if working ...)
--[[
- Namespace: _G.dlg
- Description: Provides functions with basic layouts for aegisub.dialog.display
- Constants:
MSG_OK = 0
MSG_OKCANCEL = 1
MSG_YESNO = 2
MSG_YESNOALL = 3
- Members:
msg(style, msg)
prompt(style, msg)
* function msg(style, msg)
Works similar to usual message boxes.
@style (integer)
One of the MSG_* constants
@msg
The message that shall be displayed
end
* function prompt(flags, msg)
Something like an "Input Box" (eg JavaScript) that asks for a user input
end
- Author: FichteFoll
- Date Modified: 2012-02-26
- Version: 0.1
]]
if not _G.dlg then
local dlg = {
-- configuration constants
MSG_OK = 0, -- starting with 0 here (!)
MSG_OKCANCEL = 1,
MSG_YESNO = 2,
MSG_YESNOALL = 3
}
_G.dlg = dlg -- add to global scope
dlg.msg = function(msg, style)
local config, buttons, result, results
buttons = {
[dlg.MSG_OK] = {
btn = {"OK"},
res = {
["OK"] = true
}
},
[dlg.MSG_OKCANCEL] = {
btn = nil,
res = {}
},
[dlg.MSG_YESNO] = {
btn = {"Yes", "No"},
res = {
["Yes"] = true,
["No"] = false,
[false] = -1 -- cast to "nil" instead of "false"
}
},
[dlg.MSG_YESNOALL] = {
btn = {"Yes", "Yes to all", "No", "No to all"},
res = {
["Yes"] = true,
["Yes to all"] = 2,
["No"] = false,
["No to all"] = 3,
[false] = -1 -- cast to "nil" instead of "false"
}
}
}
-- show dialog using internal function
if not buttons[style] then style = dlg.MSG_OK; end
return dlg._show_msg(msg, buttons[style])
end
-- **INTERNAL USE ONLY** / Or if you want to have your own button set
dlg._show_msg = function(msg, buttons)
local config = {
{
class = "label",
x = 0, y = 0, width = 3, height = 3,
label = msg
}
}
-- display dialog
local result, _ = aegisub.dialog.display(config, buttons.btn)
-- return corresponding value, or <result>
if buttons.res[result] == -1 then
-- there are only two values that give "false" when used in a conditional block
-- use this as a workaround for a 2nd "false" value since "nil" is used to indicate the presence of a field
return nil
elseif buttons.res[result] ~= nil then
-- (v1 ~= nil and v1 or v2) wont work; it is by design that these tables may contain "false" as a value
return buttons.res[result]
end
return result
end
end
return _G.dlg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment