From 95b0434f954f17fe62253eb15ceef7ac9b3987c9 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Tue, 30 Nov 2021 02:28:11 +0100 Subject: [PATCH] Bags can have custom names --- src/bags.lua | 10 +++++++--- src/callbacks.lua | 20 +++++++++++++++++++- src/common.lua | 24 ++++++++++++++++++++---- src/gui.lua | 28 ++++++++++++++++++++-------- src/styles.lua | 6 +++++- textures/i3_edit.png | Bin 0 -> 749 bytes textures/i3_flag.png | Bin 818 -> 745 bytes 7 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 textures/i3_edit.png diff --git a/src/bags.lua b/src/bags.lua index b654278..5b3deca 100644 --- a/src/bags.lua +++ b/src/bags.lua @@ -1,6 +1,7 @@ local set_fs = i3.set_fs -IMPORT("S", "ES", "fmt", "msg", "slz", "dslz", "ItemStack") +IMPORT("get_bag_description", "ItemStack") +IMPORT("S", "ES", "fmt", "msg", "slz", "dslz") IMPORT("get_group", "play_sound", "get_detached_inv", "create_inventory") local function get_content(content) @@ -51,6 +52,8 @@ local function init_bags(player) on_take = function() data.bag = nil + data.bag_rename = nil + local content = get_detached_inv("bag_content", name) content:set_list("main", {}) @@ -67,9 +70,10 @@ local function init_bags(player) local function save_content(inv) local bagstack = bag:get_stack("main", 1) local meta = bagstack:get_meta() - meta:set_string("description", "") + local desc = get_bag_description(data, bagstack) if inv:is_empty"main" then + meta:set_string("description", desc) meta:set_string("content", "") else local list = inv:get_list"main" @@ -87,7 +91,7 @@ local function init_bags(player) local bag_size = get_group(bagstack:get_name(), "bag") local percent = fmt("%d", (c * 100) / (bag_size * 4)) - meta:set_string("description", ES("@1 (@2% full)", bagstack:get_description(), percent)) + meta:set_string("description", ES("@1 (@2% full)", desc, percent)) meta:set_string("content", slz(t)) end diff --git a/src/callbacks.lua b/src/callbacks.lua index 6ee984f..57c55a1 100644 --- a/src/callbacks.lua +++ b/src/callbacks.lua @@ -3,8 +3,8 @@ local set_fs = i3.set_fs IMPORT("vec_eq", "vec_round") IMPORT("reg_items", "reg_aliases") -IMPORT("S", "min", "random", "ItemStack") IMPORT("sort", "copy", "insert", "remove", "indexof") +IMPORT("S", "min", "random", "translate", "ItemStack") IMPORT("fmt", "find", "match", "sub", "lower", "split") IMPORT("msg", "is_fav", "pos_to_str", "str_to_pos", "add_hud_waypoint", "play_sound", "spawn_item") IMPORT("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "get_recipes", "get_detached_inv") @@ -118,6 +118,7 @@ i3.new_tab("inventory", { data.confirm_trash = nil data.show_settings = nil data.waypoint_see = nil + data.bag_rename = nil elseif fields.trash then data.show_settings = nil @@ -175,6 +176,23 @@ i3.new_tab("inventory", { elseif fields.set_home then data.home = pos_to_str(player:get_pos(), 1) + elseif fields.bag_rename then + data.bag_rename = true + + elseif fields.confirm_rename then + local bag = get_detached_inv("bag", name) + local bagstack = bag:get_stack("main", 1) + local meta = bagstack:get_meta() + local desc = translate(data.lang_code, bagstack:get_description()) + local str = desc:match("(.*)%(+") or desc + local newname = fields.bag_newname:gsub("([%(%)])", "") + + meta:set_string("description", desc:gsub(str:trim(), newname)) + bag:set_stack("main", 1, bagstack) + + data.bag = bagstack:to_string() + data.bag_rename = nil + elseif sb_inv and sub(sb_inv, 1, 3) == "CHG" then data.scrbar_inv = tonumber(match(sb_inv, "%d+")) return diff --git a/src/common.lua b/src/common.lua index 73af003..a4c63d5 100644 --- a/src/common.lua +++ b/src/common.lua @@ -1,11 +1,13 @@ local ItemStack = ItemStack local loadstring = loadstring - +local ESC = core.formspec_escape +local translate = core.get_translated_string local vec_new, vec_add, vec_mul = vector.new, vector.add, vector.multiply local sort, concat, insert = table.sort, table.concat, table.insert local min, floor, ceil = math.min, math.floor, math.ceil -local fmt, find, match, gmatch, sub, split, lower = - string.format, string.find, string.match, string.gmatch, string.sub, string.split, string.lower +local fmt, find, match, gmatch, sub, split, lower, upper = + string.format, string.find, string.match, string.gmatch, + string.sub, string.split, string.lower, string.upper local old_is_creative_enabled = core.is_creative_enabled @@ -63,6 +65,18 @@ local function round(num, decimal) return floor(num * mul + 0.5) / mul end +local function toupper(str) + return str:gsub("%f[%w]%l", upper):gsub("_", " ") +end + +local function get_bag_description(data, stack) + local desc = translate(data.lang_code, stack:get_description()) + desc = desc:match("(.*)%(+") or desc + desc = ESC(toupper(desc:trim())) + + return desc +end + local function search(data) reset_compression(data) @@ -87,7 +101,7 @@ local function search(data) for i = 1, #data.items_raw do local item = data.items_raw[i] local def = core.registered_items[item] - local desc = lower(core.get_translated_string(data.lang_code, def and def.description)) or "" + local desc = lower(translate(data.lang_code, def and def.description)) or "" local search_in = fmt("%s %s", item, desc) local temp, j, to_add = {}, 1 @@ -648,6 +662,7 @@ local _ = { get_stack = get_stack, craft_stack = craft_stack, get_detached_inv = get_detached_inv, + get_bag_description = get_bag_description, create_inventory = core.create_detached_inventory, -- Registered items @@ -672,6 +687,7 @@ local _ = { split = string.split, match = string.match, gmatch = string.gmatch, + toupper = toupper, -- Table maxn = table.maxn, diff --git a/src/gui.lua b/src/gui.lua index 7e382f5..e492cfd 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -9,13 +9,14 @@ local VoxelArea, VoxelManip = VoxelArea, VoxelManip IMPORT("clr", "ESC", "check_privs") IMPORT("find", "match", "sub", "upper") IMPORT("vec_new", "vec_sub", "vec_round") -IMPORT("S", "ES", "translate", "ItemStack") IMPORT("min", "max", "floor", "ceil", "round") IMPORT("reg_items", "reg_tools", "reg_entities") +IMPORT("get_bag_description", "get_detached_inv") +IMPORT("S", "ES", "translate", "ItemStack", "toupper") IMPORT("groups_to_items", "compression_active", "compressible") IMPORT("true_str", "is_fav", "is_num", "get_group", "str_to_pos") IMPORT("maxn", "sort", "concat", "copy", "insert", "remove", "unpack") -IMPORT("get_sorting_idx", "is_group", "extract_groups", "item_has_groups", "get_detached_inv") +IMPORT("get_sorting_idx", "is_group", "extract_groups", "item_has_groups") local function fmt(elem, ...) if not fs_elements[elem] then @@ -34,10 +35,6 @@ local function weird_desc(str) return not true_str(str) or find(str, "\n") or not find(str, "%u") end -local function toupper(str) - return str:gsub("%f[%w]%l", upper):gsub("_", " ") -end - local function snip(str, limit) return #str > limit and fmt("%s...", sub(str, 1, limit - 3)) or str end @@ -405,12 +402,27 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa local v = {{1.9, 2, 0.12}, {3.05, 5, 0.06}, {4.2, 10}, {4.75, 10}} local h, m, yy = unpack(v[bag_size]) + local bagstack = bag:get_stack("main", 1) + local desc = get_bag_description(data, bagstack) + fs("image", 0.5, yextra + 1.85, 0.6, 0.6, PNG.arrow_content) fs(fmt("style[bg_content;bgimg=%s;fgimg=i3_blank.png;bgimg_middle=10,%u;sound=]", PNG.bg_content, m)) fs("image_button", 1.1, yextra + 0.5 + (yy or 0), 4.75, h, "", "bg_content", "") - fs("hypertext", 1.3, yextra + 0.8, 4.3, 0.6, "content", - fmt("
%s
", ES"Content")) + + if not data.bag_rename then + fs("hypertext", 1.3, yextra + 0.8, 4.3, 0.6, "content", + fmt("
%s
", desc)) + fs("image_button", 5.22, yextra + 0.835, 0.25, 0.25, "", "bag_rename", "") + fs(fmt("tooltip[%s;%s]", "bag_rename", ES"Rename bag")) + else + fs("box", 1.7, yextra + 0.82, 2.6, 0.4, "#707070") + fs(fmt("field[1.8,%f;2.5,0.4;bag_newname;;%s]", yextra + 0.82, desc), + "field_close_on_enter[bag_newname;false]") + fs("hypertext", 4.4, yextra + 0.88, 0.8, 0.5, "confirm_rename", + fmt("" .. + "
OK
", colors.yellow)) + end local x, size, spacing = 1.45, 0.9, 0.12 diff --git a/src/styles.lua b/src/styles.lua index 1b61899..56796cf 100644 --- a/src/styles.lua +++ b/src/styles.lua @@ -39,6 +39,7 @@ local PNG = { exit = "i3_exit.png", home = "i3_home.png", flag = "i3_flag.png", + edit = "i3_edit.png", cancel_hover = "i3_cancel.png^\\[brighten", search_hover = "i3_search.png^\\[brighten", @@ -61,6 +62,7 @@ local PNG = { refresh_hover = "i3_refresh.png^\\[brighten", exit_hover = "i3_exit.png^\\[brighten", home_hover = "i3_home.png^\\[brighten", + edit_hover = "i3_edit.png^\\[brighten", } local styles = string.format([[ @@ -82,6 +84,7 @@ local styles = string.format([[ style[prev_usage;fgimg=%s;fgimg_hovered=%s] style[next_usage;fgimg=%s;fgimg_hovered=%s] style[waypoint_add;fgimg=%s;fgimg_hovered=%s;content_offset=0] + style[bag_rename;fgimg=%s;fgimg_hovered=%s;content_offset=0] style[btn_bag,btn_armor,btn_skins;font=bold;font_size=18;content_offset=0;sound=i3_click] style[craft_rcp,craft_usg;noclip=true;font_size=16;sound=i3_craft; bgimg=i3_btn9.png;bgimg_hovered=i3_btn9_hovered.png; @@ -101,7 +104,8 @@ PNG.prev, PNG.prev_hover, PNG.next, PNG.next_hover, PNG.prev, PNG.prev_hover, PNG.next, PNG.next_hover, -PNG.add, PNG.add_hover) +PNG.add, PNG.add_hover, +PNG.edit, PNG.edit_hover) local fs_elements = { label = "label[%f,%f;%s]", diff --git a/textures/i3_edit.png b/textures/i3_edit.png new file mode 100644 index 0000000000000000000000000000000000000000..78abdec06a55ac955c6faa0bf17d4765fb9cde7a GIT binary patch literal 749 zcmVDs>AsP)?t)kdi#2YqVIvf1}V&OH~#Lu9ejfHqA)yneM_yGhjg_lM_R8Si= zf~X*PH-%9!_t?C`u&icxvxYgdGce6I!}FZ^zjMyn^LBM5e4!h?*oYIjgLn9V$2gB2 zsG_^8NLRr3V-N0O6u&TqX-wcdMsN(PQ7Iby0Xz%Xxxd^q@kX4(n&c(az(0=>pPT2N z#pmQD1bnqg_^gCaIGdz|%*YSnMU(Kg5>8=NQiTyc*omht!e=GC$8oGkNk9dIxPqSn z(yD|Ryu_~5V$g$axX}g)W4M(2+}HVS3AVRE!X!rWT=14*E&6IjWF%Z)fP`86$aA&d zk6ReV$~qEyuoYKZlrV#Dd7tEr;}wqnLqe-@7{~n-W_Z){NNBYtjN)V})7y4Sn800Z zN@;H2sD$|n<5WCnH+oZB*i1K26aEy&A9W;5#WZ>-p4*)gd@~ZZ)RB;duciv$s)WfR z!8a@63Z4`Vz8MMYV-?w7H26j(R6;}{;Tw{W@-|j05{A*2H~58YW6d==zrP);$nHGA z_bliw48JF;5u4iui06qX{_#T*gN{zy%zR fcJyM5@I2RVnluXRhozL+00000NkvXXu0mjf)<0`a literal 0 HcmV?d00001 diff --git a/textures/i3_flag.png b/textures/i3_flag.png index 06f21e16dce4061474d3781145093636c5507584..07ff5d962e6ac6ede35298759411d05fa605a87b 100644 GIT binary patch delta 10 RcmdnQ_L6mi^2VHFOaK@}1U~=( delta 82 zcmaFKx`}OqvO5EFv6E*A2M5RPhyD*37#KJUJR*x37#MehFr(F*$B%%5k|nMYCBgY= jCFO}lsSM@i<$9TU*~Q6;1*v-ZMd`EO*+^|vJi!D2C&wG5