From d73f8faafa4d22e45af92cb2442d1b629ed8196f Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 13 Apr 2019 12:17:38 +0200 Subject: [PATCH] - added monospacing support to Screen.DrawText and its native counterparts. --- src/g_statusbar/shared_sbar.cpp | 24 ++++--------------- src/rendering/2d/v_draw.cpp | 12 +++++++++- src/rendering/2d/v_drawtext.cpp | 22 ++++++++++++++++- src/v_video.h | 12 ++++++++++ wadsrc/static/zscript/base.zs | 2 ++ wadsrc/static/zscript/constants.zs | 8 +++++++ wadsrc/static/zscript/ui/statusbar/alt_hud.zs | 16 ++----------- 7 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/g_statusbar/shared_sbar.cpp b/src/g_statusbar/shared_sbar.cpp index 85f0ae665..46e3af0ca 100644 --- a/src/g_statusbar/shared_sbar.cpp +++ b/src/g_statusbar/shared_sbar.cpp @@ -552,23 +552,6 @@ void DBaseStatusBar::BeginHUD(int resW, int resH, double Alpha, bool forcescaled void FormatMapName(FLevelLocals *self, int cr, FString *result); -static void DrawAMText(FFont *fnt, int color, const char *text, int vwidth, int vheight, int x, int y) -{ - int zerowidth = fnt->GetCharWidth('0'); - - x += zerowidth / 2; - for (int i = 0; text[i]; i++) - { - int c = text[i]; - int width = fnt->GetCharWidth(c); - - screen->DrawChar(fnt, color, x, y, c, DTA_KeepRatio, true, - DTA_VirtualWidth, vwidth, DTA_VirtualHeight, vheight, DTA_LeftOffset, width / 2, TAG_DONE); - x += zerowidth; - } -} - - void DBaseStatusBar::DoDrawAutomapHUD(int crdefault, int highlight) { auto scale = GetUIScale(hud_scale); @@ -580,12 +563,14 @@ void DBaseStatusBar::DoDrawAutomapHUD(int crdefault, int highlight) int sec; int y = 0; int textdist = 4; + int zerowidth = font->GetCharWidth('0'); if (am_showtime) { sec = Tics2Seconds(primaryLevel->time); textbuffer.Format("%02d:%02d:%02d", sec / 3600, (sec % 3600) / 60, sec % 60); - DrawAMText(font, crdefault, textbuffer, vwidth, vheight, vwidth - font->GetCharWidth('0') * 8 - textdist, y); + screen->DrawText(font, crdefault, vwidth - zerowidth * 8 - textdist, y, textbuffer, DTA_VirtualWidth, vwidth, DTA_VirtualHeight, vheight, + DTA_Monospace, EMonospacing::CellCenter, DTA_Spacing, zerowidth, DTA_KeepRatio, true, TAG_END); y += fheight; } @@ -593,7 +578,8 @@ void DBaseStatusBar::DoDrawAutomapHUD(int crdefault, int highlight) { sec = Tics2Seconds(primaryLevel->totaltime); textbuffer.Format("%02d:%02d:%02d", sec / 3600, (sec % 3600) / 60, sec % 60); - DrawAMText(font, crdefault, textbuffer, vwidth, vheight, vwidth - font->GetCharWidth('0') * 8 - textdist, y); + screen->DrawText(font, crdefault, vwidth - zerowidth * 8 - textdist, y, textbuffer, DTA_VirtualWidth, vwidth, DTA_VirtualHeight, vheight, + DTA_Monospace, EMonospacing::CellCenter, DTA_Spacing, zerowidth, DTA_KeepRatio, true, TAG_END); } if (!deathmatch) diff --git a/src/rendering/2d/v_draw.cpp b/src/rendering/2d/v_draw.cpp index 5cf593bbb..0f77380b9 100644 --- a/src/rendering/2d/v_draw.cpp +++ b/src/rendering/2d/v_draw.cpp @@ -545,6 +545,8 @@ bool DFrameBuffer::ParseDrawTextureTags(FTexture *img, double x, double y, uint3 parms->srcwidth = 1.; parms->srcheight = 1.; parms->burn = false; + parms->monospace = EMonospacing::Off; + parms->spacing = 0; // Parse the tag list for attributes. (For floating point attributes, // consider that the C ABI dictates that all floats be promoted to @@ -896,7 +898,15 @@ bool DFrameBuffer::ParseDrawTextureTags(FTexture *img, double x, double y, uint3 case DTA_CellY: parms->celly = ListGetInt(tags); break; - + + case DTA_Monospace: + parms->monospace = ListGetInt(tags); + break; + + case DTA_Spacing: + parms->spacing = ListGetInt(tags); + break; + case DTA_Burn: parms->burn = true; break; diff --git a/src/rendering/2d/v_drawtext.cpp b/src/rendering/2d/v_drawtext.cpp index 2d1996a4b..2e0302dca 100644 --- a/src/rendering/2d/v_drawtext.cpp +++ b/src/rendering/2d/v_drawtext.cpp @@ -296,6 +296,11 @@ void DFrameBuffer::DrawTextCommon(FFont *font, int normalcolor, double x, double cx = x; cy = y; + if (parms.monospace == EMonospacing::CellCenter) + cx += parms.spacing / 2; + else if (parms.monospace == EMonospacing::CellRight) + cx += parms.spacing; + auto currentcolor = normalcolor; while (ch - string < parms.maxstrlen) @@ -334,9 +339,24 @@ void DFrameBuffer::DrawTextCommon(FFont *font, int normalcolor, double x, double parms.destwidth = parms.cellx; parms.destheight = parms.celly; } + if (parms.monospace == EMonospacing::CellLeft) + parms.left = 0; + else if (parms.monospace == EMonospacing::CellCenter) + parms.left = w / 2.; + else if (parms.monospace == EMonospacing::CellRight) + parms.left = w; + DrawTextureParms(pic, parms); } - cx += (w + kerning) * parms.scalex; + if (parms.monospace == EMonospacing::Off) + { + cx += (w + kerning + parms.spacing) * parms.scalex; + } + else + { + cx += (parms.spacing) * parms.scalex; + } + } } diff --git a/src/v_video.h b/src/v_video.h index 39089388c..be3246515 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -236,6 +236,16 @@ enum DTA_SrcHeight, DTA_LegacyRenderStyle, // takes an old-style STYLE_* constant instead of an FRenderStyle DTA_Burn, // activates the burn shader for this element + DTA_Spacing, // Strings only: Additional spacing between characters + DTA_Monospace, // Fonts only: Use a fixed distance between characters. +}; + +enum EMonospacing +{ + Off = 0, + CellLeft = 1, + CellCenter = 2, + CellRight = 3 }; enum @@ -286,6 +296,8 @@ struct DrawParms int desaturate; int scalex, scaley; int cellx, celly; + int monospace; + int spacing; int maxstrlen; bool fortext; bool virtBottom; diff --git a/wadsrc/static/zscript/base.zs b/wadsrc/static/zscript/base.zs index 3c86611f3..2a9295d67 100644 --- a/wadsrc/static/zscript/base.zs +++ b/wadsrc/static/zscript/base.zs @@ -186,6 +186,8 @@ enum DrawTextureTags DTA_SrcHeight, DTA_LegacyRenderStyle, // takes an old-style STYLE_* constant instead of an FRenderStyle DTA_Internal3, + DTA_Spacing, // Strings only: Additional spacing between characters + DTA_Monospace, // Strings only: Use a fixed distance between characters. }; class Shape2D : Object native diff --git a/wadsrc/static/zscript/constants.zs b/wadsrc/static/zscript/constants.zs index ad3065fe4..a4b6b756b 100644 --- a/wadsrc/static/zscript/constants.zs +++ b/wadsrc/static/zscript/constants.zs @@ -1344,3 +1344,11 @@ enum ECompatFlags COMPATF2_EXPLODE2 = 1 << 9, // Use original explosion code throughout. COMPATF2_RAILING = 1 << 10, // Bugged Strife railings. }; + +enum EMonospacing +{ + Mono_Off = 0, + Mono_CellLeft = 1, + Mono_CellCenter = 2, + Mono_CellRight = 3 +}; diff --git a/wadsrc/static/zscript/ui/statusbar/alt_hud.zs b/wadsrc/static/zscript/ui/statusbar/alt_hud.zs index 6c4fbbb3f..317999921 100644 --- a/wadsrc/static/zscript/ui/statusbar/alt_hud.zs +++ b/wadsrc/static/zscript/ui/statusbar/alt_hud.zs @@ -120,20 +120,8 @@ class AltHud ui void DrawHudText(Font fnt, int color, String text, int x, int y, double trans = 0.75) { int zerowidth = fnt.GetCharWidth("0"); - - x += zerowidth / 2; - for(int i=0; i < text.length(); i++) - { - int c = text.ByteAt(i); - int width = fnt.GetCharWidth(c); - double offset = fnt.GetBottomAlignOffset(c); - - screen.DrawChar(fnt, color, x, y, c, - DTA_KeepRatio, true, - DTA_VirtualWidth, hudwidth, DTA_VirtualHeight, hudheight, DTA_Alpha, trans, - DTA_LeftOffset, width/2, DTA_TopOffsetF, offset); - x += zerowidth; - } + screen.DrawText(fnt, color, x, y-fnt.GetHeight(), text, DTA_VirtualWidth, hudwidth, DTA_VirtualHeight, hudheight, + DTA_KeepRatio, true, DTA_Alpha, trans, DTA_Monospace, MONO_CellCenter, DTA_Spacing, zerowidth); }