2013-07-29 07:13:03 -04:00
|
|
|
// LuaWindow.cpp
|
|
|
|
|
|
|
|
// Implements the cLuaWindow class representing a virtual window that plugins may create and open for the player
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "LuaWindow.h"
|
2013-12-08 06:17:54 -05:00
|
|
|
#include "../UI/SlotArea.h"
|
2013-08-09 09:15:56 -04:00
|
|
|
#include "PluginLua.h"
|
2014-07-17 16:15:34 -04:00
|
|
|
#include "lua/src/lauxlib.h" // Needed for LUA_REFNIL
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-07-29 07:13:03 -04:00
|
|
|
// cLuaWindow:
|
|
|
|
|
|
|
|
cLuaWindow::cLuaWindow(cWindow::WindowType a_WindowType, int a_SlotsX, int a_SlotsY, const AString & a_Title) :
|
|
|
|
super(a_WindowType, a_Title),
|
|
|
|
m_Contents(a_SlotsX, a_SlotsY),
|
2014-10-20 16:55:07 -04:00
|
|
|
m_Plugin(nullptr),
|
2013-07-29 07:13:03 -04:00
|
|
|
m_LuaRef(LUA_REFNIL),
|
|
|
|
m_OnClosingFnRef(LUA_REFNIL),
|
|
|
|
m_OnSlotChangedFnRef(LUA_REFNIL)
|
|
|
|
{
|
|
|
|
m_Contents.AddListener(*this);
|
|
|
|
m_SlotAreas.push_back(new cSlotAreaItemGrid(m_Contents, *this));
|
|
|
|
|
|
|
|
// If appropriate, add an Armor slot area:
|
|
|
|
switch (a_WindowType)
|
|
|
|
{
|
2013-10-28 08:30:24 -04:00
|
|
|
case cWindow::wtInventory:
|
|
|
|
case cWindow::wtWorkbench:
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
m_SlotAreas.push_back(new cSlotAreaArmor(*this));
|
|
|
|
break;
|
|
|
|
}
|
2013-12-09 09:17:39 -05:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
m_SlotAreas.push_back(new cSlotAreaInventory(*this));
|
|
|
|
m_SlotAreas.push_back(new cSlotAreaHotBar(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cLuaWindow::~cLuaWindow()
|
|
|
|
{
|
2013-08-17 18:39:15 -04:00
|
|
|
m_Contents.RemoveListener(*this);
|
|
|
|
|
|
|
|
// Must delete slot areas now, because they are referencing this->m_Contents and would try to access it in cWindow's
|
|
|
|
// destructor, when the member is already gone.
|
|
|
|
for (cSlotAreas::iterator itr = m_SlotAreas.begin(), end = m_SlotAreas.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
delete *itr;
|
|
|
|
}
|
|
|
|
m_SlotAreas.clear();
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
ASSERT(m_OpenedBy.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-19 03:39:18 -04:00
|
|
|
void cLuaWindow::SetLuaRef(cPluginLua * a_Plugin, int a_LuaRef)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// Either m_Plugin is not set or equal to the passed plugin; only one plugin can use one cLuaWindow object
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT((m_Plugin == nullptr) || (m_Plugin == a_Plugin));
|
2013-07-29 07:13:03 -04:00
|
|
|
ASSERT(m_LuaRef == LUA_REFNIL);
|
|
|
|
m_Plugin = a_Plugin;
|
|
|
|
m_LuaRef = a_LuaRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cLuaWindow::IsLuaReferenced(void) const
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
return ((m_Plugin != nullptr) && (m_LuaRef != LUA_REFNIL));
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-19 03:39:18 -04:00
|
|
|
void cLuaWindow::SetOnClosing(cPluginLua * a_Plugin, int a_FnRef)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// Either m_Plugin is not set or equal to the passed plugin; only one plugin can use one cLuaWindow object
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT((m_Plugin == nullptr) || (m_Plugin == a_Plugin));
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
// If there already was a function, unreference it first
|
|
|
|
if (m_OnClosingFnRef != LUA_REFNIL)
|
|
|
|
{
|
|
|
|
m_Plugin->Unreference(m_OnClosingFnRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the new reference
|
|
|
|
m_Plugin = a_Plugin;
|
|
|
|
m_OnClosingFnRef = a_FnRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-19 03:39:18 -04:00
|
|
|
void cLuaWindow::SetOnSlotChanged(cPluginLua * a_Plugin, int a_FnRef)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// Either m_Plugin is not set or equal to the passed plugin; only one plugin can use one cLuaWindow object
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT((m_Plugin == nullptr) || (m_Plugin == a_Plugin));
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
// If there already was a function, unreference it first
|
|
|
|
if (m_OnSlotChangedFnRef != LUA_REFNIL)
|
|
|
|
{
|
|
|
|
m_Plugin->Unreference(m_OnSlotChangedFnRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the new reference
|
|
|
|
m_Plugin = a_Plugin;
|
|
|
|
m_OnSlotChangedFnRef = a_FnRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cLuaWindow::ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse)
|
|
|
|
{
|
|
|
|
// First notify the plugin through the registered callback:
|
|
|
|
if (m_OnClosingFnRef != LUA_REFNIL)
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT(m_Plugin != nullptr);
|
2013-07-29 07:13:03 -04:00
|
|
|
if (m_Plugin->CallbackWindowClosing(m_OnClosingFnRef, *this, a_Player, a_CanRefuse))
|
|
|
|
{
|
|
|
|
// The callback disagrees (the higher levels check the CanRefuse flag compliance)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super::ClosedByPlayer(a_Player, a_CanRefuse);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cLuaWindow::Destroy(void)
|
|
|
|
{
|
|
|
|
super::Destroy();
|
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
if ((m_LuaRef != LUA_REFNIL) && (m_Plugin != nullptr))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// The object is referenced by Lua, un-reference it
|
|
|
|
m_Plugin->Unreference(m_LuaRef);
|
|
|
|
}
|
|
|
|
|
2015-05-09 05:16:56 -04:00
|
|
|
// Lua will take care of this object, it will garbage-collect it, so we must not delete it!
|
2013-07-29 07:13:03 -04:00
|
|
|
m_IsDestroyed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-09 05:16:56 -04:00
|
|
|
void cLuaWindow::DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply)
|
2014-12-17 13:14:01 -05:00
|
|
|
{
|
|
|
|
cSlotAreas Areas;
|
2015-05-09 05:16:56 -04:00
|
|
|
for (auto && Area : m_SlotAreas)
|
2014-12-17 13:14:01 -05:00
|
|
|
{
|
|
|
|
if (Area != a_ClickedArea)
|
|
|
|
{
|
|
|
|
Areas.push_back(Area);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super::DistributeStackToAreas(a_ItemStack, a_Player, Areas, a_ShouldApply, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
void cLuaWindow::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum)
|
|
|
|
{
|
|
|
|
if (a_ItemGrid != &m_Contents)
|
|
|
|
{
|
|
|
|
ASSERT(!"Invalid ItemGrid in callback");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an OnSlotChanged callback has been registered, call it:
|
|
|
|
if (m_OnSlotChangedFnRef != LUA_REFNIL)
|
|
|
|
{
|
|
|
|
m_Plugin->CallbackWindowSlotChanged(m_OnSlotChangedFnRef, *this, a_SlotNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|