Cuberite
A lightweight, fast and extensible game server for Minecraft
LuaWindow.h
Go to the documentation of this file.
1 
2 // LuaWindow.h
3 
4 // Declares the cLuaWindow class representing a virtual window that plugins may create and open for the player
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "LuaState.h"
13 #include "../UI/Window.h"
14 #include "../ItemGrid.h"
15 
16 
17 class cPlayer;
19 
20 
31 // tolua_begin
32 class cLuaWindow :
33  public cWindow
34  // tolua_end
35  , public cItemGrid::cListener
36 { // tolua_export
37  using Super = cWindow;
38 
39 public:
42  cLuaWindow(cLuaState & a_LuaState, cWindow::WindowType a_WindowType, int a_SlotsX, int a_SlotsY, const AString & a_Title);
43 
44  // tolua_begin
45  virtual ~cLuaWindow() override;
46 
48  cItemGrid & GetContents(void) { return m_Contents; }
49 
50  // tolua_end
51 
54  void SetOnClicked(cLuaState::cCallbackPtr && a_OnClicked);
55 
57  void SetOnClosing(cLuaState::cCallbackPtr && a_OnClosing);
58 
60  void SetOnSlotChanged(cLuaState::cCallbackPtr && a_OnSlotChanged);
61 
62 protected:
63 
66 
69 
72 
75 
78 
81  std::atomic<int> m_PlayerCount;
82 
86 
87 
88  // cWindow overrides:
89  virtual void OpenedByPlayer(cPlayer & a_Player) override;
90  virtual void Clicked(
91  cPlayer & a_Player, int a_WindowID,
92  short a_SlotNum, eClickAction a_ClickAction,
93  const cItem & a_ClickedItem
94  ) override;
95  virtual bool ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse) override;
96  virtual void Destroy(void) override;
97  virtual void DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply) override;
98 
99  // cItemGrid::cListener overrides:
100  virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override;
101 } ; // tolua_export
102 
103 
104 
105 
106 
cFunctionRef< bool(cPlayer &)> cPlayerListCallback
Definition: LuaWindow.h:17
eClickAction
Individual actions sent in the WindowClick packet.
Definition: Defines.h:82
std::string AString
Definition: StringUtils.h:11
Encapsulates a Lua state and provides some syntactic sugar for common operations.
Definition: LuaState.h:56
std::unique_ptr< cCallback > cCallbackPtr
Definition: LuaState.h:326
Used for storing references to object in the global registry.
Definition: LuaState.h:160
A window that has been created by a Lua plugin and is handled entirely by that plugin This object nee...
Definition: LuaWindow.h:36
std::atomic< int > m_PlayerCount
Number of players that are currently using the window.
Definition: LuaWindow.h:81
virtual void OnSlotChanged(cItemGrid *a_ItemGrid, int a_SlotNum) override
Called whenever a slot changes.
Definition: LuaWindow.cpp:200
cItemGrid & GetContents(void)
Returns the internal representation of the contents that are manipulated by Lua.
Definition: LuaWindow.h:48
void SetOnClicked(cLuaState::cCallbackPtr &&a_OnClicked)
Sets the Lua callback to call when the player clicks on the window.
Definition: LuaWindow.cpp:85
cLuaWindow(cLuaState &a_LuaState, cWindow::WindowType a_WindowType, int a_SlotsX, int a_SlotsY, const AString &a_Title)
Create a window of the specified type, with a slot grid of a_SlotsX * a_SlotsY size.
Definition: LuaWindow.cpp:23
void SetOnClosing(cLuaState::cCallbackPtr &&a_OnClosing)
Sets the Lua callback function to call when the window is about to close.
Definition: LuaWindow.cpp:98
virtual void Clicked(cPlayer &a_Player, int a_WindowID, short a_SlotNum, eClickAction a_ClickAction, const cItem &a_ClickedItem) override
Handles a click event from a player.
Definition: LuaWindow.cpp:215
virtual ~cLuaWindow() override
Definition: LuaWindow.cpp:55
void SetOnSlotChanged(cLuaState::cCallbackPtr &&a_OnSlotChanged)
Sets the Lua callback function to call when a slot is changed.
Definition: LuaWindow.cpp:111
cItemGrid m_Contents
Contents of the non-inventory part.
Definition: LuaWindow.h:65
virtual bool ClosedByPlayer(cPlayer &a_Player, bool a_CanRefuse) override
Called when a player closes this window; notifies all slot areas.
Definition: LuaWindow.cpp:140
virtual void Destroy(void) override
Sets the internal flag as "destroyed"; notifies the owner that the window is destroying.
Definition: LuaWindow.cpp:170
cLuaState::cCallbackPtr m_OnSlotChanged
The Lua callback to call when a slot has changed.
Definition: LuaWindow.h:77
cLuaState::cCallbackPtr m_OnClicked
The Lua callback to call when the player clicked on a slot.
Definition: LuaWindow.h:71
virtual void OpenedByPlayer(cPlayer &a_Player) override
Definition: LuaWindow.cpp:124
cLuaState::cRef m_LuaRef
Reference to self, to keep Lua from GCing the object while a player is still using it.
Definition: LuaWindow.h:85
cLuaState * m_LuaState
The canon Lua state that has opened the window and owns the m_LuaRef.
Definition: LuaWindow.h:68
cLuaState::cCallbackPtr m_OnClosing
The Lua callback to call when the window is closing for any player.
Definition: LuaWindow.h:74
virtual void DistributeStack(cItem &a_ItemStack, int a_Slot, cPlayer &a_Player, cSlotArea *a_ClickedArea, bool a_ShouldApply) override
Called on shift-clicking to distribute the stack into other areas; Modifies a_ItemStack as it is dist...
Definition: LuaWindow.cpp:182
Definition: Player.h:29
Definition: Item.h:37
This class is used as a callback for when a slot changes.
Definition: ItemGrid.h:26
Represents a UI window.
Definition: Window.h:54
WindowType
Definition: Window.h:57
cWindow(WindowType a_WindowType, const AString &a_WindowTitle)
Definition: Window.cpp:30