From aa6aa20ba5a3cecbf253e8490204b36ebeefe286 Mon Sep 17 00:00:00 2001 From: linshier Date: Wed, 1 Dec 2010 08:01:13 +0000 Subject: [PATCH] Fixed left offset parameter handling for center/right align string drawing. A brief note about string drawing, offset and display width is added in the WFont.h --- JGE/src/JLBFont.cpp | 25 +++++++++++++++++++------ projects/mtg/include/WFont.h | 10 ++++++++-- projects/mtg/src/WFont.cpp | 32 ++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/JGE/src/JLBFont.cpp b/JGE/src/JLBFont.cpp index 312b34077..85d13ebae 100644 --- a/JGE/src/JLBFont.cpp +++ b/JGE/src/JLBFont.cpp @@ -113,10 +113,22 @@ void JLBFont::DrawString(const char *string, float x, float y, int align, float float width = GetStringWidth(string); - if (align == JGETEXT_RIGHT) - dx0 -= width; - else if (align == JGETEXT_CENTER) - dx0 -= width/2; + if (align == JGETEXT_RIGHT) { + if (displayWidth) { + dx0 -= displayWidth; + leftOffset += width - displayWidth; + } + else + dx0 -= width; + } + else if (align == JGETEXT_CENTER) { + if (displayWidth) { + dx0 -= displayWidth/2; + leftOffset += width/2 - displayWidth/2; + } + else + dx0 -= width/2; + } float dx = floorf(dx0); dy = floorf(dy); @@ -147,7 +159,7 @@ void JLBFont::DrawString(const char *string, float x, float y, int align, float if (leftOffset < 0){ dx-=leftOffset; leftOffset = 0; - continue; + //continue; }else if (leftOffset - delta > 0){ leftOffset -= delta; p++; @@ -160,7 +172,8 @@ void JLBFont::DrawString(const char *string, float x, float y, int align, float charWidth = (delta/mScale) - mTracking; } } - else if (displayWidth){ + if (displayWidth){ + // This condiction must be tested indepently in the case leftOffset and displayWidth need a fix at the same time. if (dx > x0+displayWidth) return; if (dx+delta > x0+displayWidth) { delta = x0 + displayWidth - dx; diff --git a/projects/mtg/include/WFont.h b/projects/mtg/include/WFont.h index de1abff7a..a2ac19403 100644 --- a/projects/mtg/include/WFont.h +++ b/projects/mtg/include/WFont.h @@ -28,8 +28,14 @@ class WFont public: int mFontID; // Rendering text to screen. - virtual void DrawString(const char *s, float x, float y, int align=JGETEXT_LEFT, float leftOffset = 0, float width = 0) = 0; - virtual void DrawString(std::string s, float x, float y, int align=JGETEXT_LEFT, float leftOffset = 0, float width = 0) = 0; + // Note: + // align=JGETEXT_LEFT, string region (x-leftOffset, x-leftOffset+StringWidth), display window (x, x+displayWidth) + // align=JGETEXT_CENTER, string region (x-leftOffset-StringWidth/2, x-leftOffset+StringWidth/2), display window (x-displayWidth/2, x+displayWidth/2) + // align=JGETEXT_RIGHT, string region (x-leftOffset-StringWidth, x-leftOffset), display window (x-displayWidth, x) + // Only when width is NOT zero, characters outside the display window are not rendered. + // + virtual void DrawString(const char *s, float x, float y, int align=JGETEXT_LEFT, float leftOffset = 0, float displayWidth = 0) = 0; + virtual void DrawString(std::string s, float x, float y, int align=JGETEXT_LEFT, float leftOffset = 0, float displayWidth = 0) = 0; // Set font color. virtual void SetColor(PIXEL_TYPE color) = 0; // Get font color. diff --git a/projects/mtg/src/WFont.cpp b/projects/mtg/src/WFont.cpp index e3aa55872..da345b0fe 100644 --- a/projects/mtg/src/WFont.cpp +++ b/projects/mtg/src/WFont.cpp @@ -386,10 +386,20 @@ void WFBFont::DrawString(const char *s, float x, float y, int align, float leftO switch (align) { case JGETEXT_RIGHT: - x -= GetStringWidth(s); + if (width) { + x -= width; + leftOffset += GetStringWidth(s) - width; + } + else + x -= GetStringWidth(s); break; case JGETEXT_CENTER: - x -= GetStringWidth(s) / 2; + if (width) { + x -= width/2; + leftOffset += GetStringWidth(s)/2 - width/2; + } + else + x -= GetStringWidth(s)/2; break; case JGETEXT_LEFT: default: @@ -473,7 +483,7 @@ void WFBFont::DrawString(const char *s, float x, float y, int align, float leftO charW = delta / mScale; } } - else if (width) + if (width) { if (xx > x + width) return; if (xx + delta > x + width) @@ -778,10 +788,20 @@ void WGBKFont::DrawString(const char *s, float x, float y, int align, float left switch (align) { case JGETEXT_RIGHT: - x -= GetStringWidth(s); + if (width) { + x -= width; + leftOffset += GetStringWidth(s) - width; + } + else + x -= GetStringWidth(s); break; case JGETEXT_CENTER: - x -= GetStringWidth(s) / 2; + if (width) { + x -= width/2; + leftOffset += GetStringWidth(s)/2 - width/2; + } + else + x -= GetStringWidth(s)/2; break; case JGETEXT_LEFT: default: @@ -882,7 +902,7 @@ void WGBKFont::DrawString(const char *s, float x, float y, int align, float left charW = delta / mScale; } } - else if (width) + if (width) { if (xx > x + width) return; if (xx + delta > x + width)