k_dialogue: Seperate srb2::Dialogue::Typewriter out from srb2::Dialogue

This is the minimal subset of properties useful for various non-popup Dialoglogs, such as the impending Goner Setup.
This commit is contained in:
toaster 2023-12-02 16:50:42 +00:00
parent 299e0c3b71
commit 1cf14f29dc
2 changed files with 187 additions and 157 deletions

View file

@ -35,96 +35,16 @@
using srb2::Dialogue; using srb2::Dialogue;
void Dialogue::Init(void) // Dialogue::Typewriter
void Dialogue::Typewriter::ClearText(void)
{ {
active = true; text.clear();
syllable = true; textDest.clear();
} }
void Dialogue::SetSpeaker(void) void Dialogue::Typewriter::NewText(std::string newText)
{ {
// Unset speaker
speaker.clear();
portrait = nullptr;
portraitColormap = nullptr;
voiceSfx = sfx_ktalk;
}
void Dialogue::SetSpeaker(std::string skinName, int portraitID)
{
Init();
// Set speaker based on a skin
int skinID = -1;
if (!skinName.empty())
{
skinID = R_SkinAvailable(skinName.c_str());
}
if (skinID >= 0 && skinID < numskins)
{
const skin_t *skin = &skins[skinID];
const spritedef_t *sprdef = &skin->sprites[SPR2_TALK];
if (sprdef->numframes > 0)
{
portraitID %= sprdef->numframes;
const spriteframe_t *sprframe = &sprdef->spriteframes[portraitID];
portrait = static_cast<patch_t *>( W_CachePatchNum(sprframe->lumppat[0], PU_CACHE) );
portraitColormap = R_GetTranslationColormap(skinID, static_cast<skincolornum_t>(skin->prefcolor), GTC_CACHE);
}
else
{
portrait = nullptr;
portraitColormap = nullptr;
}
speaker = skin->realname;
voiceSfx = skin->soundsid[ S_sfx[sfx_ktalk].skinsound ];
}
else
{
SetSpeaker();
}
}
void Dialogue::SetSpeaker(std::string name, patch_t *patch, UINT8 *colormap, sfxenum_t voice)
{
Init();
// Set custom speaker
speaker = name;
if (speaker.empty())
{
portrait = nullptr;
portraitColormap = nullptr;
voiceSfx = sfx_ktalk;
return;
}
portrait = patch;
portraitColormap = colormap;
voiceSfx = voice;
}
void Dialogue::NewText(std::string newText)
{
Init();
newText = V_ScaledWordWrap(
290 << FRACBITS,
FRACUNIT, FRACUNIT, FRACUNIT,
0, HU_FONT,
newText.c_str()
);
text.clear(); text.clear();
textDest = newText; textDest = newText;
@ -133,30 +53,15 @@ void Dialogue::NewText(std::string newText)
textTimer = kTextPunctPause; textTimer = kTextPunctPause;
textSpeed = kTextSpeedDefault; textSpeed = kTextSpeedDefault;
textDone = false; textDone = false;
syllable = true;
} }
bool Dialogue::Active(void) void Dialogue::Typewriter::WriteText(void)
{ {
return active; if (textDone)
} return;
bool Dialogue::TextDone(void)
{
return textDone;
}
bool Dialogue::Dismissable(void)
{
return dismissable;
}
void Dialogue::SetDismissable(bool value)
{
dismissable = value;
}
void Dialogue::WriteText(void)
{
bool voicePlayed = false; bool voicePlayed = false;
textTimer -= textSpeed; textTimer -= textSpeed;
@ -206,6 +111,132 @@ void Dialogue::WriteText(void)
textDone = (textTimer <= 0 && textDest.empty()); textDone = (textTimer <= 0 && textDest.empty());
} }
void Dialogue::Typewriter::CompleteText(void)
{
while (!textDest.empty())
{
text.push_back( textDest.back() );
textDest.pop_back();
}
textTimer = 0;
textDone = true;
}
// Dialogue
void Dialogue::Init(void)
{
active = true;
}
void Dialogue::SetSpeaker(void)
{
// Unset speaker
speaker.clear();
portrait = nullptr;
portraitColormap = nullptr;
typewriter.voiceSfx = sfx_ktalk;
}
void Dialogue::SetSpeaker(std::string skinName, int portraitID)
{
Init();
// Set speaker based on a skin
int skinID = -1;
if (!skinName.empty())
{
skinID = R_SkinAvailable(skinName.c_str());
}
if (skinID >= 0 && skinID < numskins)
{
const skin_t *skin = &skins[skinID];
const spritedef_t *sprdef = &skin->sprites[SPR2_TALK];
if (sprdef->numframes > 0)
{
portraitID %= sprdef->numframes;
const spriteframe_t *sprframe = &sprdef->spriteframes[portraitID];
portrait = static_cast<patch_t *>( W_CachePatchNum(sprframe->lumppat[0], PU_CACHE) );
portraitColormap = R_GetTranslationColormap(skinID, static_cast<skincolornum_t>(skin->prefcolor), GTC_CACHE);
}
else
{
portrait = nullptr;
portraitColormap = nullptr;
}
speaker = skin->realname;
typewriter.voiceSfx = skin->soundsid[ S_sfx[sfx_ktalk].skinsound ];
}
else
{
SetSpeaker();
}
}
void Dialogue::SetSpeaker(std::string name, patch_t *patch, UINT8 *colormap, sfxenum_t voice)
{
Init();
// Set custom speaker
speaker = name;
if (speaker.empty())
{
portrait = nullptr;
portraitColormap = nullptr;
typewriter.voiceSfx = sfx_ktalk;
return;
}
portrait = patch;
portraitColormap = colormap;
typewriter.voiceSfx = voice;
}
void Dialogue::NewText(std::string newText)
{
Init();
newText = V_ScaledWordWrap(
290 << FRACBITS,
FRACUNIT, FRACUNIT, FRACUNIT,
0, HU_FONT,
newText.c_str()
);
typewriter.NewText(newText);
}
bool Dialogue::Active(void)
{
return active;
}
bool Dialogue::TextDone(void)
{
return typewriter.textDone;
}
bool Dialogue::Dismissable(void)
{
return dismissable;
}
void Dialogue::SetDismissable(bool value)
{
dismissable = value;
}
bool Dialogue::Held(void) bool Dialogue::Held(void)
{ {
return ((players[serverplayer].cmd.buttons & BT_VOTE) == BT_VOTE); return ((players[serverplayer].cmd.buttons & BT_VOTE) == BT_VOTE);
@ -219,18 +250,6 @@ bool Dialogue::Pressed(void)
); );
} }
void Dialogue::CompleteText(void)
{
while (!textDest.empty())
{
text.push_back( textDest.back() );
textDest.pop_back();
}
textTimer = 0;
textDone = true;
}
void Dialogue::Tick(void) void Dialogue::Tick(void)
{ {
if (Active()) if (Active())
@ -260,7 +279,7 @@ void Dialogue::Tick(void)
return; return;
} }
WriteText(); typewriter.WriteText();
if (Dismissable() == true) if (Dismissable() == true)
{ {
@ -272,7 +291,7 @@ void Dialogue::Tick(void)
} }
else else
{ {
CompleteText(); typewriter.CompleteText();
} }
} }
} }
@ -421,7 +440,7 @@ void Dialogue::Draw(void)
drawer drawer
.xy(10 - BASEVIDWIDTH, -3-32) .xy(10 - BASEVIDWIDTH, -3-32)
.font(srb2::Draw::Font::kConsole) .font(srb2::Draw::Font::kConsole)
.text( text.c_str() ); .text( typewriter.text.c_str() );
if (Dismissable()) if (Dismissable())
{ {
@ -441,8 +460,7 @@ void Dialogue::Draw(void)
void Dialogue::Dismiss(void) void Dialogue::Dismiss(void)
{ {
active = false; active = false;
text.clear(); typewriter.ClearText();
textDest.clear();
} }
void Dialogue::Unset(void) void Dialogue::Unset(void)

View file

@ -27,41 +27,7 @@ namespace srb2
class Dialogue class Dialogue
{ {
private:
patch_t *bgPatch;
patch_t *confirmPatch;
std::string speaker;
patch_t *portrait;
UINT8 *portraitColormap;
sfxenum_t voiceSfx;
std::string text;
std::string textDest;
bool active;
fixed_t slide;
fixed_t textTimer;
fixed_t textSpeed;
bool textDone;
bool syllable;
bool dismissable;
bool freeze;
void Init(void);
//void Unset(void);
void WriteText(void);
void CompleteText(void);
bool Pressed(void);
bool Held(void);
public: public:
static constexpr fixed_t kTextSpeedDefault = FRACUNIT;
static constexpr fixed_t kTextPunctPause = (FRACUNIT * TICRATE * 2) / 5;
static constexpr fixed_t kSlideSpeed = FRACUNIT / (TICRATE / 5); static constexpr fixed_t kSlideSpeed = FRACUNIT / (TICRATE / 5);
void SetSpeaker(void); void SetSpeaker(void);
@ -82,6 +48,52 @@ public:
void Dismiss(void); void Dismiss(void);
void Unset(void); void Unset(void);
class Typewriter
{
public:
static constexpr fixed_t kTextSpeedDefault = FRACUNIT;
static constexpr fixed_t kTextPunctPause = (FRACUNIT * TICRATE * 2) / 5;
std::string text;
std::string textDest;
fixed_t textTimer;
fixed_t textSpeed;
bool textDone;
sfxenum_t voiceSfx;
bool syllable;
void NewText(std::string newText);
void ClearText(void);
void WriteText(void);
void CompleteText(void);
};
private:
Typewriter typewriter;
patch_t *bgPatch;
patch_t *confirmPatch;
std::string speaker;
patch_t *portrait;
UINT8 *portraitColormap;
bool active;
fixed_t slide;
bool dismissable;
bool freeze;
void Init(void);
//void Unset(void);
bool Pressed(void);
bool Held(void);
}; };
}; // namespace srb2 }; // namespace srb2