1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-22 03:38:48 +02:00

Merge pull request #10 from Gerkiz/master

Required for poll
This commit is contained in:
MewMew 2019-02-26 21:00:35 +01:00 committed by GitHub
commit d1070dad33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 19 deletions

View File

@ -307,7 +307,7 @@ end
local function draw_main_frame(left, player)
local frame = left.add {type = 'frame', name = main_frame_name, caption = 'Polls', direction = 'vertical'}
frame.style.maximal_width = 320
frame.style.maximal_width = 360
local poll_viewer_top_flow = frame.add {type = 'table', column_count = 5}
poll_viewer_top_flow.style.horizontal_spacing = 0
@ -559,7 +559,7 @@ local function draw_create_poll_frame(parent, player, previous_data)
local frame =
parent.add {type = 'frame', name = create_poll_frame_name, caption = title_text, direction = 'vertical'}
frame.style.maximal_width = 320
frame.style.maximal_width = 360
local scroll_pane = frame.add {type = 'scroll-pane', vertical_scroll_policy = 'always'}
scroll_pane.style.maximal_height = 250

View File

@ -1,32 +1,39 @@
local Token = require 'utils.global_token'
local Token = require 'utils.token'
local Event = require 'utils.event'
local Game = require 'utils.game'
local Global = require 'utils.global'
local Gui = {}
global.Gui_data = {}
local data = {}
Global.register(
data,
function(tbl)
data = tbl
end
)
local top_elements = {}
local on_visible_handlers = {}
local on_pre_hidden_handlers = {}
function Gui.uid_name()
if _DEBUG then
-- https://stackoverflow.com/questions/48402876/getting-current-file-name-in-lua
local filename = debug.getinfo(2, 'S').source:match('^.+/(.+)$'):sub(1, -5)
return filename .. ',' .. Token.uid()
else
return tostring(Token.uid())
end
return tostring(Token.uid())
end
-- Associates data with the LuaGuiElement. If data is nil then removes the data
function Gui.set_data(element, data)
global.Gui_data[element.player_index * 0x100000000 + element.index] = data
function Gui.set_data(element, value)
data[element.player_index * 0x100000000 + element.index] = value
end
-- Gets the Associated data with this LuaGuiElement if any.
function Gui.get_data(element)
return global.Gui_data[element.player_index * 0x100000000 + element.index]
return data[element.player_index * 0x100000000 + element.index]
end
-- Removes data associated with LuaGuiElement and its children recursivly.
function Gui.remove_data_recursivly(element)
-- Removes data associated with LuaGuiElement and its children recursively.
function Gui.remove_data_recursively(element)
Gui.set_data(element, nil)
local children = element.children
@ -37,7 +44,7 @@ function Gui.remove_data_recursivly(element)
for _, child in ipairs(children) do
if child.valid then
Gui.remove_data_recursivly(child)
Gui.remove_data_recursively(child)
end
end
end
@ -58,7 +65,7 @@ function Gui.remove_children_data(element)
end
function Gui.destroy(element)
Gui.remove_data_recursivly(element)
Gui.remove_data_recursively(element)
element.destroy()
end
@ -81,7 +88,7 @@ local function handler_factory(event_id)
return
end
local player = game.players[event.player_index]
local player = Game.get_player_by_index(event.player_index)
if not player or not player.valid then
return
end
@ -100,6 +107,21 @@ local function handler_factory(event_id)
end
end
local function custom_handler_factory(handlers)
return function(element_name, handler)
handlers[element_name] = handler
end
end
local function custom_raise(handlers, element, player)
local handler = handlers[element.name]
if not handler then
return
end
handler({element = element, player = player})
end
-- Register a handler for the on_gui_checked_state_changed event for LuaGuiElements with element_name.
-- Can only have one handler per element name.
-- Guarantees that the element and the player are valid when calling the handler.
@ -142,4 +164,28 @@ Gui.on_text_changed = handler_factory(defines.events.on_gui_text_changed)
-- Adds a player field to the event table.
Gui.on_value_changed = handler_factory(defines.events.on_gui_value_changed)
-- Register a handler for when the player shows the top LuaGuiElements with element_name.
-- Assuming the element_name has been added with Gui.allow_player_to_toggle_top_element_visibility.
-- Can only have one handler per element name.
-- Guarantees that the element and the player are valid when calling the handler.
-- Adds a player field to the event table.
Gui.on_player_show_top = custom_handler_factory(on_visible_handlers)
-- Register a handler for when the player hides the top LuaGuiElements with element_name.
-- Assuming the element_name has been added with Gui.allow_player_to_toggle_top_element_visibility.
-- Can only have one handler per element name.
-- Guarantees that the element and the player are valid when calling the handler.
-- Adds a player field to the event table.
Gui.on_pre_player_hide_top = custom_handler_factory(on_pre_hidden_handlers)
--- Allows the player to show / hide this element.
-- The element must be part in gui.top.
-- This function must be called in the control stage, i.e not inside an event.
-- @param element_name<string> This name must be globally unique.
function Gui.allow_player_to_toggle_top_element_visibility(element_name)
top_elements[#top_elements + 1] = element_name
end
local toggle_button_name = Gui.uid_name()
return Gui