DeselList refactoring

Make CRobotMain hold selected object history instead of
every object holding some part of it
master
Piotr Dziwinski 2015-07-14 22:05:12 +02:00
parent 9af1422589
commit 94ea1ff106
7 changed files with 46 additions and 94 deletions

View File

@ -1552,7 +1552,7 @@ void CPyro::ExploStart()
m_camera->SetType(CAM_TYPE_EXPLO);
m_main->DeselectAll();
}
m_object->DeleteDeselList(m_object);
m_main->RemoveFromSelectionHistory(m_object);
for (int i = 0; i < OBJECTMAXPART; i++)
{
@ -1625,7 +1625,7 @@ void CPyro::BurnStart()
m_camera->SetType(CAM_TYPE_EXPLO);
m_main->DeselectAll();
}
m_object->DeleteDeselList(m_object);
m_main->RemoveFromSelectionHistory(m_object);
for (int i = 0; i < OBJECTMAXPART; i++)
{

View File

@ -265,7 +265,6 @@ COldObject::COldObject(int id)
m_bVirusMode = false;
m_virusTime = 0.0f;
m_lastVirusParticle = 0.0f;
m_totalDesectList = 0;
m_bLock = false;
m_bIgnoreBuildCheck = false;
m_bExplo = false;
@ -347,11 +346,7 @@ void COldObject::DeleteObject(bool bAll)
{
m_camera->SetControllingObject(0);
}
for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects())
{
obj->DeleteDeselList(this);
}
m_main->RemoveFromSelectionHistory(this);
if ( !bAll )
{
@ -711,7 +706,7 @@ bool COldObject::ExplodeObject(ExplosionType type, float force, float decay)
m_camera->SetType(Gfx::CAM_TYPE_EXPLO);
m_main->DeselectAll();
}
DeleteDeselList(this);
m_main->RemoveFromSelectionHistory(this);
if ( m_botVar != 0 )
{
@ -3272,51 +3267,6 @@ bool COldObject::GetTooltipName(std::string& name)
return !name.empty();
}
// Adds the object previously selected in the list.
void COldObject::AddDeselList(CObject* pObj)
{
int i;
if ( m_totalDesectList >= OBJECTMAXDESELLIST )
{
for ( i=0 ; i<OBJECTMAXDESELLIST-1 ; i++ )
{
m_objectDeselectList[i] = m_objectDeselectList[i+1];
}
m_totalDesectList --;
}
m_objectDeselectList[m_totalDesectList++] = pObj;
}
// Removes the previously selected object in the list.
CObject* COldObject::SubDeselList()
{
if ( m_totalDesectList == 0 ) return 0;
return m_objectDeselectList[--m_totalDesectList];
}
// Removes an object reference if it is in the list.
void COldObject::DeleteDeselList(CObject* pObj)
{
int i, j;
j = 0;
for ( i=0 ; i<m_totalDesectList ; i++ )
{
if ( m_objectDeselectList[i] != pObj )
{
m_objectDeselectList[j++] = m_objectDeselectList[i];
}
}
m_totalDesectList = j;
}
Math::Vector COldObject::GetPosition() const
{
return GetPartPosition(0);

View File

@ -35,7 +35,6 @@
// The father of all parts must always be the part number zero!
const int OBJECTMAXPART = 40;
const int OBJECTMAXDESELLIST = 10;
struct ObjectPart
{
@ -279,10 +278,6 @@ public:
bool GetTooltipName(std::string& name) override;
void AddDeselList(CObject* pObj) override;
CObject* SubDeselList() override;
void DeleteDeselList(CObject* pObj) override;
bool CreateShadowCircle(float radius, float intensity, Gfx::EngineShadowType type = Gfx::ENG_SHADOW_NORM);
bool CreateShadowLight(float height, Gfx::Color color);
bool CreateEffectLight(float height, Gfx::Color color);
@ -393,9 +388,6 @@ protected:
int m_totalPart;
ObjectPart m_objectPart[OBJECTMAXPART];
int m_totalDesectList;
CObject* m_objectDeselectList[OBJECTMAXDESELLIST];
int m_partiSel[4];
float m_infoReturn;

View File

@ -443,24 +443,6 @@ bool COldObjectInterface::GetTooltipName(std::string& name)
throw std::logic_error("GetTooltipName: not implemented!");
}
void COldObjectInterface::AddDeselList(CObject* pObj)
{
throw std::logic_error("AddDeselList: not implemented!");
}
CObject* COldObjectInterface::SubDeselList()
{
throw std::logic_error("SubDeselList: not implemented!");
}
void COldObjectInterface::DeleteDeselList(CObject* pObj)
{
// TODO: temporary hack
return;
//throw std::logic_error("DeleteDeselList: not implemented!");
}
void COldObjectInterface::FlatParent()
{
throw std::logic_error("FlatParent: not implemented!");

View File

@ -189,10 +189,6 @@ public:
virtual bool GetTooltipName(std::string& name);
virtual void AddDeselList(CObject* pObj);
virtual CObject* SubDeselList();
virtual void DeleteDeselList(CObject* pObj);
virtual void FlatParent();
virtual float GetInfoReturn();

View File

@ -1167,7 +1167,7 @@ void CRobotMain::ExecuteCmd(char *cmd)
CObject* prev = DeselectAll();
if (prev != nullptr && prev != m_controller)
m_controller->AddDeselList(prev);
PushToSelectionHistory(prev);
SelectOneObject(m_controller, true);
m_short->UpdateShortcuts();
@ -1889,7 +1889,7 @@ bool CRobotMain::SelectObject(CObject* obj, bool displayError)
CObject* prev = DeselectAll();
if (prev != nullptr && prev != obj)
obj->AddDeselList(prev);
PushToSelectionHistory(prev);
SelectOneObject(obj, displayError);
m_short->UpdateShortcuts();
@ -1899,14 +1899,9 @@ bool CRobotMain::SelectObject(CObject* obj, bool displayError)
//! Deselects the selected object
bool CRobotMain::DeselectObject()
{
CObject* obj = nullptr;
CObject* prev = DeselectAll();
if (prev == nullptr)
obj = SearchHuman();
else
obj = prev->SubDeselList();
DeselectAll();
CObject* obj = PopFromSelectionHistory();
if (obj == nullptr)
obj = SearchHuman();
@ -2223,7 +2218,7 @@ bool CRobotMain::DeleteObject()
obj->SetSelect(false); // deselects the object
m_camera->SetType(Gfx::CAM_TYPE_EXPLO);
DeselectAll();
obj->DeleteDeselList(obj);
RemoveFromSelectionHistory(obj);
return true;
}
@ -6274,3 +6269,32 @@ bool CRobotMain::CanFactory(ObjectType type, int team)
{
return CanFactoryError(type, team) == ERR_OK;
}
void CRobotMain::PushToSelectionHistory(CObject* obj)
{
if (!m_selectionHistory.empty() && m_selectionHistory.back() == obj)
return; // already in history
m_selectionHistory.push_back(obj);
if (m_selectionHistory.size() > 50) // to avoid infinite growth
m_selectionHistory.pop_front();
}
CObject* CRobotMain::PopFromSelectionHistory()
{
if (m_selectionHistory.empty())
return nullptr;
CObject* obj = m_selectionHistory.back();
m_selectionHistory.pop_back();
return obj;
}
void CRobotMain::RemoveFromSelectionHistory(CObject* object)
{
auto it = std::remove_if(m_selectionHistory.begin(), m_selectionHistory.end(),
[object](const CObject* obj) { return obj == object; });
m_selectionHistory.erase(it, m_selectionHistory.end());
}

View File

@ -38,6 +38,8 @@
#include "app/pausemanager.h"
#include <deque>
enum Phase
{
PHASE_INIT,
@ -370,6 +372,8 @@ public:
Error CanFactoryError(ObjectType type, int team);
//@}
void RemoveFromSelectionHistory(CObject* object);
protected:
bool EventFrame(const Event &event);
bool EventObject(const Event &event);
@ -406,6 +410,8 @@ protected:
int AutosaveRotate(bool freeOne);
void Autosave();
void PushToSelectionHistory(CObject* obj);
CObject* PopFromSelectionHistory();
protected:
@ -582,4 +588,6 @@ protected:
int m_autosaveInterval;
int m_autosaveSlots;
float m_autosaveLast;
std::deque<CObject*> m_selectionHistory;
};