Skip to content

Commit

Permalink
Forms: General Styles: ability to configure default styles. Closes #1708
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 committed Oct 7, 2024
1 parent 8f60ad3 commit cdf7cc4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 31 deletions.
6 changes: 4 additions & 2 deletions mods/lord/Game/lord_forms/src/forms.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
local DefaultStyle = require('forms.DefaultStyle')
local FormsPrepend = require('forms.prepend')
local FormsSpec = require('forms.spec')
local FormsSpec = require('forms.Spec')


forms = {}

local function register_api()
_G.forms = {
spec = FormsSpec
DefaultStyle = DefaultStyle,
Spec = FormsSpec,
}
end

Expand Down
24 changes: 21 additions & 3 deletions mods/lord/Game/lord_forms/src/forms/DefaultStyle.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
local config = require('forms.DefaultStyle.config')


--- @class forms.DefaultStyle
local DefaultStyle = {
label = { font = 'normal', font_size = '+0', textcolor = '#fff', --[[noclip = 'false'--]] },
}
local DefaultStyle = {}

--- @param name string
--- @return minetest.FormSpec.Style
function DefaultStyle.get(name)
return config[name]
end

--- @param name string
--- @param style minetest.FormSpec.Style
--- @return forms.DefaultStyle
function DefaultStyle.set(name, style)
config[name] = style

return DefaultStyle
end

---@return fun(tbl: table<string, minetest.FormSpec.Style>):string, minetest.FormSpec.Style
function DefaultStyle.list()
return pairs(config)
end


return DefaultStyle
31 changes: 31 additions & 0 deletions mods/lord/Game/lord_forms/src/forms/DefaultStyle/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


--- @alias forms.DefaultStyle.config minetest.FormSpec.Style[]|table<string,minetest.FormSpec.Style>

--- @type forms.DefaultStyle.config
local config = {
--- @type minetest.FormSpec.Style
label = { font = 'normal', font_size = '+0', textcolor = '#fff', --[[noclip = 'false'--]] },
button = {
bgimg = 'button_bg.png',
bgimg_middle = 4,
font = 'bold',
textcolor = '#fffc',
padding = '0',
},
--- @type minetest.FormSpec.Style
some = {

}

-- as for now, this styles is not supported by MT
--.. spec.style_type({'field', 'field:focused', 'field:hovered', 'field:pressed'}, {
-- bgcolor = '#600',
-- bgcolor_hovered = '#000',
-- bgcolor_pressed = '#000',
-- padding = 0
--})
}


return config
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ Spec.label = spec.label
--- @param element_name string name of element type (`label`, ...). Valid: one of `forms.DefaultStyle.<name>`
--- @return string
function Spec.reset_style(element_name)
return spec.style_type(element_name, DefaultStyle[element_name])
return spec.style_type(element_name, DefaultStyle.get(element_name))
end

--- @param x number
--- @param y number
--- @param text string
--- @param style table additional style to merge with `{ font = 'bold' }`
--- @param style minetest.FormSpec.Style additional style to merge with `{ font = 'bold' }`
--- @return string
function Spec.text(x, y, text, style)
return ''
Expand All @@ -42,7 +42,7 @@ end
--- @param x number
--- @param y number
--- @param text string
--- @param style table additional style to merge with `{ font = 'bold' }`
--- @param style minetest.FormSpec.Style additional style to merge with `{ font = 'bold' }`
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.bold(x, y, text, style)
Expand All @@ -54,7 +54,7 @@ end
--- @param x number
--- @param y number
--- @param text string
--- @param style table additional style to merge with `{ font = 'italic' }`
--- @param style minetest.FormSpec.Style additional style to merge with `{ font = 'italic' }`
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.italic(x, y, text, style)
Expand All @@ -66,7 +66,7 @@ end
--- @param x number
--- @param y number
--- @param text string
--- @param style table additional style to merge with `{ textcolor = '#ccc' }`
--- @param style minetest.FormSpec.Style additional style to merge with `{ textcolor = '#ccc' }`
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.muted(x, y, text, style)
Expand All @@ -78,7 +78,7 @@ end
--- @param x number
--- @param y number
--- @param text string
--- @param style table
--- @param style minetest.FormSpec.Style
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.small(x, y, text, style)
Expand Down
37 changes: 18 additions & 19 deletions mods/lord/Game/lord_forms/src/forms/prepend.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
local spec = minetest.formspec
local DefaultStyle = require('forms.DefaultStyle')
local spec = minetest.formspec


local formspec_prepend = ''
.. spec.bgcolor('#000c', 'true')
.. spec.background9(5, 5, 1, 1, 'gui_formbg.png', 'true', 10)
.. spec.listcolors('#0007', '#5a5a5a', '#141318', '#1238', '#fffc')
.. spec.style_type('button', {
bgimg = 'button_bg.png',
bgimg_middle = 4,
font = 'bold',
textcolor = '#fffc',
padding = '0',
})
-- as for now, this styles is not supported by MT
--.. spec.style_type({'field', 'field:focused', 'field:hovered', 'field:pressed'}, {
-- bgcolor = '#600',
-- bgcolor_hovered = '#000',
-- bgcolor_pressed = '#000',
-- padding = 0
--})


local function build_formspec_prepend()
formspec_prepend = ''
.. spec.bgcolor('#000c', 'true')
.. spec.background9(5, 5, 1, 1, 'gui_formbg.png', 'true', 10)
.. spec.listcolors('#0007', '#5a5a5a', '#141318', '#1238', '#fffc')

for name, style in DefaultStyle.list() do
formspec_prepend = formspec_prepend
.. spec.style_type('button', DefaultStyle.get('button'))
end
end


return {
register = function()
minetest.register_on_mods_loaded(build_formspec_prepend)

minetest.register_on_joinplayer(function(player)
player:set_formspec_prepend(formspec_prepend)
end)
end
end,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local S = minetest.get_translator('lord_inventory')
local spec = minetest.formspec
local lord_spec = forms.spec
local lord_spec = forms.Spec


-- TODO #1709
Expand Down

0 comments on commit cdf7cc4

Please sign in to comment.