2017-09-02 03:45:06 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// LuaWindow.h
|
|
|
|
|
|
|
|
// Declares the cLuaWindow class representing a virtual window that plugins may create and open for the player
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-06-10 15:30:07 -04:00
|
|
|
#include "LuaState.h"
|
2013-12-08 06:17:54 -05:00
|
|
|
#include "../UI/Window.h"
|
|
|
|
#include "../ItemGrid.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
2017-05-29 15:33:30 -04:00
|
|
|
class cPlayer;
|
2017-09-11 17:20:49 -04:00
|
|
|
typedef cFunctionRef<bool(cPlayer &)> cPlayerListCallback;
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
/** A window that has been created by a Lua plugin and is handled entirely by that plugin
|
|
|
|
This object needs extra care with its lifetime management:
|
|
|
|
- It is created by Lua, so Lua expects to garbage-collect it later
|
2016-06-10 15:30:07 -04:00
|
|
|
- Normal cWindow objects are deleted in their ClosedByPlayer() function if the last player closes them
|
|
|
|
To overcome this, this object overloads the Destroy functions, which doesn't let the ClosedByPlayer()
|
|
|
|
delete the window, but rather leaves it dangling, with only Lua having the reference to it.
|
|
|
|
- Lua could GC the window while a player is still using it
|
|
|
|
The object creates a Lua reference to itself when opened by a player and
|
|
|
|
removes the reference when the last player closes the window.
|
|
|
|
*/
|
2013-12-16 16:02:28 -05:00
|
|
|
// tolua_begin
|
|
|
|
class cLuaWindow :
|
2015-03-19 16:26:38 -04:00
|
|
|
public cWindow
|
|
|
|
// tolua_end
|
|
|
|
, public cItemGrid::cListener
|
2016-06-10 15:30:07 -04:00
|
|
|
{ // tolua_export
|
2020-04-13 12:38:06 -04:00
|
|
|
using Super = cWindow;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
public:
|
2016-06-10 15:30:07 -04:00
|
|
|
/** Create a window of the specified type, with a slot grid of a_SlotsX * a_SlotsY size.
|
|
|
|
Exported in ManualBindings.cpp */
|
|
|
|
cLuaWindow(cLuaState & a_LuaState, cWindow::WindowType a_WindowType, int a_SlotsX, int a_SlotsY, const AString & a_Title);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-06-10 15:30:07 -04:00
|
|
|
// tolua_begin
|
2017-05-20 02:16:28 -04:00
|
|
|
virtual ~cLuaWindow() override;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns the internal representation of the contents that are manipulated by Lua */
|
2013-07-29 07:13:03 -04:00
|
|
|
cItemGrid & GetContents(void) { return m_Contents; }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// tolua_end
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-08-17 10:27:43 -04:00
|
|
|
/** Sets the Lua callback to call when the player clicks on the window.
|
|
|
|
The window can stop the click from propogating. */
|
|
|
|
void SetOnClicked(cLuaState::cCallbackPtr && a_OnClicked);
|
|
|
|
|
2016-06-10 15:30:07 -04:00
|
|
|
/** Sets the Lua callback function to call when the window is about to close */
|
2016-06-27 14:49:59 -04:00
|
|
|
void SetOnClosing(cLuaState::cCallbackPtr && a_OnClosing);
|
2016-03-21 04:58:15 -04:00
|
|
|
|
2016-06-10 15:30:07 -04:00
|
|
|
/** Sets the Lua callback function to call when a slot is changed */
|
2016-06-27 14:49:59 -04:00
|
|
|
void SetOnSlotChanged(cLuaState::cCallbackPtr && a_OnSlotChanged);
|
2016-03-16 05:58:28 -04:00
|
|
|
|
2016-03-21 04:58:15 -04:00
|
|
|
protected:
|
2016-06-10 15:30:07 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Contents of the non-inventory part */
|
2013-07-29 07:13:03 -04:00
|
|
|
cItemGrid m_Contents;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2017-01-09 09:56:16 -05:00
|
|
|
/** The canon Lua state that has opened the window and owns the m_LuaRef */
|
2016-06-10 15:30:07 -04:00
|
|
|
cLuaState * m_LuaState;
|
|
|
|
|
2017-08-17 10:27:43 -04:00
|
|
|
/** The Lua callback to call when the player clicked on a slot */
|
|
|
|
cLuaState::cCallbackPtr m_OnClicked;
|
|
|
|
|
2016-06-10 15:30:07 -04:00
|
|
|
/** The Lua callback to call when the window is closing for any player */
|
|
|
|
cLuaState::cCallbackPtr m_OnClosing;
|
|
|
|
|
|
|
|
/** The Lua callback to call when a slot has changed */
|
|
|
|
cLuaState::cCallbackPtr m_OnSlotChanged;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-06-10 15:30:07 -04:00
|
|
|
/** Number of players that are currently using the window.
|
|
|
|
Used to manager the m_LuaRef lifetime. */
|
|
|
|
std::atomic<int> m_PlayerCount;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2016-06-10 15:30:07 -04:00
|
|
|
/** Reference to self, to keep Lua from GCing the object while a player is still using it.
|
|
|
|
Created when the first player opens the window, destroyed when the last player closes the window. */
|
|
|
|
cLuaState::cRef m_LuaRef;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// cWindow overrides:
|
2016-06-10 15:30:07 -04:00
|
|
|
virtual void OpenedByPlayer(cPlayer & a_Player) override;
|
2017-08-17 10:27:43 -04:00
|
|
|
virtual void Clicked(
|
|
|
|
cPlayer & a_Player, int a_WindowID,
|
|
|
|
short a_SlotNum, eClickAction a_ClickAction,
|
|
|
|
const cItem & a_ClickedItem
|
|
|
|
) override;
|
2013-07-29 07:13:03 -04:00
|
|
|
virtual bool ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse) override;
|
|
|
|
virtual void Destroy(void) override;
|
2014-12-17 13:14:01 -05:00
|
|
|
virtual void DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply) override;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
// cItemGrid::cListener overrides:
|
|
|
|
virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override;
|
|
|
|
} ; // tolua_export
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|