almost finish CKGroup

This commit is contained in:
2023-09-01 12:19:06 +08:00
parent 3755a2e148
commit fd69914a25
15 changed files with 310 additions and 88 deletions

View File

@ -22,4 +22,9 @@ namespace LibCmo::CK2::ObjImpls {
return false;
}
void CKBeObject::CKGroup_SetGroups(CKDWORD pos, bool val) {
if (pos >= m_Groups.size()) m_Groups.resize(pos + 1);
m_Groups[pos] = val;
}
}

View File

@ -22,6 +22,7 @@ namespace LibCmo::CK2::ObjImpls {
//virtual void PostLoad() override;
bool IsInGroup(CKGroup* group);
void CKGroup_SetGroups(CKDWORD pos, bool val);
protected:
XContainer::XBitArray m_Groups;

View File

@ -1,27 +1,92 @@
#include "CKSceneObject.hpp"
#include "../CKStateChunk.hpp"
#include "CKGroup.hpp"
#include "../CKStateChunk.hpp"
#include "../CKContext.hpp"
#include <algorithm>
namespace LibCmo::CK2::ObjImpls {
CKGroup::CKGroup(CKContext* ctx, CK_ID ckid, CKSTRING name) :
CKBeObject(ctx, ckid, name),
m_ObjectArray(),
m_GroupIndex(m_Context->AllocateGroupGlobalIndex()) {}
CKGroup::~CKGroup() {
m_Context->FreeGroupGlobalIndex(m_GroupIndex);
}
bool CKGroup::Save(CKStateChunk* chunk, CKFileVisitor* file, CKDWORD flags) {
return false;
bool suc = CKBeObject::Save(chunk, file, flags);
if (!suc) return false;
return true;
}
bool CKGroup::Load(CKStateChunk* chunk, CKFileVisitor* file) {
return false;
bool suc = CKBeObject::Load(chunk, file);
if (!suc) return false;
return true;
}
CKDWORD CKGroup::GetGroupIndex() {
return m_GroupIndex;
}
CKObject* CKGroup::GetObject(CKDWORD pos) {
return nullptr;
CKERROR CKGroup::AddObject(CKBeObject* o) {
if (o == nullptr || o == this || !CKIsChildClassOf(o->GetClassID(), CK_CLASSID::CKCID_BEOBJECT)) {
return CKERROR::CKERR_INVALIDPARAMETER;
}
if (o->IsInGroup(this)) {
return CKERROR::CKERR_ALREADYPRESENT;
}
// set object
o->CKGroup_SetGroups(m_GroupIndex, true);
// set self
m_ObjectArray.emplace_back(o);
return CKERROR::CKERR_OK;
}
CKBeObject* CKGroup::RemoveObject(CKDWORD pos) {
// check pos
if (pos >= m_ObjectArray.size()) return nullptr;
auto it = m_ObjectArray.begin() + pos;
CKBeObject* obj = static_cast<CKBeObject*>(*it);
// set object
obj->CKGroup_SetGroups(m_GroupIndex, false);
// remove self
m_ObjectArray.erase(it);
return obj;
}
void CKGroup::RemoveObject(CKBeObject* obj) {
// find first
auto finder = std::find(m_ObjectArray.begin(), m_ObjectArray.end(), static_cast<CKObject*>(obj));
if (finder != m_ObjectArray.end()) {
// set object
static_cast<CKBeObject*>(*finder)->CKGroup_SetGroups(m_GroupIndex, false);
// remove self
m_ObjectArray.erase(finder);
}
}
void CKGroup::Clear() {
for (auto& beobj : m_ObjectArray) {
// set object
static_cast<CKBeObject*>(beobj)->CKGroup_SetGroups(m_GroupIndex, false);
}
m_ObjectArray.clear();
}
CKBeObject* CKGroup::GetObject(CKDWORD pos) {
if (pos >= m_ObjectArray.size()) return nullptr;
else return static_cast<CKBeObject*>(m_ObjectArray[pos]);
}
CKDWORD CKGroup::GetObjectCount() {
return CKDWORD();
return static_cast<CKDWORD>(m_ObjectArray.size());
}
}

View File

@ -7,14 +7,8 @@ namespace LibCmo::CK2::ObjImpls {
class CKGroup : public CKBeObject {
public:
CKGroup(CKContext* ctx, CK_ID ckid, CKSTRING name) :
CKBeObject(ctx, ckid, name),
m_ObjectArray(),
m_GroupIndex() // todo: allocate group id
{}
virtual ~CKGroup() {
// todo: free allocated group id
}
CKGroup(CKContext* ctx, CK_ID ckid, CKSTRING name);
virtual ~CKGroup();
LIBCMO_DISABLE_COPY_MOVE(CKGroup);
virtual CK_CLASSID GetClassID(void) override {
@ -29,20 +23,14 @@ namespace LibCmo::CK2::ObjImpls {
// ===== Insert =====
CKERROR AddObject(CKBeObject *o);
CKERROR AddObjectFront(CKBeObject *o);
CKERROR InsertObjectAt(CKBeObject *o, CKDWORD pos);
// ===== Remove =====
CKBeObject* RemoveObject(CKDWORD pos);
void RemoveObject(CKBeObject *obj);
void Clear();
// ===== Order =====
void MoveObjectUp(CKBeObject *o);
void MoveObjectDown(CKBeObject *o);
// ===== Access =====
CKObject* GetObject(CKDWORD pos);
CKBeObject* GetObject(CKDWORD pos);
CKDWORD GetObjectCount();
protected: