Inputs cleen

This commit is contained in:
Cooldude2606
2017-10-15 20:50:56 +01:00
parent 4d563ec3ec
commit 9d17497c9b

View File

@@ -12,20 +12,20 @@ local inputs = ExpGui.inputs
--allows defining of new buttons;;name how to call button;;default_display what is shown on the button;;default_tooltip the tooltip display;;event function(player,element) that runs on click --allows defining of new buttons;;name how to call button;;default_display what is shown on the button;;default_tooltip the tooltip display;;event function(player,element) that runs on click
function add_input.button(name,default_display,default_tooltip,event) function add_input.button(name,default_display,default_tooltip,event)
if not name then error('Button requires a name') end if not name then error('Button requires a name') end
table.insert(inputs.buttons,{name=name,display=default_display,tooltip=default_tooltip,event=event}) inputs.buttons[name] = {name=name,display=default_display,tooltip=default_tooltip,event=event})
end end
--allows defining of text box inputs;;name how to call button;;default_display what is shown on the button;;event function(player,element) that runs on text change --allows defining of text box inputs;;name how to call button;;default_display what is shown on the button;;event function(player,element) that runs on text change
function add_input.text(name,default_display,event) function add_input.text(name,default_display,event)
if not name then error('Text Filed requires a name') end if not name then error('Text Filed requires a name') end
table.insert(inputs.text,{name=name,display=default_display,event=event}) inputs.text[name] = {name=name,display=default_display,event=event})
end end
--draws the button into a gui;;frame the frame to draw to;;name name of button to draw;;display(opptinal) overrides the default;;tooltip(opptinal) overrides the default --draws the button into a gui;;frame the frame to draw to;;name name of button to draw;;display(opptinal) overrides the default;;tooltip(opptinal) overrides the default
function add_input.draw_button(frame,name,display,tooltip) function add_input.draw_button(frame,name,display,tooltip)
if not frame or not frame.valid then error('No frame to draw to') end if not frame or not frame.valid then error('No frame to draw to') end
if not name then error('No button to draw') end if not name then error('No button to draw') end
debug_write({'GUI','INPUT'},name) debug_write({'GUI','INPUT'},name)
for _,button in pairs(inputs.buttons) do if inputs.buttons[name] then
if button.name == name then local button = inputs.buttons[name]
local display = display or button.display or button.name local display = display or button.display or button.name
local tooltip = tooltip or button.tooltip local tooltip = tooltip or button.tooltip
if frame.gui.is_valid_sprite_path(display) then if frame.gui.is_valid_sprite_path(display) then
@@ -34,7 +34,6 @@ function add_input.draw_button(frame,name,display,tooltip)
frame.add{name=name, type = "button", caption=display, tooltip=tooltip, style = mod_gui.button_style} frame.add{name=name, type = "button", caption=display, tooltip=tooltip, style = mod_gui.button_style}
end return end return
end end
end
error('No Button by the name of '..name) error('No Button by the name of '..name)
end end
--draws the text into a gui;;frame the frame to draw to;;name name of button to draw;;display(opptinal) overrides the default;;tooltip(opptinal) overrides the default --draws the text into a gui;;frame the frame to draw to;;name name of button to draw;;display(opptinal) overrides the default;;tooltip(opptinal) overrides the default
@@ -42,33 +41,32 @@ function add_input.draw_text(frame,name,display)
if not frame or not frame.valid then error('No frame to draw to') end if not frame or not frame.valid then error('No frame to draw to') end
if not name then error('No text filed to draw') end if not name then error('No text filed to draw') end
debug_write({'GUI','INPUT'},name) debug_write({'GUI','INPUT'},name)
for _,text in pairs(inputs.text) do if inputs.text[name] then
if text.name == name then local text = inputs.text[name]
local display = display or text.display or text.name local display = display or text.display or text.name
frame.add{name=name, type='textfield'} frame.add{name=name, type='textfield'}
frame[name].caption=display frame[name].caption=display
break return
end
end end
error('No Text by the name of '..name)
end end
--the magic behind the buttons --the magic behind the buttons
Event.register(defines.events.on_gui_click, function(event) Event.register(defines.events.on_gui_click, function(event)
local player = game.players[event.player_index] local player = game.players[event.player_index]
if event.element.type == 'button' or event.element.type == 'sprite-button' then if event.element.type == 'button' or event.element.type == 'sprite-button' then
for _,button in pairs(inputs.buttons) do if inputs.buttons[event.element.name] then
if button.name == event.element.name then local button = inputs.buttons[event.element.name]
if button.event then button.event(player,event.element) else ranking.rank_print('Button without Function '..button.name,'Mod') end break if button.event then button.event(player,event.element)
end else ranking.rank_print('Button without Function '..button.name,'Mod') end
end end
end end
end) end)
--the magic behind the text inputs --the magic behind the text inputs
Event.register(defines.events.on_gui_text_changed, function(event) Event.register(defines.events.on_gui_text_changed, function(event)
local player = game.players[event.player_index] local player = game.players[event.player_index]
for _,text in pairs(inputs.text) do if inputs.text[event.element.name] then
if text.name == event.element.name then local text = inputs.text[event.element.name]
if text.event then text.event(player,event.element) end break if text.event then text.event(player,event.element) end
end
end end
end) end)
--Please Only Edit Above This Line----------------------------------------------------------- --Please Only Edit Above This Line-----------------------------------------------------------