From ad4b870b59a27b5de0641b460d32eb27a7e58aa7 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sun, 22 Sep 2019 19:21:21 +0100 Subject: [PATCH] Added some elements to expstyle --- expcore/gui/concepts/label.lua | 2 +- expcore/gui/core.lua | 2 +- expcore/gui/prototype.lua | 9 +- expcore/gui/styles/expstyle/alignment.lua | 47 +++++++++++ expcore/gui/styles/expstyle/container.lua | 36 ++++++++ expcore/gui/styles/expstyle/header.lua | 49 +++++++++++ expcore/gui/styles/expstyle/index.lua | 13 +++ expcore/gui/styles/expstyle/scroll_table.lua | 56 +++++++++++++ expcore/gui/styles/expstyle/time_label.lua | 86 ++++++++++++++++++++ 9 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 expcore/gui/styles/expstyle/alignment.lua create mode 100644 expcore/gui/styles/expstyle/container.lua create mode 100644 expcore/gui/styles/expstyle/header.lua create mode 100644 expcore/gui/styles/expstyle/index.lua create mode 100644 expcore/gui/styles/expstyle/scroll_table.lua create mode 100644 expcore/gui/styles/expstyle/time_label.lua diff --git a/expcore/gui/concepts/label.lua b/expcore/gui/concepts/label.lua index 1cf70eac..9c2df448 100644 --- a/expcore/gui/concepts/label.lua +++ b/expcore/gui/concepts/label.lua @@ -6,7 +6,7 @@ local Gui = require 'expcore.gui.core' --[[-- A piece of text. -@element frame +@element label @tparam ?string|Concepts.LocalisedString caption the caption that will show in the label @tparam ?string|Concepts.LocalisedString description the description that will show on the label diff --git a/expcore/gui/core.lua b/expcore/gui/core.lua index 1edcd0b3..5365d54a 100644 --- a/expcore/gui/core.lua +++ b/expcore/gui/core.lua @@ -30,7 +30,7 @@ end Gui.require_concept('expgaming') --- @dep Gui.style.frame ]] function Gui.require_style(style_name) - require('expcore.gui.styles.'..style_name) + require('expcore.gui.styles.'..style_name..'.index') end --[[-- Gets a gui concept from name, id, or its self diff --git a/expcore/gui/prototype.lua b/expcore/gui/prototype.lua index 902aa4dc..269b4023 100644 --- a/expcore/gui/prototype.lua +++ b/expcore/gui/prototype.lua @@ -417,6 +417,7 @@ end --[[-- Calls all the draw functions in order to create this concept in game; will also store and sync the instance if stores are used @tparam LuaGuiElement parent_element the element that the concept will use as a base +@tparam[opt] string override_name when given this will be the name of the element rather than the concept id @treturn LuaGuiElement the element that was created and then passed though and returned by the draw functions @usage-- Drawing the custom button concept local custom_button = @@ -425,10 +426,12 @@ Gui.get_concept('CustomButton') -- Note that the draw function from button was cloned, so unless we want to alter the base button we dont need a new draw define custom_button:draw(game.player.gui.left) ]] -function Prototype:draw(parent_element,...) +function Prototype:draw(parent_element,override_name,...) + local old_name = self.properties.name local parent = parent_element local element + if override_name then self.properties.name = override_name end -- Loop over all the draw defines, element is updated when a value is returned for _,draw_callback in pairs(self.draw_callbacks) do local success, _element, _parent = pcall(draw_callback,self.properties,parent,element,...) @@ -436,10 +439,14 @@ function Prototype:draw(parent_element,...) if _element then element = _element end if _parent then parent = _parent end elseif not success then + self.properties.name = old_name error('Gui draw handler error with '..self.debug_name..':\n\t'.._element) end end + -- Return the name back to its previous value + self.properties.name = old_name + -- Adds the instance if instance store is used if self.add_instance then self.add_instance(element) diff --git a/expcore/gui/styles/expstyle/alignment.lua b/expcore/gui/styles/expstyle/alignment.lua new file mode 100644 index 00000000..a00c325f --- /dev/null +++ b/expcore/gui/styles/expstyle/alignment.lua @@ -0,0 +1,47 @@ +--[[-- Core Module - ExpStyle + @module ExpStyle +]] + +local Gui = require 'expcore.gui' --- @dep expcore.gui + +Gui.require_concept 'flow' --- @dep gui.concept.flow + +--[[-- A flow which can be used to align text and other elements +@element alignment +@usage-- Concept Structure +-- Root +--> [alignment] - the alignment area +Gui.new_concept('alignment') +:set_horizontal_align('center') +]] + +Gui.new_concept('flow') +:save_as('alignment') + +:new_property('horizontal_align',nil,'right') +:new_property('vertical_align',nil,'center') +:new_property('width') +:new_property('height') + +:define_draw(function(properties,parent,element) + local style = element.style + Gui.set_padding(element,1,1,2,2) + + -- Set the alignment of the flow + style.horizontal_align = properties.horizontal_align + style.vertical_align = properties.vertical_align + + -- Set the stretchablity based on the alignment + style.horizontally_stretchable = style.horizontal_align ~= 'center' + style.vertically_stretchable = style.vertical_align ~= 'center' + + -- Set the width if given + local width = properties.width + if width then style.width = width end + + -- Set the hieght if given + local height = properties.height + if height then style.height = height end + + return element +end) \ No newline at end of file diff --git a/expcore/gui/styles/expstyle/container.lua b/expcore/gui/styles/expstyle/container.lua new file mode 100644 index 00000000..c475c95d --- /dev/null +++ b/expcore/gui/styles/expstyle/container.lua @@ -0,0 +1,36 @@ +--[[-- Core Module - ExpStyle + @module ExpStyle +]] + +local Gui = require 'expcore.gui' --- @dep expcore.gui + +Gui.require_concept 'frame' --- @dep gui.concept.frame + +--[[-- A container frame that can be used to add a boader around your content +@element container +@usage-- Concept Structure +-- Root +--> [container] - the outer frame +-->> container - the content area +Gui.new_concept('container') +]] + +Gui.new_concept('frame') +:save_as('container') +:define_draw(function(properties,parent,element) + -- Update the outter frame padding + element.style.padding = 2 + + -- Add the inner frame + element = element.add{ + name = 'container', + type = 'frame', + direction = properties.direction, + style = 'window_content_frame_packed' + } + + -- Update the inner frame padding + element.style.padding = 0 + + return element +end) \ No newline at end of file diff --git a/expcore/gui/styles/expstyle/header.lua b/expcore/gui/styles/expstyle/header.lua new file mode 100644 index 00000000..c5699fb5 --- /dev/null +++ b/expcore/gui/styles/expstyle/header.lua @@ -0,0 +1,49 @@ +--[[-- Core Module - ExpStyle + @module ExpStyle +]] + +local Gui = require 'expcore.gui' --- @dep expcore.gui + +Gui.require_concept 'frame' --- @dep gui.concept.table + +local right_align = +Gui.new_concept('alignment') + +--[[-- A frame that acts as a header to a section of content +@element header +@tparam string tooltip the tooltip to show on the title +@usage-- Concept Structure +-- Root +--> [header] - the header frame +-->> header_caption - the lable with the title in it +-->> header_content - the area to contain butons +]] + +Gui.new_concept('frame') +:save_as('header') +:new_property('tooltip') + +-- Draw +:define_draw(function(properties,parent,element) + -- Update the table style + Gui.set_padding(element,2,2,4,4) + element.style = 'subheader_frame' + element.caption = nil + + local style = element.style + style.horizontally_stretchable = true + style.use_header_filler = false + + -- Add the caption to the frame + element.add{ + type = 'label', + name = 'header_caption', + caption = properties.title, + tooltip = properties.tooltip + } + + -- Add the right align area + local align = right_align:draw(element,'header_content') + + return align +end) \ No newline at end of file diff --git a/expcore/gui/styles/expstyle/index.lua b/expcore/gui/styles/expstyle/index.lua new file mode 100644 index 00000000..aa8afa39 --- /dev/null +++ b/expcore/gui/styles/expstyle/index.lua @@ -0,0 +1,13 @@ +--[[-- Core Module - ExpStyle + @core ExpStyle +]] + +local function r(name) + require('expcore.gui.styles.expstyle.'..name) +end + +r 'container' +r 'alignment' +r 'header' +r 'scroll_table' +r 'time_label' \ No newline at end of file diff --git a/expcore/gui/styles/expstyle/scroll_table.lua b/expcore/gui/styles/expstyle/scroll_table.lua new file mode 100644 index 00000000..0c1473d5 --- /dev/null +++ b/expcore/gui/styles/expstyle/scroll_table.lua @@ -0,0 +1,56 @@ +--[[-- Core Module - ExpStyle + @module ExpStyle +]] + +local Gui = require 'expcore.gui' --- @dep expcore.gui + +Gui.require_concept 'table' --- @dep gui.concept.table +Gui.require_concept 'scroll' --- @dep gui.concept.scroll + +local scroll_area = +Gui.new_concept('scroll') +:set_vertical_scroll('auto-and-reserve-space') +:set_horizontal_scroll('never') + +--[[-- A table that is inside a vertical scroll area +@element scroll_table +@tparam number hight the max hight of the scroll area +@usage-- Concept Structure +-- Root +--> [scroll_table] - the scroll area +-->> table - the table area +]] + +Gui.new_concept('table') +:save_as('scroll_table') +:new_property('hight',nil,100) + +-- Add a scroll before the table is drawn +:define_pre_draw(function(properties,parent,element) + local scroll = scroll_area:draw(parent,properties.name) + + -- Set the scroll style + Gui.set_padding(scroll,1,1,2,2) + scroll.style.horizontally_stretchable = true + scroll.style.maximal_height = properties.hight + + -- Change the name of the element to table before it is drawn + properties.name = 'table' + + return element, scroll +end) + +-- Draw +:define_draw(function(properties,parent,element) + -- Update the table style + local style = element.style + style.padding = 0 + style.horizontally_stretchable = true + style.vertical_align = 'center' + style.cell_padding = 0 + + -- Change the stored name back to the actual name + properties.name = element.parent.name + + return element +end) \ No newline at end of file diff --git a/expcore/gui/styles/expstyle/time_label.lua b/expcore/gui/styles/expstyle/time_label.lua new file mode 100644 index 00000000..efa7fd1f --- /dev/null +++ b/expcore/gui/styles/expstyle/time_label.lua @@ -0,0 +1,86 @@ +--[[-- Core Module - ExpStyle + @module ExpStyle +]] + +local Gui = require 'expcore.gui' --- @dep expcore.gui +local format_time = ext_require('expcore.common','format_time') --- @dep expcore.common + +--- Converts a tick into string format with workds and symbols +local function get_format(properties,time) + local caption, tooltip + + -- Check if a custom format is wanted + if properties.time_format then + -- Get the caption + local format = table.deep_copy(properties.time_format) + caption = format_time(time,format) + + -- Get the tooltip, always long format + format.long = true + tooltip = format_time(time,format) + + else + -- Get the caption + caption = format_time(time,{ + hours = properties.use_hours, + minutes = true, + seconds = true + }) + + -- Get the tooltip, same as the caption but long format + tooltip = format_time(time,{ + hours = properties.use_hours, + minutes = true, + seconds = true, + long = true + }) + + end + + return caption, tooltip +end + +--[[-- A label that show time in a nice, user friendly way +@element time_label +@tparam number time the time to display in tick +@usage-- Concept Structure +-- Root +--> [time_label] - the label with the time +]] + +local time_label = +Gui.new_concept() +:save_as('time_label') + +-- Properties +:new_property('time') +:new_property('use_hours',nil,false) +:new_property('time_format') + +-- Draw +:define_draw(function(properties,parent,element,time) + -- Get the caption and tooltip + local caption, tooltip = get_format(properties,time or properties.time) + + -- Draw a label + element = parent.add{ + name = properties.name, + type = 'label', + caption = caption, + tooltip = tooltip + } + + return element +end) + +--[[-- Updates the time that is on a label +@tparam LuaGuiElement element the label that you want to update +@tparam number time the number of tick you want it to show +@usage-- Update the time to show game time +time_label:update_time(element,game.time) +]] +function time_label:update_time(element,time) + local caption, tooltip = get_format(self.properties,time) + element.caption = caption + element.tooltip = tooltip +end \ No newline at end of file