Skip to content

Commit

Permalink
- moved FormatNumber to the generic base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Oct 31, 2020
1 parent fd6b7f9 commit 3f61ab7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
45 changes: 45 additions & 0 deletions src/g_statusbar/base_sbar.cpp
Expand Up @@ -42,6 +42,7 @@
#include "cmdlib.h"
#include "texturemanager.h"
#include "c_cvars.h"
#include "vm.h"

FGameTexture* CrosshairImage;
static int CrosshairNum;
Expand Down Expand Up @@ -196,3 +197,47 @@ void ST_DrawCrosshair(int phealth, double xpos, double ypos, double scale)
}


//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------

enum ENumFlags
{
FNF_WHENNOTZERO = 0x1,
FNF_FILLZEROS = 0x2,
};

void FormatNumber(int number, int minsize, int maxsize, int flags, const FString& prefix, FString* result)
{
static int maxvals[] = { 1, 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999 };

if (number == 0 && (flags & FNF_WHENNOTZERO))
{
*result = "";
return;
}
if (maxsize > 0 && maxsize < 10)
{
number = clamp(number, -maxvals[maxsize - 1], maxvals[maxsize]);
}
FString& fmt = *result;
if (minsize <= 1) fmt.Format("%s%d", prefix.GetChars(), number);
else if (flags & FNF_FILLZEROS) fmt.Format("%s%0*d", prefix.GetChars(), minsize, number);
else fmt.Format("%s%*d", prefix.GetChars(), minsize, number);
}

DEFINE_ACTION_FUNCTION_NATIVE(DStatusBarCore, FormatNumber, FormatNumber)
{
PARAM_PROLOGUE;
PARAM_INT(number);
PARAM_INT(minsize);
PARAM_INT(maxsize);
PARAM_INT(flags);
PARAM_STRING(prefix);
FString fmt;
FormatNumber(number, minsize, maxsize, flags, prefix, &fmt);
ACTION_RETURN_STRING(fmt);
}

29 changes: 0 additions & 29 deletions src/g_statusbar/shared_sbar.cpp
Expand Up @@ -1092,10 +1092,6 @@ void DBaseStatusBar::RefreshBackground () const

void DBaseStatusBar::DrawCrosshair ()
{
uint32_t color;
double size;
int w, h;

if (!crosshairon)
{
return;
Expand Down Expand Up @@ -1950,31 +1946,6 @@ static DObject *InitObject(PClass *type, int paramnum, VM_ARGS)



enum ENumFlags
{
FNF_WHENNOTZERO = 0x1,
FNF_FILLZEROS = 0x2,
};

void FormatNumber(int number, int minsize, int maxsize, int flags, const FString &prefix, FString *result)
{
static int maxvals[] = { 1, 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999 };

if (number == 0 && (flags & FNF_WHENNOTZERO))
{
*result = "";
return;
}
if (maxsize > 0 && maxsize < 10)
{
number = clamp(number, -maxvals[maxsize - 1], maxvals[maxsize]);
}
FString &fmt = *result;
if (minsize <= 1) fmt.Format("%s%d", prefix.GetChars(), number);
else if (flags & FNF_FILLZEROS) fmt.Format("%s%0*d", prefix.GetChars(), minsize, number);
else fmt.Format("%s%*d", prefix.GetChars(), minsize, number);
}

//---------------------------------------------------------------------------
//
// Weapons List
Expand Down
15 changes: 0 additions & 15 deletions src/scripting/vmthunks.cpp
Expand Up @@ -2295,21 +2295,6 @@ DEFINE_ACTION_FUNCTION_NATIVE(DBaseStatusBar, GetGlobalACSArrayValue, GetGlobalA
ACTION_RETURN_INT(ACS_GlobalArrays[arrayno][index]);
}

void FormatNumber(int number, int minsize, int maxsize, int flags, const FString &prefix, FString *result);

DEFINE_ACTION_FUNCTION_NATIVE(DBaseStatusBar, FormatNumber, FormatNumber)
{
PARAM_PROLOGUE;
PARAM_INT(number);
PARAM_INT(minsize);
PARAM_INT(maxsize);
PARAM_INT(flags);
PARAM_STRING(prefix);
FString fmt;
FormatNumber(number, minsize, maxsize, flags, prefix, &fmt);
ACTION_RETURN_STRING(fmt);
}

static void ReceivedWeapon(DBaseStatusBar *self)
{
self->mugshot.Grin();
Expand Down
2 changes: 1 addition & 1 deletion wadsrc/static/zscript/ui/statusbar/statusbar.zs
@@ -1,5 +1,6 @@
class StatusBarCore native
{
native static String FormatNumber(int number, int minsize = 0, int maxsize = 0, int format = 0, String prefix = "");
}

struct MugShot
Expand Down Expand Up @@ -354,7 +355,6 @@ class BaseStatusBar : StatusBarCore native ui
native void DrawString(HUDFont font, String string, Vector2 pos, int flags = 0, int translation = Font.CR_UNTRANSLATED, double Alpha = 1., int wrapwidth = -1, int linespacing = 4, Vector2 scale = (1, 1));
native double, double, double, double TransformRect(double x, double y, double w, double h, int flags = 0);
native void Fill(Color col, double x, double y, double w, double h, int flags = 0);
native static String FormatNumber(int number, int minsize = 0, int maxsize = 0, int format = 0, String prefix = "");
native double, double, double, double StatusbarToRealCoords(double x, double y=0, double w=0, double h=0);
native int GetTopOfStatusBar();
native void SetClipRect(double x, double y, double w, double h, int flags = 0);
Expand Down

0 comments on commit 3f61ab7

Please sign in to comment.