Skip to content

Commit

Permalink
Allow a dialog setup function
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 29, 2011
1 parent 81e8f4f commit d0e451b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
18 changes: 12 additions & 6 deletions UIDialog.cpp
Expand Up @@ -23,7 +23,8 @@ UIDialog::UIDialog(UIManager *ui, const char *name) :
m_expand = true;
m_step = 0;
m_status = 0;
m_handler = NULL;
m_handleInit = NULL;
m_handleDone = NULL;
}

UIDialog::~UIDialog()
Expand Down Expand Up @@ -52,6 +53,10 @@ UIDialog::Show()
}
m_status = 0;

if (m_handleInit) {
m_handleInit(this);
}

UIPanel::Show();
}

Expand All @@ -60,8 +65,8 @@ UIDialog::Hide()
{
UIPanel::Hide();

if (m_handler) {
m_handler(this, m_status);
if (m_handleDone) {
m_handleDone(this, m_status);
}
}

Expand Down Expand Up @@ -131,11 +136,12 @@ UIDialog::HandleEvent(const SDL_Event &event)
return false;
}

UIDialogLauncher::UIDialogLauncher(UIManager *ui, const char *name, UIDialogHandler handler)
UIDialogLauncher::UIDialogLauncher(UIManager *ui, const char *name, UIDialogInitHandler handleInit, UIDialogDoneHandler handleDone)
{
m_ui = ui;
m_name = name;
m_handler = handler;
m_handleInit = handleInit;
m_handleDone = handleDone;
}

void
Expand All @@ -145,7 +151,7 @@ UIDialogLauncher::OnClick()

dialog = m_ui->GetPanel<UIDialog>(m_name);
if (dialog) {
dialog->SetDialogHandler(m_handler);
dialog->SetDialogHandlers(m_handleInit, m_handleDone);

m_ui->ShowPanel(dialog);
}
Expand Down
20 changes: 14 additions & 6 deletions UIDialog.h
Expand Up @@ -7,10 +7,14 @@

class UIDialog;

/* This function gets called when the dialog is shown.
*/
typedef void (*UIDialogInitHandler)(UIDialog *dialog);

/* This function gets called when the dialog is hidden.
The status defaults to 0, but can be changed by dialog buttons.
*/
typedef void (*UIDialogHandler)(UIDialog *dialog, int status);
typedef void (*UIDialogDoneHandler)(UIDialog *dialog, int status);

class UIDialog : public UIPanel
{
Expand All @@ -23,8 +27,10 @@ class UIDialog : public UIPanel
}

/* Set a function that's called when the dialog is hidden */
void SetDialogHandler(UIDialogHandler handler) {
m_handler = handler;
void SetDialogHandlers(UIDialogInitHandler handleInit,
UIDialogDoneHandler handleDone) {
m_handleInit = handleInit;
m_handleDone = handleDone;
}
void SetDialogStatus(int status) {
m_status = status;
Expand All @@ -50,7 +56,8 @@ class UIDialog : public UIPanel
bool m_expand;
int m_step;
int m_status;
UIDialogHandler m_handler;
UIDialogInitHandler m_handleInit;
UIDialogDoneHandler m_handleDone;

protected:
static UIElementType s_elementType;
Expand All @@ -70,14 +77,15 @@ class UIDialog : public UIPanel
class UIDialogLauncher : public UIButtonDelegate
{
public:
UIDialogLauncher(UIManager *ui, const char *name, UIDialogHandler handler = NULL);
UIDialogLauncher(UIManager *ui, const char *name, UIDialogInitHandler = NULL, UIDialogDoneHandler handleDone = NULL);

virtual void OnClick();

protected:
UIManager *m_ui;
const char *m_name;
UIDialogHandler m_handler;
UIDialogInitHandler m_handleInit;
UIDialogDoneHandler m_handleDone;
};

#endif // _UIDialog_h
2 changes: 1 addition & 1 deletion main.cpp
Expand Up @@ -362,7 +362,7 @@ MainPanelDelegate::OnLoad()
}
button = m_panel->GetElement<UIElementButton>("ZapButton");
if (button) {
button->SetButtonDelegate(new UIDialogLauncher(ui, DIALOG_ZAP, ZapHighScores));
button->SetButtonDelegate(new UIDialogLauncher(ui, DIALOG_ZAP, NULL, ZapHighScores));
}
button = m_panel->GetElement<UIElementButton>("AboutButton");
if (button) {
Expand Down

0 comments on commit d0e451b

Please sign in to comment.