From 8b12c84df77b5a50b608701a0110fb447f7e2e2a Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Wed, 30 Mar 2005 05:43:22 +0000 Subject: [PATCH] ScEd: allowed creation of non-entity objects, pending total entitisation This was SVN commit r2085. --- source/graphics/ObjectManager.cpp | 88 +++++++++++++++++++++++- source/graphics/ObjectManager.h | 20 +++++- source/ps/CStr.cpp | 22 ++++++ source/ps/CStr.h | 8 +++ source/tools/sced/PaintObjectCommand.cpp | 25 ++----- source/tools/sced/PaintObjectCommand.h | 5 +- source/tools/sced/PaintObjectTool.cpp | 8 +-- source/tools/sced/ui/UnitToolsDlgBar.cpp | 65 +++++++++-------- source/tools/sced/ui/UnitToolsDlgBar.h | 2 + 9 files changed, 181 insertions(+), 62 deletions(-) diff --git a/source/graphics/ObjectManager.cpp b/source/graphics/ObjectManager.cpp index eb4116b4f0..a33c9feb50 100755 --- a/source/graphics/ObjectManager.cpp +++ b/source/graphics/ObjectManager.cpp @@ -8,6 +8,13 @@ #include "VFSUtil.h" #include "ObjectBase.h" #include "ObjectEntry.h" +#include "Entity.h" +#include "EntityManager.h" +#include "BaseEntity.h" +#include "Game.h" +#include "Model.h" +#include "Unit.h" +#include "Matrix3D.h" #define LOG_CATEGORY "graphics" @@ -21,7 +28,7 @@ bool operator< (const CObjectManager::ObjectKey& a, const CObjectManager::Object return a.ActorVariation < b.ActorVariation; } -CObjectManager::CObjectManager() : m_SelectedEntity(NULL) +CObjectManager::CObjectManager() : m_SelectedThing(NULL) { m_ObjectTypes.reserve(32); } @@ -152,3 +159,82 @@ void CObjectManager::LoadObjects() { AddObjectType(""); } + + +static void GetObjectThunk(const char* path, const vfsDirEnt* ent, void* context) +{ + std::vector* names = (std::vector*)context; + CStr name (path); + names->push_back(name.AfterFirst("actors/")); +} +void CObjectManager::GetAllObjectNames(std::vector& names) +{ + VFSUtil::EnumDirEnts("art/actors/", "*.xml", true, GetObjectThunk, &names); +} + + +struct CObjectThing_Entity : public CObjectThing +{ + CObjectThing_Entity(CBaseEntity* b) : base(b) {} + ~CObjectThing_Entity() {} + CBaseEntity* base; + CEntity* ent; + void Create(CMatrix3D& transform, int playerID) + { + CVector3D orient = transform.GetIn(); + CVector3D position = transform.GetTranslation(); + ent = g_EntityManager.create(base, position, atan2(-orient.X, -orient.Z)); + ent->SetPlayer(g_Game->GetPlayer(playerID)); + } + void SetTransform(CMatrix3D& transform) + { + CVector3D orient = transform.GetIn(); + CVector3D position = transform.GetTranslation(); + + // This is quite yucky, but nothing else seems to actually work: + ent->m_position = + ent->m_position_previous = + ent->m_graphics_position = position; + ent->teleport(); + ent->m_orientation = + ent->m_orientation_previous = + ent->m_graphics_orientation = atan2(-orient.X, -orient.Z); + ent->reorient(); + } + CObjectEntry* GetObjectEntry() + { + return g_ObjMan.FindObject((CStr)base->m_actorName); + } +}; +struct CObjectThing_Object : public CObjectThing +{ + CObjectThing_Object(CObjectEntry* o) : obj(o) {} + ~CObjectThing_Object() {} + CObjectEntry* obj; + CUnit* unit; + void Create(CMatrix3D& transform, int playerID) + { + unit = new CUnit(obj, obj->m_Model->Clone()); + unit->GetModel()->SetTransform(transform); + g_UnitMan.AddUnit(unit); + } + void SetTransform(CMatrix3D& transform) + { + unit->GetModel()->SetTransform(transform); + } + CObjectEntry* GetObjectEntry() + { + return obj; + } +}; + +void CObjectManager::SetSelectedEntity(CBaseEntity* thing) +{ + delete m_SelectedThing; + m_SelectedThing = (thing ? new CObjectThing_Entity(thing) : NULL); +} +void CObjectManager::SetSelectedObject(CObjectEntry* thing) +{ + delete m_SelectedThing; + m_SelectedThing = (thing ? new CObjectThing_Object(thing) : NULL); +} diff --git a/source/graphics/ObjectManager.h b/source/graphics/ObjectManager.h index 567138d306..514ffb5598 100755 --- a/source/graphics/ObjectManager.h +++ b/source/graphics/ObjectManager.h @@ -10,10 +10,21 @@ class CObjectBase; class CObjectEntry; class CBaseEntity; +class CMatrix3D; // access to sole CObjectManager object #define g_ObjMan CObjectManager::GetSingleton() +// Slight hack, to allow ScEd to place either entities or objects +class CObjectThing +{ +public: + virtual ~CObjectThing() {} + virtual void Create(CMatrix3D& transform, int playerID)=0; + virtual void SetTransform(CMatrix3D& transform)=0; + virtual CObjectEntry* GetObjectEntry()=0; +}; + /////////////////////////////////////////////////////////////////////////////////////////// // CObjectManager: manager class for all possible actor types class CObjectManager : public Singleton @@ -41,6 +52,8 @@ public: }; public: + CObjectThing* m_SelectedThing; + // constructor, destructor CObjectManager(); ~CObjectManager(); @@ -55,7 +68,12 @@ public: CObjectBase* FindObjectBase(const char* objname); - CBaseEntity* m_SelectedEntity; + // Get all names, quite slowly. (Intended only for ScEd.) + void GetAllObjectNames(std::vector& names); + + //CBaseEntity* m_SelectedEntity; + void SetSelectedEntity(CBaseEntity* thing); + void SetSelectedObject(CObjectEntry* thing); std::vector m_ObjectTypes; }; diff --git a/source/ps/CStr.cpp b/source/ps/CStr.cpp index e0d4c99060..1e664453d1 100755 --- a/source/ps/CStr.cpp +++ b/source/ps/CStr.cpp @@ -245,6 +245,28 @@ CStr CStr::BeforeLast(const CStr& Str) const return substr(0, pos); } +// Retrieve the substring following the first occurrence of Str +// (or the whole string if it doesn't contain Str) +CStr CStr::AfterFirst(const CStr& Str) const +{ + long pos = Find(Str); + if (pos == -1) + return *this; + else + return substr(pos + Str.length()); +} + +// Retrieve the substring preceding the first occurrence of Str +// (or the whole string if it doesn't contain Str) +CStr CStr::BeforeFirst(const CStr& Str) const +{ + long pos = Find(Str); + if (pos == -1) + return *this; + else + return substr(0, pos); +} + // Remove all occurrences of some character or substring void CStr::Remove(const CStr& Str) { diff --git a/source/ps/CStr.h b/source/ps/CStr.h index cfb66f67c1..c20b977a00 100755 --- a/source/ps/CStr.h +++ b/source/ps/CStr.h @@ -184,6 +184,14 @@ public: // (or the whole string if it doesn't contain Str) CStr BeforeLast(const CStr& Str) const; + // Retrieve the substring following the first occurrence of Str + // (or the whole string if it doesn't contain Str) + CStr AfterFirst(const CStr& Str) const; + + // Retrieve the substring preceding the first occurrence of Str + // (or the whole string if it doesn't contain Str) + CStr BeforeFirst(const CStr& Str) const; + // Remove all occurrences of some character or substring void Remove(const CStr& Str); diff --git a/source/tools/sced/PaintObjectCommand.cpp b/source/tools/sced/PaintObjectCommand.cpp index 20f7cdcc44..6f11d5a142 100755 --- a/source/tools/sced/PaintObjectCommand.cpp +++ b/source/tools/sced/PaintObjectCommand.cpp @@ -10,9 +10,10 @@ #include "BaseEntity.h" #include "BaseEntityCollection.h" #include "EntityManager.h" +#include "ObjectManager.h" -CPaintObjectCommand::CPaintObjectCommand(CBaseEntity* object,const CMatrix3D& transform) - : m_BaseEntity(object), m_Transform(transform), m_Entity() +CPaintObjectCommand::CPaintObjectCommand(CObjectThing* object,const CMatrix3D& transform) + : m_Thing(object), m_Transform(transform), m_Entity() { } @@ -23,28 +24,12 @@ CPaintObjectCommand::~CPaintObjectCommand() void CPaintObjectCommand::Execute() { - CVector3D orient = m_Transform.GetIn(); - CVector3D position = m_Transform.GetTranslation(); - m_Entity = g_EntityManager.create(m_BaseEntity, position, atan2(-orient.X, -orient.Z)); - m_Entity->SetPlayer(g_Game->GetPlayer(1)); + m_Thing->Create(m_Transform, 1); } void CPaintObjectCommand::UpdateTransform(CMatrix3D& transform) { - CVector3D orient = transform.GetIn(); - CVector3D position = transform.GetTranslation(); - - // This is quite yucky, but nothing else seems to actually work: - - m_Entity->m_position = - m_Entity->m_position_previous = - m_Entity->m_graphics_position = position; - m_Entity->teleport(); - - m_Entity->m_orientation = - m_Entity->m_orientation_previous = - m_Entity->m_graphics_orientation = atan2(-orient.X, -orient.Z); - m_Entity->reorient(); + m_Thing->SetTransform(transform); } ////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/source/tools/sced/PaintObjectCommand.h b/source/tools/sced/PaintObjectCommand.h index 2558ef46fe..c185a51a28 100755 --- a/source/tools/sced/PaintObjectCommand.h +++ b/source/tools/sced/PaintObjectCommand.h @@ -7,12 +7,13 @@ class CUnit; class CBaseEntity; +class CObjectThing; class CPaintObjectCommand : public CCommand { public: // constructor, destructor - CPaintObjectCommand(CBaseEntity* entity, const CMatrix3D& transform); + CPaintObjectCommand(CObjectThing* entity, const CMatrix3D& transform); ~CPaintObjectCommand(); // return the texture name of this command @@ -41,7 +42,7 @@ private: // unit to add to world HEntity m_Entity; // entity to paint - CBaseEntity* m_BaseEntity; + CObjectThing* m_Thing; // model transformation CMatrix3D m_Transform; }; diff --git a/source/tools/sced/PaintObjectTool.cpp b/source/tools/sced/PaintObjectTool.cpp index d267bd238b..7718ad671a 100755 --- a/source/tools/sced/PaintObjectTool.cpp +++ b/source/tools/sced/PaintObjectTool.cpp @@ -45,7 +45,7 @@ void CPaintObjectTool::PaintSelection() m_Position=m_SelectionPoint; m_LastTriggerTime=get_time(); - CBaseEntity* obj=g_ObjMan.m_SelectedEntity; + CObjectThing* obj = g_ObjMan.m_SelectedThing; if (obj) { // get up to date transform BuildTransform(); @@ -75,11 +75,11 @@ void CPaintObjectTool::OnDraw() // don't draw object if we're currently rotating it on the terrain if (m_PaintCmd) return; - CBaseEntity* ent=g_ObjMan.m_SelectedEntity; - if (!ent) return; + CObjectThing* thing = g_ObjMan.m_SelectedThing; + if (!thing) return; // don't draw unless we have a valid object to apply - CObjectEntry* obj = g_ObjMan.FindObject((CStr)ent->m_actorName); + CObjectEntry* obj = thing->GetObjectEntry(); if (!obj || !obj->m_Model) return; // try to get object transform, in world space diff --git a/source/tools/sced/ui/UnitToolsDlgBar.cpp b/source/tools/sced/ui/UnitToolsDlgBar.cpp index 00b234507a..fe7f79d7e9 100755 --- a/source/tools/sced/ui/UnitToolsDlgBar.cpp +++ b/source/tools/sced/ui/UnitToolsDlgBar.cpp @@ -71,27 +71,19 @@ BOOL CUnitToolsDlgBar::OnInitDialog() // build combo box for object types CComboBox* objecttypes=(CComboBox*) GetDlgItem(IDC_COMBO_OBJECTTYPES); - - const std::vector& types=g_ObjMan.m_ObjectTypes; - for (uint i=0;iAddString((const char*) types[i].m_Name); - } - if (types.size()>0) { - // select first type - objecttypes->SetCurSel(0); - // set initial list contents -// if (types.size()) { -// const std::vector& objects=types[0].m_Objects; -// for (uint i=0;iInsertItem(i,(const char*) objects[i]->m_Name,i); -// } -// } - std::vector names; - g_EntityTemplateCollection.getTemplateNames(names); - for (size_t i = 0; i < names.size(); ++i) - listctrl->InsertItem(i, (CStr)names[i], i); - } + objecttypes->AddString("Entities"); + objecttypes->AddString("Actors"); + + objecttypes->SetCurSel(0); + + std::vector names; + g_EntityTemplateCollection.getBaseEntityNames(names); + for (size_t i = 0; i < names.size(); ++i) + m_ObjectNames[0].push_back((CStr)names[i]); + + g_ObjMan.GetAllObjectNames(m_ObjectNames[1]); + CButton* addunit=(CButton*) GetDlgItem(IDC_BUTTON_ADDUNIT); addunit->SetBitmap(::LoadBitmap(::GetModuleHandle(0),MAKEINTRESOURCE(IDB_BITMAP_ADDUNIT))); @@ -101,6 +93,8 @@ BOOL CUnitToolsDlgBar::OnInitDialog() OnButtonSelect(); + OnSelChangeObjectTypes(); + return TRUE; } @@ -175,8 +169,11 @@ void CUnitToolsDlgBar::OnClickListObjectBrowser(NMHDR* pNMHDR, LRESULT* pResult) // deselect current selection in list ctrl, if any POSITION pos=listctrl->GetFirstSelectedItemPosition(); if (pos) { -// g_ObjMan.SetSelectedObject(g_ObjMan.m_ObjectTypes[GetCurrentObjectType()].m_Objects[(intptr_t)pos-1]); - g_ObjMan.m_SelectedEntity = g_EntityTemplateCollection.getTemplateByID((intptr_t)pos-1); + CStrW name (CStr(listctrl->GetItemText((int)(intptr_t)pos-1, 0))); + if (GetCurrentObjectType() == 0) + g_ObjMan.SetSelectedEntity(g_EntityTemplateCollection.getTemplate(name)); + else + g_ObjMan.SetSelectedObject(g_ObjMan.FindObject((CStr)name)); } // shift to add mode @@ -193,18 +190,18 @@ int CUnitToolsDlgBar::GetCurrentObjectType() void CUnitToolsDlgBar::OnSelChangeObjectTypes() { -// // clear out the listctrl -// CListCtrl* listctrl=(CListCtrl*) GetDlgItem(IDC_LIST_OBJECTBROWSER); -// listctrl->DeleteAllItems(); -// -// // add new items back to listbox -// std::vector& objects=g_ObjMan.m_ObjectTypes[GetCurrentObjectType()].m_Objects; -// for (uint i=0;iInsertItem(i,(const char*) objects[i]->m_Name,i); -// } -// -// g_ObjMan.SetSelectedObject(0); + // clear out the listctrl + CListCtrl* listctrl=(CListCtrl*) GetDlgItem(IDC_LIST_OBJECTBROWSER); + listctrl->DeleteAllItems(); + + // add new items back to listbox + std::vector& names=m_ObjectNames[GetCurrentObjectType()]; + for (uint i=0;iInsertItem(i, names[i], i); + } + + g_ObjMan.SetSelectedObject(0); } void CUnitToolsDlgBar::OnButtonSelect() diff --git a/source/tools/sced/ui/UnitToolsDlgBar.h b/source/tools/sced/ui/UnitToolsDlgBar.h index 5c2a9746f8..960483adbe 100755 --- a/source/tools/sced/ui/UnitToolsDlgBar.h +++ b/source/tools/sced/ui/UnitToolsDlgBar.h @@ -20,6 +20,8 @@ protected: BOOL OnInitDialog(); int GetCurrentObjectType(); + std::vector m_ObjectNames[2]; + // Generated message map functions //{{AFX_MSG(CUnitToolsDlgBar) afx_msg void OnButtonAdd();