Skip to content

Commit

Permalink
Forms: extract common form elements from AboutTab into forms.spec
Browse files Browse the repository at this point in the history
… api. Relates to #1708
  • Loading branch information
alek13 committed Oct 7, 2024
1 parent f585c4f commit 8f60ad3
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 79 deletions.
26 changes: 1 addition & 25 deletions mods/lord/Game/lord_forms/init.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@


minetest.mod(function(mod)
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
--})

minetest.register_on_joinplayer(function(player)
player:set_formspec_prepend(formspec_prepend)
end)

require('forms').init()
end)
20 changes: 20 additions & 0 deletions mods/lord/Game/lord_forms/src/forms.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local FormsPrepend = require('forms.prepend')
local FormsSpec = require('forms.spec')


forms = {}

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


return {
--- @param mod minetest.Mod
init = function(mod)
register_api()
FormsPrepend.register()
end
}
10 changes: 10 additions & 0 deletions mods/lord/Game/lord_forms/src/forms/DefaultStyle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


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



return DefaultStyle
30 changes: 30 additions & 0 deletions mods/lord/Game/lord_forms/src/forms/prepend.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
--})


return {
register = function()
minetest.register_on_joinplayer(function(player)
player:set_formspec_prepend(formspec_prepend)
end)
end
}
213 changes: 213 additions & 0 deletions mods/lord/Game/lord_forms/src/forms/spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
local table_merge
= table.merge

local DefaultStyle = require('forms.DefaultStyle')
local spec = minetest.formspec


local BOLD = { font = 'bold' }
local ITALIC = { font = 'italic' }
local MUTED = { textcolor = '#ccc' }
local SMALL = { font_size = '-1' }

local HEADER1_SIZE = '+7'
local HEADER2_SIZE = '+5'
local HEADER3_SIZE = '+3'


--- @class forms.Spec
local Spec = {}

Spec.label = spec.label

--- @see forms.DefaultStyle
--- @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])
end

--- @param x number
--- @param y number
--- @param text string
--- @param style table additional style to merge with `{ font = 'bold' }`
--- @return string
function Spec.text(x, y, text, style)
return ''
.. spec.style_type('label', style)
.. spec.label(x, y, text)
.. Spec.reset_style('label')
end

--- @param x number
--- @param y number
--- @param text string
--- @param style table 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)
style = table_merge(BOLD, style or {})

return Spec.text(x, y, text, style)
end

--- @param x number
--- @param y number
--- @param text string
--- @param style table 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)
style = table_merge(ITALIC, style or {})

return Spec.text(x, y, text, style)
end

--- @param x number
--- @param y number
--- @param text string
--- @param style table 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)
style = table_merge(MUTED, style or {})

return Spec.text(x, y, text, style)
end

--- @param x number
--- @param y number
--- @param text string
--- @param style table
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.small(x, y, text, style)
return Spec.text(x, y, text, table_merge(SMALL, style or {}))
end

--- @param x number
--- @param y number
--- @param text string
--- @param color string
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.muted_bold(x, y, text, color)
color = color and { textcolor = color } or {}

return Spec.muted(x, y, text, table_merge(BOLD, color))
end

--- @param x number
--- @param y number
--- @param text string
--- @param color string
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.muted_italic(x, y, text, color)
color = color and { textcolor = color } or {}

return Spec.muted(x, y, text, table_merge(ITALIC, color))
end

--- @param x number
--- @param y number
--- @param text string
--- @param color string
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.small_bold(x, y, text, color)
color = color and { textcolor = color } or {}

return Spec.small(x, y, text, table_merge(BOLD, color))
end

--- @param x number
--- @param y number
--- @param text string
--- @param color string
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.small_italic(x, y, text, color)
color = color and { textcolor = color } or {}

return Spec.small(x, y, text, table_merge(ITALIC, color))
end

--- @param x number
--- @param y number
--- @param text string
--- @param font_size string see minetest.FormSpec.Style.font_size . Default: `HEADER1_SIZE` (`"+7"`)
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.title(x, y, text, font_size)
font_size = font_size or HEADER1_SIZE

return Spec.bold(x, y, text, { font_size = font_size })
end

--- @param x number
--- @param y number
--- @param text string
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.header1(x, y, text)
return Spec.title(x, y, text, HEADER1_SIZE)
end

--- @param x number
--- @param y number
--- @param text string
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.header2(x, y, text)
return Spec.title(x, y, text, HEADER2_SIZE)
end

--- @param x number
--- @param y number
--- @param text string
--- @overload fun(x:number,y:number,text:string):string
--- @return string
function Spec.header3(x, y, text)
return Spec.title(x, y, text, HEADER3_SIZE)
end

--- @param x number
--- @param y number
--- @param width number
--- @param height number
--- @param name string
--- @param title string
--- @param url string
--- @param exit boolean
--- @return string
function Spec.button(x, y, width, height, name, title, url, exit)
return '' ..
(url
and (exit
and spec.button_url_exit(x, y, width, height, name, title, url)
or spec.button_url (x, y, width, height, name, title, url)
)
or (exit
and spec.button_exit(x, y, width, height, name, title)
or spec.button (x, y, width, height, name, title)
)
)
end

--- @param x number
--- @param y number
--- @param name string
--- @param title string
--- @param url string
--- @param exit boolean
--- @return string
function Spec.icon_button(x, y, name, icon, title, url, exit)
return ''
.. spec.style(name, { padding= '20,0,0,0' })
.. Spec.button(x, y, 2, 1, name, title, url)
.. spec.image(x+.2, y+.24, 0.4, 0.4, icon)
end


return Spec
2 changes: 1 addition & 1 deletion mods/lord/Player/lord_inventory/mod.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name = lord_inventory
depends = builtin, default, lord_classes, lord_equipment
depends = builtin, default, lord_forms, lord_classes, lord_equipment
Loading

0 comments on commit 8f60ad3

Please sign in to comment.