Oop Menu
Oop Menu
1
MENU_LIB_LINK = "https://fatality.win/threads/object-oriented-gui.11630/"
function MENU_LIB_ERROR_OUTDATED()
error(string.format("You have an outdated version of the \"Object-oriented
gui\" please redownload it at: %s.", MENU_LIB_LINK))
end
local CallbackItems = {}
function Base:set_visible(state)
gui.set_visible(self.full_path or self.path, state)
if self.call_callbacks_on_set_visible then
for Callback, _ in pairs(CallbackItems[self.full_path or self.path]) do
Callback(self:get())
end
end
end
if call then
func(self:get())
end
self.call_callbacks_on_set_visible = on_set_vis
return func
end
function Base:get()
return self.cfg_value:get_int()
end
function Base:set(val)
self.cfg_value:set_int(tonumber(val))
end
function Base:has_updated(Old)
return self:get() ~= Old
end
function Base:update_callback_value()
return self:get()
end
function Base:bind_to(other)
if self.type == "reference" or other.type == "reference" or other.type ==
self.type then
other:add_callback(function (value)
self:set(value)
end)
else
utils.error_print("menu lib. base:bind_to other type differed from self
type")
end
end
Base.Items = {gui.get_config_item(path)}
function Base:get()
if not self.Items[2] then
return self.Items[1]:get_int()
else
local Ret = {}
for _, Item in pairs(self.Items) do
table.insert(Ret, Item:get_int())
end
return Ret
end
end
function Base:get_combo_items()
return gui.get_combo_items(self.full_path or self.path)
end
function Base:set_combo_items(items)
return gui.set_combo_items(self.full_path or self.path, items)
end
function Base:get_listbox_items()
return gui.get_listbox_items(self.full_path or self.path)
end
function Base:set_listbox_items(items)
return gui.set_listbox_items(self.full_path or self.path, items)
end
function Base:set(value)
local ValueType = type(value)
if not self.Items[2] then
if ValueType == "table" then
error("menu lib. reference:set value type differed from reference
type")
end
-- need to do this or api freaks out
local IntValue = ValueType == "boolean" and (value == true and 1 or 0)
or tonumber(value)
self.Items[1]:set_int(IntValue)
else
if ValueType ~= "table" then
error("menu lib. reference:set value type differed from reference
type")
end
for i, v in pairs(value) do
if not self.Items[i] then
error("menu lib. reference:set reference index does not exist:
%s", i)
end
-- need to do this or api freaks out
local IntValue = type(v) == "boolean" and (v == true and 1 or 0) or
tonumber(v)
self.Items[i]:set_int(IntValue)
end
end
end
function Base:has_updated(Old)
if not self.Items[2] then
return self:get() ~= Old
else
for i, Item in pairs(self.Items) do
if Item:get_int() ~= Old[i] then
return true, i
end
end
return false
end
end
function Base:update_callback_value()
return self:get()
end
function Base:get()
return self.cfg_value:get_bool()
end
function Base:set(val)
if type(val) == "boolean" then
self.cfg_value:set_bool(val)
elseif type(val) == "number" then
self.cfg_value:set_bool(val > 0)
else
error("menu lib. checkbox:set invalid value type. Accepted types
[number, boolean]")
end
end
Base.items = items
function Base:get_items()
self.items = gui.get_combo_items(self.full_path)
return self.items
end
function Base:set_items(items)
Base.items = items
gui.set_combo_items(self.full_path, self.items)
end
function Base:get(flags)
local ignore_array = flags and string.find(flags, "-a")
local ignore_dict = flags and string.find(flags, "-d")
local Ret = {}
for i, CfgValue in pairs(self.cfg_values) do
local IndexValue = CfgValue:get_bool()
function Base:set(values)
if type(values) ~= "table" then
error("menu lib. multi_combo:set value type must be a table")
end
end
end
function Base:has_updated(Old)
for i, CfgItem in pairs(self.cfg_values) do
if Old[i] == nil then
return true, i
end
function Base:at(idx)
if type(idx) == "string" then
for ItemIndex, ItemName in pairs(Base.items) do
if ItemName == idx then
idx = ItemIndex
break
end
end
end
function Base:get_active_names()
local ActiveNames = {}
for Index, ConfigValue in pairs(Base.cfg_values) do
if ConfigValue:get_bool() then
table.insert(ActiveNames, self.items[Index])
end
end
return ActiveNames
end
if Base.is_confirm then
if info.fatality.allow_insecure then
-- try a nuch of times to make a spaced name (this is so we can have
multiple confirm ones in one spot)
for i = 1, 50 do
local rep = (" "):rep(i)
local ConfirmName = string.format("%s%s%s", rep, "Confirm", rep)
local CancelName = string.format("%s%s%s", rep, "Cancel", rep)
break
end
end
else
utils.error_print("Cannot create confirmable button without \"Allow
unsafe scripts\" on")
end
else
gui.add_button(name, path, callback)
end
function Base:get()
return self.cfg_value:get_string()
end
function Base:set(str)
self.cfg_value:set_string(tostring(str))
end
Base.items = items
function Base:get_items()
self.items = gui.get_listbox_items(self.full_path)
return self.items
end
function Base:set_items(items)
self.items = items
gui.set_listbox_items(self.full_path, self.items)
end
Base.get = nil
Base.set = nil
Base.add_callback = nil
Base.has_updated = nil
Base.update_callback_value = nil
function Base:get_info()
return gui.get_keybind(self.path)
end
function Base:get()
return self.cfg_value:get_color()
end
function Base:set(color)
self.cfg_value:set_color(color)
end
function Base:has_updated(Old)
local self_color = self:get()
return self_color.r ~= Old.r or self_color.g ~= Old.g or self_color.b ~=
Old.b or self_color.a ~= Old.a
end
utils.new_timer(1, function()
for FullPath, Callbacks in pairs(CallbackItems) do
for Callback, Inst in pairs(Callbacks) do
local HasUpdated, Other = Inst.Item:has_updated(Inst.OldValue)
if HasUpdated then
Inst.OldValue = Inst.Item:update_callback_value()
Callback(Inst.OldValue, Other)
end
end
end
end):start()
return
{
add_checkbox = CheckboxMT.new,
add_slider = SliderMT.new,
add_combo = ComboMT.new,
add_multi_combo = MultiComboMT.new,
add_button = ButtonMT.new,
add_textbox = TextboxMT.new,
add_listbox = ListboxMT.new,
add_keybind = KeybindMT.new,
add_colorpicker = ColorpickerMT.new,
get_reference = ReferenceMT.new,
}