-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
58 changed files
with
5,239 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
--- Gui class define for buttons and sprite buttons | ||
--[[ | ||
>>>> Functions | ||
Button.new_button(name) --- Creates a new button element define | ||
Button._prototype:on_click(player,element) --- Registers a handler for when the button is clicked | ||
Button._prototype:on_left_click(player,element) --- Registers a handler for when the button is clicked with the left mouse button | ||
Button._prototype:on_right_click(player,element) --- Registers a handler for when the button is clicked with the right mouse button | ||
Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite) --- Adds sprites to a button making it a spirte button | ||
Button._prototype:set_click_filter(filter,...) --- Adds a click / mouse button filter to the button | ||
Button._prototype:set_key_filter(filter,...) --- Adds a control key filter to the button | ||
Other functions present from expcore.gui.core | ||
]] | ||
local mod_gui = require 'mod-gui' | ||
local Gui = require 'expcore.gui.core' | ||
|
||
local Button = { | ||
_prototype=Gui._prototype_factory{ | ||
on_click = Gui._event_factory('on_click'), | ||
on_left_click = Gui._event_factory('on_left_click'), | ||
on_right_click = Gui._event_factory('on_right_click'), | ||
} | ||
} | ||
|
||
--- Creates a new button element define | ||
-- @tparam[opt] name string the optional debug name that can be added | ||
-- @treturn table the new button element define | ||
function Button.new_button(name) | ||
|
||
local self = Gui._define_factory(Button._prototype) | ||
self.draw_data.type = 'button' | ||
self.draw_data.style = mod_gui.button_style | ||
|
||
if name then | ||
self:debug_name(name) | ||
end | ||
|
||
Gui.on_click(self.name,function(event) | ||
local mouse_button = event.button | ||
local keys = {alt=event.alt,control=event.control,shift=event.shift} | ||
event.keys = keys | ||
|
||
if self.post_authenticator then | ||
if not self.post_authenticator(event.player,self.name) then return end | ||
end | ||
|
||
if mouse_button == defines.mouse_button_type.left and self.events.on_left_click then | ||
self.events.on_left_click(event.player,event.element) | ||
elseif mouse_button == defines.mouse_button_type.right and self.events.on_right_click then | ||
self.events.on_right_click(event.player,event.element) | ||
end | ||
|
||
if self.mouse_button_filter and not self.mouse_button_filter[mouse_button] then return end | ||
if self.key_button_filter then | ||
for key,state in pairs(self.key_button_filter) do | ||
if state and not keys[key] then return end | ||
end | ||
end | ||
|
||
if self.events.on_click then | ||
self.events.on_click(event.player,event.element,event) | ||
end | ||
end) | ||
|
||
return self | ||
end | ||
|
||
--- Adds sprites to a button making it a spirte button | ||
-- @tparam sprite SpritePath the sprite path for the default sprite for the button | ||
-- @tparam[opt] hovered_sprite SpritePath the sprite path for the sprite when the player hovers over the button | ||
-- @tparam[opt] clicked_sprite SpritePath the sprite path for the sprite when the player clicks the button | ||
-- @treturn self returns the button define to allow chaining | ||
function Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite) | ||
self.draw_data.type = 'sprite-button' | ||
self.draw_data.sprite = sprite | ||
self.draw_data.hovered_sprite = hovered_sprite | ||
self.draw_data.clicked_sprite = clicked_sprite | ||
return self | ||
end | ||
|
||
--- Adds a click / mouse button filter to the button | ||
-- @tparam filter ?string|table either a table of mouse buttons or the first mouse button to filter, with a table true means allowed | ||
-- @tparam[opt] ... when filter is not a table you can add the mouse buttons one after each other | ||
-- @treturn self returns the button define to allow chaining | ||
function Button._prototype:set_click_filter(filter,...) | ||
if type(filter) == 'string' then | ||
filter = {[filter]=true} | ||
for _,v in pairs({...}) do | ||
filter[v] = true | ||
end | ||
end | ||
|
||
for k,v in pairs(filter) do | ||
if type(v) == 'string' then | ||
filter[k] = defines.mouse_button_type[v] | ||
end | ||
end | ||
|
||
self.mouse_button_filter = filter | ||
return self | ||
end | ||
|
||
--- Adds a control key filter to the button | ||
-- @tparam filter ?string|table either a table of control keys or the first control keys to filter, with a table true means allowed | ||
-- @tparam[opt] ... when filter is not a table you can add the control keyss one after each other | ||
-- @treturn self returns the button define to allow chaining | ||
function Button._prototype:set_key_filter(filter,...) | ||
if type(filter) == 'string' then | ||
filter = {[filter]=true} | ||
for _,v in pairs({...}) do | ||
filter[v] = true | ||
end | ||
end | ||
|
||
self.key_button_filter = filter | ||
return self | ||
end | ||
|
||
return Button |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
--- Gui structure define for center gui frames | ||
--[[ | ||
>>>> Functions | ||
CenterFrames.get_flow(player) --- Gets the center flow for a player | ||
CenterFrames.clear_flow(player) --- Clears the center flow for a player | ||
CenterFrames.draw_frame(player,name) --- Draws the center frame for a player, if already open then will do nothing | ||
CenterFrames.redraw_frame(player,name) --- Draws the center frame for a player, if already open then will destroy it and redraw | ||
CenterFrames.toggle_frame(player,name,state) --- Toggles if the frame is currently open or not, will open if closed and close if open | ||
CenterFrames.new_frame(permision_name) --- Sets the frame to be the current active gui when opened and closes all other frames | ||
CenterFrames._prototype:on_draw(player,frame) --- Use to draw your elements onto the new frame | ||
CenterFrames._prototype:set_auto_focus(state) --- Sets the frame to be the current active gui when opened and closes all other frames | ||
CenterFrames._prototype:draw_frame(player) --- Draws this frame to the player, if already open does nothing (will call on_draw to draw to the frame) | ||
CenterFrames._prototype:redraw_frame(player) --- Draws this frame to the player, if already open it will remove it and redraw it (will call on_draw to draw to the frame) | ||
CenterFrames._prototype:toggle_frame(player) --- Toggles if the frame is open, if open it will close it and if closed it will open it | ||
CenterFrames._prototype:event_handler(action) --- Creates an event handler that will trigger one of its functions, use with Event.add | ||
]] | ||
local Gui = require 'expcore.gui.core' | ||
local Toolbar = require 'expcore.gui.toolbar' | ||
local Game = require 'utils.game' | ||
|
||
local CenterFrames = { | ||
_prototype = Gui._prototype_factory{ | ||
on_draw = Gui._event_factory('on_draw') | ||
} | ||
} | ||
|
||
--- Gets the center flow for a player | ||
-- @tparam player LuapPlayer the player to get the flow for | ||
-- @treturn LuaGuiElement the center flow | ||
function CenterFrames.get_flow(player) | ||
player = Game.get_player_from_any(player) | ||
return player.gui.center | ||
end | ||
|
||
--- Clears the center flow for a player | ||
-- @tparam player LuapPlayer the player to clear the flow for | ||
function CenterFrames.clear_flow(player) | ||
local flow = CenterFrames.get_flow(player) | ||
flow.clear() | ||
end | ||
|
||
--- Draws the center frame for a player, if already open then will do nothing | ||
-- @tparam player LuapPlayer the player that will have the frame drawn | ||
-- @tparam name string the name of the hui that will drawn | ||
-- @treturn LuaGuiElement the new frame that was made | ||
function CenterFrames.draw_frame(player,name) | ||
local define = Gui.get_define(name,true) | ||
if define then | ||
return define:draw_frame(player) | ||
end | ||
end | ||
|
||
--- Draws the center frame for a player, if already open then will destroy it and redraw | ||
-- @tparam player LuapPlayer the player that will have the frame drawn | ||
-- @tparam name string the name of the hui that will drawn | ||
-- @treturn LuaGuiElement the new frame that was made | ||
function CenterFrames.redraw_frame(player,name) | ||
local define = Gui.get_define(name,true) | ||
if define then | ||
return define:draw_frame(player) | ||
end | ||
end | ||
|
||
--- Toggles if the frame is currently open or not, will open if closed and close if open | ||
-- @tparam player LuapPlayer the player that will have the frame toggled | ||
-- @tparam name string the name of the hui that will be toggled | ||
-- @tparam[opt] state boolean when set will force a state for the frame | ||
-- @treturn boolean if the frame if no open or closed | ||
function CenterFrames.toggle_frame(player,name,state) | ||
local define = Gui.get_define(name,true) | ||
if define then | ||
if state == true then | ||
define:draw_frame(player) | ||
return true | ||
elseif state == false then | ||
local flow = CenterFrames.get_flow(player) | ||
if flow[define.name] then | ||
flow[define.name].destroy() | ||
end | ||
return false | ||
else | ||
return define:toggle_frame(player) | ||
end | ||
end | ||
end | ||
|
||
--- Creates a new center frame define | ||
-- @tparam permision_name string the name that can be used with the permision system | ||
-- @treturn table the new center frame define | ||
function CenterFrames.new_frame(permision_name) | ||
local self = Toolbar.new_button(permision_name) | ||
|
||
self:on_click(function(player,element) | ||
self:toggle_frame(player) | ||
end) | ||
|
||
local mt = getmetatable(self) | ||
mt.__index = CenterFrames._prototype | ||
mt.__call = self.event_handler | ||
|
||
Gui.on_custom_close(self.name,function(event) | ||
local element = event.element | ||
if element and element.valid then element.destroy() end | ||
end) | ||
|
||
return self | ||
end | ||
|
||
--- Sets the frame to be the current active gui when opened and closes all other frames | ||
-- @tparam[opt=true] state boolean when true will auto close other frames and set this frame as player.opened | ||
function CenterFrames._prototype:set_auto_focus(state) | ||
if state == false then | ||
self.auto_focus = false | ||
else | ||
self.auto_focus = true | ||
end | ||
end | ||
|
||
--- Draws this frame to the player, if already open does nothing (will call on_draw to draw to the frame) | ||
-- @tparam player LuaPlayer the player to draw the frame for | ||
-- @treturn LuaGuiElement the new frame that was drawn | ||
function CenterFrames._prototype:draw_frame(player) | ||
player = Game.get_player_from_any(player) | ||
local flow = CenterFrames.get_flow(player) | ||
|
||
if flow[self.name] then | ||
return flow[self.name] | ||
end | ||
|
||
if self.auto_focus then | ||
flow.clear() | ||
end | ||
|
||
local frame = flow.add{ | ||
type='frame', | ||
name=self.name | ||
} | ||
|
||
if self.auto_focus then | ||
player.opened = frame | ||
end | ||
|
||
if self.events.on_draw then | ||
self.events.on_draw(player,frame) | ||
end | ||
|
||
return frame | ||
end | ||
|
||
--- Draws this frame to the player, if already open it will remove it and redraw it (will call on_draw to draw to the frame) | ||
-- @tparam player LuaPlayer the player to draw the frame for | ||
-- @treturn LuaGuiElement the new frame that was drawn | ||
function CenterFrames._prototype:redraw_frame(player) | ||
player = Game.get_player_from_any(player) | ||
local flow = CenterFrames.get_flow(player) | ||
|
||
if flow[self.name] then | ||
flow[self.name].destroy() | ||
end | ||
|
||
return self:draw_frame(player) | ||
end | ||
|
||
--- Toggles if the frame is open, if open it will close it and if closed it will open it | ||
-- @tparam player LuaPlayer the player to draw the frame for | ||
-- @treturn boolean with the gui frame is now open | ||
function CenterFrames._prototype:toggle_frame(player) | ||
player = Game.get_player_from_any(player) | ||
local flow = CenterFrames.get_flow(player) | ||
|
||
if flow[self.name] then | ||
flow[self.name].destroy() | ||
return false | ||
else | ||
self:draw_frame(player) | ||
return true | ||
end | ||
end | ||
|
||
--- Creates an event handler that will trigger one of its functions, use with Event.add | ||
-- @tparam[opt=update] action string the action to take on this event | ||
function CenterFrames._prototype:event_handler(action) | ||
action = action or 'update' | ||
return function(event) | ||
local player = Game.get_player_by_index(event.player_index) | ||
self[action](self,player) | ||
end | ||
end | ||
|
||
return CenterFrames |
Oops, something went wrong.