summaryrefslogtreecommitdiffstats
path: root/source/UI/Window.h
blob: 86ea1849b2ed532db9a7b6bc2de29892d7c640e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

// Window.h

// Interfaces to the cWindow class representing a UI window for a specific block





#pragma once

#include "../Item.h"





class cPlayer;
class cWindowOwner;
class cClientHandle;
class cChestEntity;
class cDropSpenserEntity;
class cFurnaceEntity;
class cSlotArea;
class cWorld;

typedef std::list<cPlayer *> cPlayerList;
typedef std::vector<cSlotArea *> cSlotAreas;





/** 
Represents a UI window.

Each window has a list of players that are currently using it
When there's no player using a window, it is destroyed.
A window consists of several areas of slots with similar functionality - for example the crafting grid area, or 
the inventory area. Each area knows what its slots are (GetSlot() function) and can handle mouse clicks.
The window acts only as a top-level container for those areas, redirecting the click events to the correct areas.
*/
class cWindow
{
public:
	enum WindowType
	{
		Inventory   = -1,  // This value is never actually sent to a client
		Chest       = 0,
		Workbench   = 1,
		Furnace     = 2,
		DropSpenser = 3,  // Dropper or Dispenser
		Enchantment = 4,
		Brewery     = 5,
		NPCTrade    = 6,
		Beacon      = 7,
		Anvil       = 8,
		Hopper      = 9,
	};
	
	static const int c_NumInventorySlots = 36;

	cWindow(WindowType a_WindowType, const AString & a_WindowTitle);
	virtual ~cWindow();

	char GetWindowID(void) const { return m_WindowID; }
	int GetWindowType(void) const { return m_WindowType; }

	cWindowOwner * GetOwner(void) { return m_Owner; }
	void SetOwner( cWindowOwner * a_Owner ) { m_Owner = a_Owner; }
	
	int GetNumSlots(void) const;
	
	/// Fills a_Slots with the slots read from m_SlotAreas[], for the specified player
	void GetSlots(cPlayer & a_Player, cItems & a_Slots) const;

	void Clicked(
		cPlayer & a_Player, int a_WindowID, 
		short a_SlotNum, eClickAction a_ClickAction,
		const cItem & a_ClickedItem
	);

	void OpenedByPlayer(cPlayer & a_Player);
	void ClosedByPlayer(cPlayer & a_Player);

	void SendWholeWindow(cClientHandle & a_Client);
	void BroadcastWholeWindow(void);
	void BroadcastInventoryProgress(short a_Progressbar, short a_Value);

	const AString & GetWindowTitle() const { return m_WindowTitle; }
	void SetWindowTitle(const AString & a_WindowTitle ) { m_WindowTitle = a_WindowTitle; }

	void OwnerDestroyed(void);
	
	/// Calls the callback safely for each player that has this window open; returns true if all players have been enumerated
	bool ForEachPlayer(cItemCallback<cPlayer> & a_Callback);

	/// Calls the callback safely for each client that has this window open; returns true if all clients have been enumerated
	bool ForEachClient(cItemCallback<cClientHandle> & a_Callback);
	
	/** Called on shift-clicking to distribute the stack into other areas; Modifies a_ItemStack as it is distributed!
	if a_ShouldApply is true, the changes are written into the slots;
	if a_ShouldApply is false, only a_ItemStack is modified to reflect the number of fits (for fit-testing purposes)
	*/
	void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, cSlotArea * a_ExcludeArea, bool a_ShouldApply);
	
	/// Used by cSlotAreas to send individual slots to clients, a_RelativeSlotNum is the slot number relative to a_SlotArea
	void SendSlot(cPlayer & a_Player, cSlotArea * a_SlotArea, int a_RelativeSlotNum);
	
protected:
	cSlotAreas m_SlotAreas;
	
	char    m_WindowID;
	int     m_WindowType;
	AString m_WindowTitle;

	cCriticalSection m_CS;
	cPlayerList      m_OpenedBy;
	
	bool m_IsDestroyed;
	bool m_ShouldDistributeToHotbarFirst;  ///< If set (default), shift+click tries to distribute to hotbar first, then other areas. False for doublechests
	
	cWindowOwner * m_Owner;
	
	static char m_WindowIDCounter;

	void Destroy(void);
} ;





class cCraftingWindow :
	public cWindow
{
public:
	cCraftingWindow(int a_BlockX, int a_BlockY, int a_BlockZ);
} ;





class cFurnaceWindow :
	public cWindow
{
public:
	cFurnaceWindow(int a_BlockX, int a_BlockY, int a_BlockZ, cFurnaceEntity * a_Furnace);
} ;





class cDropSpenserWindow :
	public cWindow
{
public:
	cDropSpenserWindow(int a_BlockX, int a_BlockY, int a_BlockZ, cDropSpenserEntity * a_Dispenser);
} ;





class cChestWindow :
	public cWindow
{
public:
	cChestWindow(cChestEntity * a_Chest);
	cChestWindow(cChestEntity * a_PrimaryChest, cChestEntity * a_SecondaryChest);
	~cChestWindow();
	
protected:
	cWorld * m_World;
	int m_BlockX, m_BlockY, m_BlockZ;  // Position of the chest, for the window-close packet
} ;





class cInventoryWindow :
	public cWindow
{
public:
	cInventoryWindow(cPlayer & a_Player);
	
protected:
	cPlayer & m_Player;

} ;