Scripting: Allow PCX and custom text for preview

This commit is contained in:
d0k3 2017-12-20 00:13:31 +01:00
parent c92a6fa164
commit 52b3f128bf
3 changed files with 44 additions and 30 deletions

View File

@ -145,6 +145,22 @@ void DrawStringF(u8* screen, int x, int y, int color, int bgcolor, const char *f
DrawString(screen, text, x, y, color, bgcolor); DrawString(screen, text, x, y, color, bgcolor);
} }
void DrawStringCenter(u8* screen, int color, int bgcolor, const char *format, ...)
{
char str[STRBUF_SIZE] = { 0 };
va_list va;
va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va);
u32 w = GetDrawStringWidth(str);
u32 h = GetDrawStringHeight(str);
int x = (w >= SCREEN_WIDTH(screen)) ? 0 : (SCREEN_WIDTH(screen) - w) >> 1;
int y = (h >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - h) >> 1;
DrawStringF(screen, x, y, color, bgcolor, str);
}
u32 GetDrawStringHeight(const char* str) { u32 GetDrawStringHeight(const char* str) {
u32 height = FONT_HEIGHT; u32 height = FONT_HEIGHT;
for (char* lf = strchr(str, '\n'); (lf != NULL); lf = strchr(lf + 1, '\n')) for (char* lf = strchr(str, '\n'); (lf != NULL); lf = strchr(lf + 1, '\n'))
@ -238,22 +254,14 @@ void FormatBytes(char* str, u64 bytes) { // str should be 32 byte in size, just
void ShowString(const char *format, ...) void ShowString(const char *format, ...)
{ {
if (format && *format) { // only if there is something in there if (format && *format) { // only if there is something in there
u32 str_width, str_height;
u32 x, y;
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE] = { 0 };
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
str_width = GetDrawStringWidth(str);
str_height = GetDrawStringHeight(str);
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, str); DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, str);
} else ClearScreenF(true, false, COLOR_STD_BG); } else ClearScreenF(true, false, COLOR_STD_BG);
} }
@ -286,8 +294,6 @@ void ShowIconString(u8* icon, int w, int h, const char *format, ...)
bool ShowPrompt(bool ask, const char *format, ...) bool ShowPrompt(bool ask, const char *format, ...)
{ {
u32 str_width, str_height;
u32 x, y;
bool ret = true; bool ret = true;
char str[STRBUF_SIZE] = { 0 }; char str[STRBUF_SIZE] = { 0 };
@ -296,15 +302,9 @@ bool ShowPrompt(bool ask, const char *format, ...)
vsnprintf(str, STRBUF_SIZE, format, va); vsnprintf(str, STRBUF_SIZE, format, va);
va_end(va); va_end(va);
str_width = GetDrawStringWidth(str);
str_height = GetDrawStringHeight(str) + (2 * 10);
if (str_width < 18 * FONT_WIDTH) str_width = 18 * FONT_WIDTH;
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, str); DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s\n \n%s", str,
DrawStringF(MAIN_SCREEN, x, y + str_height - (1*10), COLOR_STD_FONT, COLOR_STD_BG, (ask) ? "(<A> yes, <B> no)" : "(<A> to continue)"); (ask) ? "(<A> yes, <B> no)" : "(<A> to continue)");
while (true) { while (true) {
u32 pad_state = InputWait(0); u32 pad_state = InputWait(0);

View File

@ -58,6 +58,7 @@ void DrawQrCode(u8* screen, u8* qrcode);
void DrawCharacter(unsigned char *screen, int character, int x, int y, int color, int bgcolor); void DrawCharacter(unsigned char *screen, int character, int x, int y, int color, int bgcolor);
void DrawString(unsigned char *screen, const char *str, int x, int y, int color, int bgcolor); void DrawString(unsigned char *screen, const char *str, int x, int y, int color, int bgcolor);
void DrawStringF(unsigned char *screen, int x, int y, int color, int bgcolor, const char *format, ...); void DrawStringF(unsigned char *screen, int x, int y, int color, int bgcolor, const char *format, ...);
void DrawStringCenter(u8* screen, int color, int bgcolor, const char *format, ...);
u32 GetDrawStringHeight(const char* str); u32 GetDrawStringHeight(const char* str);
u32 GetDrawStringWidth(const char* str); u32 GetDrawStringWidth(const char* str);

View File

@ -12,6 +12,7 @@
#include "sha.h" #include "sha.h"
#include "hid.h" #include "hid.h"
#include "ui.h" #include "ui.h"
#include "pcx.h"
#define _MAX_ARGS 4 #define _MAX_ARGS 4
@ -255,9 +256,9 @@ static inline u32 get_lno(const char* text, u32 len, const char* line) {
void set_preview(const char* name, const char* content) { void set_preview(const char* name, const char* content) {
if (strncmp(name, "PREVIEW_MODE", _VAR_NAME_LEN) == 0) { if (strncmp(name, "PREVIEW_MODE", _VAR_NAME_LEN) == 0) {
if (strncasecmp(content, "off", _VAR_CNT_LEN) == 0) preview_mode = 0; if (strncasecmp(content, "quick", _VAR_CNT_LEN) == 0) preview_mode = 1;
else if (strncasecmp(content, "quick", _VAR_CNT_LEN) == 0) preview_mode = 1;
else if (strncasecmp(content, "full", _VAR_CNT_LEN) == 0) preview_mode = 2; else if (strncasecmp(content, "full", _VAR_CNT_LEN) == 0) preview_mode = 2;
else preview_mode = 0xFF; // unknown preview mode
} else if (strncmp(name, "PREVIEW_COLOR_ACTIVE", _VAR_NAME_LEN) == 0) { } else if (strncmp(name, "PREVIEW_COLOR_ACTIVE", _VAR_NAME_LEN) == 0) {
u8 rgb[4] = { 0 }; u8 rgb[4] = { 0 };
if (strntohex(content, rgb, 3)) if (strntohex(content, rgb, 3))
@ -1327,21 +1328,33 @@ bool ExecuteGM9Script(const char* path_script) {
// update script viewer // update script viewer
if (MAIN_SCREEN != TOP_SCREEN) { if (MAIN_SCREEN != TOP_SCREEN) {
bool show_preview = preview_mode;
if (preview_mode != preview_mode_local) { if (preview_mode != preview_mode_local) {
if (!preview_mode || !preview_mode_local) ClearScreen(TOP_SCREEN, COLOR_STD_BG); if (!preview_mode || (preview_mode > 2) || !preview_mode_local)
if (!preview_mode) DrawString(TOP_SCREEN, "(preview disabled)", ClearScreen(TOP_SCREEN, COLOR_STD_BG);
(SCREEN_WIDTH_TOP - (18*FONT_WIDTH_EXT)) / 2, if (preview_mode > 2) {
(SCREEN_HEIGHT - FONT_HEIGHT_EXT) / 2, char* preview_str = get_var("PREVIEW_MODE", NULL);
COLOR_STD_FONT, COLOR_STD_BG); u8* pcx = TEMP_BUFFER + TEMP_BUFFER_SIZE / 2;
u32 pcx_size = FileGetData(preview_str, pcx, TEMP_BUFFER_SIZE / 2, 0);
if ((pcx_size > 0) && (pcx_size < TEMP_BUFFER_SIZE / 2) &&
(PCX_Decompress(TEMP_BUFFER, TEMP_BUFFER_SIZE / 2, pcx, pcx_size))) {
PCXHdr* hdr = (PCXHdr*) (void*) pcx;
DrawBitmap(TOP_SCREEN, -1, -1, PCX_Width(hdr), PCX_Height(hdr), TEMP_BUFFER);
} else {
if (strncmp(preview_str, "off", _VAR_CNT_LEN) == 0) preview_str = "(preview disabled)";
DrawStringCenter(TOP_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, preview_str);
}
preview_mode = 0;
}
preview_mode_local = preview_mode; preview_mode_local = preview_mode;
} }
bool show_preview = preview_mode;
if (preview_mode == 1) { if (preview_mode == 1) {
show_preview = false; show_preview = false;
for (char* c = ptr; (c < line_end) && !show_preview; c++) { for (char* c = ptr; (c < line_end) && !show_preview; c++) {
// check for comments // check for comments / labels
if (IS_WHITESPACE(*c)) continue; if (IS_WHITESPACE(*c)) continue;
else if (*c == '#') break; else if ((*c == '#') || (*c == '@')) break;
else show_preview = true; else show_preview = true;
} }
} }