#include <SDL.h>
#include "private.h"
#include "drawing.h"
#include "font.h"
#include "world.h"
static const char passwordPattern[] = "\u25CF";
static int utf8charlen(const char *c) {
if (!*c) return 0;
if ( (c[0] & 0x80) == 0x00 )
return 1;
if ( (c[0] & 0xE0) == 0xC0
&& (c[1] & 0xC0) == 0x80 )
return 2;
if ( (c[0] & 0xF0) == 0xE0
&& (c[1] & 0xC0) == 0x80
&& (c[2] & 0xC0) == 0x80 )
return 3;
if ( (c[0] & 0xF8) == 0xF0
&& (c[1] & 0xC0) == 0x80
&& (c[2] & 0xC0) == 0x80
&& (c[3] & 0xC0) == 0x80 )
return 4;
return -1;
}
static int utf8len(const char *c) {
int len = 0;
while(*c) {
int l = utf8charlen(c);
c += l > 0 ? l : 1;
++len;
}
return len;
}
static int utf8pos(const char *c, int pos) {
int p = 0;
const char *end = c + pos;
while(*c && c < end) {
int l = utf8charlen(c);
c += l > 0 ? l : 1;
++p;
}
return p;
}
static int utf8shift(const char *s, int pos, int offset) {
int len = strlen(s);
if (pos > len) { offset += pos - len; pos = len; }
if (pos < 0) { offset += pos; pos = 0; }
while(offset > 0 && pos < len) {
int l = utf8charlen(s + pos);
if (l > 0) {
pos += l;
} else {
while(pos < len && utf8charlen(s + pos) <= 0) ++pos;
}
--offset;
}
while(offset < 0 && pos > 0) {
do --pos; while(pos > 0 && utf8charlen(s + pos) <= 0);
++offset;
}
return pos;
}
static void fixPos(HeliDialog *dialog) {
int answerLen = strlen(dialog->answer);
if (dialog->pos < 0) dialog->pos = 0;
if (dialog->pos > answerLen) dialog->pos = answerLen;
if (dialog->selPos < 0) dialog->selPos = 0;
if (dialog->selPos > answerLen) dialog->selPos = answerLen;
}
static void insert(HeliDialog *dialog, const char *text) {
int answerLen = strlen(dialog->answer);
fixPos(dialog);
int pos0 = dialog->pos < dialog->selPos ? dialog->pos : dialog->selPos;
int pos1 = dialog->pos < dialog->selPos ? dialog->selPos : dialog->pos;
int selLen = pos1 - pos0;
int textLen = strlen(text);
int dl = answerLen - selLen + textLen - dialog->maxAnswerSize;
if (dl > 0) textLen -= dl;
if (textLen < 0) textLen = 0;
int tailPos = pos0 + selLen;
int offset = textLen - selLen;
int tailLen = answerLen - tailPos;
if (offset && tailLen > 0)
memmove(
dialog->answer + tailPos + offset,
dialog->answer + tailPos,
tailLen);
tailPos += offset;
dialog->answer[tailPos + tailLen] = 0;
if (textLen > 0)
memcpy(dialog->answer + pos0, text, textLen);
dialog->pos = dialog->selPos = tailPos;
}
static void copy(HeliDialog *dialog) {
if (!dialog->password && dialog->pos != dialog->selPos) {
int pos0 = dialog->pos < dialog->selPos ? dialog->pos : dialog->selPos;
int pos1 = dialog->pos < dialog->selPos ? dialog->selPos : dialog->pos;
char c = dialog->answer[pos1];
dialog->answer[pos1] = 0;
SDL_SetClipboardText(dialog->answer + pos0);
dialog->answer[pos1] = c;
}
}
static void paste(HeliDialog *dialog) {
const char *text = SDL_GetClipboardText();
if (text && text[0]) insert(dialog, text);
}
static void draw(HeliDialog *dialog) {
saveState();
const double time = SDL_GetTicks()*1e-3;
const double w = worldGetWidth();
const double h = worldGetHeight();
const double border = 16;
double title = 64;
double buttons = 32;
double l = border;
double t = border + title;
double r = w - border;
double b = h - border - buttons;
if (!dialog->multiline && b - t > buttons) {
t = round(0.5*(b + t - buttons));
b = t + buttons;
title = t - border;
}
const double cursorSrcollWidth = 64;
const double cursorSrcollHeight = 16;
const double bt = h - buttons - border + 8;
const double bb = h - border;
const double bh = bb - bt;
double bw = (w - 2*border)/4;
if (bw < bh) bw = bh;
const double bl0 = border - 2;
const double br0 = bl0 + bw;
const double br1 = w - border + 2;
const double bl1 = br1 - bw;
unsigned int strokeColor = colorByName("white");
unsigned int fillColor = colorByName("0.3 0.3 0.3");
unsigned int selTextColor = colorByName("black");
unsigned int selFillColor = colorByName("white");
strokeWidth(1);
textFontDefault();
textSize(16);
noStroke();
fill(fillColor);
rect(0, 0, w, h);
noFill();
stroke(strokeColor);
textAlign(HALIGN_LEFT, VALIGN_TOP);
TextLayout layout = createTextLayout(dialog->password ? dialog->passwordText : dialog->answer);
int shift = keyDown("left shift") || keyDown("right shift");
if (keyWentDown("up")) {
dialog->pos = textLayoutCursorUp(layout, dialog->pos);
if (!shift) dialog->selPos = dialog->pos;
}
if (keyWentDown("down")) {
dialog->pos = textLayoutCursorDown(layout, dialog->pos);
if (!shift) dialog->selPos = dialog->pos;
}
int pos = dialog->pos;
int selPos = dialog->selPos;
if (dialog->password) {
pos = utf8pos(dialog->answer, pos)*((int)sizeof(passwordPattern) - 1);
selPos = utf8pos(dialog->answer, selPos)*((int)sizeof(passwordPattern) - 1);
}
cliprect(l-2, t-2, r-l+2, b-t+2);
double tx = l;
double ty = t;
double cx = textLayoutCursorGetX(layout, pos) + tx;
double cy = textLayoutCursorGetY(layout, pos) + ty;
double ch = textLayoutCursorGetHeight(layout, pos);
double minScrollY = t + ch + cursorSrcollHeight - cy;
double maxScrollY = b - cursorSrcollHeight - cy;
if (maxScrollY > 0) maxScrollY = 0;
if (dialog->scrollY < minScrollY) dialog->scrollY = minScrollY;
if (dialog->scrollY > maxScrollY) dialog->scrollY = maxScrollY;
double minScrollX = l + cursorSrcollWidth - cx;
double maxScrollX = r - cursorSrcollWidth - cx;
if (maxScrollX > 0) maxScrollX = 0;
if (dialog->scrollX < minScrollX) dialog->scrollX = minScrollX;
if (dialog->scrollX > maxScrollX) dialog->scrollX = maxScrollX;
tx += dialog->scrollX;
ty += dialog->scrollY;
cx += dialog->scrollX;
cy += dialog->scrollY;
if (pos == selPos) {
textLayoutDraw(layout, tx, ty);
} else {
int p0 = pos < selPos ? pos : selPos;
int p1 = pos < selPos ? selPos : pos;
fill(selFillColor);
stroke(selTextColor);
textLayoutDrawSubstr(layout, tx, ty, p0, p1 - p0);
noFill();
stroke(strokeColor);
textLayoutDrawSubstr(layout, tx, ty, 0, p0);
textLayoutDrawFrom(layout, tx, ty, p1);
}
stroke(colorByRGBA(1, 1, 1, 0.5 + 0.5*sin(time/0.5*2*PI)));
line(cx, cy, cx, cy - ch);
textLayoutDestroy(layout);
noClip();
noFill();
stroke(strokeColor);
rect(l - 2, t - 2, r - l + 4, b - t + 4);
textAlign(HALIGN_CENTER, VALIGN_CENTER);
text(dialog->question, w/2, border + title/2);
rect(bl0, bt, bw, bh);
rect(bl1, bt, bw, bh);
text("<<", (bl0 + br0)/2, (bt + bb)/2);
text("\u23CE", (bl1 + br1)/2, (bt + bb)/2);
if (mouseWentDown("left")) {
double x = mouseX();
double y = mouseY();
if (y >= bt && y <= bb) {
if (x >= bl0 && x <= br0) dialog->shown = FALSE;
if (x >= bl1 && x <= br1) { dialog->success = TRUE; dialog->shown = FALSE; }
}
}
restoreState();
}
void heliDialogDraw(HeliDialog *dialog) {
if (dialog->newText[0]) insert(dialog, dialog->newText);
int shift = keyDown("left shift") || keyDown("right shift");
int ctrl = keyDown("left ctrl") || keyDown("right ctrl");
if (keyWentDown("backspace")) {
if (dialog->pos == dialog->selPos) {
dialog->selPos = dialog->pos;
dialog->pos = utf8shift(dialog->answer, dialog->pos, -1);
}
insert(dialog, "");
}
if (keyWentDown("delete")) {
if (dialog->pos == dialog->selPos) {
dialog->selPos = dialog->pos;
dialog->pos = utf8shift(dialog->answer, dialog->pos, 1);
} else
if (shift && !dialog->password) {
copy(dialog);
}
insert(dialog, "");
}
if (keyWentDown("left")) {
dialog->pos = utf8shift(dialog->answer, dialog->pos, -1);
if (!shift) dialog->selPos = dialog->pos;
}
if (keyWentDown("right")) {
dialog->pos = utf8shift(dialog->answer, dialog->pos, 1);
if (!shift) dialog->selPos = dialog->pos;
}
if (keyWentDown("home")) {
if (ctrl) {
dialog->pos = 0;
} else {
while( dialog->pos > 0
&& dialog->answer[dialog->pos-1] != '\r'
&& dialog->answer[dialog->pos-1] != '\n' ) --dialog->pos;
}
if (!shift) dialog->selPos = dialog->pos;
}
if (keyWentDown("end")) {
if (ctrl) {
dialog->pos = strlen(dialog->answer);
} else {
while( dialog->answer[dialog->pos] != 0
&& dialog->answer[dialog->pos] != '\r'
&& dialog->answer[dialog->pos] != '\n' ) ++dialog->pos;
}
if (!shift) dialog->selPos = dialog->pos;
}
if (keyWentDown("return")) {
if (!dialog->multiline || ctrl) {
dialog->success = TRUE;
dialog->shown = FALSE;
} else {
insert(dialog, "\n");
}
}
if (ctrl && keyWentDown("a"))
{ dialog->selPos = 0; dialog->pos = strlen(dialog->answer); }
if (!dialog->password && ctrl && keyWentDown("c"))
copy(dialog);
if (!dialog->password && ctrl && keyWentDown("insert"))
copy(dialog);
if (ctrl && keyWentDown("v"))
paste(dialog);
if (shift && keyWentDown("insert"))
paste(dialog);
if (!dialog->password && ctrl && keyWentDown("x"))
{ copy(dialog); insert(dialog, ""); }
if (keyWentDown("escape")) dialog->shown = FALSE;
if (!dialog->multiline) {
char *c = strpbrk(dialog->answer, "\n\r");
if (c) *c = 0;
fixPos(dialog);
}
if (dialog->password) {
int len = utf8len(dialog->answer);
char *c = dialog->passwordText;
for(int i = 0; i < len; ++i)
for(const char *p = passwordPattern; *p; ++p, ++c)
*c = *p;
*c = 0;
}
draw(dialog);
}