Fix and change text cursor

This commit is contained in:
Jupeyy 2020-10-16 20:00:57 +02:00
parent 28250897d3
commit 186206d40c
3 changed files with 66 additions and 11 deletions

View file

@ -1041,7 +1041,7 @@ public:
pCursor->m_MaxCharacterHeight = maximum(pCursor->m_MaxCharacterHeight, CharHeight + BearingY);
if(NextCharacter == 0 && (m_RenderFlags & TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE) != 0)
if(NextCharacter == 0 && (m_RenderFlags & TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE) != 0 && Character != ' ')
DrawX += BearingX + CharKerning + CharWidth;
else
DrawX += Advance * Size + CharKerning;
@ -1394,7 +1394,7 @@ public:
pCursor->m_MaxCharacterHeight = maximum(pCursor->m_MaxCharacterHeight, CharHeight + BearingY);
if(NextCharacter == 0 && (RenderFlags & TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE) != 0)
if(NextCharacter == 0 && (RenderFlags & TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE) != 0 && Character != ' ')
DrawX += BearingX + CharKerning + CharWidth;
else
DrawX += Advance * Size + CharKerning;
@ -1873,6 +1873,32 @@ public:
return FontSize;
}
virtual float GetGlyphOffsetX(int FontSize, char TextCharacter)
{
CFont *pFont = m_pDefaultFont;
FT_Set_Pixel_Sizes(pFont->m_FtFace, 0, FontSize);
const char *pTmp = &TextCharacter;
int NextCharacter = str_utf8_decode(&pTmp);
if(NextCharacter)
{
FT_Int32 FTFlags = 0;
#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 7 && (FREETYPE_MINOR > 7 || FREETYPE_PATCH >= 1)
FTFlags = FT_LOAD_BITMAP_METRICS_ONLY | FT_LOAD_NO_BITMAP;
#else
FTFlags = FT_LOAD_RENDER | FT_LOAD_NO_BITMAP;
#endif
if(FT_Load_Char(pFont->m_FtFace, NextCharacter, FTFlags))
{
dbg_msg("GetGlyphOffsetX", "error loading glyph %d", NextCharacter);
return -1;
}
return (float)(pFont->m_FtFace->glyph->metrics.horiBearingX >> 6);
}
return 0;
}
virtual int CalculateTextWidth(const char *pText, int TextLength, int FontWidth, int FontHeight)
{
CFont *pFont = m_pDefaultFont;

View file

@ -115,6 +115,8 @@ public:
virtual int TextLineCount(void *pFontSetV, float Size, const char *pText, float LineWidth) = 0;
virtual void OnWindowResize() = 0;
virtual float GetGlyphOffsetX(int FontSize, char TextCharacter) = 0;
};
class IEngineTextRender : public ITextRender

View file

@ -489,29 +489,56 @@ int CMenus::DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrS
UI()->ClipEnable(pRect);
Textbox.x -= *Offset;
int StrLenDispl = str_length(pDisplayStr);
UI()->DoLabel(&Textbox, pDisplayStr, FontSize, -1);
TextRender()->TextColor(1, 1, 1, 1);
float ScreenX0, ScreenY0, ScreenX1, ScreenY1;
Graphics()->GetScreen(&ScreenX0, &ScreenY0, &ScreenX1, &ScreenY1);
float OnePixelWidth = ((ScreenX1 - ScreenX0) / Graphics()->ScreenWidth());
// render the cursor
if(UI()->LastActiveItem() == pID && !JustGotActive)
{
float OffsetGlyph = TextRender()->GetGlyphOffsetX(FontSize, '|');
CUIRect TmpTextBox = Textbox;
if(str_length(aInputing))
{
float OffsetGlyphThis = OffsetGlyph;
if(StrLenDispl == s_AtIndex + Input()->GetEditingCursor() && pDisplayStr[StrLenDispl - 1] != ' ')
OffsetGlyphThis = 0;
float w = TextRender()->TextWidth(0, FontSize, pDisplayStr, s_AtIndex + Input()->GetEditingCursor(), -1.0f);
Textbox = *pRect;
Textbox.VSplitLeft(2.0f, 0, &Textbox);
Textbox.x += (w - *Offset - TextRender()->TextWidth(0, FontSize, "|", -1, -1.0f) / 2);
Textbox.x += w + OffsetGlyphThis;
UI()->DoLabel(&Textbox, "|", FontSize, -1);
Graphics()->TextureClear();
Graphics()->QuadsBegin();
Graphics()->SetColor(0, 0, 0, 0.3f);
IGraphics::CQuadItem CursorTBack(Textbox.x - (OnePixelWidth * 2.0f) / 2.0f, Textbox.y, OnePixelWidth * 2 * 2.0f, Textbox.h);
Graphics()->QuadsDrawTL(&CursorTBack, 1);
Graphics()->SetColor(1, 1, 1, 1);
IGraphics::CQuadItem CursorT(Textbox.x, Textbox.y + OnePixelWidth * 1.5f, OnePixelWidth * 2.0f, Textbox.h - OnePixelWidth * 1.5f * 2);
Graphics()->QuadsDrawTL(&CursorT, 1);
Graphics()->QuadsEnd();
}
if(StrLenDispl == s_AtIndex && pDisplayStr[StrLenDispl - 1] != ' ')
OffsetGlyph = 0;
float w = TextRender()->TextWidth(0, FontSize, pDisplayStr, s_AtIndex, -1.0f);
Textbox = *pRect;
Textbox.VSplitLeft(2.0f, 0, &Textbox);
Textbox.x += (w - *Offset - TextRender()->TextWidth(0, FontSize, "|", -1, -1.0f) / 2);
Textbox = TmpTextBox;
Textbox.x += w + OffsetGlyph;
if((2 * time_get() / time_freq()) % 2) // make it blink
UI()->DoLabel(&Textbox, "|", FontSize, -1);
if((2 * time_get() / time_freq()) % 2)
{
Graphics()->TextureClear();
Graphics()->QuadsBegin();
Graphics()->SetColor(0, 0, 0, 0.3f);
IGraphics::CQuadItem CursorTBack(Textbox.x - (OnePixelWidth * 2.0f) / 2.0f, Textbox.y, OnePixelWidth * 2 * 2.0f, Textbox.h);
Graphics()->QuadsDrawTL(&CursorTBack, 1);
Graphics()->SetColor(1, 1, 1, 1);
IGraphics::CQuadItem CursorT(Textbox.x, Textbox.y + OnePixelWidth * 1.5f, OnePixelWidth * 2.0f, Textbox.h - OnePixelWidth * 1.5f * 2);
Graphics()->QuadsDrawTL(&CursorT, 1);
Graphics()->QuadsEnd();
}
Input()->SetEditingPosition(Textbox.x, Textbox.y + FontSize);
}