Left/right arrow nav in menu fields

This commit is contained in:
BjossiAlfreds 2024-10-16 02:00:53 +00:00
parent 6408cc7099
commit aae0524687
3 changed files with 60 additions and 66 deletions

View file

@ -103,7 +103,6 @@ typedef struct
int cursor; int cursor;
int length; int length;
int visible_length; int visible_length;
int visible_offset;
} menufield_s; } menufield_s;
typedef struct typedef struct

View file

@ -311,17 +311,14 @@ Default_MenuKey(menuframework_s *m, int key)
if (m) if (m)
{ {
menucommon_s *item; menucommon_s *item = Menu_ItemAtCursor(m);
if ((item = Menu_ItemAtCursor(m)) != 0) if (item && item->type == MTYPE_FIELD)
{ {
if (item->type == MTYPE_FIELD) if (Field_Key((menufield_s *)item, key))
{ {
if (Field_Key((menufield_s *)item, key)) return NULL;
{ }
return NULL;
}
}
} }
} }
@ -5237,6 +5234,7 @@ AddressBook_MenuInit(void)
for (i = 0; i < NUM_ADDRESSBOOK_ENTRIES; i++) for (i = 0; i < NUM_ADDRESSBOOK_ENTRIES; i++)
{ {
menufield_s *f;
const cvar_t *adr; const cvar_t *adr;
char buffer[20]; char buffer[20];
@ -5244,19 +5242,22 @@ AddressBook_MenuInit(void)
adr = Cvar_Get(buffer, "", CVAR_ARCHIVE); adr = Cvar_Get(buffer, "", CVAR_ARCHIVE);
s_addressbook_fields[i].generic.type = MTYPE_FIELD; f = &s_addressbook_fields[i];
s_addressbook_fields[i].generic.name = 0;
s_addressbook_fields[i].generic.callback = 0;
s_addressbook_fields[i].generic.x = 0;
s_addressbook_fields[i].generic.y = i * 18 + 0;
s_addressbook_fields[i].generic.localdata[0] = i;
s_addressbook_fields[i].cursor = 0;
s_addressbook_fields[i].length = 60;
s_addressbook_fields[i].visible_length = 30;
strcpy(s_addressbook_fields[i].buffer, adr->string); f->generic.type = MTYPE_FIELD;
f->generic.name = 0;
f->generic.callback = 0;
f->generic.x = 0;
f->generic.y = i * 18 + 0;
f->generic.localdata[0] = i;
Menu_AddItem(&s_addressbook_menu, &s_addressbook_fields[i]); f->length = 60;
f->visible_length = 30;
Q_strlcpy(f->buffer, adr->string, f->length);
f->cursor = strlen(f->buffer);
Menu_AddItem(&s_addressbook_menu, f);
} }
} }

View file

@ -166,7 +166,8 @@ Field_Draw(menufield_s *f)
n = sizeof(tempbuffer); n = sizeof(tempbuffer);
} }
Q_strlcpy(tempbuffer, f->buffer + f->visible_offset, n); i = (f->cursor > f->visible_length) ? (f->cursor - f->visible_length) : 0;
Q_strlcpy(tempbuffer, f->buffer + i, n);
Draw_CharScaled(x + (16 * scale), Draw_CharScaled(x + (16 * scale),
(y - 4) * scale, 18, scale); (y - 4) * scale, 18, scale);
@ -191,30 +192,23 @@ Field_Draw(menufield_s *f)
if (Menu_ItemAtCursor(f->generic.parent) == f) if (Menu_ItemAtCursor(f->generic.parent) == f)
{ {
int offset;
if (f->visible_offset)
{
offset = f->visible_length;
}
else
{
offset = f->cursor;
}
if (((int)(Sys_Milliseconds() / 250)) & 1) if (((int)(Sys_Milliseconds() / 250)) & 1)
{ {
int offset;
if (f->cursor > f->visible_length)
{
offset = f->visible_length;
}
else
{
offset = f->cursor;
}
Draw_CharScaled( Draw_CharScaled(
x + (24 * scale) + (offset * (8 * scale)), x + (24 * scale) + (offset * (8 * scale)),
y * scale, 11, scale); y * scale, 11, scale);
} }
else
{
Draw_CharScaled(
x + (24 * scale) + (offset * (8 * scale)),
y * scale, ' ', scale);
}
} }
} }
@ -223,36 +217,39 @@ extern int keydown[];
qboolean qboolean
Field_Key(menufield_s *f, int key) Field_Key(menufield_s *f, int key)
{ {
if (key > 127) char txt[2];
{
return false;
}
switch (key) switch (key)
{ {
case K_KP_LEFTARROW: case K_KP_LEFTARROW:
case K_LEFTARROW: case K_LEFTARROW:
case K_BACKSPACE:
if (f->cursor > 0) if (f->cursor > 0)
{ {
memmove(&f->buffer[f->cursor - 1],
&f->buffer[f->cursor],
strlen(&f->buffer[f->cursor]) + 1);
f->cursor--; f->cursor--;
if (f->visible_offset)
{
f->visible_offset--;
}
} }
break;
case K_RIGHTARROW:
if (f->buffer[f->cursor] != '\0')
{
f->cursor++;
}
break;
case K_BACKSPACE:
if (f->cursor > 0)
{
Q_strdel(f->buffer, f->cursor - 1, 1);
f->cursor--;
}
break; break;
case K_KP_DEL: case K_KP_DEL:
case K_DEL: case K_DEL:
memmove(&f->buffer[f->cursor], &f->buffer[f->cursor + 1], if (f->buffer[f->cursor] != '\0')
strlen(&f->buffer[f->cursor + 1]) + 1); {
Q_strdel(f->buffer, f->cursor, 1);
}
break; break;
case K_KP_ENTER: case K_KP_ENTER:
@ -261,24 +258,21 @@ Field_Key(menufield_s *f, int key)
case K_TAB: case K_TAB:
return false; return false;
case K_SPACE:
default: default:
if (key > 127)
{
return false;
}
if (!isdigit(key) && (f->generic.flags & QMF_NUMBERSONLY)) if (!isdigit(key) && (f->generic.flags & QMF_NUMBERSONLY))
{ {
return false; return false;
} }
if (f->cursor < f->length) *txt = key;
{ *(txt + 1) = '\0';
f->buffer[f->cursor++] = key;
f->buffer[f->cursor] = 0;
if (f->cursor > f->visible_length) f->cursor += Q_strins(f->buffer, txt, f->cursor, f->length);
{
f->visible_offset++;
}
}
} }
return true; return true;