Skip to content

Commit

Permalink
Merge pull request #4 from daretmavi/devtest
Browse files Browse the repository at this point in the history
Add skindb tab option
  • Loading branch information
daretmavi authored Jul 7, 2021
2 parents 9dadeaf + e767aa3 commit 4e39096
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 5 deletions.
251 changes: 251 additions & 0 deletions i3_mods_tabs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
-- ****************************************************************************
-- Funnctions and Variables from i3 init.lua
local modpath = core.get_modpath "i3"

local maxn, sort, concat, copy, insert, remove, indexof =
table.maxn, table.sort, table.concat, table.copy,
table.insert, table.remove, table.indexof

local sprintf, find, gmatch, match, sub, split, upper, lower =
string.format, string.find, string.gmatch, string.match,
string.sub, string.split, string.upper, string.lower

local PNG, styles, fs_elements = loadfile(modpath .. "/etc/styles.lua")()

local ESC = core.formspec_escape
local S = core.get_translator "i3"

local ES = function(...)
return ESC(S(...))
end

local function fmt(elem, ...)
if not fs_elements[elem] then
return sprintf(elem, ...)
end

return sprintf(fs_elements[elem], ...)
end

local function add_subtitle(fs, name, y, ctn_len, font_size, sep, label)
fs(fmt("style[%s;font=bold;font_size=%u]", name, font_size))
fs("button", 0, y, ctn_len, 0.5, name, ESC(label))

if sep then
fs("image", 0, y + 0.55, ctn_len, 0.035, PNG.bar)
end
end

-- ****************************************************************************
-- Function to get detail info aout texture
-- Forked from skinsdb mod
local function get_skin_info_formspec(skin, xoffset, yoffset)
local texture = skin:get_texture()
local m_name = skin:get_meta_string("name")
local m_author = skin:get_meta_string("author")
local m_license = skin:get_meta_string("license")
local m_format = skin:get_meta("format")
-- overview page
local raw_size = m_format == "1.8" and "2,2" or "2,1"

--local lxoffs = 0.8 + xoffset
local cxoffs = 2 + xoffset
local rxoffs = 2 + xoffset

local formspec = "" -- = "image["..lxoffs..","..0.6 + yoffset..";1,2;"..minetest.formspec_escape(skin:get_preview()).."]"
if texture then
formspec = formspec.."label["..rxoffs..","..2 + yoffset..";"..S("Raw texture")..":]"
.."image["..1 + rxoffs..","..2.5 + yoffset..";"..raw_size..";"..texture.."]"
end
if m_name ~= "" then
formspec = formspec.."label["..cxoffs..","..0.5 + yoffset..";"..S("Name")..": "..minetest.formspec_escape(m_name).."]"
end
if m_author ~= "" then
formspec = formspec.."label["..cxoffs..","..1 + yoffset..";"..S("Author")..": "..minetest.formspec_escape(m_author).."]"
end
if m_license ~= "" then
formspec = formspec.."label["..cxoffs..","..1.5 + yoffset..";"..S("License")..": "..minetest.formspec_escape(m_license).."]"
end
return formspec
end

-- skin images and pages
local function get_skin_selection_formspec(player, context)
context.skins_list = skins.get_skinlist_for_player(player:get_player_name())
context.total_pages = 1
local xoffs = 0.8
local yoffs = 6.5
local xspc = 1.1
local yspc = 2.1
local skinwidth = 1
local skinheight = 2
local xscale = 1
local btn_y = yoffs + 4.5
local drop_y = yoffs + 4.5
local btn_width = 0.35
local btn_heigh = 0.35
local droppos = xoffs + 1.3
local droplen = 6.2
local btn_left = droppos - btn_width
local btn_right = droppos + droplen
local maxdisp = 16

local ctrls_height = 0.4

for i, skin in ipairs(context.skins_list ) do
local page = math.floor((i-1) / maxdisp)+1
skin:set_meta("inv_page", page)
skin:set_meta("inv_page_index", (i-1)%maxdisp+1)
context.total_pages = page
end
context.skins_page = context.skins_page or skins.get_player_skin(player):get_meta("inv_page") or 1
context.dropdown_values = nil

local page = context.skins_page
local formspec = ""

