2015-11-11 07:57:18 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-09-30 02:56:11 +02:00
|
|
|
#include "Entity/Keybind.hpp"
|
2015-11-01 00:31:46 +01:00
|
|
|
#include "Core.hpp"
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
2015-11-01 00:31:46 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
const Int32 CKeybind::Max = SQMOD_KEYBIND_POOL;
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
SQInteger CKeybind::Typename(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
static SQChar name[] = _SC("SqKeybind");
|
|
|
|
sq_pushstring(vm, name, sizeof(name));
|
|
|
|
return 1;
|
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CKeybind::CKeybind(Int32 id)
|
|
|
|
: m_ID(VALID_ENTITYGETEX(id, SQMOD_KEYBIND_POOL))
|
2016-03-10 04:57:13 +01:00
|
|
|
, m_Tag(ToStrF("%d", id))
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
CKeybind::~CKeybind()
|
2015-10-29 21:09:53 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
/* ... */
|
2015-10-29 21:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 CKeybind::Cmp(const CKeybind & o) const
|
2015-10-29 21:09:53 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
if (m_ID == o.m_ID)
|
|
|
|
return 0;
|
|
|
|
else if (m_ID > o.m_ID)
|
|
|
|
return 1;
|
2015-10-29 21:09:53 +01:00
|
|
|
else
|
2016-02-20 23:25:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-29 21:09:53 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
const String & CKeybind::ToString() const
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
return m_Tag;
|
2015-10-29 21:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
const String & CKeybind::GetTag() const
|
2015-10-29 21:09:53 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
return m_Tag;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
void CKeybind::SetTag(CSStr tag)
|
|
|
|
{
|
|
|
|
m_Tag.assign(tag);
|
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
Object & CKeybind::GetData()
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the managed identifier
|
|
|
|
Validate();
|
|
|
|
// Return the requested information
|
|
|
|
return m_Data;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-10-29 21:09:53 +01:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
void CKeybind::SetData(Object & data)
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the managed identifier
|
|
|
|
Validate();
|
|
|
|
// Apply the specified value
|
|
|
|
m_Data = data;
|
2015-10-29 21:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
bool CKeybind::Destroy(Int32 header, Object & payload)
|
2015-10-29 21:09:53 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the managed identifier
|
|
|
|
Validate();
|
|
|
|
// Perform the requested operation
|
2016-02-20 23:25:00 +01:00
|
|
|
return _Core->DelKeybind(m_ID, header, payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
void CKeybind::BindEvent(Int32 evid, Object & env, Function & func) const
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the managed identifier
|
|
|
|
Validate();
|
|
|
|
// Obtain the function instance called for this event
|
2016-02-20 23:25:00 +01:00
|
|
|
Function & event = _Core->GetKeybindEvent(m_ID, evid);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Is the specified callback function null?
|
2016-02-20 23:25:00 +01:00
|
|
|
if (func.IsNull())
|
2016-03-12 21:51:44 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
event.Release(); // Then release the current callback
|
2016-03-12 21:51:44 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Assign the specified environment and function
|
2015-10-29 21:09:53 +01:00
|
|
|
else
|
2016-03-12 21:51:44 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
event = Function(env.GetVM(), env, func.GetFunc());
|
2016-03-12 21:51:44 +01:00
|
|
|
}
|
2015-10-29 21:09:53 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 00:31:46 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
Int32 CKeybind::GetFirst() const
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the managed identifier
|
|
|
|
Validate();
|
|
|
|
// Return the requested information
|
|
|
|
return _Core->GetKeybind(m_ID).mFirst;
|
2015-11-01 00:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 CKeybind::GetSecond() const
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the managed identifier
|
|
|
|
Validate();
|
|
|
|
// Return the requested information
|
|
|
|
return _Core->GetKeybind(m_ID).mSecond;
|
2015-11-01 00:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 CKeybind::GetThird() const
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the managed identifier
|
|
|
|
Validate();
|
|
|
|
// Return the requested information
|
|
|
|
return _Core->GetKeybind(m_ID).mThird;
|
2015-11-01 00:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
bool CKeybind::IsRelease() const
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the managed identifier
|
|
|
|
Validate();
|
|
|
|
// Return the requested information
|
|
|
|
return _Core->GetKeybind(m_ID).mRelease;
|
2015-11-01 00:31:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
static Object & Keybind_CreateEx(Int32 slot, bool release, Int32 primary, Int32 secondary,
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 alternative)
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
return _Core->NewKeybind(slot, release, primary, secondary, alternative,
|
|
|
|
SQMOD_CREATE_DEFAULT, NullObject());
|
2015-11-01 00:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
static Object & Keybind_CreateEx(Int32 slot, bool release, Int32 primary, Int32 secondary,
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 alternative, Int32 header, Object & payload)
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
return _Core->NewKeybind(slot, release, primary, secondary, alternative, header, payload);
|
2015-11-01 00:31:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 04:57:13 +01:00
|
|
|
static Object & Keybind_Create(bool release, Int32 primary, Int32 secondary, Int32 alternative)
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
return _Core->NewKeybind(-1, release, primary, secondary, alternative,
|
|
|
|
SQMOD_CREATE_DEFAULT, NullObject());
|
2015-11-01 00:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
static Object & Keybind_Create(bool release, Int32 primary, Int32 secondary, Int32 alternative,
|
2016-02-20 23:25:00 +01:00
|
|
|
Int32 header, Object & payload)
|
2015-11-01 00:31:46 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
return _Core->NewKeybind(-1, release, primary, secondary, alternative, header, payload);
|
2015-11-01 00:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 05:18:39 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static const Object & Keybind_FindByID(Int32 id)
|
|
|
|
{
|
|
|
|
// Perform a range check on the specified identifier
|
|
|
|
if (INVALID_ENTITYEX(id, SQMOD_KEYBIND_POOL))
|
2016-03-12 21:51:44 +01:00
|
|
|
{
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("The specified keybind identifier is invalid: %d", id);
|
2016-03-12 21:51:44 +01:00
|
|
|
}
|
2016-03-10 05:18:39 +01:00
|
|
|
// Obtain the ends of the entity pool
|
|
|
|
Core::Keybinds::const_iterator itr = _Core->GetKeybinds().cbegin();
|
|
|
|
Core::Keybinds::const_iterator end = _Core->GetKeybinds().cend();
|
|
|
|
// Process each entity in the pool
|
|
|
|
for (; itr != end; ++itr)
|
|
|
|
{
|
|
|
|
// Does the identifier match the specified one?
|
|
|
|
if (itr->mID == id)
|
2016-03-12 21:51:44 +01:00
|
|
|
{
|
2016-03-10 05:18:39 +01:00
|
|
|
return itr->mObj; // Stop searching and return this entity
|
2016-03-12 21:51:44 +01:00
|
|
|
}
|
2016-03-10 05:18:39 +01:00
|
|
|
}
|
|
|
|
// Unable to locate a keybind matching the specified identifier
|
|
|
|
return NullObject();
|
|
|
|
}
|
|
|
|
|
2016-03-26 17:16:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 05:18:39 +01:00
|
|
|
static const Object & Keybind_FindByTag(CSStr tag)
|
|
|
|
{
|
|
|
|
// Perform a validity check on the specified tag
|
2016-03-12 21:51:44 +01:00
|
|
|
if (!tag || *tag == '\0')
|
|
|
|
{
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("The specified keybind tag is invalid: null/empty");
|
2016-03-12 21:51:44 +01:00
|
|
|
}
|
2016-03-10 05:18:39 +01:00
|
|
|
// Obtain the ends of the entity pool
|
|
|
|
Core::Keybinds::const_iterator itr = _Core->GetKeybinds().cbegin();
|
|
|
|
Core::Keybinds::const_iterator end = _Core->GetKeybinds().cend();
|
|
|
|
// Process each entity in the pool
|
|
|
|
for (; itr != end; ++itr)
|
|
|
|
{
|
|
|
|
// Does this entity even exist and does the tag match the specified one?
|
|
|
|
if (itr->mInst != nullptr && itr->mInst->GetTag().compare(tag) == 0)
|
2016-03-12 21:51:44 +01:00
|
|
|
{
|
2016-03-10 05:18:39 +01:00
|
|
|
return itr->mObj; // Stop searching and return this entity
|
2016-03-12 21:51:44 +01:00
|
|
|
}
|
2016-03-10 05:18:39 +01:00
|
|
|
}
|
|
|
|
// Unable to locate a keybind matching the specified tag
|
|
|
|
return NullObject();
|
|
|
|
}
|
|
|
|
|
2016-03-26 17:16:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static Array Keybind_FindActive()
|
|
|
|
{
|
|
|
|
// Remember the initial stack size
|
|
|
|
StackGuard sg;
|
|
|
|
// Obtain the ends of the entity pool
|
|
|
|
Core::Keybinds::const_iterator itr = _Core->GetKeybinds().cbegin();
|
|
|
|
Core::Keybinds::const_iterator end = _Core->GetKeybinds().cend();
|
|
|
|
// Allocate an empty array on the stack
|
|
|
|
sq_newarray(DefaultVM::Get(), 0);
|
|
|
|
// Process each entity in the pool
|
|
|
|
for (; itr != end; ++itr)
|
|
|
|
{
|
|
|
|
// Is this entity instance active?
|
|
|
|
if (VALID_ENTITY(itr->mID))
|
|
|
|
{
|
|
|
|
// Push the script object on the stack
|
|
|
|
sq_pushobject(DefaultVM::Get(), (HSQOBJECT &)((*itr).mObj));
|
|
|
|
// Append the object at the back of the array
|
|
|
|
if (SQ_FAILED(sq_arrayappend(DefaultVM::Get(), -1)))
|
|
|
|
{
|
|
|
|
STHROWF("Unable to append entity instance to the list");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Return the array at the top of the stack
|
|
|
|
return Var< Array >(DefaultVM::Get(), -1).value;
|
|
|
|
}
|
|
|
|
|
2015-10-29 21:09:53 +01:00
|
|
|
// ================================================================================================
|
2016-02-20 23:25:00 +01:00
|
|
|
void Register_CKeybind(HSQUIRRELVM vm)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
RootTable(vm).Bind(_SC("SqKeybind"),
|
|
|
|
Class< CKeybind, NoConstructor< CKeybind > >(vm, _SC("SqKeybind"))
|
2016-03-10 04:57:13 +01:00
|
|
|
// Metamethods
|
2016-02-20 23:25:00 +01:00
|
|
|
.Func(_SC("_cmp"), &CKeybind::Cmp)
|
2016-03-10 04:57:13 +01:00
|
|
|
.SquirrelFunc(_SC("_typename"), &CKeybind::Typename)
|
2016-02-20 23:25:00 +01:00
|
|
|
.Func(_SC("_tostring"), &CKeybind::ToString)
|
2016-03-10 05:18:39 +01:00
|
|
|
// Static Values
|
2016-03-10 04:57:13 +01:00
|
|
|
.SetStaticValue(_SC("MaxID"), CKeybind::Max)
|
|
|
|
// Core Properties
|
2016-02-20 23:25:00 +01:00
|
|
|
.Prop(_SC("ID"), &CKeybind::GetID)
|
|
|
|
.Prop(_SC("Tag"), &CKeybind::GetTag, &CKeybind::SetTag)
|
|
|
|
.Prop(_SC("Data"), &CKeybind::GetData, &CKeybind::SetData)
|
|
|
|
.Prop(_SC("Active"), &CKeybind::IsActive)
|
2016-03-10 05:18:39 +01:00
|
|
|
// Core Methods
|
2016-02-20 23:25:00 +01:00
|
|
|
.Func(_SC("Bind"), &CKeybind::BindEvent)
|
2016-03-10 04:57:13 +01:00
|
|
|
// Core Overloads
|
2016-02-20 23:25:00 +01:00
|
|
|
.Overload< bool (CKeybind::*)(void) >(_SC("Destroy"), &CKeybind::Destroy)
|
|
|
|
.Overload< bool (CKeybind::*)(Int32) >(_SC("Destroy"), &CKeybind::Destroy)
|
|
|
|
.Overload< bool (CKeybind::*)(Int32, Object &) >(_SC("Destroy"), &CKeybind::Destroy)
|
2016-03-10 04:57:13 +01:00
|
|
|
// Properties
|
|
|
|
.Prop(_SC("First"), &CKeybind::GetFirst)
|
|
|
|
.Prop(_SC("Second"), &CKeybind::GetSecond)
|
|
|
|
.Prop(_SC("Third"), &CKeybind::GetThird)
|
2016-02-20 23:25:00 +01:00
|
|
|
.Prop(_SC("Release"), &CKeybind::IsRelease)
|
2016-03-10 05:18:39 +01:00
|
|
|
// Static Functions
|
|
|
|
.StaticFunc(_SC("FindByID"), &Keybind_FindByID)
|
|
|
|
.StaticFunc(_SC("FindByTag"), &Keybind_FindByTag)
|
2016-03-26 17:16:50 +01:00
|
|
|
.StaticFunc(_SC("FindActive"), &Keybind_FindActive)
|
2016-03-10 04:57:13 +01:00
|
|
|
// Static Overloads
|
|
|
|
.StaticOverload< Object & (*)(Int32, bool, Int32, Int32, Int32) >
|
|
|
|
(_SC("CreateEx"), &Keybind_CreateEx)
|
|
|
|
.StaticOverload< Object & (*)(Int32, bool, Int32, Int32, Int32, Int32, Object &) >
|
|
|
|
(_SC("CreateEx"), &Keybind_CreateEx)
|
|
|
|
.StaticOverload< Object & (*)(bool, Int32, Int32, Int32) >
|
|
|
|
(_SC("Create"), &Keybind_Create)
|
|
|
|
.StaticOverload< Object & (*)(bool, Int32, Int32, Int32, Int32, Object &) >
|
|
|
|
(_SC("Create"), &Keybind_Create)
|
2015-09-30 02:56:11 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
} // Namespace:: SqMod
|