Add control stage asserts

This commit is contained in:
Cooldude2606
2025-01-08 22:40:17 +00:00
parent e67959a239
commit 0b665bbd46
2 changed files with 22 additions and 21 deletions

View File

@@ -101,6 +101,7 @@ end
--- @param scope string --- @param scope string
--- @return ExpGui.GuiData --- @return ExpGui.GuiData
function GuiData.create(scope) function GuiData.create(scope)
ExpUtil.assert_not_runtime()
assert(GuiData._scopes[scope] == nil, "Scope already exists with name: " .. scope) assert(GuiData._scopes[scope] == nil, "Scope already exists with name: " .. scope)
local instance = { local instance = {

View File

@@ -6,9 +6,11 @@ local GuiIter = require("./iter")
--- @class ExpGui_ExpElement --- @class ExpGui_ExpElement
local ExpElement = { local ExpElement = {
_elements = {} _elements = {},
} }
ExpElement.events = {}
--- @alias ExpElement.DrawCallback fun(def: ExpElement, parent: LuaGuiElement, ...): LuaGuiElement?, function? --- @alias ExpElement.DrawCallback fun(def: ExpElement, parent: LuaGuiElement, ...): LuaGuiElement?, function?
--- @alias ExpElement.PostDrawCallback fun(def: ExpElement, element: LuaGuiElement?, parent: LuaGuiElement, ...): table? --- @alias ExpElement.PostDrawCallback fun(def: ExpElement, element: LuaGuiElement?, parent: LuaGuiElement, ...): table?
--- @alias ExpElement.PostDrawCallbackAdder fun(self: ExpElement, definition: table | ExpElement.PostDrawCallback): ExpElement --- @alias ExpElement.PostDrawCallbackAdder fun(self: ExpElement, definition: table | ExpElement.PostDrawCallback): ExpElement
@@ -162,6 +164,7 @@ end
--- Enable tracking of all created elements --- Enable tracking of all created elements
--- @return ExpElement --- @return ExpElement
function ExpElement._prototype:track_all_elements() function ExpElement._prototype:track_all_elements()
ExpUtil.assert_not_runtime()
self._track_elements = true self._track_elements = true
return self return self
end end
@@ -170,6 +173,7 @@ end
--- @param definition table | ExpElement.DrawCallback --- @param definition table | ExpElement.DrawCallback
--- @return ExpElement --- @return ExpElement
function ExpElement._prototype:draw(definition) function ExpElement._prototype:draw(definition)
ExpUtil.assert_not_runtime()
if type(definition) == "function" then if type(definition) == "function" then
self._draw = definition self._draw = definition
return self return self
@@ -205,6 +209,7 @@ end
--- @return ExpElement.PostDrawCallbackAdder --- @return ExpElement.PostDrawCallbackAdder
local function definition_factory(prop_name, debug_def, debug_args) local function definition_factory(prop_name, debug_def, debug_args)
return function(self, definition) return function(self, definition)
ExpUtil.assert_not_runtime()
if type(definition) == "function" then if type(definition) == "function" then
self[prop_name] = definition self[prop_name] = definition
return self return self
@@ -335,10 +340,6 @@ function ExpElement._prototype:unlink_element(element)
return element, ExpElement._prototype.unlink_element return element, ExpElement._prototype.unlink_element
end end
local e = defines.events
local events = {
}
--- Handle any gui events --- Handle any gui events
--- @param event EventData.on_gui_click --- @param event EventData.on_gui_click
local function event_handler(event) local function event_handler(event)
@@ -372,7 +373,7 @@ end
--- @param handler fun(def: ExpElement, event: EventData) --- @param handler fun(def: ExpElement, event: EventData)
--- @return ExpElement --- @return ExpElement
function ExpElement._prototype:on_event(event, handler) function ExpElement._prototype:on_event(event, handler)
events[event] = event_handler ExpElement.events[event] = event_handler
self._has_handlers = true self._has_handlers = true
local handlers = self._events[event] or {} local handlers = self._events[event] or {}
@@ -393,60 +394,59 @@ end
--- Called when LuaGuiElement checked state is changed (related to checkboxes and radio buttons). --- Called when LuaGuiElement checked state is changed (related to checkboxes and radio buttons).
--- @type ExpElement.OnEventAdder<EventData.on_gui_checked_state_changed> --- @type ExpElement.OnEventAdder<EventData.on_gui_checked_state_changed>
ExpElement._prototype.on_checked_state_changed = event_factory(e.on_gui_checked_state_changed) ExpElement._prototype.on_checked_state_changed = event_factory(defines.events.on_gui_checked_state_changed)
--- Called when LuaGuiElement is clicked. --- Called when LuaGuiElement is clicked.
--- @type ExpElement.OnEventAdder<EventData.on_gui_click> --- @type ExpElement.OnEventAdder<EventData.on_gui_click>
ExpElement._prototype.on_click = event_factory(e.on_gui_click) ExpElement._prototype.on_click = event_factory(defines.events.on_gui_click)
--- Called when the player closes the GUI they have open. --- Called when the player closes the GUI they have open.
--- @type ExpElement.OnEventAdder<EventData.on_gui_closed> --- @type ExpElement.OnEventAdder<EventData.on_gui_closed>
ExpElement._prototype.on_closed = event_factory(e.on_gui_closed) ExpElement._prototype.on_closed = event_factory(defines.events.on_gui_closed)
--- Called when a LuaGuiElement is confirmed, for example by pressing Enter in a textfield. --- Called when a LuaGuiElement is confirmed, for example by pressing Enter in a textfield.
--- @type ExpElement.OnEventAdder<EventData.on_gui_confirmed> --- @type ExpElement.OnEventAdder<EventData.on_gui_confirmed>
ExpElement._prototype.on_confirmed = event_factory(e.on_gui_confirmed) ExpElement._prototype.on_confirmed = event_factory(defines.events.on_gui_confirmed)
--- Called when LuaGuiElement element value is changed (related to choose element buttons). --- Called when LuaGuiElement element value is changed (related to choose element buttons).
--- @type ExpElement.OnEventAdder<EventData.on_gui_elem_changed> --- @type ExpElement.OnEventAdder<EventData.on_gui_elem_changed>
ExpElement._prototype.on_elem_changed = event_factory(e.on_gui_elem_changed) ExpElement._prototype.on_elem_changed = event_factory(defines.events.on_gui_elem_changed)
--- Called when LuaGuiElement is hovered by the mouse. --- Called when LuaGuiElement is hovered by the mouse.
--- @type ExpElement.OnEventAdder<EventData.on_gui_hover> --- @type ExpElement.OnEventAdder<EventData.on_gui_hover>
ExpElement._prototype.on_hover = event_factory(e.on_gui_hover) ExpElement._prototype.on_hover = event_factory(defines.events.on_gui_hover)
--- Called when the player's cursor leaves a LuaGuiElement that was previously hovered. --- Called when the player's cursor leaves a LuaGuiElement that was previously hovered.
--- @type ExpElement.OnEventAdder<EventData.on_gui_leave> --- @type ExpElement.OnEventAdder<EventData.on_gui_leave>
ExpElement._prototype.on_leave = event_factory(e.on_gui_leave) ExpElement._prototype.on_leave = event_factory(defines.events.on_gui_leave)
--- Called when LuaGuiElement element location is changed (related to frames in player.gui.screen). --- Called when LuaGuiElement element location is changed (related to frames in player.gui.screen).
--- @type ExpElement.OnEventAdder<EventData.on_gui_location_changed> --- @type ExpElement.OnEventAdder<EventData.on_gui_location_changed>
ExpElement._prototype.on_location_changed = event_factory(e.on_gui_location_changed) ExpElement._prototype.on_location_changed = event_factory(defines.events.on_gui_location_changed)
--- Called when the player opens a GUI. --- Called when the player opens a GUI.
--- @type ExpElement.OnEventAdder<EventData.on_gui_opened> --- @type ExpElement.OnEventAdder<EventData.on_gui_opened>
ExpElement._prototype.on_opened = event_factory(e.on_gui_opened) ExpElement._prototype.on_opened = event_factory(defines.events.on_gui_opened)
--- Called when LuaGuiElement selected tab is changed (related to tabbed-panes). --- Called when LuaGuiElement selected tab is changed (related to tabbed-panes).
--- @type ExpElement.OnEventAdder<EventData.on_gui_selected_tab_changed> --- @type ExpElement.OnEventAdder<EventData.on_gui_selected_tab_changed>
ExpElement._prototype.on_selected_tab_changed = event_factory(e.on_gui_selected_tab_changed) ExpElement._prototype.on_selected_tab_changed = event_factory(defines.events.on_gui_selected_tab_changed)
--- Called when LuaGuiElement selection state is changed (related to drop-downs and listboxes). --- Called when LuaGuiElement selection state is changed (related to drop-downs and listboxes).
--- @type ExpElement.OnEventAdder<EventData.on_gui_selection_state_changed> --- @type ExpElement.OnEventAdder<EventData.on_gui_selection_state_changed>
ExpElement._prototype.on_selection_state_changed = event_factory(e.on_gui_selection_state_changed) ExpElement._prototype.on_selection_state_changed = event_factory(defines.events.on_gui_selection_state_changed)
--- Called when LuaGuiElement switch state is changed (related to switches). --- Called when LuaGuiElement switch state is changed (related to switches).
--- @type ExpElement.OnEventAdder<EventData.on_gui_switch_state_changed> --- @type ExpElement.OnEventAdder<EventData.on_gui_switch_state_changed>
ExpElement._prototype.on_switch_state_changed = event_factory(e.on_gui_switch_state_changed) ExpElement._prototype.on_switch_state_changed = event_factory(defines.events.on_gui_switch_state_changed)
--- Called when LuaGuiElement text is changed by the player. --- Called when LuaGuiElement text is changed by the player.
--- @type ExpElement.OnEventAdder<EventData.on_gui_text_changed> --- @type ExpElement.OnEventAdder<EventData.on_gui_text_changed>
ExpElement._prototype.on_text_changed = event_factory(e.on_gui_text_changed) ExpElement._prototype.on_text_changed = event_factory(defines.events.on_gui_text_changed)
--- Called when LuaGuiElement slider value is changed (related to the slider element). --- Called when LuaGuiElement slider value is changed (related to the slider element).
--- @type ExpElement.OnEventAdder<EventData.on_gui_value_changed> --- @type ExpElement.OnEventAdder<EventData.on_gui_value_changed>
ExpElement._prototype.on_value_changed = event_factory(e.on_gui_value_changed) ExpElement._prototype.on_value_changed = event_factory(defines.events.on_gui_value_changed)
ExpElement._metatable.__call = ExpElement._prototype.create ExpElement._metatable.__call = ExpElement._prototype.create
ExpElement.events = events --- @package
return ExpElement return ExpElement