Skip to content

Commit

Permalink
client: add cursor management for edit field UI, refs #442
Browse files Browse the repository at this point in the history
* Create Text_PaintWithCursor_Ext and CG_Text_PaintWithCursor_Ext
* Add cursor implementation in BG_PanelButton_EditClick
* Add cursor for debriefing chat and limbopanel name editing
  • Loading branch information
Aranud committed Oct 5, 2019
1 parent d8fbd8d commit f1d7039
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 235 deletions.
88 changes: 37 additions & 51 deletions src/cgame/cg_debriefing.c
Expand Up @@ -43,7 +43,7 @@ int QDECL CG_SortPlayersByXP(const void *a, const void *b);
panel_button_text_t debriefPlayerHeadingSmallerFont =
{
0.2f, 0.2f,
{ 0.6f, 0.6f, 0.6f, 1.f },
{ 0.6f, 0.6f,0.6f, 1.f },
0, 0,
&cgs.media.limboFont2,
};
Expand Down Expand Up @@ -505,7 +505,7 @@ panel_button_t debriefPlayerListScroll =
panel_button_text_t debriefPlayerInfoFont =
{
0.2f, 0.2f,
{ 0.6f, 0.6f, 0.6f, 1.f },
{ 0.6f, 0.6f,0.6f, 1.f },
0, 0,
&cgs.media.limboFont2,
};
Expand Down Expand Up @@ -642,11 +642,11 @@ panel_button_t debriefPlayerInfoHitRegions =
{
NULL,
NULL,
{ 146, 156, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
&debriefPlayerInfoFont, // font
NULL, // keyDown
NULL, // keyUp
{ 146, 156, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
&debriefPlayerInfoFont, // font
NULL, // keyDown
NULL, // keyUp
CG_Debriefing_PlayerHitRegions_Draw,
NULL,
};
Expand All @@ -655,8 +655,8 @@ panel_button_t debriefPlayerInfoHitRegions =
panel_button_t debriefPlayerInfoSkills ## number = { \
NULL, \
NULL, \
{ 24, 136 + (number * 14), 12, 12 }, \
{ number, 0, 0, 0, 0, 0, 0, 0}, \
{ 24, 136 + (number * 14), 12, 12 }, \
{ number, 0, 0, 0, 0, 0, 0, 0}, \
&debriefPlayerInfoFont, /* font */ \
NULL, /* keyDown */ \
NULL, /* keyUp */ \
Expand Down Expand Up @@ -844,8 +844,8 @@ panel_button_t chatPanelVoteButton =
{
NULL,
"MAP VOTE",
{ SCREEN_WIDTH - 10 - 60 - 4 - 60 - 4, SCREEN_HEIGHT - 30, 60, 16 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ SCREEN_WIDTH - 10 - 60 - 4 - 60 - 4,SCREEN_HEIGHT - 30, 60, 16 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
NULL, // font
CG_Debriefing_VoteButton_KeyDown,// keyDown
NULL, // keyUp
Expand All @@ -858,8 +858,8 @@ panel_button_t chatPanelQCButton =
{
NULL,
"QUICK CHAT",
{ SCREEN_WIDTH - 10 - 60 - 4 - 60 - 4 - 80 - 4, SCREEN_HEIGHT - 30, 80, 16 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ SCREEN_WIDTH - 10 - 60 - 4 - 60 - 4 - 80 - 4,SCREEN_HEIGHT - 30, 80, 16 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
NULL, // font
CG_Debriefing_QCButton_KeyDown,// keyDown
NULL, // keyUp
Expand All @@ -872,8 +872,8 @@ panel_button_t chatPanelReadyButton =
{
NULL,
"READY",
{ SCREEN_WIDTH - 10 - 60 - 4 - 60 - 4 - 80 - 4 - 60 - 4, SCREEN_HEIGHT - 30, 60, 16 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ SCREEN_WIDTH - 10 - 60 - 4 - 60 - 4 - 80 - 4 - 60 - 4,SCREEN_HEIGHT - 30, 60, 16 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
NULL, // font
CG_Debriefing_ReadyButton_KeyDown,// keyDown
NULL, // keyUp
Expand Down Expand Up @@ -1100,7 +1100,7 @@ void CG_MapVoteList_Draw(panel_button_t *button)
CG_Text_Paint_Ext(DB_MAPVOTE_X + cgs.wideXoffset, y, button->font->scalex,
button->font->scaley, button->font->colour,
va("%3d%% (%d)", cgs.dbMapVotesSum > 0 ? 100 * cgs.dbMapVotes[i + cgs.dbMapVoteListOffset] / cgs.dbMapVotesSum : 0,
cgs.dbMapVotes[i + cgs.dbMapVoteListOffset]),
cgs.dbMapVotes[i + cgs.dbMapVoteListOffset]),
0, 0, 0, button->font->font);
y += 12;
}
Expand Down Expand Up @@ -1235,8 +1235,8 @@ panel_button_t mapVoteHeadingVotes =
{
NULL,
"Votes",
{ DB_MAPVOTE_X, DB_MAPVOTE_Y, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ DB_MAPVOTE_X, DB_MAPVOTE_Y, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0},
&mapVoteFont, // font
NULL, // keyDown
NULL, // keyUp
Expand Down Expand Up @@ -1378,28 +1378,12 @@ panel_button_t mapVoteBorder3 =
*/
void CG_Debriefing_ChatEdit_Draw(panel_button_t *button)
{
int offset = -1;
char buffer[256 + 1];
const char *cs;
int offset = -1;
char buffer[MAX_EDITFIELD + 1];
vec4_t *color;

trap_Cvar_VariableStringBuffer(button->text, buffer, sizeof(buffer));

if ((cg.time / 1000) % 2)
{
if (trap_Key_GetOverstrikeMode())
{
Q_strcat(buffer, sizeof(buffer), "^7|");
}
else
{
Q_strcat(buffer, sizeof(buffer), "^7_");
}
}
else
{
Q_strcat(buffer, sizeof(buffer), " ");
}

do
{
offset++;
Expand All @@ -1413,20 +1397,20 @@ void CG_Debriefing_ChatEdit_Draw(panel_button_t *button)
switch (cgs.dbChatMode)
{
case 0:
cs = va("^7%s", buffer + offset);
color = &colorWhite;
break;
case 1:
cs = va("^5%s", buffer + offset);
color = &colorCyan;
break;
case 2:
cs = va("^3%s", buffer + offset);
color = &colorYellow;
break;
default:
cs = "";
color = &button->font->colour;
break;
}

CG_Text_Paint_Ext(button->rect.x, button->rect.y + button->rect.h, button->font->scalex, button->font->scaley, button->font->colour, cs, 0, 0, button->font->style, button->font->font);
CG_Text_PaintWithCursor_Ext(button->rect.x, button->rect.y + button->rect.h, button->font->scalex, *color, va("%s", buffer + offset), button->data[2], trap_Key_GetOverstrikeMode() ? "_" : "|", offset, button->font->style, button->font->font);
}

/**
Expand Down Expand Up @@ -1493,7 +1477,7 @@ void CG_Debriefing_ChatBox_Draw(panel_button_t *button)
panel_button_t *chatPanelButtons[] =
{
&chatPanelWindow, &chatPanelText,
&chatPanelNextButton, &chatPanelVoteButton, &chatPanelQCButton, &chatTypeButton, &chatPanelReadyButton,
&chatPanelNextButton, &chatPanelVoteButton,&chatPanelQCButton, &chatTypeButton, &chatPanelReadyButton,
&charPanelEditSurround, &charPanelEdit,
NULL
};
Expand Down Expand Up @@ -2602,13 +2586,13 @@ qhandle_t imgL;
/**
* @brief
*/
void CG_Debriefing_PlayerHitRegions_Draw(panel_button_t* button)
void CG_Debriefing_PlayerHitRegions_Draw(panel_button_t *button)
{
int totalHits = cgs.dbHitRegions[HR_HEAD] + cgs.dbHitRegions[HR_ARMS] + cgs.dbHitRegions[HR_BODY] + cgs.dbHitRegions[HR_LEGS];
float hitsHead = (totalHits && cgs.dbHitRegions[HR_HEAD]) ? (cgs.dbHitRegions[HR_HEAD]/(float)totalHits) : 0.0f;
float hitsArms = (totalHits && cgs.dbHitRegions[HR_ARMS]) ? (cgs.dbHitRegions[HR_ARMS]/(float)totalHits) : 0.0f;
float hitsBody = (totalHits && cgs.dbHitRegions[HR_BODY]) ? (cgs.dbHitRegions[HR_BODY]/(float)totalHits) : 0.0f;
float hitsLegs = (totalHits && cgs.dbHitRegions[HR_LEGS]) ? (cgs.dbHitRegions[HR_LEGS]/(float)totalHits) : 0.0f;
float hitsHead = (totalHits && cgs.dbHitRegions[HR_HEAD]) ? (cgs.dbHitRegions[HR_HEAD] / (float)totalHits) : 0.0f;
float hitsArms = (totalHits && cgs.dbHitRegions[HR_ARMS]) ? (cgs.dbHitRegions[HR_ARMS] / (float)totalHits) : 0.0f;
float hitsBody = (totalHits && cgs.dbHitRegions[HR_BODY]) ? (cgs.dbHitRegions[HR_BODY] / (float)totalHits) : 0.0f;
float hitsLegs = (totalHits && cgs.dbHitRegions[HR_LEGS]) ? (cgs.dbHitRegions[HR_LEGS] / (float)totalHits) : 0.0f;
float alphaH = hitsHead > 0.f ? (hitsHead * 0.8f) + 0.2f : 0.0f;
float alphaA = hitsArms > 0.f ? (hitsArms * 0.8f) + 0.2f : 0.0f;
float alphaB = hitsBody > 0.f ? (hitsBody * 0.8f) + 0.2f : 0.0f;
Expand Down Expand Up @@ -2670,7 +2654,7 @@ void CG_Debriefing_PlayerHitRegions_Draw(panel_button_t* button)

if (alphaA)
{
Vector4Set(colorA, 1.0f, 0.f, 0.f, alphaA );
Vector4Set(colorA, 1.0f, 0.f, 0.f, alphaA);
trap_R_SetColor(colorA);
CG_DrawPic(button->rect.x + 4, button->rect.y + 12, 54, 54, imgA);
trap_R_SetColor(NULL);
Expand Down Expand Up @@ -3127,8 +3111,8 @@ void CG_Debriefing_NextButton_Draw(panel_button_t *button)
*/
void CG_Debriefing_ChatEditFinish(panel_button_t *button)
{
char buffer[256];
trap_Cvar_VariableStringBuffer(button->text, buffer, 256);
char buffer[MAX_EDITFIELD];
trap_Cvar_VariableStringBuffer(button->text, buffer, MAX_EDITFIELD);

switch (cgs.dbChatMode)
{
Expand All @@ -3143,6 +3127,8 @@ void CG_Debriefing_ChatEditFinish(panel_button_t *button)
break;
}

button->data[2] = 0;

trap_Cvar_Set(button->text, "");
}

Expand Down
127 changes: 125 additions & 2 deletions src/cgame/cg_draw.c
Expand Up @@ -208,8 +208,6 @@ void CG_Text_PaintChar_Ext(float x, float y, float w, float h, float scalex, flo
* @param[in] s2
* @param[in] t2
* @param[in] hShader
*
* @note Unused
*/
void CG_Text_PaintChar(float x, float y, float width, float height, float scale, float s, float t, float s2, float t2, qhandle_t hShader)
{
Expand Down Expand Up @@ -334,6 +332,131 @@ void CG_Text_Paint(float x, float y, float scale, vec4_t color, const char *text
CG_Text_Paint_Ext(x, y, scale, scale, color, text, adjust, limit, style, font);
}

/**
* @brief CG_Text_PaintWithCursor_Ext
* @param[in] x
* @param[in] y
* @param[in] scale
* @param[in] color
* @param[in] text
* @param[in] cursorPos
* @param[in] cursor
* @param[in] limit
* @param[in] style
*/
void CG_Text_PaintWithCursor_Ext(float x, float y, float scale, vec4_t color, const char *text, int cursorPos, const char *cursor, int limit, int style, fontHelper_t *font)
{
vec4_t newColor = { 0, 0, 0, 0 };
glyphInfo_t *glyph, *glyph2;
float useScale = scale * Q_UTF8_GlyphScale(font);

if (text)
{
float yadj;
int len = Q_UTF8_Strlen(text);
int count = 0;
const char *s = text;

trap_R_SetColor(color);
Com_Memcpy(&newColor[0], &color[0], sizeof(vec4_t));

if (limit > 0 && len > limit)
{
len = limit;
}

glyph2 = Q_UTF8_GetGlyph(font, cursor);
while (s && *s && count < len)
{
glyph = Q_UTF8_GetGlyph(font, s);
yadj = useScale * glyph->top;

if (style == ITEM_TEXTSTYLE_SHADOWED || style == ITEM_TEXTSTYLE_SHADOWEDMORE)
{
int ofs = style == ITEM_TEXTSTYLE_SHADOWED ? 1 : 2;

colorBlack[3] = newColor[3];
trap_R_SetColor(colorBlack);
CG_Text_PaintChar(x + (glyph->pitch * useScale) + ofs, y - yadj + ofs,
glyph->imageWidth,
glyph->imageHeight,
useScale,
glyph->s,
glyph->t,
glyph->s2,
glyph->t2,
glyph->glyph);
colorBlack[3] = 1.0;
trap_R_SetColor(newColor);
}
CG_Text_PaintChar(x + (glyph->pitch * useScale), y - yadj,
glyph->imageWidth,
glyph->imageHeight,
useScale,
glyph->s,
glyph->t,
glyph->s2,
glyph->t2,
glyph->glyph);

// CG_DrawPic(x, y - yadj, scale * uiDC.Assets.textFont.glyphs[text[i]].imageWidth, scale * uiDC.Assets.textFont.glyphs[text[i]].imageHeight, uiDC.Assets.textFont.glyphs[text[i]].glyph);
yadj = useScale * glyph2->top;
if (count == cursorPos && !((cgDC.realTime / BLINK_DIVISOR) & 1))
{
CG_Text_PaintChar(x + (glyph->pitch * useScale), y - yadj,
glyph2->imageWidth,
glyph2->imageHeight,
useScale,
glyph2->s,
glyph2->t,
glyph2->s2,
glyph2->t2,
glyph2->glyph);
}

x += (glyph->xSkip * useScale);
s += Q_UTF8_Width(s);
count++;

}
// need to paint cursor at end of text
if (cursorPos == len && !((cgDC.realTime / BLINK_DIVISOR) & 1))
{
yadj = useScale * glyph2->top;
CG_Text_PaintChar(x + (glyph2->pitch * useScale), y - yadj,
glyph2->imageWidth,
glyph2->imageHeight,
useScale,
glyph2->s,
glyph2->t,
glyph2->s2,
glyph2->t2,
glyph2->glyph);
}

trap_R_SetColor(NULL);
}
}

/**
* @brief CG_Text_PaintWithCursor
* @param[in] x
* @param[in] y
* @param[in] scale
* @param[in] color
* @param[in] text
* @param[in] cursorPos
* @param[in] cursor
* @param[in] limit
* @param[in] style
*/
void CG_Text_PaintWithCursor(float x, float y, float scale, vec4_t color, const char *text, int cursorPos, const char *cursor, int limit, int style)
{
fontHelper_t *font = &cgDC.Assets.fonts[activeFont];

CG_Text_PaintWithCursor_Ext(x, y, scale, color, text, cursorPos, cursor, limit, style, font);
}

/**
* @brief CG_DrawTeamBackground
* @param[in] x
Expand Down
6 changes: 4 additions & 2 deletions src/cgame/cg_limbopanel.c
Expand Up @@ -1222,10 +1222,12 @@ void CG_LimboPanel_BriefingButton_Draw(panel_button_t *button)
*/
void CG_LimboPanel_NameEditFinish(panel_button_t *button)
{
char buffer[256];
char buffer[MAX_EDITFIELD];

trap_Cvar_VariableStringBuffer(button->text, buffer, 256);
trap_Cvar_VariableStringBuffer(button->text, buffer, MAX_EDITFIELD);
trap_Cvar_Set("name", buffer);

BG_PanelButtons_SetFocusButton(NULL);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/cgame/cg_local.h
Expand Up @@ -2747,6 +2747,8 @@ void CG_DrawTeamBackground(int x, int y, int w, int h, float alpha, int team);
void CG_Text_Paint_Ext(float x, float y, float scalex, float scaley, vec4_t color, const char *text, float adjust, int limit, int style, fontHelper_t *font);
void CG_Text_Paint_Centred_Ext(float x, float y, float scalex, float scaley, vec4_t color, const char *text, float adjust, int limit, int style, fontHelper_t *font);
void CG_Text_Paint(float x, float y, float scale, vec4_t color, const char *text, float adjust, int limit, int style);
void CG_Text_PaintWithCursor_Ext(float x, float y, float scale, vec4_t color, const char *text, int cursorPos, const char *cursor, int limit, int style, fontHelper_t *font);
void CG_Text_PaintWithCursor(float x, float y, float scale, vec4_t color, const char *text, int cursorPos, const char *cursor, int limit, int style);
void CG_Text_SetActiveFont(int font);
int CG_Text_Width_Ext(const char *text, float scale, int limit, fontHelper_t *font);
int CG_Text_Width(const char *text, float scale, int limit);
Expand Down

0 comments on commit f1d7039

Please sign in to comment.