Skip to content

Commit 794ce5d

Browse files
committed
[core] Enhanced GUI_Screen with modal widget support.
Also minor refactoring for focus widget and some cleanup.
1 parent 73de6dd commit 794ce5d

File tree

5 files changed

+77
-48
lines changed

5 files changed

+77
-48
lines changed

exports.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,11 @@ GUI_ContainerIsVisibleWidget
10041004
#Screen API
10051005
GUI_ScreenCreate
10061006
GUI_ScreenSetContents
1007+
GUI_ScreenGetContents
10071008
GUI_ScreenSetBackground
10081009
GUI_ScreenSetFocusWidget
1009-
GUI_ScreenClearFocusWidget
1010+
GUI_ScreenSetModalWidget
1011+
GUI_ScreenGetModalWidget
10101012
GUI_ScreenSetBackgroundColor
10111013
GUI_ScreenGetFocusWidget
10121014
GUI_ScreenGetSurface

include/SDL/SDL_gui.h

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ class GUI_Screen : public GUI_Drawable
815815
GUI_Surface *background;
816816
GUI_Widget *contents;
817817
GUI_Widget *focus_widget;
818-
//GUI_Mouse *mouse;
818+
GUI_Widget *modal_widget;
819819
GUI_Widget **joysel;
820820
int joysel_size;
821821
int joysel_cur;
@@ -837,12 +837,14 @@ class GUI_Screen : public GUI_Drawable
837837
virtual void RemoveWidget(GUI_Widget *widget);
838838

839839
void SetContents(GUI_Widget *widget);
840+
GUI_Widget *GetContents(void);
840841
void SetBackground(GUI_Surface *image);
841842
void SetFocusWidget(GUI_Widget *item);
842-
void ClearFocusWidget(void);
843+
void SetModalWidget(GUI_Widget *widget);
843844
void SetJoySelectState(int value);
844845
void SetBackgroundColor(SDL_Color c);
845846
GUI_Widget *GetFocusWidget(void);
847+
GUI_Widget *GetModalWidget(void);
846848
GUI_Surface *GetSurface(void);
847849

