From 64cc2a9e8d2a9695c6f299c7fff0c22c8cd58e0b Mon Sep 17 00:00:00 2001 From: mcmartin Date: Fri, 19 May 2006 16:07:03 +0000 Subject: [PATCH] Partial implementation and test case for TextEntry widget git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2363 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/sc2code/libs/graphics/widgets.c | 100 ++++++++++++++++++++---- sc2/src/sc2code/libs/graphics/widgets.h | 16 ++++ sc2/src/sc2code/setupmenu.c | 32 ++++++-- 3 files changed, 128 insertions(+), 20 deletions(-) diff --git a/sc2/src/sc2code/libs/graphics/widgets.c b/sc2/src/sc2code/libs/graphics/widgets.c index 60c940b6e..bbd4536c5 100644 --- a/sc2/src/sc2code/libs/graphics/widgets.c +++ b/sc2/src/sc2code/libs/graphics/widgets.c @@ -5,6 +5,11 @@ WIDGET *widget_focus = NULL; +/* Some basic color defines */ +#define WIDGET_ACTIVE_COLOR BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x00), 0x0E) +#define WIDGET_INACTIVE_COLOR BUILD_COLOR (MAKE_RGB15 (0x18, 0x18, 0x1F), 0x09) +#define WIDGET_INACTIVE_SELECTED_COLOR BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x1F), 0x0F) + void DrawShadowedBox(PRECT r, COLOR bg, COLOR dark, COLOR medium) { @@ -50,7 +55,7 @@ Widget_DrawToolTips (int numlines, const char **tips) RECT r; FONT oldfont = SetContextFont (StarConFont); FRAME oldFontEffect = SetContextFontEffect (NULL); - COLOR oldtext = SetContextForeGroundColor (BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x1F), 0x0F)); + COLOR oldtext = SetContextForeGroundColor (WIDGET_INACTIVE_SELECTED_COLOR); TEXT t; int i; @@ -95,9 +100,9 @@ Widget_DrawMenuScreen (WIDGET *_self, int x, int y) r.extent.width = SCREEN_WIDTH - 4; r.extent.height = SCREEN_HEIGHT - 4; - title = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x1F), 0x0F); - selected = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x00), 0x0E); - inactive = BUILD_COLOR (MAKE_RGB15 (0x18, 0x18, 0x1F), 0x07); + title = WIDGET_INACTIVE_SELECTED_COLOR; + selected = WIDGET_ACTIVE_COLOR; + inactive = WIDGET_INACTIVE_COLOR; default_color = title; DrawStamp (&self->bgStamp); @@ -151,9 +156,9 @@ Widget_DrawChoice (WIDGET *_self, int x, int y) TEXT t; int i, home_x, home_y; - default_color = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x1F), 0x0F); - selected = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x00), 0x0E); - inactive = BUILD_COLOR (MAKE_RGB15 (0x18, 0x18, 0x1F), 0x07); + default_color = WIDGET_INACTIVE_SELECTED_COLOR; + selected = WIDGET_ACTIVE_COLOR; + inactive = WIDGET_INACTIVE_COLOR; t.baseline.x = x; t.baseline.y = y; @@ -210,8 +215,8 @@ Widget_DrawButton (WIDGET *_self, int x, int y) FRAME oldFontEffect = SetContextFontEffect (NULL); TEXT t; - selected = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x00), 0x0E); - inactive = BUILD_COLOR (MAKE_RGB15 (0x18, 0x18, 0x1F), 0x07); + selected = WIDGET_ACTIVE_COLOR; + inactive = WIDGET_INACTIVE_COLOR; t.baseline.x = 160; t.baseline.y = y; @@ -239,8 +244,7 @@ void Widget_DrawLabel (WIDGET *_self, int x, int y) { WIDGET_LABEL *self = (WIDGET_LABEL *)_self; - COLOR oldtext = SetContextForeGroundColor ( - BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x1F), 0x0F)); + COLOR oldtext = SetContextForeGroundColor (WIDGET_INACTIVE_SELECTED_COLOR); FONT oldfont = SetContextFont (StarConFont); FRAME oldFontEffect = SetContextFontEffect (NULL); TEXT t; @@ -276,9 +280,9 @@ Widget_DrawSlider(WIDGET *_self, int x, int y) RECT r; int tick = (SCREEN_WIDTH - x) / 8; - default_color = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x1F), 0x0F); - selected = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x00), 0x0E); - inactive = BUILD_COLOR (MAKE_RGB15 (0x18, 0x18, 0x1F), 0x07); + default_color = WIDGET_INACTIVE_SELECTED_COLOR; + selected = WIDGET_ACTIVE_COLOR; + inactive = WIDGET_INACTIVE_COLOR; t.baseline.x = x; t.baseline.y = y; @@ -335,6 +339,61 @@ Widget_Slider_DrawValue (WIDGET_SLIDER *self, int x, int y) font_DrawText (&t); } +void +Widget_DrawTextEntry (WIDGET *_self, int x, int y) +{ + WIDGET_TEXTENTRY *self = (WIDGET_TEXTENTRY *)_self; + COLOR oldtext; + COLOR inactive, default_color, selected; + FONT oldfont = SetContextFont (StarConFont); + FRAME oldFontEffect = SetContextFontEffect (NULL); + TEXT t; + int i, home_x, home_y; + + default_color = WIDGET_INACTIVE_SELECTED_COLOR; + selected = WIDGET_ACTIVE_COLOR; + inactive = WIDGET_INACTIVE_COLOR; + + t.baseline.x = x; + t.baseline.y = y; + t.align = ALIGN_LEFT; + t.valign = VALIGN_BOTTOM; + t.CharCount = ~0; + t.pStr = self->category; + if (widget_focus == _self) + { + oldtext = SetContextForeGroundColor (selected); + } + else + { + oldtext = SetContextForeGroundColor (default_color); + } + font_DrawText (&t); + + /* Force string termination */ + self->value[WIDGET_TEXTENTRY_WIDTH-1] = 0; + + t.baseline.x = 160; + t.baseline.y = y; + t.align = ALIGN_CENTER; + t.valign = VALIGN_BOTTOM; + t.CharCount = ~0; + t.pStr = self->value; + if (widget_focus == _self) + { + oldtext = SetContextForeGroundColor (selected); + } + else + { + oldtext = SetContextForeGroundColor (inactive); + } + font_DrawText (&t); + + SetContextFontEffect (oldFontEffect); + SetContextFont (oldfont); + SetContextForeGroundColor (oldtext); +} + int Widget_HeightChoice (WIDGET *_self) { @@ -529,6 +588,19 @@ Widget_HandleEventMenuScreen (WIDGET *_self, int event) return FALSE; } +int +Widget_HandleEventTextEntry (WIDGET *_self, int event) +{ + WIDGET_TEXTENTRY *self = (WIDGET_TEXTENTRY *)_self; + if (event == WIDGET_EVENT_SELECT) { + /* The TextEntry call goes here, editing self->value. + * The string should cap at self->maxlen + * characters. */ + } + (void)self; + return FALSE; +} + int Widget_Event (int event) { diff --git a/sc2/src/sc2code/libs/graphics/widgets.h b/sc2/src/sc2code/libs/graphics/widgets.h index 171243a5f..caa6d62bd 100644 --- a/sc2/src/sc2code/libs/graphics/widgets.h +++ b/sc2/src/sc2code/libs/graphics/widgets.h @@ -31,6 +31,8 @@ enum { NUM_WIDGET_EVENTS }; +#define WIDGET_TEXTENTRY_WIDTH 30 + typedef struct _widget { struct _widget *parent; int (*handleEvent)(struct _widget *self, int event); @@ -110,6 +112,18 @@ typedef struct _widget_slider { const char *tooltip[3]; } WIDGET_SLIDER; +typedef struct _widget_textentry { + struct _widget *parent; + int (*handleEvent)(struct _widget *self, int event); + int (*receiveFocus)(struct _widget *self, int event); + void (*draw)(struct _widget *self, int x, int y); + int (*height)(struct _widget *self); + int (*width)(struct _widget *self); + const char *category; + char value[WIDGET_TEXTENTRY_WIDTH]; + int maxlen; +} WIDGET_TEXTENTRY; + void DrawShadowedBox (PRECT r, COLOR bg, COLOR dark, COLOR medium); int Widget_Event (int event); @@ -125,6 +139,7 @@ int Widget_ReceiveFocusRefuseFocus (WIDGET *_self, int event); int Widget_HandleEventMenuScreen (WIDGET *_self, int event); int Widget_HandleEventChoice (WIDGET *_self, int event); int Widget_HandleEventSlider (WIDGET *_self, int event); +int Widget_HandleEventTextEntry (WIDGET *_self, int event); int Widget_HandleEventIgnoreAll (WIDGET *_self, int event); int Widget_HeightChoice (WIDGET *_self); @@ -139,6 +154,7 @@ void Widget_DrawChoice (WIDGET *_self, int x, int y); void Widget_DrawButton (WIDGET *_self, int x, int y); void Widget_DrawLabel (WIDGET *_self, int x, int y); void Widget_DrawSlider (WIDGET *_self, int x, int y); +void Widget_DrawTextEntry (WIDGET *_self, int x, int y); void Widget_Slider_DrawValue (WIDGET_SLIDER *self, int x, int y); diff --git a/sc2/src/sc2code/setupmenu.c b/sc2/src/sc2code/setupmenu.c index 589afbcc7..c32363b10 100644 --- a/sc2/src/sc2code/setupmenu.c +++ b/sc2/src/sc2code/setupmenu.c @@ -65,11 +65,12 @@ static int do_advanced (WIDGET *self, int event); #define RES_OPTS 2 #endif -#define MENU_COUNT 7 -#define CHOICE_COUNT 20 -#define SLIDER_COUNT 3 -#define BUTTON_COUNT 8 -#define LABEL_COUNT 2 +#define MENU_COUNT 7 +#define CHOICE_COUNT 20 +#define SLIDER_COUNT 3 +#define BUTTON_COUNT 8 +#define LABEL_COUNT 2 +#define TEXTENTRY_COUNT 1 /* The space for our widgets */ static WIDGET_MENU_SCREEN menus[MENU_COUNT]; @@ -77,6 +78,7 @@ static WIDGET_CHOICE choices[CHOICE_COUNT]; static WIDGET_SLIDER sliders[SLIDER_COUNT]; static WIDGET_BUTTON buttons[BUTTON_COUNT]; static WIDGET_LABEL labels[LABEL_COUNT]; +static WIDGET_TEXTENTRY textentries[TEXTENTRY_COUNT]; /* The hardcoded data that isn't strings */ @@ -92,7 +94,7 @@ static HANDLER button_handlers[BUTTON_COUNT] = { do_audio, do_resources, do_keyconfig, do_advanced }; static int menu_sizes[MENU_COUNT] = { - 7, 5, 6, 9, 2, 4, + 8, 5, 6, 9, 2, 4, #ifdef HAVE_OPENGL 5 }; #else @@ -110,6 +112,7 @@ static WIDGET *main_widgets[] = { (WIDGET *)(&buttons[5]), (WIDGET *)(&buttons[6]), (WIDGET *)(&buttons[7]), + (WIDGET *)(&textentries[0]), (WIDGET *)(&buttons[0]) }; static WIDGET *graphics_widgets[] = { @@ -682,6 +685,23 @@ init_widgets (void) log_add (log_Warning, "WARNING: Setup strings had %d garbage entries at the end.", count - index); } + + /* Hardcode the test textentry for now. */ + for (i = 0; i < TEXTENTRY_COUNT; i++) + { + textentries[i].parent = NULL; + textentries[i].handleEvent = Widget_HandleEventTextEntry; + textentries[i].receiveFocus = Widget_ReceiveFocusSimple; + textentries[i].draw = Widget_DrawTextEntry; + textentries[i].height = Widget_HeightOneLine; + textentries[i].width = Widget_WidthFullScreen; + textentries[i].category = NULL; + textentries[i].value[0] = 0; + textentries[i].maxlen = WIDGET_TEXTENTRY_WIDTH-1; + } + + textentries[0].category = "TestText"; + strcpy (textentries[0].value, "Sample value"); } static void