diff --git a/demo/src/common.c b/demo/src/common.c index f1bb965..ffb12e0 100644 --- a/demo/src/common.c +++ b/demo/src/common.c @@ -9,6 +9,8 @@ static int answer = 0; +static int intAnswer = 0; +static double doubleAnswer = 0; static char buffer[4096] = {}; static Sound beep; @@ -73,15 +75,22 @@ void commonDraw() { noFill(); textFontDefault(); textSize(16); - text(16, 48, buffer); - - textf(16, 16, "answer: %d, fps: %6.2f", answer, 1/windowGetFrameTime()); + textf(16, 16, "fps: %6.2f", 1/windowGetFrameTime()); + textf(16, 48, "answer: %d, int: %d, double: %g", answer, intAnswer, doubleAnswer); + text(16, 80, buffer); + + if (mouseWentDown("left")) soundPlay(beep, FALSE); - if (mouseWentDown("middle")) - askTextEx("Test\ntext input", buffer, sizeof(buffer), keyDown("any shift"), keyDown("any ctrl")); + if (mouseWentDown("middle")) { + if (keyDown("any alt")) { + answer = askTextf("Test\nformatted text input.\nEnter two numbers: int and double", "%d%lg", &intAnswer, &doubleAnswer); + } else { + askTextEx("Test\ntext input", buffer, sizeof(buffer), keyDown("any shift"), keyDown("any ctrl")); + } + } if (mouseWentDown("right")) { if (keyDown("any ctrl")) { diff --git a/demo/src/font.c b/demo/src/font.c index f805ca6..84613ad 100644 --- a/demo/src/font.c +++ b/demo/src/font.c @@ -46,11 +46,11 @@ void fontDraw() { double xx = x + (1-i)*w/2; double yy = y + (1-j)*h/2; textAlign(haligns[i], valigns[j]); - textf(xx, yy, "%s\n%s", ht[i], vt[j]); if (i == 1 && j == 1) { + TextLayout layout = createTextLayoutf("%s\n%s", ht[i], vt[j]); + textLayoutDraw(layout, xx, yy); saveState(); strokeWidth(0.5); - TextLayout layout = createTextLayout("center I\ncenter q"); double ex = 10; double l = textLayoutGetLeft(layout); double t = textLayoutGetTop(layout); @@ -67,6 +67,9 @@ void fontDraw() { line( xx+l-ex , yy+x , xx+l+w+ex , yy+x ); line( xx+l-ex , yy+b , xx+l+w+ex , yy+b ); restoreState(); + textLayoutDestroy(layout); + } else { + textf(xx, yy, "%s\n%s", ht[i], vt[j]); } } } diff --git a/doc/helianthus-doc-ru.odt b/doc/helianthus-doc-ru.odt index 1b5bb43..9a4938e 100644 Binary files a/doc/helianthus-doc-ru.odt and b/doc/helianthus-doc-ru.odt differ diff --git a/src/font.c b/src/font.c index 86f4696..8d360c2 100644 --- a/src/font.c +++ b/src/font.c @@ -589,6 +589,26 @@ TextLayout createTextLayout(const char *text) { } +TextLayout createTextLayoutf(const char *format, ...) { + va_list args; + va_start(args, format); + + va_list args_copy; + va_copy(args_copy, args); + int size = vsnprintf(NULL, 0, format, args_copy); + va_end(args_copy); + if (size < 0) size = 0; + ++size; + char *buffer = calloc(1, size); + vsnprintf(buffer, size, format, args); + va_end(args); + + TextLayout layout = createTextLayout(buffer); + free(buffer); + return layout; +} + + void textLayoutDestroy(TextLayout layout) { if (layout) { free(layout->lines); diff --git a/src/font.h b/src/font.h index 0d6b000..61d59b7 100644 --- a/src/font.h +++ b/src/font.h @@ -37,6 +37,7 @@ void textSize(double size); TextLayout createTextLayout(const char *text); +TextLayout createTextLayoutf(const char *format, ...); void textLayoutDestroy(TextLayout layout); void textLayoutDraw(TextLayout layout, double x, double y); void textLayoutDrawFrom(TextLayout layout, double x, double y, int start); diff --git a/src/window.c b/src/window.c index 4c76ee5..d2fc879 100644 --- a/src/window.c +++ b/src/window.c @@ -382,6 +382,17 @@ int questionBox3(const char* question, const char* answer0, const char* answer1, int askText(const char *question, char *answer, int maxAnswerSize) { return askTextEx(question, answer, maxAnswerSize, FALSE, FALSE); } +int askTextf(const char *question, const char *format, ...) { + char buf[2048] = {}; + if (!askText(question, buf, sizeof(buf))) + return EOF; + va_list args; + va_start(args, format); + int result = vsscanf(buf, format, args); + va_end(args); + return result; +} + static void resetEvents() { dialog.newText[0] = 0; diff --git a/src/window.h b/src/window.h index 5dd029e..d80a170 100644 --- a/src/window.h +++ b/src/window.h @@ -42,6 +42,7 @@ void messageBox(const char *message); int questionBox(const char *question, const char *answer0, const char *answer1); int questionBox3(const char *question, const char *answer0, const char *answer1, const char *answer2); int askText(const char *question, char *answer, int maxAnswerSize); +int askTextf(const char *question, const char *format, ...); int askTextEx(const char *question, char *answer, int maxAnswerSize, int multiline, int password); int windowGetWidth();