for i = (page-1)*maxdisp+1, page*maxdisp do
local skin = context.skins_list[i]
if not skin then
break
end

local index_p = skin:get_meta("inv_page_index")
local x = ((index_p-1) % 8) * xspc + xoffs
local y
if index_p > 8 then
y = yoffs + yspc
else
y = yoffs
end
formspec = formspec..
string.format("image_button[%f,%f;%f,%f;%s;skins_set$%i;]",
x, y, skinwidth, skinheight,
minetest.formspec_escape(skin:get_preview()), i)..
"tooltip[skins_set$"..i..";"..minetest.formspec_escape(skin:get_meta_string("name")).."]"
end

if context.total_pages > 1 then
local page_prev = page - 1
local page_next = page + 1
if page_prev < 1 then
page_prev = context.total_pages
end
if page_next > context.total_pages then
page_next = 1
end
local page_list = ""
context.dropdown_values = {}
for pg=1, context.total_pages do
local pagename = S("Page").." "..pg.."/"..context.total_pages
context.dropdown_values[pagename] = pg
if pg > 1 then page_list = page_list.."," end
page_list = page_list..pagename
end
formspec = formspec..
string.format("image_button[%f,%f;%f,%f;%s;skins_page$%i;]",
btn_left, btn_y, btn_width, btn_heigh, PNG.prev, page_prev)..
string.format("image_button[%f,%f;%f,%f;%s;skins_page$%i;]",
btn_right, btn_y, btn_width, btn_heigh, PNG.next, page_next)..
string.format("dropdown[%f,%f;%f,%f;skins_selpg;%s;%i]",
droppos, drop_y, droplen, ctrls_height, page_list, page)
end
return formspec
end

-- ****************************************************************************
-- i3 Tab definition

