From f8bbe977c0acd3e7e70c56d169d9fae43a2805de Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Sun, 20 Dec 2009 10:21:06 +0000 Subject: [PATCH] Move background highlighting to utils --- menu.cpp | 39 ++------------------------------------- menu.hh | 4 ---- utils.cpp | 36 ++++++++++++++++++++++++++++++++++++ utils.hh | 6 ++++++ virtual_keyboard.cpp | 10 ++++------ 5 files changed, 48 insertions(+), 47 deletions(-) diff --git a/menu.cpp b/menu.cpp index e7fe4b9..67cbea2 100644 --- a/menu.cpp +++ b/menu.cpp @@ -60,41 +60,6 @@ void Menu::printText(SDL_Surface *where, const char *msg, SDL_Color clr, free(buf); } -void Menu::highlightBackground(SDL_Surface *where, - SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right, - int x, int y, int w, int h) -{ - SDL_Rect dst; - - /* Can't highlight without images */ - if (!bg_left || !bg_middle || !bg_right) - return; - - int font_height = this->font->getHeight("X"); - int bg_y_start = y + font_height / 2 - - bg_left->h / 2; - int bg_x_start = x - bg_left->w / 3; - int bg_x_end = x + w - (2 * bg_right->w) / 3; - int n_mid = (bg_x_end - bg_x_start) / bg_middle->w; - - /* Left */ - dst = (SDL_Rect){bg_x_start, bg_y_start, 0,0}; - SDL_BlitSurface(bg_left, NULL, where, &dst); - - /* Middle */ - for (int i = 1; i < n_mid; i++) - { - dst = (SDL_Rect){bg_x_start + i * bg_middle->w, bg_y_start, 0,0}; - SDL_BlitSurface(bg_middle, NULL, where, &dst); - } - dst = (SDL_Rect){bg_x_end - bg_middle->w, bg_y_start, 0,0}; - SDL_BlitSurface(bg_middle, NULL, where, &dst); - - /* Right */ - dst = (SDL_Rect){bg_x_end, bg_y_start, 0,0}; - SDL_BlitSurface(bg_right, NULL, where, &dst); -} - void Menu::draw(SDL_Surface *where, int x, int y, int w, int h) { @@ -144,7 +109,7 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h) tw = this->font->getWidth(msg); th = this->font->getHeight(msg); - this->highlightBackground(where, + highlight_background(where, this->font, this->text_bg_left, this->text_bg_middle, this->text_bg_right, x_start, cur_y, tw, th); } @@ -185,7 +150,7 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h) tw = this->font->getWidth(p); th = this->font->getHeight(p); - this->highlightBackground(where, + highlight_background(where, this->font, this->submenu_bg_left, this->submenu_bg_middle, this->submenu_bg_right, x_start + tw_first, cur_y, tw, th); free(p); diff --git a/menu.hh b/menu.hh index 092482e..1273d50 100644 --- a/menu.hh +++ b/menu.hh @@ -61,10 +61,6 @@ public: int x, int y, int w, int h); protected: - void highlightBackground(SDL_Surface *where, - SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right, - int x, int y, int w, int h); - void printText(SDL_Surface *where, const char *msg, SDL_Color clr, int x, int y, int w, int h); diff --git a/utils.cpp b/utils.cpp index cda11b7..0d04645 100644 --- a/utils.cpp +++ b/utils.cpp @@ -6,6 +6,7 @@ #include #include "utils.hh" +#include "font.hh" TTF_Font *read_and_alloc_font(const char *path, int pt_size) { @@ -238,3 +239,38 @@ void *sdl_surface_to_png(SDL_Surface *surf, size_t *out_sz) return out.data; } + +void highlight_background(SDL_Surface *where, Font *font, + SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right, + int x, int y, int w, int h) +{ + SDL_Rect dst; + + /* Can't highlight without images */ + if (!bg_left || !bg_middle || !bg_right) + return; + + int font_height = font->getHeight("X"); + int bg_y_start = y + font_height / 2 - + bg_left->h / 2; + int bg_x_start = x - bg_left->w / 3; + int bg_x_end = x + w - (2 * bg_right->w) / 3; + int n_mid = (bg_x_end - bg_x_start) / bg_middle->w; + + /* Left */ + dst = (SDL_Rect){bg_x_start, bg_y_start, 0,0}; + SDL_BlitSurface(bg_left, NULL, where, &dst); + + /* Middle */ + for (int i = 1; i < n_mid; i++) + { + dst = (SDL_Rect){bg_x_start + i * bg_middle->w, bg_y_start, 0,0}; + SDL_BlitSurface(bg_middle, NULL, where, &dst); + } + dst = (SDL_Rect){bg_x_end - bg_middle->w, bg_y_start, 0,0}; + SDL_BlitSurface(bg_middle, NULL, where, &dst); + + /* Right */ + dst = (SDL_Rect){bg_x_end, bg_y_start, 0,0}; + SDL_BlitSurface(bg_right, NULL, where, &dst); +} diff --git a/utils.hh b/utils.hh index 00dc4b6..4e6243b 100644 --- a/utils.hh +++ b/utils.hh @@ -8,6 +8,8 @@ #include #include +class Font; + #define BUG_ON(cond) #define panic(x...) do \ @@ -67,4 +69,8 @@ const char **get_file_list(const char *base_dir, const char *exts[]); void *sdl_surface_to_png(SDL_Surface *src, size_t *out_sz); +void highlight_background(SDL_Surface *where, Font *font, + SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right, + int x, int y, int w, int h); + #endif /* __UTILS_H__ */ diff --git a/virtual_keyboard.cpp b/virtual_keyboard.cpp index dca39d4..9f98899 100644 --- a/virtual_keyboard.cpp +++ b/virtual_keyboard.cpp @@ -115,12 +115,10 @@ void VirtualKeyboard::draw(SDL_Surface *where, int x, int y, int w, int h) what = shifted_names[which]; if (this->sel_x == x && this->sel_y == y) - { - SDL_Rect dst = (SDL_Rect){x * key_w + border_x - 8, - y * key_h + border_y - 4, 0,0}; - - SDL_BlitSurface(Gui::gui->selected_key, NULL, where, &dst); - } + highlight_background(where, Gui::gui->small_font, + Gui::gui->bg_left, Gui::gui->bg_middle, Gui::gui->bg_right, + x * key_w + border_x, y * key_h + border_y, + this->font->getWidth(what), h); this->font->draw(where, what, x * key_w + border_x, y * key_h + border_y, w, h); }