From 5ec545ff1125c7dddb152270b92069380976fa8e Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Sat, 16 Jan 2010 09:08:54 +0000 Subject: [PATCH] Implement status bar --- gui.cpp | 13 +++++++++- gui.hh | 3 +++ status_bar.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ status_bar.hh | 37 ++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 status_bar.cpp create mode 100644 status_bar.hh diff --git a/gui.cpp b/gui.cpp index 0d885d1..e916ac7 100644 --- a/gui.cpp +++ b/gui.cpp @@ -68,6 +68,7 @@ Gui::Gui() this->main_menu_bg = NULL; this->infobox = NULL; this->textbox = NULL; + this->status_bar_bg = NULL; this->default_font = NULL; this->dialogue_bg = NULL; this->small_font = NULL; @@ -104,6 +105,7 @@ bool Gui::setTheme(const char *path) this->background = this->loadThemeImage(path, "background.png"); this->main_menu_bg = this->loadThemeImage(path, "main_menu_bg.png"); + this->status_bar_bg = this->loadThemeImage(path, "status_bar.png"); this->infobox = this->loadThemeImage(path, "infobox.png"); this->textbox = this->loadThemeImage(path, "textbox.png"); this->dialogue_bg = this->loadThemeImage(path, "dialogue_box.png"); @@ -122,6 +124,7 @@ bool Gui::setTheme(const char *path) !this->disc_info || !this->selected_key || !this->highlighted_key || + !this->status_bar_bg || !this->default_font || !this->small_font) { @@ -151,6 +154,8 @@ bool Gui::setTheme(const char *path) /* Create the views */ if (!this->mv) { + this->status_bar = new StatusBar(); + this->mv = new MainView(); this->dv = new DiscView(); this->ov = new OptionsView(); @@ -178,6 +183,9 @@ void Gui::runLogic(void) { GuiView *cur_view = this->peekView(); + this->status_bar->runLogic(); + this->timerController->tick(); + if (!this->is_active || !cur_view) return; if (this->dlg) @@ -186,7 +194,6 @@ void Gui::runLogic(void) this->kbd->runLogic(); else cur_view->runLogic(); - this->timerController->tick(); } void Gui::pushView(GuiView *view) @@ -266,7 +273,10 @@ void Gui::draw(SDL_Surface *where) GuiView *cur_view = this->peekView(); if (!this->is_active || !cur_view) + { + this->status_bar->draw(where); return; + } SDL_BlitSurface(this->background, NULL, screen, NULL); cur_view->draw(where); @@ -274,6 +284,7 @@ void Gui::draw(SDL_Surface *where) this->kbd->draw(where); if (this->dlg) this->dlg->draw(where); + this->status_bar->draw(where); } void Gui::activate() diff --git a/gui.hh b/gui.hh index 933dd01..052cd23 100644 --- a/gui.hh +++ b/gui.hh @@ -12,6 +12,7 @@ #include class DialogueBox; +class StatusBar; class MainView; class BindKeysView; @@ -73,6 +74,7 @@ public: SDL_Surface *background; SDL_Surface *main_menu_bg; + SDL_Surface *status_bar_bg; SDL_Surface *infobox; SDL_Surface *textbox; SDL_Surface *dialogue_bg; @@ -89,6 +91,7 @@ public: /* Handled specially */ VirtualKeyboard *kbd; DialogueBox *dlg; + StatusBar *status_bar; MainView *mv; DiscView *dv; diff --git a/status_bar.cpp b/status_bar.cpp new file mode 100644 index 0000000..38b5bb1 --- /dev/null +++ b/status_bar.cpp @@ -0,0 +1,65 @@ +#include "status_bar.hh" +#include "gui.hh" + +StatusBar::StatusBar() : Menu(Gui::gui->small_font), TimeoutHandler() +{ + memset(this->messages, 0, sizeof(this->messages)); + this->head = this->tail = 0; + this->cur_message = NULL; + this->setSelectedBackground(NULL, NULL, NULL, NULL, NULL, NULL); +} + + +void StatusBar::queueMessage(const char *message) +{ + this->messages[this->head] = message; + + /* If this is the first message, display it as soon as possible */ + if (this->head == this->tail) + Gui::gui->timerController->arm(this, 1); + + this->head = (this->head + 1) % N_STATUS_MESSAGES; + if (this->head == this->tail) + this->tail = (this->tail + 1) % N_STATUS_MESSAGES; +} + +const char *StatusBar::dequeueMessage() +{ + const char *out = this->messages[this->tail]; + + if (this->head == this->tail) + return NULL; + this->tail = (this->tail + 1) % N_STATUS_MESSAGES; + + return out; +} + +void StatusBar::timeoutCallback() +{ + static const char *text[2]; + + this->cur_message = this->dequeueMessage(); + if (this->cur_message) + Gui::gui->timerController->arm(this, 500); + text[0] = this->cur_message; + text[1] = NULL; + this->setText(text); +} + +void StatusBar::draw(SDL_Surface *where) +{ + SDL_Rect dst; + int x = 130; + int y = 12; + int w = 496; + int h = 56; + + if (!this->cur_message) + return; + + /* Blit the backgrounds */ + dst = (SDL_Rect){x,y,0,0}; + SDL_BlitSurface(Gui::gui->status_bar_bg, NULL, where, &dst); + + Menu::draw(where, x+4, y+4, w, h); +} diff --git a/status_bar.hh b/status_bar.hh new file mode 100644 index 0000000..0f11bbb --- /dev/null +++ b/status_bar.hh @@ -0,0 +1,37 @@ +#ifndef __STATUS_BAR_HH__ +#define __STATUS_BAR_HH__ + +#include "menu.hh" +#include "gui.hh" +#include "timer.hh" + +#define N_STATUS_MESSAGES 8 + +class StatusBar : public Menu, TimeoutHandler +{ +public: + StatusBar(); + + void queueMessage(const char *message); + + virtual void draw(SDL_Surface *where); + + virtual void hoverCallback(int which) {}; + + virtual void selectCallback(int which) {}; + + virtual void escapeCallback(int which) {}; + +protected: + virtual void timeoutCallback(); + + const char *dequeueMessage(); + + const char *messages[N_STATUS_MESSAGES]; + const char *cur_message; + int head, tail; + int x; + int y; +}; + +#endif /* __DIALOGUE_BOX_HH__ */