i3.new_tab {
name = "Skins",
description = "Skins",
image = "i3_skin.png",

formspec = function(player, data, fs)
--fs("label[3,1;Test 1]")
local name = player:get_player_name()

local ctn_len, ctn_hgt = 5.7, 6.3

local yextra = 1
local yoffset = 0
local xpos = 5

local _skins = skins.get_skinlist_for_player(name)
local skin_name = skins.get_player_skin(player).name
local sks, id = {}, 1

local props = player:get_properties()

for i, skin in ipairs(_skins) do
if skin.name == skin_name then
id = i
end

sks[#sks + 1] = skin.name
end

sks = concat(sks, ","):gsub(";", "")

add_subtitle(fs, "player_name", 0, ctn_len + 4.5, 22, true, ESC(name))

if props.mesh ~= "" then
local anim = player:get_local_animation()
local armor_skin = __3darmor or __skinsdb
local t = {}

for _, v in ipairs(props.textures) do
t[#t + 1] = ESC(v):gsub(",", "!")
end

local textures = concat(t, ","):gsub("!", ",")
local skinxoffset = 1.3

--fs("style[player_model;bgcolor=black]")
fs("model", skinxoffset, 0.2, armor_skin and 4 or 3.4, ctn_hgt,
"player_model", props.mesh, textures, "0,-150", "false", "false",
fmt("%u,%u%s", anim.x, anim.y, data.fs_version >= 5 and ";30" or ""))
else
local size = 2.5
fs("image", 0.7, 0.2, size, size * props.visual_size.y, props.textures[1])
end

fs("label", xpos, yextra, fmt("%s:", ES"Select a skin"))
fs(fmt("dropdown[%f,%f;4,0.6;skins;%s;%u;true]", xpos, yextra + 0.2, sks, id))

local skin = skins.get_player_skin(player)
local formspec = get_skin_info_formspec(skin, 3, 2)
fs(formspec)
--core.log("fs skins: "..dump(formspec))

local context = skins.get_formspec_context(player)
local formspec = get_skin_selection_formspec(player, context)
--core.log("skins context: "..dump(context))
--core.log("fs skins: "..dump(formspec))
fs(formspec)
end,

-- click button handlers - giving fields
fields = function(player, data, fields)
local name = player:get_player_name()
local sb_inv = fields.scrbar_inv

--core.log("fields: "..dump(fields))

-- set skin with dropdown from original i3 inventory
if fields.skins then
local id = tonumber(fields.skins)
local _skins = skins.get_skinlist_for_player(name)
skins.set_player_skin(player, _skins[id])
end

-- set skin with skinddb image an page change
local context = skins.get_formspec_context(player)
local action = skins.on_skin_selection_receive_fields(player, context, fields)

-- update formspec
return i3.set_fs(player)
end,
}

-- ****************************************************************************
21 changes: 17 additions & 4 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ local PNG, styles, fs_elements = loadfile(modpath .. "/etc/styles.lua")()
local compress_groups, compressed = loadfile(modpath .. "/etc/compress.lua")()
local group_stereotypes, group_names = loadfile(modpath .. "/etc/groups.lua")()

local progressive_mode = core.settings:get_bool("i3_progressive_mode", false)
local progressive_mode = core.settings:get_bool("i3_progressive_mode", true)
local item_compression = core.settings:get_bool("i3_item_compression", true)
local damage_enabled = core.settings:get_bool "enable_damage"

-- new settings
local creative_trash_only = core.settings:get_bool("i3_no_trash_in_survival", true)
local use_skinsdb_tab = core.settings:get_bool("i3_skinsdb_tab", true)

-- ************

local __3darmor, __skinsdb, __awards

local http = core.request_http_api()
Expand Down Expand Up @@ -2009,7 +2015,6 @@ local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, a
-- fmt("list[detached:i3_trash;main;%f,%f;1,1;]", 4.45, yoffset + 3.75))
--fs("image", 4.45, yoffset + 3.75, 1, 1, PNG.trash)

local creative_trash_only = core.settings:get_bool("i3_no_trash_in_survival", true)
if core.is_creative_enabled(name) or not creative_trash_only then
fs(fmt("list[detached:i3_trash;main;%f,%f;1,1;]", 4.45, yoffset + 3.75))
fs("image", 4.45, yoffset + 3.75, 1, 1, PNG.trash)
Expand All @@ -2018,6 +2023,8 @@ local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, a
local yextra = damage_enabled and 5.5 or 5

for i, title in ipairs(SUBCAT) do
if title ~= "skins" or not use_skinsdb_tab then

local btn_name = fmt("btn_%s", title)

fs(fmt("style[btn_%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]", title,
Expand All @@ -2026,6 +2033,8 @@ local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, a

fs("image_button", 0.25 + ((i - 1) * 1.18), yextra - 0.2, 0.5, 0.5, "", btn_name, "")
fs(fmt("tooltip[%s;%s]", btn_name, title:gsub("^%l", upper)))

end
end

fs("box", 0, yextra + 0.45, ctn_len, 0.045, "#bababa50")
Expand Down Expand Up @@ -2057,7 +2066,7 @@ local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, a
end

elseif data.subcat == 3 then
if __skinsdb then
if __skinsdb and not use_skinsdb_tab then
local _skins = skins.get_skinlist_for_player(name)
local skin_name = skins.get_player_skin(player).name
local sks, id = {}, 1
Expand All @@ -2074,7 +2083,7 @@ local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, a

fs("label", 0, yextra + 0.85, fmt("%s:", ES"Select a skin"))
fs(fmt("dropdown[0,%f;4,0.6;skins;%s;%u;true]", yextra + 1.1, sks, id))
else
elseif not use_skinsdb_tab then
not_installed("skinsdb")
end

Expand Down Expand Up @@ -3428,3 +3437,7 @@ end

--dofile(modpath .. "/tests/test_tabs.lua")
--dofile(modpath .. "/tests/test_custom_recipes.lua")

if __skinsdb and use_skinsdb_tab then
dofile(modpath .. "/i3_mods_tabs.lua")
end
9 changes: 8 additions & 1 deletion settingtypes.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# The progressive mode shows recipes you can craft from items you ever had in your inventory.
i3_progressive_mode (Learn crafting recipes progressively) bool false
i3_progressive_mode (Learn crafting recipes progressively) bool true

# Regroup the items of the same type in the item list.
i3_item_compression (Regroup items of the same type) bool true


# ------------ NEW SETTINGS ------------
# not in original i3 available

# Show trash only in creative mode, or with creative privileges
i3_no_trash_in_survival (Trash is available only in creative) bool true

# For skin choice use tab instead of inventory dropdown only
i3_skinsdb_tab (Use tab for skins) bool true

0 comments on commit 4e39096

Please sign in to comment.