Files
assaultcube-/source/src/console.h
T
2026-10-25 08:51:39 +00:00

118 lines
3.1 KiB
C++

struct cline { char *line; int millis; };
template<class LINE> struct consolebuffer
{
int maxlines;
vector<LINE> conlines;
consolebuffer(int maxlines = 100) : maxlines(maxlines) {}
LINE &addline(const char *sf, int millis) // add a line to the console buffer
{
LINE cl;
cl.line = conlines.length()>maxlines ? conlines.pop().line : newstringbuf(""); // constrain the buffer size
cl.millis = millis; // for how long to keep line on screen
extern void encodeigraphs(char *d, const char *s, int len);
encodeigraphs(cl.line, sf, MAXSTRLEN);
return conlines.insert(0, cl);
}
void setmaxlines(int numlines)
{
maxlines = numlines;
while(conlines.length() > maxlines) delete[] conlines.pop().line;
}
virtual ~consolebuffer()
{
while(conlines.length()) delete[] conlines.pop().line;
}
virtual void render() = 0;
};
struct textinputbuffer
{
string buf;
int pos, max;
textinputbuffer() : pos(-1), max(0)
{
buf[0] = '\0';
}
int maxlen() const
{
return max ? max : sizeof(buf);
}
bool say(const char *c); // returns true if buffer was modified
void pasteclipboard();
bool key(int code) // returns true if buffer was modified
{
switch(code)
{
case SDLK_RETURN:
case SDLK_KP_ENTER:
break;
case SDLK_HOME:
if(strlen(buf)) pos = 0;
break;
case SDLK_END:
pos = -1;
break;
case SDLK_DELETE:
{
int len = (int)strlen(buf);
if(pos<0) break;
int cl = utf8charlen(&buf[pos]); // a multibyte character is removed as a whole
memmove(&buf[pos], &buf[pos+cl], len - pos - cl + 1);
if(pos>=len-cl) pos = -1;
return true;
}
case SDLK_BACKSPACE:
{
int len = (int)strlen(buf), i = pos>=0 ? pos : len;
if(i<1) break;
int p = utf8prev(buf, i); // start of the character before the cursor
memmove(&buf[p], &buf[i], len - i + 1);
if(pos>0) pos = p;
else if(!pos && len<=1) pos = -1;
return true;
}
case SDLK_LEFT:
if(pos > 0) pos = utf8prev(buf, pos);
else if(pos < 0) { int len = (int)strlen(buf); pos = len > 0 ? utf8prev(buf, len) : -1; }
break;
case SDLK_RIGHT:
if(pos>=0)
{
pos += utf8charlen(&buf[pos]);
if(pos>=(int)strlen(buf)) pos = -1;
}
break;
case SDLK_v:
if(SDL_GetModState() & MOD_KEYS_CTRL)
{
pasteclipboard();
return true;
}
break;
default:
break;
}
return false;
}
};