]> git.lizzy.rs Git - minetest.git/blob - src/gui/guiInventoryList.h
Add keybind to swap items between hands
[minetest.git] / src / gui / guiInventoryList.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "inventorymanager.h"
23 #include "irrlichttypes_extrabloated.h"
24 #include "util/string.h"
25
26 class GUIFormSpecMenu;
27
28 class GUIInventoryList : public gui::IGUIElement
29 {
30 public:
31         struct ItemSpec
32         {
33                 ItemSpec() = default;
34
35                 ItemSpec(const InventoryLocation &a_inventoryloc,
36                                 const std::string &a_listname,
37                                 s32 a_i,
38                                 const v2s32 slotsize) :
39                         inventoryloc(a_inventoryloc),
40                         listname(a_listname),
41                         i(a_i),
42                         slotsize(slotsize)
43                 {
44                 }
45
46                 bool isValid() const { return i != -1; }
47
48                 InventoryLocation inventoryloc;
49                 std::string listname;
50                 s32 i = -1;
51                 v2s32 slotsize;
52         };
53
54         // options for inventorylists that are setable with the lua api
55         struct Options {
56                 // whether a one-pixel border for the slots should be drawn and its color
57                 bool slotborder = false;
58                 video::SColor slotbordercolor = video::SColor(200, 0, 0, 0);
59                 // colors for normal and highlighted slot background
60                 video::SColor slotbg_n = video::SColor(255, 128, 128, 128);
61                 video::SColor slotbg_h = video::SColor(255, 192, 192, 192);
62         };
63
64         GUIInventoryList(gui::IGUIEnvironment *env,
65                 gui::IGUIElement *parent,
66                 s32 id,
67                 const core::rect<s32> &rectangle,
68                 InventoryManager *invmgr,
69                 const InventoryLocation &inventoryloc,
70                 const std::string &listname,
71                 const v2s32 &geom,
72                 const s32 start_item_i,
73                 const v2s32 &slot_size,
74                 const v2f32 &slot_spacing,
75                 GUIFormSpecMenu *fs_menu,
76                 const Options &options,
77                 gui::IGUIFont *font);
78
79         virtual void draw() override;
80
81         virtual bool OnEvent(const SEvent &event) override;
82
83         const InventoryLocation &getInventoryloc() const
84         {
85                 return m_inventoryloc;
86         }
87
88         const std::string &getListname() const
89         {
90                 return m_listname;
91         }
92
93         void setSlotBGColors(const video::SColor &slotbg_n, const video::SColor &slotbg_h)
94         {
95                 m_options.slotbg_n = slotbg_n;
96                 m_options.slotbg_h = slotbg_h;
97         }
98
99         void setSlotBorders(bool slotborder, const video::SColor &slotbordercolor)
100         {
101                 m_options.slotborder = slotborder;
102                 m_options.slotbordercolor = slotbordercolor;
103         }
104
105         const v2s32 getSlotSize() const noexcept
106         {
107                 return m_slot_size;
108         }
109
110         // returns -1 if not item is at pos p
111         s32 getItemIndexAtPos(v2s32 p) const;
112
113 private:
114         InventoryManager *m_invmgr;
115         const InventoryLocation m_inventoryloc;
116         const std::string m_listname;
117
118         // the specified width and height of the shown inventorylist in itemslots
119         const v2s32 m_geom;
120         // the first item's index in inventory
121         const s32 m_start_item_i;
122
123         // specifies how large the slot rects are
124         const v2s32 m_slot_size;
125         // specifies how large the space between slots is (space between is spacing-size)
126         const v2f32 m_slot_spacing;
127
128         // the GUIFormSpecMenu can have an item selected and co.
129         GUIFormSpecMenu *m_fs_menu;
130
131         Options m_options;
132
133         // the font
134         gui::IGUIFont *m_font;
135
136         // the index of the hovered item; -1 if no item is hovered
137         s32 m_hovered_i;
138
139         // we do not want to write a warning on every draw
140         bool m_already_warned;
141 };