forked from lord-server/lord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Player Inventory: Refactoring: refactoring & make Main as Tab. Closes l…
…ord-server#1699. Relates to lord-server#1020
- Loading branch information
Showing
6 changed files
with
229 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
|
||
minetest.mod(function(mod) | ||
require("inventory") | ||
require("inventory").init(mod) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
local MainTab = require('inventory.Form.MainTab') | ||
--local AboutTab = require('inventory.Form.BagsTab') TODO #1698 | ||
--local AboutTab = require('inventory.Form.AboutTab') TODO #1701 | ||
|
||
|
||
--- @class inventory.Form: base_classes.Form.Mixin.WithTabs | ||
local Form = base_classes.Form:personal():with_tabs():extended({ | ||
--- @const | ||
--- @type string | ||
NAME = "", | ||
|
||
player_name = '', | ||
|
||
--- @type table<string,number> | ||
tab = { MAIN = 1, ABOUT = 2, }, | ||
--- @type number | ||
current_tab = 1, | ||
--- Replaces by player forms collection in Form.on_register callback | ||
--- @see inventory.Form:on_register() @ below. | ||
--- @type inventory.Form[]|table<string,inventory.Form> | ||
opened_for = {}, | ||
}) | ||
|
||
function Form:get_spec_head() | ||
return 'size[8,8.5]' | ||
end | ||
|
||
--- @param player Player | ||
function Form:instantiate(player) | ||
self | ||
:add_tab(MainTab:new(self)) | ||
--:add_tab(AboutTab:new(self)) | ||
:refresh() | ||
end | ||
|
||
--- @param preview string | ||
--- @return inventory.Form | ||
function Form:set_main_preview(preview) | ||
--- @type inventory.Form.MainTab | ||
local main_tab = self.tabs[self.tab.MAIN] | ||
main_tab:set_preview(preview) | ||
|
||
return self | ||
end | ||
|
||
function Form:refresh() | ||
minetest.get_player_by_name(self.player_name):set_inventory_formspec(self:get_spec()) | ||
end | ||
|
||
--- @param fields table | ||
function Form:handle(fields) | ||
end | ||
|
||
--- @param self inventory.Form | ||
--- @param player_forms_collection inventory.Form[]|table<string,inventory.Form> | ||
Form.on_register(function(self, player_forms_collection) | ||
-- To open some-kind our custom inventory for player, MT provides `player:set_inventory_formspec(spec)` function. | ||
-- And then MT by itself catch pressed `i` button & opens form with that spec. | ||
-- So, we never/nowhere calls `Form:open()`. | ||
-- But! Mixin `WithTabs` creates an instance exactly in `Form:open()` and put it into `Form.opened_for[player_name]` | ||
-- Then this var used to get the instance of the player to handle. | ||
-- But the `Form:open()` is never/nowhere calls. | ||
-- So we need to creates this instances manually and somehow put into `Form.opened_for[player_name]`. | ||
-- We already do it in `_G.inventory.for_player`. (see `inventory.lua`) | ||
-- And here we just replace this `Form.opened_for` array with that `player_forms_collection`. | ||
-- So `WithTabs` mixin works with the same collection as `_G.inventory.for_player` | ||
Form.opened_for = player_forms_collection | ||
end) | ||
|
||
|
||
return Form |
84 changes: 84 additions & 0 deletions
84
mods/lord/Player/lord_inventory/src/inventory/Form/MainTab.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
local S = minetest.get_translator('lord_inventory') | ||
|
||
|
||
--- Trash / Recycle Bin | ||
local detached_inv_trash = minetest.create_detached_inventory('trash', { | ||
allow_put = function(inv, listname, index, stack, player) | ||
return stack:get_count() | ||
end, | ||
on_put = function(inv, listname, index, stack, player) | ||
inv:set_stack(listname, index, '') | ||
end, | ||
}) | ||
detached_inv_trash:set_size('main', 1) | ||
--- / | ||
|
||
--- Form Spec Template | ||
local formspec_template = '' | ||
-- Armor slots | ||
.. 'image[0,0;1,1;lottarmor_helmet.png]' | ||
.. 'image[0,1;1,1;lottarmor_chestplate.png]' | ||
.. 'image[0,2;1,1;lottarmor_leggings.png]' | ||
.. 'image[0,3;1,1;lottarmor_boots.png]' | ||
.. 'list[detached:{{armor_slots}};armor;0,0;1,4;]' | ||
-- Clothes slots | ||
.. 'image[3,0;1,1;lottarmor_helmet.png]' | ||
.. 'image[3,1;1,1;lottarmor_shirt.png]' | ||
.. 'image[3,2;1,1;lottarmor_trousers.png]' | ||
.. 'image[3,3;1,1;lottarmor_shoes.png]' | ||
.. 'list[detached:{{clothing_slots}};clothing;3,0;1,4;]' | ||
.. 'image[4,0;1,1;lottarmor_cloak.png]' | ||
.. 'list[detached:{{clothing_slots}};clothing;4,0;1,1;4]' | ||
-- Preview & Shield | ||
.. 'image[1.16,0.25;2,4;{{armor_preview}}]' | ||
.. 'image[2,2;1,1;lottarmor_shield.png]' | ||
.. 'list[detached:{{armor_slots}};armor;2,2;1,1;4]' | ||
-- Crafting | ||
.. 'image[5.05,0;3.5,1;lottarmor_crafting.png]' | ||
.. 'list[current_player;craft;4,1;3,3;]' | ||
.. 'list[current_player;craftpreview;7,2;1,1;]' | ||
-- Bags & Trash | ||
.. 'image_button[7,1;1,1;bags.png;bags;]' | ||
.. 'image[7,3;1,1;lottarmor_trash.png]' | ||
.. 'list[detached:trash;main;7,3;1,1;]' | ||
-- Player inventory content | ||
.. 'list[current_player;main;0,4.25;8,1;]' | ||
.. 'list[current_player;main;0,5.5;8,3;8]' | ||
-- options | ||
.. 'listring[current_player;main]' | ||
.. 'listring[current_player;craft]' | ||
--- / | ||
|
||
|
||
--- @class inventory.Form.MainTab: base_classes.Form.Element.Tab | ||
local MainTab = base_classes.Form.Element.Tab:extended({ | ||
title = S('Main'), | ||
--- @type inventory.Form parent form | ||
form = nil, | ||
--- @type string | ||
template = formspec_template, | ||
--- @type string | ||
preview = '' | ||
}) | ||
|
||
--- @param preview | ||
--- @return self|inventory.Form.MainTab | ||
function MainTab:set_preview(preview) | ||
self.preview = preview | ||
|
||
return self | ||
end | ||
|
||
function MainTab:get_spec() | ||
local armor_inv_name = equipment.get_inventory_name(self.form.player_name, equipment.Kind.ARMOR) | ||
local clothing_inv_name = equipment.get_inventory_name(self.form.player_name, equipment.Kind.CLOTHING) | ||
local formspec = self.template | ||
formspec = formspec:replace('{{armor_slots}}', armor_inv_name) | ||
formspec = formspec:replace('{{clothing_slots}}', clothing_inv_name) | ||
formspec = formspec:replace('{{armor_preview}}', self.preview) | ||
|
||
return formspec | ||
end | ||
|
||
|
||
return MainTab |
File renamed without changes.
66 changes: 0 additions & 66 deletions
66
mods/lord/Player/lord_inventory/src/inventory/main_form.lua
This file was deleted.
Oops, something went wrong.