- split off the interface part of DHUDMessage into an abstract base class.

The purpose is to allow creating custom message types in ZScript.
This commit is contained in:
Christoph Oelckers 2018-03-01 10:32:19 +01:00
parent 711471e9dd
commit dd893b6a0c
3 changed files with 61 additions and 38 deletions

View file

@ -43,12 +43,14 @@
EXTERN_CVAR(Int, con_scaletext) EXTERN_CVAR(Int, con_scaletext)
IMPLEMENT_CLASS(DHUDMessage, false, true) IMPLEMENT_CLASS(DHUDMessageBase, true, true)
IMPLEMENT_POINTERS_START(DHUDMessageBase)
IMPLEMENT_POINTERS_START(DHUDMessage)
IMPLEMENT_POINTER(Next) IMPLEMENT_POINTER(Next)
IMPLEMENT_POINTERS_END IMPLEMENT_POINTERS_END
IMPLEMENT_CLASS(DHUDMessage, false, false)
IMPLEMENT_CLASS(DHUDMessageFadeOut, false, false) IMPLEMENT_CLASS(DHUDMessageFadeOut, false, false)
IMPLEMENT_CLASS(DHUDMessageFadeInOut, false, false) IMPLEMENT_CLASS(DHUDMessageFadeInOut, false, false)
IMPLEMENT_CLASS(DHUDMessageTypeOnFadeOut, false, false) IMPLEMENT_CLASS(DHUDMessageTypeOnFadeOut, false, false)
@ -57,6 +59,13 @@ IMPLEMENT_CLASS(DHUDMessageTypeOnFadeOut, false, false)
* Basic HUD message. Appears and disappears without any special effects * * Basic HUD message. Appears and disappears without any special effects *
*************************************************************************/ *************************************************************************/
void DHUDMessageBase::Serialize(FSerializer &arc)
{
Super::Serialize(arc);
arc("next", Next)
("sbarid", SBarID);
}
//============================================================================ //============================================================================
// //
// DHUDMessage Constructor // DHUDMessage Constructor
@ -130,7 +139,6 @@ DHUDMessage::DHUDMessage (FFont *font, const char *text, float x, float y, int h
WrapWidth = 0; WrapWidth = 0;
HandleAspect = true; HandleAspect = true;
Top = y; Top = y;
Next = NULL;
Lines = NULL; Lines = NULL;
HoldTics = (int)(holdTime * TICRATE); HoldTics = (int)(holdTime * TICRATE);
Tics = 0; Tics = 0;
@ -184,10 +192,8 @@ void DHUDMessage::Serialize(FSerializer &arc)
("tics", Tics) ("tics", Tics)
("state", State) ("state", State)
.Enum("textcolor", TextColor) .Enum("textcolor", TextColor)
("sbarid", SBarID)
("sourcetext", SourceText) ("sourcetext", SourceText)
("font", Font) ("font", Font)
("next", Next)
("hudwidth", HUDWidth) ("hudwidth", HUDWidth)
("hudheight", HUDHeight) ("hudheight", HUDHeight)
("nowrap", NoWrap) ("nowrap", NoWrap)

View file

@ -59,10 +59,30 @@ bool ST_IsLatencyVisible();
// HUD Message base object -------------------------------------------------- // HUD Message base object --------------------------------------------------
class DHUDMessage : public DObject // This is a mo-op base class to allow derived ZScript message types that can be managed by the status bar.
class DHUDMessageBase : public DObject
{ {
DECLARE_CLASS (DHUDMessage, DObject) DECLARE_CLASS(DHUDMessageBase, DObject)
HAS_OBJECT_POINTERS HAS_OBJECT_POINTERS
public:
virtual void Serialize(FSerializer &arc);
virtual bool Tick() { return true; } // Returns true to indicate time for removal
virtual void ScreenSizeChanged() {}
virtual void Draw(int bottom, int visibility) {}
private:
TObjPtr<DHUDMessageBase*> Next = nullptr;
uint32_t SBarID = 0;
friend class DBaseStatusBar;
};
// HUD Message --------------------------------------------------
class DHUDMessage : public DHUDMessageBase
{
DECLARE_CLASS (DHUDMessage, DHUDMessageBase)
public: public:
DHUDMessage (FFont *font, const char *text, float x, float y, int hudwidth, int hudheight, DHUDMessage (FFont *font, const char *text, float x, float y, int hudwidth, int hudheight,
EColorRange textColor, float holdTime); EColorRange textColor, float holdTime);
@ -70,12 +90,12 @@ public:
virtual void Serialize(FSerializer &arc); virtual void Serialize(FSerializer &arc);
void Draw (int bottom, int visibility); virtual void Draw (int bottom, int visibility) override;
virtual void ResetText (const char *text); virtual void ResetText (const char *text);
virtual void DrawSetup (); virtual void DrawSetup ();
virtual void DoDraw (int linenum, int x, int y, bool clean, int hudheight); virtual void DoDraw (int linenum, int x, int y, bool clean, int hudheight);
virtual bool Tick (); // Returns true to indicate time for removal virtual bool Tick () override;
virtual void ScreenSizeChanged (); virtual void ScreenSizeChanged () override;
void SetVisibility(int vis) void SetVisibility(int vis)
{ {
@ -130,11 +150,8 @@ protected:
DHUDMessage () : SourceText(NULL) {} DHUDMessage () : SourceText(NULL) {}
private: private:
TObjPtr<DHUDMessage*> Next;
uint32_t SBarID;
char *SourceText; char *SourceText;
friend class DBaseStatusBar;
}; };
// HUD message visibility flags // HUD message visibility flags
@ -360,9 +377,9 @@ public:
void SetSize(int reltop = 32, int hres = 320, int vres = 200, int hhres = -1, int hvres = -1); void SetSize(int reltop = 32, int hres = 320, int vres = 200, int hhres = -1, int hvres = -1);
void OnDestroy() override; void OnDestroy() override;
void AttachMessage (DHUDMessage *msg, uint32_t id=0, int layer=HUDMSGLayer_Default); void AttachMessage (DHUDMessageBase *msg, uint32_t id=0, int layer=HUDMSGLayer_Default);
DHUDMessage *DetachMessage (DHUDMessage *msg); DHUDMessageBase *DetachMessage (DHUDMessageBase *msg);
DHUDMessage *DetachMessage (uint32_t id); DHUDMessageBase *DetachMessage (uint32_t id);
void DetachAllMessages (); void DetachAllMessages ();
void ShowPlayerName (); void ShowPlayerName ();
double GetDisplacement() { return Displacement; } double GetDisplacement() { return Displacement; }
@ -455,7 +472,7 @@ private:
void DrawWaiting () const; void DrawWaiting () const;
void SetDrawSize(int reltop, int hres, int vres); void SetDrawSize(int reltop, int hres, int vres);
TObjPtr<DHUDMessage*> Messages[NUM_HUDMSGLAYERS]; TObjPtr<DHUDMessageBase*> Messages[NUM_HUDMSGLAYERS];
int BaseRelTop; int BaseRelTop;
int BaseSBarHorizontalResolution; int BaseSBarHorizontalResolution;

View file

@ -410,10 +410,10 @@ void DBaseStatusBar::OnDestroy ()
{ {
for (size_t i = 0; i < countof(Messages); ++i) for (size_t i = 0; i < countof(Messages); ++i)
{ {
DHUDMessage *msg = Messages[i]; DHUDMessageBase *msg = Messages[i];
while (msg) while (msg)
{ {
DHUDMessage *next = msg->Next; DHUDMessageBase *next = msg->Next;
msg->Destroy(); msg->Destroy();
msg = next; msg = next;
} }
@ -594,12 +594,12 @@ void DBaseStatusBar::Tick ()
{ {
for (size_t i = 0; i < countof(Messages); ++i) for (size_t i = 0; i < countof(Messages); ++i)
{ {
DHUDMessage *msg = Messages[i]; DHUDMessageBase *msg = Messages[i];
DHUDMessage **prev = &Messages[i]; DHUDMessageBase **prev = &Messages[i];
while (msg) while (msg)
{ {
DHUDMessage *next = msg->Next; DHUDMessageBase *next = msg->Next;
if (msg->Tick ()) if (msg->Tick ())
{ {
@ -662,10 +662,10 @@ void DBaseStatusBar::CallTick()
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void DBaseStatusBar::AttachMessage (DHUDMessage *msg, uint32_t id, int layer) void DBaseStatusBar::AttachMessage (DHUDMessageBase *msg, uint32_t id, int layer)
{ {
DHUDMessage *old = NULL; DHUDMessageBase *old = NULL;
DHUDMessage **prev; DHUDMessageBase **prev;
DObject *container = this; DObject *container = this;
old = (id == 0 || id == 0xFFFFFFFF) ? NULL : DetachMessage (id); old = (id == 0 || id == 0xFFFFFFFF) ? NULL : DetachMessage (id);
@ -703,12 +703,12 @@ void DBaseStatusBar::AttachMessage (DHUDMessage *msg, uint32_t id, int layer)
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
DHUDMessage *DBaseStatusBar::DetachMessage (DHUDMessage *msg) DHUDMessageBase *DBaseStatusBar::DetachMessage (DHUDMessageBase *msg)
{ {
for (size_t i = 0; i < countof(Messages); ++i) for (size_t i = 0; i < countof(Messages); ++i)
{ {
DHUDMessage *probe = Messages[i]; DHUDMessageBase *probe = Messages[i];
DHUDMessage **prev = &Messages[i]; DHUDMessageBase **prev = &Messages[i];
while (probe && probe != msg) while (probe && probe != msg)
{ {
@ -725,12 +725,12 @@ DHUDMessage *DBaseStatusBar::DetachMessage (DHUDMessage *msg)
return NULL; return NULL;
} }
DHUDMessage *DBaseStatusBar::DetachMessage (uint32_t id) DHUDMessageBase *DBaseStatusBar::DetachMessage (uint32_t id)
{ {
for (size_t i = 0; i < countof(Messages); ++i) for (size_t i = 0; i < countof(Messages); ++i)
{ {
DHUDMessage *probe = Messages[i]; DHUDMessageBase *probe = Messages[i];
DHUDMessage **prev = &Messages[i]; DHUDMessageBase **prev = &Messages[i];
while (probe && probe->SBarID != id) while (probe && probe->SBarID != id)
{ {
@ -757,12 +757,12 @@ void DBaseStatusBar::DetachAllMessages ()
{ {
for (size_t i = 0; i < countof(Messages); ++i) for (size_t i = 0; i < countof(Messages); ++i)
{ {
DHUDMessage *probe = Messages[i]; DHUDMessageBase *probe = Messages[i];
Messages[i] = NULL; Messages[i] = NULL;
while (probe != NULL) while (probe != NULL)
{ {
DHUDMessage *next = probe->Next; DHUDMessageBase *next = probe->Next;
probe->Destroy(); probe->Destroy();
probe = next; probe = next;
} }
@ -940,7 +940,7 @@ void DBaseStatusBar::FlashCrosshair ()
void DBaseStatusBar::DrawMessages (int layer, int bottom) void DBaseStatusBar::DrawMessages (int layer, int bottom)
{ {
DHUDMessage *msg = Messages[layer]; DHUDMessageBase *msg = Messages[layer];
int visibility = 0; int visibility = 0;
if (viewactive) if (viewactive)
@ -953,7 +953,7 @@ void DBaseStatusBar::DrawMessages (int layer, int bottom)
} }
while (msg) while (msg)
{ {
DHUDMessage *next = msg->Next; DHUDMessageBase *next = msg->Next;
msg->Draw (bottom, visibility); msg->Draw (bottom, visibility);
msg = next; msg = next;
} }
@ -1307,7 +1307,7 @@ void DBaseStatusBar::ScreenSizeChanged ()
for (size_t i = 0; i < countof(Messages); ++i) for (size_t i = 0; i < countof(Messages); ++i)
{ {
DHUDMessage *message = Messages[i]; DHUDMessageBase *message = Messages[i];
while (message != NULL) while (message != NULL)
{ {
message->ScreenSizeChanged (); message->ScreenSizeChanged ();