848850
//void SetMouse(GUI_Mouse *m);
@@ -922,21 +924,6 @@ extern "C" {
922924
void GUI_SetScreen(GUI_Screen *);
923925
GUI_Screen *GUI_GetScreen(void);
924926

925-
/*
926-
int GUI_Init(void);
927-
void GUI_Run(void);
928-
void GUI_Quit(void);
929-
930-
int GUI_MustLock(void);
931-
int GUI_Lock(void);
932-
int GUI_Unlock(void);
933-
934-
void GUI_SetThread(Uint32 id);
935-
int GUI_GetRunning(void);
936-
void GUI_SetRunning(int value);
937-
938-
GUI_Mouse *GUI_MouseCreate(char *name, SDL_Surface * sf);
939-
*/
940927

941928
int GUI_ClipRect(SDL_Rect *sr, SDL_Rect *dr, const SDL_Rect *clip);
942929
void GUI_TriggerUpdate(void);
@@ -1050,9 +1037,11 @@ int GUI_ContainerIsVisibleWidget(GUI_Widget *container, GUI_Widget *widget);
10501037

10511038
GUI_Screen *GUI_ScreenCreate(int w, int h, int d, int f);
10521039
void GUI_ScreenSetContents(GUI_Screen *screen, GUI_Widget *contents);
1040+
GUI_Widget *GUI_ScreenGetContents(GUI_Screen *screen);
10531041
void GUI_ScreenSetBackground(GUI_Screen *screen, GUI_Surface *surface);
10541042
void GUI_ScreenSetFocusWidget(GUI_Screen *screen, GUI_Widget *item);
1055-
void GUI_ScreenClearFocusWidget(GUI_Screen *screen);
1043+
void GUI_ScreenSetModalWidget(GUI_Screen *screen, GUI_Widget *widget);
1044+
GUI_Widget *GUI_ScreenGetModalWidget(GUI_Screen *screen);
10561045
void GUI_ScreenSetBackgroundColor(GUI_Screen *screen, SDL_Color c);
10571046
GUI_Widget *GUI_ScreenGetFocusWidget(GUI_Screen *screen);
10581047
//void GUI_ScreenDrawMouse(GUI_Screen *screen);
@@ -1064,8 +1053,7 @@ void GUI_ScreenDoUpdate(GUI_Screen *screen, int force);
10641053
void GUI_ScreenDraw(GUI_Screen *screen, GUI_Surface *image,
10651054
const SDL_Rect *src_r, const SDL_Rect *dst_r);
10661055
void GUI_ScreenFill(GUI_Screen *screen, const SDL_Rect *dst_r, SDL_Color c);
1067-
void GUI_ScreenErase(GUI_Screen *screen, const SDL_Rect *area);
1068-
//void GUI_ScreenSetMouse(GUI_Screen *screen, GUI_Mouse *m);
1056+
void GUI_ScreenErase(GUI_Screen *screen, const SDL_Rect *area);
10691057
GUI_Surface *GUI_ScreenGetSurface(GUI_Screen *screen);
10701058

10711059
GUI_Screen *GUI_RealScreenCreate(const char *aname, SDL_Surface *surface);

lib/SDL_gui/Screen.cc

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ GUI_Screen::GUI_Screen(const char *aname, SDL_Surface *surface)
2222
background = 0;
2323
contents = 0;
2424
focus_widget = 0;
25+
modal_widget = 0;
2526
background_color = 0;
26-
//mouse = 0;
2727
joysel = new GUI_Widget *[64];
2828
joysel_size = 0;
2929
joysel_cur = 0;
@@ -34,6 +34,7 @@ GUI_Screen::~GUI_Screen(void)
3434
{
3535
if (background) background->DecRef();
3636
if (focus_widget) focus_widget->DecRef();
37+
if (modal_widget) modal_widget->DecRef();
3738
if (contents) contents->DecRef();
3839
if (screen_surface) screen_surface->DecRef();
3940
}
@@ -283,6 +284,7 @@ int GUI_Screen::Event(const SDL_Event *event, int xoffset, int yoffset)
283284
if(joysel_enabled) {
284285

285286
SDL_Event evt;
287+
GUI_Container *container = modal_widget ? (GUI_Container *)modal_widget : (GUI_Container *)contents;
286288

287289
switch(event->type) {
288290
case SDL_JOYHATMOTION:
@@ -291,7 +293,7 @@ int GUI_Screen::Event(const SDL_Event *event, int xoffset, int yoffset)
291293
break;
292294
}
293295

294-
if (contents) {
296+
if (container) {
295297
switch(event->jhat.value) {
296298
case SDL_HAT_UP: //UP
297299
case SDL_HAT_DOWN: //DOWN
@@ -303,7 +305,7 @@ int GUI_Screen::Event(const SDL_Event *event, int xoffset, int yoffset)
303305
}
304306

305307
joysel_size = 0;
306-
find_widget_rec((GUI_Container *)contents);
308+
find_widget_rec(container);
307309

308310
if(joysel_size) {
309311

@@ -334,7 +336,7 @@ int GUI_Screen::Event(const SDL_Event *event, int xoffset, int yoffset)
334336
joysel[i] = NULL;
335337
}
336338
joysel_size = 0;
337-
find_widget_rec((GUI_Container *)contents);
339+
find_widget_rec(container);
338340
joysel_cur = event->jhat.value == SDL_HAT_LEFT ? 0 : joysel_size - 1;
339341

340342
if(joysel_size && joysel[joysel_cur]) {
@@ -354,7 +356,7 @@ int GUI_Screen::Event(const SDL_Event *event, int xoffset, int yoffset)
354356

355357
case SDL_KEYDOWN:
356358
{
357-
if (contents) {
359+
if (container) {
358360
switch (event->key.keysym.sym) {
359361
default:
360362
break;
@@ -369,7 +371,7 @@ int GUI_Screen::Event(const SDL_Event *event, int xoffset, int yoffset)
369371
}
370372

371373
joysel_size = 0;
372-
find_widget_rec((GUI_Container *)contents);
374+
find_widget_rec(container);
373375

374376
if(joysel_size) {
375377

@@ -399,7 +401,7 @@ int GUI_Screen::Event(const SDL_Event *event, int xoffset, int yoffset)
399401
joysel[i] = NULL;
400402
}
401403
joysel_size = 0;
402-
find_widget_rec((GUI_Container *)contents);
404+
find_widget_rec(container);
403405
joysel_cur = event->key.keysym.sym == SDLK_LEFT ? 0 : joysel_size - 1;
404406

405407
if(joysel_size && joysel[joysel_cur]) {
@@ -419,9 +421,16 @@ int GUI_Screen::Event(const SDL_Event *event, int xoffset, int yoffset)
419421
}
420422
}
421423

422-
if (contents)
423-
if (contents->Event(event, xoffset, yoffset))
424+
if(modal_widget)
425+
{
426+
if(modal_widget->Event(event, xoffset, yoffset))
427+
return 1;
428+
}
429+
else if(contents)
430+
{
431+
if(contents->Event(event, xoffset, yoffset))
424432
return 1;
433+
}
425434
return GUI_Drawable::Event(event, xoffset, yoffset);
426435
}
427436

@@ -433,7 +442,8 @@ void GUI_Screen::RemoveWidget(GUI_Widget *widget)
433442

434443
void GUI_Screen::SetContents(GUI_Widget *widget)
435444
{
436-
445+
SetFocusWidget(NULL);
446+
SetModalWidget(NULL);
437447
Keep(&contents, widget);
438448

439449
joysel_size = 0;
@@ -448,6 +458,21 @@ void GUI_Screen::SetContents(GUI_Widget *widget)
448458
MarkChanged();
449459
}
450460

461+
GUI_Widget *GUI_Screen::GetContents(void)
462+
{
463+
return contents;
464+
}
465+
466+
void GUI_Screen::SetModalWidget(GUI_Widget *widget)
467+
{
468+
if (GUI_ObjectKeep((GUI_Object **) &modal_widget, widget))
469+
{
470+
MarkChanged();
471+
joysel_size = 0;
472+
joysel_cur = -1;
473+
}
474+
}
475+
451476
void GUI_Screen::SetBackground(GUI_Surface *image)
452477
{
453478
if (GUI_ObjectKeep((GUI_Object **) &background, image))
@@ -468,31 +493,33 @@ void GUI_Screen::SetBackgroundColor(SDL_Color c)
468493

469494
void GUI_Screen::SetFocusWidget(GUI_Widget *widget)
470495
{
471-
//assert(widget != NULL);
472-
473-
if (widget != NULL && focus_widget != widget)
474-
{
475-
ClearFocusWidget();
476-
widget->SetFlags(WIDGET_HAS_FOCUS);
477-
widget->IncRef();
478-
focus_widget = widget;
479-
}
480-
}
496+
if (focus_widget == widget)
497+
return;
481498

482-
void GUI_Screen::ClearFocusWidget()
483-
{
484499
if (focus_widget)
485500
{
486501
focus_widget->ClearFlags(WIDGET_HAS_FOCUS);
487502
focus_widget->DecRef();
488503
focus_widget = 0;
489504
}
505+
506+
if (widget)
507+
{
508+
focus_widget = widget;
509+
focus_widget->IncRef();
510+
focus_widget->SetFlags(WIDGET_HAS_FOCUS);
511+
}
490512
}
491513

492514
GUI_Widget *GUI_Screen::GetFocusWidget()
493515
{
494516
return focus_widget;
495517
}
518+
519+
GUI_Widget *GUI_Screen::GetModalWidget()
520+
{
521+
return modal_widget;
522+
}
496523
/*
497524
void GUI_Screen::SetMouse(GUI_Mouse *m)
498525
{
@@ -522,6 +549,11 @@ void GUI_ScreenSetContents(GUI_Screen *screen, GUI_Widget *widget)
522549
screen->SetContents(widget);
523550
}
524551

552+
GUI_Widget *GUI_ScreenGetContents(GUI_Screen *screen)
553+
{
554+
return screen->GetContents();
555+
}
556+
525557
void GUI_ScreenSetBackground(GUI_Screen *screen, GUI_Surface *image)
526558
{
527559
screen->SetBackground(image);
@@ -532,9 +564,14 @@ void GUI_ScreenSetFocusWidget(GUI_Screen *screen, GUI_Widget *widget)
532564
screen->SetFocusWidget(widget);
533565
}
534566

535-
void GUI_ScreenClearFocusWidget(GUI_Screen *screen)
567+
void GUI_ScreenSetModalWidget(GUI_Screen *screen, GUI_Widget *widget)
568+
{
569+
screen->SetModalWidget(widget);
570+
}
571+
572+
GUI_Widget *GUI_ScreenGetModalWidget(GUI_Screen *screen)
536573
{
537-
screen->ClearFocusWidget();
574+
return screen->GetModalWidget();
538575
}
539576

540577
void GUI_ScreenSetBackgroundColor(GUI_Screen *screen, SDL_Color c)

lib/SDL_gui/TextEntry.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void GUI_TextEntry::Clicked(int x, int y)
113113

114114
if (flags & WIDGET_HAS_FOCUS)
115115
{
116-
screen->ClearFocusWidget();
116+
screen->SetFocusWidget(NULL);
117117
if (unfocus_callback)
118118
unfocus_callback->Call(this);
119119
}
@@ -155,7 +155,7 @@ int GUI_TextEntry::Event(const SDL_Event *event, int xoffset, int yoffset)
155155
if (key == SDLK_RETURN)
156156
{
157157
GUI_Screen *screen = GUI_GetScreen();
158-
screen->ClearFocusWidget();
158+
screen->SetFocusWidget(NULL);
159159
if (unfocus_callback)
160160
unfocus_callback->Call(this);
161161
return 1;

modules/luaGUI/GUI.pkg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ int GUI_ContainerIsVisibleWidget(GUI_Widget *container, GUI_Widget *widget);
138138
/* Screen API */
139139

140140
void GUI_ScreenSetContents(GUI_Screen *screen, GUI_Widget *contents);
141+
GUI_Widget *GUI_ScreenGetContents(GUI_Screen *screen);
141142
void GUI_ScreenSetBackground(GUI_Screen *screen, GUI_Surface *surface);
142143
void GUI_ScreenSetFocusWidget(GUI_Screen *screen, GUI_Widget *item);
143-
void GUI_ScreenClearFocusWidget(GUI_Screen *screen);
144+
void GUI_ScreenSetModalWidget(GUI_Screen *screen, GUI_Widget *widget);
145+
GUI_Widget *GUI_ScreenGetModalWidget(GUI_Screen *screen);
144146
void GUI_ScreenSetBackgroundColor(GUI_Screen *screen, SDL_Color c);
145147
GUI_Widget *GUI_ScreenGetFocusWidget(GUI_Screen *screen);
146148
void GUI_ScreenSetJoySelectState(GUI_Screen *screen, int value);

0 commit comments

Comments
 (0)