diff options
Diffstat (limited to 'engines/titanic/messages')
-rw-r--r-- | engines/titanic/messages/messages.cpp | 28 | ||||
-rw-r--r-- | engines/titanic/messages/messages.h | 106 | ||||
-rw-r--r-- | engines/titanic/messages/mouse_messages.h | 62 | ||||
-rw-r--r-- | engines/titanic/messages/pet_messages.h | 10 |
4 files changed, 77 insertions, 129 deletions
diff --git a/engines/titanic/messages/messages.cpp b/engines/titanic/messages/messages.cpp index 1c3d406b1b..9f46fef6ca 100644 --- a/engines/titanic/messages/messages.cpp +++ b/engines/titanic/messages/messages.cpp @@ -23,6 +23,7 @@ #include "titanic/messages/messages.h" #include "titanic/messages/mouse_messages.h" #include "titanic/core/game_object.h" +#include "titanic/core/message_target.h" #include "titanic/core/tree_item.h" #include "titanic/titanic.h" @@ -79,6 +80,33 @@ bool CMessage::execute(const CString &target, const ClassDef *classDef, int flag return false; } +const MSGMAP_ENTRY *CMessage::findMapEntry(const CTreeItem *treeItem, const ClassDef *classDef) { + // Iterate through the class and any parent classes + for (const MSGMAP *msgMap = treeItem->getMessageMap(); msgMap->pFnGetBaseMap; + msgMap = msgMap->pFnGetBaseMap()) { + // Iterate through the map entries for this class + for (const MSGMAP_ENTRY *entry = msgMap->lpEntries; + entry->_class != nullptr; ++entry) { + // Check if the class or any of it's ancesotrs is handled by this entry + for (const ClassDef *entryDef = entry->_class; entryDef; entryDef = entryDef->_parent) { + if (entryDef == classDef) + return entry; + } + } + } + + return nullptr; +} + +bool CMessage::perform(CTreeItem *treeItem) { + const MSGMAP_ENTRY *entry = findMapEntry(treeItem, getType()); + return entry && (*treeItem.*(entry->_fn))(this); +} + +bool CMessage::supports(const CTreeItem *treeItem, ClassDef *classDef) { + return findMapEntry(treeItem, classDef) != nullptr; +} + bool CMessage::isMouseMsg() const { return dynamic_cast<const CMouseMsg *>(this) != nullptr; } diff --git a/engines/titanic/messages/messages.h b/engines/titanic/messages/messages.h index f4c16f5017..e566b5d661 100644 --- a/engines/titanic/messages/messages.h +++ b/engines/titanic/messages/messages.h @@ -35,67 +35,49 @@ enum MessageFlag { MSGFLAG_CLASS_DEF = 4 }; -#define MSGTARGET(NAME) class NAME; class NAME##Target { public: \ - virtual bool handleMessage(NAME &msg) = 0; } - -#define MESSAGE0(NAME) MSGTARGET(NAME); \ +#define MESSAGE0(NAME) \ class NAME: public CMessage { \ public: NAME() : CMessage() {} \ CLASSDEF \ static bool isSupportedBy(const CTreeItem *item) { \ - return dynamic_cast<const NAME##Target *>(item) != nullptr; } \ - virtual bool perform(CTreeItem *treeItem) { \ - NAME##Target *dest = dynamic_cast<NAME##Target *>(treeItem); \ - return dest != nullptr && dest->handleMessage(*this); \ - } } -#define MESSAGE1(NAME, F1, N1, V1) MSGTARGET(NAME); \ + return supports(item, _type); } \ +} +#define MESSAGE1(NAME, F1, N1, V1) \ class NAME: public CMessage { \ public: F1 _##N1; \ NAME() : CMessage(), _##N1(V1) {} \ NAME(F1 N1) : CMessage(), _##N1(N1) {} \ CLASSDEF \ static bool isSupportedBy(const CTreeItem *item) { \ - return dynamic_cast<const NAME##Target *>(item) != nullptr; } \ - virtual bool perform(CTreeItem *treeItem) { \ - NAME##Target *dest = dynamic_cast<NAME##Target *>(treeItem); \ - return dest != nullptr && dest->handleMessage(*this); \ - } } -#define MESSAGE2(NAME, F1, N1, V1, F2, N2, V2) MSGTARGET(NAME); \ + return supports(item, _type); } \ +} +#define MESSAGE2(NAME, F1, N1, V1, F2, N2, V2) \ class NAME: public CMessage { \ public: F1 _##N1; F2 _##N2; \ NAME() : CMessage(), _##N1(V1), _##N2(V2) {} \ NAME(F1 N1, F2 N2) : CMessage(), _##N1(N1), _##N2(N2) {} \ CLASSDEF \ static bool isSupportedBy(const CTreeItem *item) { \ - return dynamic_cast<const NAME##Target *>(item) != nullptr; } \ - virtual bool perform(CTreeItem *treeItem) { \ - NAME##Target *dest = dynamic_cast<NAME##Target *>(treeItem); \ - return dest != nullptr && dest->handleMessage(*this); \ - } } -#define MESSAGE3(NAME, F1, N1, V1, F2, N2, V2, F3, N3, V3) MSGTARGET(NAME); \ + return supports(item, _type); } \ +} +#define MESSAGE3(NAME, F1, N1, V1, F2, N2, V2, F3, N3, V3) \ class NAME: public CMessage { \ public: F1 _##N1; F2 _##N2; F3 _##N3; \ NAME() : CMessage(), _##N1(V1), _##N2(V2), _##N3(V3) {} \ NAME(F1 N1, F2 N2, F3 N3) : CMessage(), _##N1(N1), _##N2(N2), _##N3(N3) {} \ CLASSDEF \ static bool isSupportedBy(const CTreeItem *item) { \ - return dynamic_cast<const NAME##Target *>(item) != nullptr; } \ - virtual bool perform(CTreeItem *treeItem) { \ - NAME##Target *dest = dynamic_cast<NAME##Target *>(treeItem); \ - return dest != nullptr && dest->handleMessage(*this); \ - } } -#define MESSAGE4(NAME, F1, N1, V1, F2, N2, V2, F3, N3, V3, F4, N4, V4) MSGTARGET(NAME); \ + return supports(item, _type); } \ +} +#define MESSAGE4(NAME, F1, N1, V1, F2, N2, V2, F3, N3, V3, F4, N4, V4) \ class NAME: public CMessage { \ public: F1 _##N1; F2 _##N2; F3 _##N3; F4 _##N4; \ NAME() : CMessage(), _##N1(V1), _##N2(V2), _##N3(V3), _##N4(V4) {} \ NAME(F1 N1, F2 N2, F3 N3, F4 N4) : CMessage(), _##N1(N1), _##N2(N2), _##N3(N3), _##N4(N4) {} \ CLASSDEF \ static bool isSupportedBy(const CTreeItem *item) { \ - return dynamic_cast<const NAME##Target *>(item) != nullptr; } \ - virtual bool perform(CTreeItem *treeItem) { \ - NAME##Target *dest = dynamic_cast<NAME##Target *>(treeItem); \ - return dest != nullptr && dest->handleMessage(*this); \ - } } + return supports(item, _type); } \ +} class CGameObject; class CRoomItem; @@ -103,6 +85,11 @@ class CNodeItem; class CViewItem; class CMessage : public CSaveableObject { +private: + /** + * Find a map entry that supports the given class + */ + static const MSGMAP_ENTRY *findMapEntry(const CTreeItem *treeItem, const ClassDef *classDef); public: CLASSDEF CMessage(); @@ -121,7 +108,15 @@ public: bool execute(const CString &target, const ClassDef *classDef = nullptr, int flags = MSGFLAG_SCAN | MSGFLAG_BREAK_IF_HANDLED); - virtual bool perform(CTreeItem *treeItem) { return false; } + /** + * Makes the passed item execute the message + */ + virtual bool perform(CTreeItem *treeItem); + + /** + * Returns true if the passed item supports the specified message class + */ + static bool supports(const CTreeItem *treeItem, ClassDef *classDef); /** * Save the data for the class to file @@ -149,7 +144,6 @@ public: virtual bool isLeaveViewMsg() const; }; -MSGTARGET(CEditControlMsg); class CEditControlMsg : public CMessage { public: int _field4; @@ -164,16 +158,10 @@ public: _field1C(0), _field20(0) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CEditControlMsgTarget *>(item) != nullptr; - } - - virtual bool perform(CTreeItem *treeItem) { - CEditControlMsgTarget *dest = dynamic_cast<CEditControlMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return CMessage::supports(item, _type); } }; -MSGTARGET(CLightsMsg); class CLightsMsg : public CMessage { public: int _field4; @@ -186,15 +174,10 @@ public: _fieldC(0), _field10(0) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CLightsMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CLightsMsgTarget *dest = dynamic_cast<CLightsMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CIsHookedOnMsg); class CIsHookedOnMsg : public CMessage { public: int _field4; @@ -209,15 +192,10 @@ public: _field18(0), _field1C(0), _field20(0) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CIsHookedOnMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CIsHookedOnMsgTarget *dest = dynamic_cast<CIsHookedOnMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CSubAcceptCCarryMsg); class CSubAcceptCCarryMsg : public CMessage { public: CString _string1; @@ -227,15 +205,10 @@ public: CSubAcceptCCarryMsg() : _value1(0), _value2(0), _value3(0) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CSubAcceptCCarryMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CSubAcceptCCarryMsgTarget *dest = dynamic_cast<CSubAcceptCCarryMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CTransportMsg); class CTransportMsg : public CMessage { public: CString _string; @@ -245,17 +218,12 @@ public: CTransportMsg() : _value1(0), _value2(0) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CTransportMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CTransportMsgTarget *dest = dynamic_cast<CTransportMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; MESSAGE1(CTimeMsg, int, value, 0); -MSGTARGET(CTimerMsg); class CTimerMsg : public CTimeMsg { public: int _field8; @@ -266,11 +234,7 @@ public: CTimerMsg() : CTimeMsg(), _field8(0), _fieldC(0) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CTimerMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CTimerMsgTarget *dest = dynamic_cast<CTimerMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; diff --git a/engines/titanic/messages/mouse_messages.h b/engines/titanic/messages/mouse_messages.h index 1bc77b6a87..811fdf0ad0 100644 --- a/engines/titanic/messages/mouse_messages.h +++ b/engines/titanic/messages/mouse_messages.h @@ -30,7 +30,6 @@ namespace Titanic { enum MouseButton { MB_LEFT = 1, MB_MIDDLE = 2, MB_RIGHT = 4 }; -MSGTARGET(CMouseMsg); class CMouseMsg : public CMessage { public: int _buttons; @@ -38,11 +37,7 @@ public: public: CLASSDEF static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseMsg *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CMouseMsgTarget *dest = dynamic_cast<CMouseMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } CMouseMsg() : _buttons(0) {} @@ -50,7 +45,6 @@ public: _mousePos(pt), _buttons(buttons) {} }; -MSGTARGET(CMouseMoveMsg); class CMouseMoveMsg : public CMouseMsg { public: CLASSDEF @@ -58,15 +52,10 @@ public: CMouseMoveMsg(const Point &pt, int buttons) : CMouseMsg(pt, buttons) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseMoveMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CMouseMoveMsgTarget *dest = dynamic_cast<CMouseMoveMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CMouseButtonMsg); class CMouseButtonMsg : public CMouseMsg { public: int _field10; @@ -76,11 +65,10 @@ public: CMouseButtonMsg(const Point &pt, int buttons) : CMouseMsg(pt, buttons) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseButtonMsgTarget *>(item) != nullptr; + return supports(item, _type); } }; -MSGTARGET(CMouseButtonDownMsg); class CMouseButtonDownMsg : public CMouseButtonMsg { public: CLASSDEF @@ -88,15 +76,10 @@ public: CMouseButtonDownMsg(const Point &pt, int buttons) : CMouseButtonMsg(pt, buttons) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseButtonDownMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CMouseButtonDownMsgTarget *dest = dynamic_cast<CMouseButtonDownMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CMouseButtonUpMsg); class CMouseButtonUpMsg : public CMouseButtonMsg { public: CLASSDEF @@ -104,15 +87,10 @@ public: CMouseButtonUpMsg(const Point &pt, int buttons) : CMouseButtonMsg(pt, buttons) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseButtonUpMsg *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CMouseButtonUpMsgTarget *dest = dynamic_cast<CMouseButtonUpMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CMouseDoubleClickMsg); class CMouseDoubleClickMsg : public CMouseButtonMsg { public: CLASSDEF @@ -120,15 +98,10 @@ public: CMouseDoubleClickMsg(const Point &pt, int buttons) : CMouseButtonMsg(pt, buttons) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseDoubleClickMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CMouseDoubleClickMsgTarget *dest = dynamic_cast<CMouseDoubleClickMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CMouseDragMsg); class CMouseDragMsg : public CMouseMsg { public: CLASSDEF @@ -136,11 +109,10 @@ public: CMouseDragMsg(const Point &pt) : CMouseMsg(pt, 0) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseDragMsg *>(item) != nullptr; + return supports(item, _type); } }; -MSGTARGET(CMouseDragMoveMsg); class CMouseDragMoveMsg : public CMouseDragMsg { public: CLASSDEF @@ -148,15 +120,10 @@ public: CMouseDragMoveMsg(const Point &pt) : CMouseDragMsg(pt) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseDragMoveMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CMouseDragMoveMsgTarget *dest = dynamic_cast<CMouseDragMoveMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CMouseDragStartMsg); class CMouseDragStartMsg : public CMouseDragMsg { public: CTreeItem *_dragItem; @@ -168,15 +135,10 @@ public: _dragItem(nullptr), _field14(0) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseDragStartMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CMouseDragStartMsgTarget *dest = dynamic_cast<CMouseDragStartMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; -MSGTARGET(CMouseDragEndMsg); class CMouseDragEndMsg : public CMouseDragMsg { public: CTreeItem *_dropTarget; @@ -187,11 +149,7 @@ public: CMouseDragMsg(pt), _dropTarget(dragItem) {} static bool isSupportedBy(const CTreeItem *item) { - return dynamic_cast<const CMouseDragEndMsgTarget *>(item) != nullptr; - } - virtual bool perform(CTreeItem *treeItem) { - CMouseDragEndMsgTarget *dest = dynamic_cast<CMouseDragEndMsgTarget *>(treeItem); - return dest != nullptr && dest->handleMessage(*this); + return supports(item, _type); } }; diff --git a/engines/titanic/messages/pet_messages.h b/engines/titanic/messages/pet_messages.h index 7e39056742..f7d9c301a6 100644 --- a/engines/titanic/messages/pet_messages.h +++ b/engines/titanic/messages/pet_messages.h @@ -42,18 +42,16 @@ MESSAGE1(CPETStarFieldLockMsg, int, value, 0); MESSAGE0(CPETStereoFieldOnOffMsg); MESSAGE2(CPETTargetMsg, CString, name, "", int, numValue, -1); -#define PET_MESSAGE(NAME) MSGTARGET(NAME); \ +#define PET_MESSAGE(NAME) \ class NAME: public CPETTargetMsg { \ public: \ NAME() : CPETTargetMsg() {} \ NAME(const CString &name, int num) : CPETTargetMsg(name, num) {} \ CLASSDEF \ static bool isSupportedBy(const CTreeItem *item) { \ - return dynamic_cast<const NAME##Target *>(item) != nullptr; } \ - virtual bool perform(CTreeItem *treeItem) { \ - NAME##Target *dest = dynamic_cast<NAME##Target *>(treeItem); \ - return dest != nullptr && dest->handleMessage(*this); \ - } } + return supports(item, _type); \ + } \ +} PET_MESSAGE(CPETDownMsg); PET_MESSAGE(CPETUpMsg); |