2013-05-30 15:34:09 -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"
|
|
|
|
#include "UI/SlotArea.h"
|
|
|
|
#include "Plugin_NewLua.h"
|
2013-05-30 16:40:43 -04:00
|
|
|
#include "Player.h"
|
2013-05-30 15:34:09 -04:00
|
|
|
#include "lauxlib.h" // Needed for LUA_REFNIL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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),
|
|
|
|
m_Plugin(NULL),
|
2013-05-30 16:40:43 -04:00
|
|
|
m_LuaRef(LUA_REFNIL),
|
|
|
|
m_OnClosingFnRef(LUA_REFNIL),
|
|
|
|
m_OnSlotChangedFnRef(LUA_REFNIL)
|
2013-05-30 15:34:09 -04:00
|
|
|
{
|
2013-05-31 03:16:14 -04:00
|
|
|
m_Contents.AddListener(*this);
|
2013-05-30 15:34:09 -04:00
|
|
|
m_SlotAreas.push_back(new cSlotAreaItemGrid(m_Contents, *this));
|
|
|
|
|
|
|
|
// If appropriate, add an Armor slot area:
|
|
|
|
switch (a_WindowType)
|
|
|
|
{
|
|
|
|
case cWindow::Inventory:
|
|
|
|
case cWindow::Workbench:
|
|
|
|
{
|
|
|
|
m_SlotAreas.push_back(new cSlotAreaArmor(*this));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_SlotAreas.push_back(new cSlotAreaInventory(*this));
|
|
|
|
m_SlotAreas.push_back(new cSlotAreaHotBar(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cLuaWindow::~cLuaWindow()
|
|
|
|
{
|
|
|
|
ASSERT(m_OpenedBy.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cLuaWindow::SetLuaRef(cPlugin_NewLua * a_Plugin, int a_LuaRef)
|
|
|
|
{
|
2013-05-30 16:40:43 -04:00
|
|
|
// Either m_Plugin is not set or equal to the passed plugin; only one plugin can use one cLuaWindow object
|
|
|
|
ASSERT((m_Plugin == NULL) || (m_Plugin == a_Plugin));
|
2013-05-30 15:34:09 -04:00
|
|
|
ASSERT(m_LuaRef == LUA_REFNIL);
|
|
|
|
m_Plugin = a_Plugin;
|
|
|
|
m_LuaRef = a_LuaRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cLuaWindow::IsLuaReferenced(void) const
|
|
|
|
{
|
|
|
|
return ((m_Plugin != NULL) && (m_LuaRef != LUA_REFNIL));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-30 16:40:43 -04:00
|
|
|
void cLuaWindow::SetOnClosing(cPlugin_NewLua * a_Plugin, int a_FnRef)
|
|
|
|
{
|
|
|
|
// Either m_Plugin is not set or equal to the passed plugin; only one plugin can use one cLuaWindow object
|
|
|
|
ASSERT((m_Plugin == NULL) || (m_Plugin == a_Plugin));
|
|
|
|
|
|
|
|
// 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-05-31 03:16:14 -04:00
|
|
|
void cLuaWindow::SetOnSlotChanged(cPlugin_NewLua * a_Plugin, int a_FnRef)
|
|
|
|
{
|
|
|
|
// Either m_Plugin is not set or equal to the passed plugin; only one plugin can use one cLuaWindow object
|
|
|
|
ASSERT((m_Plugin == NULL) || (m_Plugin == a_Plugin));
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-02 17:59:25 -04:00
|
|
|
bool cLuaWindow::ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse)
|
2013-05-30 16:40:43 -04:00
|
|
|
{
|
|
|
|
// First notify the plugin through the registered callback:
|
|
|
|
if (m_OnClosingFnRef != LUA_REFNIL)
|
|
|
|
{
|
|
|
|
ASSERT(m_Plugin != NULL);
|
2013-06-02 17:59:25 -04:00
|
|
|
if (m_Plugin->CallbackWindowClosing(m_OnClosingFnRef, *this, a_Player, a_CanRefuse))
|
2013-05-30 16:40:43 -04:00
|
|
|
{
|
2013-06-02 17:59:25 -04:00
|
|
|
// The callback disagrees (the higher levels check the CanRefuse flag compliance)
|
2013-05-30 16:40:43 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-02 17:59:25 -04:00
|
|
|
return super::ClosedByPlayer(a_Player, a_CanRefuse);
|
2013-05-30 16:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-30 15:34:09 -04:00
|
|
|
void cLuaWindow::Destroy(void)
|
|
|
|
{
|
|
|
|
super::Destroy();
|
|
|
|
|
|
|
|
if ((m_LuaRef != LUA_REFNIL) && (m_Plugin != NULL))
|
|
|
|
{
|
|
|
|
// The object is referenced by Lua, un-reference it
|
|
|
|
m_Plugin->Unreference(m_LuaRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua will take care of this object, it will garbage-collect it, so we *must not* delete it!
|
|
|
|
m_IsDestroyed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-31 03:16:14 -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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|