From: Tobias Gläßer Date: Sun, 28 Mar 2004 01:00:16 +0000 (+0000) Subject: We have our own mouse-cursor now! (graphics by Settra Gaia) X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ed8b08f0568d887c3459568b6a479356af09e36f;p=supertux.git We have our own mouse-cursor now! (graphics by Settra Gaia) SVN-Revision: 397 --- diff --git a/src/gameloop.cpp b/src/gameloop.cpp index ba8ec364a..d59de79bf 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -570,7 +570,10 @@ void game_draw(void) } if(show_menu) + { menu_process_current(); + mouse_cursor->draw(); + } /* (Update it all!) */ updatescreen(); diff --git a/src/globals.cpp b/src/globals.cpp index 74b2e491f..383028577 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -18,6 +18,8 @@ std::string datadir; SDL_Surface * screen; text_type black_text, gold_text, blue_text, red_text, yellow_nums, white_text, white_small_text, white_big_text; +MouseCursor * mouse_cursor; + bool use_gl; bool use_joystick; bool use_fullscreen; diff --git a/src/globals.h b/src/globals.h index 9528e9440..3c52a2c3b 100644 --- a/src/globals.h +++ b/src/globals.h @@ -18,12 +18,15 @@ #include #include "text.h" #include "menu.h" +#include "mousecursor.h" extern std::string datadir; extern SDL_Surface * screen; extern text_type black_text, gold_text, white_text, white_small_text, white_big_text, blue_text, red_text, yellow_nums; +extern MouseCursor * mouse_cursor; + extern bool use_gl; extern bool use_joystick; extern bool use_fullscreen; diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index 4889c1786..0484439f5 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -275,6 +275,7 @@ int leveleditor(int levelnb) break; } } + mouse_cursor->draw(); } if(done) @@ -849,18 +850,19 @@ void le_checkevents() while(SDL_PollEvent(&event)) { + if(show_menu) + menu_event(event); + /* testing SDL_KEYDOWN, SDL_KEYUP and SDL_QUIT events*/ if(event.type == SDL_KEYDOWN || ((event.type == SDL_MOUSEBUTTONDOWN || SDL_MOUSEMOTION) && (event.motion.x > 0 && event.motion.x < screen->w - 64 && event.motion.y > 0 && event.motion.y < screen->h))) { - switch(event.type) { case SDL_KEYDOWN: // key pressed key = event.key.keysym.sym; if(show_menu) { - menu_event(event); if(key == SDLK_ESCAPE) { show_menu = false; diff --git a/src/menu.cpp b/src/menu.cpp index a69dcfdb9..61ebe5902 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -456,16 +456,11 @@ Menu::draw_item(int index, // Position of the current item in the menu } } -/* Draw the current menu. */ -void -Menu::draw() +int Menu::width() { - int menu_height; - int menu_width; - /* The width of the menu has to be more than the width of the text with the most characters */ - menu_width = 0; + int menu_width = 0; for(int i = 0; i < num_items; ++i) { int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list)); @@ -477,8 +472,20 @@ Menu::draw() } } - menu_width = menu_width * 16 + 48; - menu_height = (num_items) * 24; + return (menu_width * 16 + 48); +} + +int Menu::height() +{ + return ((num_items) * 24); +} + +/* Draw the current menu. */ +void +Menu::draw() +{ + int menu_height = height(); + int menu_width = width(); /* Draw a transparent background */ fillrect(pos_x - menu_width/2, @@ -524,11 +531,12 @@ void menu_event(SDL_Event& event) SDLKey key; switch(event.type) { - case SDL_KEYDOWN: + case SDL_KEYDOWN: key = event.key.keysym.sym; SDLMod keymod; char ch[2]; keymod = SDL_GetModState(); + int x,y; /* If the current unicode character is an ASCII character, assign it to ch. */ @@ -604,18 +612,37 @@ void menu_event(SDL_Event& event) case SDL_JOYBUTTONDOWN: menuaction = MENU_ACTION_HIT; break; + case SDL_MOUSEBUTTONDOWN: + x = event.motion.x; + y = event.motion.y; + if(x > current_menu->pos_x - current_menu->width()/2 && + x < current_menu->pos_x + current_menu->width()/2 && + y > current_menu->pos_y - current_menu->height()/2 && + y < current_menu->pos_y + current_menu->height()/2) + { + menuaction = MENU_ACTION_HIT; + } + break; + case SDL_MOUSEMOTION: + x = event.motion.x; + y = event.motion.y; + if(x > current_menu->pos_x - current_menu->width()/2 && + x < current_menu->pos_x + current_menu->width()/2 && + y > current_menu->pos_y - current_menu->height()/2 && + y < current_menu->pos_y + current_menu->height()/2) + { + current_menu->active_item = (y - (current_menu->pos_y - current_menu->height()/2)) / 24; + menu_change = true; + mouse_cursor->set_state(MC_LINK); + } + else + { + mouse_cursor->set_state(MC_NORMAL); + } + break; default: break; } - /* FIXME: NO JOYSTICK SUPPORT */ - /*#ifdef JOY_YES - else if (event.type == SDL_JOYBUTTONDOWN) - { - Joystick button: Continue: - - done = 1; - } - #endif*/ } diff --git a/src/menu.h b/src/menu.h index 15d4e08c4..3da7a9740 100644 --- a/src/menu.h +++ b/src/menu.h @@ -17,6 +17,7 @@ #include "texture.h" #include "timer.h" #include "type.h" +#include "mousecursor.h" /* Kinds of menu items */ enum MenuItemKind { @@ -51,6 +52,8 @@ void menu_item_change_input(menu_item_type* pmenu_item, const char *text); class Menu { +friend void menu_event(SDL_Event& event); + private: // position of the menu (ie. center of the menu, not top/left) int pos_x; @@ -58,6 +61,8 @@ private: int num_items; Menu* last_menu; + int width(); + int height(); public: timer_type effect; diff --git a/src/mousecursor.cpp b/src/mousecursor.cpp index 0eab12914..0d5f829f4 100644 --- a/src/mousecursor.cpp +++ b/src/mousecursor.cpp @@ -15,16 +15,16 @@ MouseCursor::MouseCursor(std::string cursor_file, int frames) { -texture_load(&cursor,cursor_file.c_str(),USE_ALPHA); + texture_load(&cursor,cursor_file.c_str(),USE_ALPHA); -cur_state = MC_NORMAL; -cur_frame = 0; -tot_frames = frames; + cur_state = MC_NORMAL; + cur_frame = 0; + tot_frames = frames; -timer_init(&timer, false); -timer_start(&timer,MC_FRAME_PERIOD); + timer_init(&timer, false); + timer_start(&timer,MC_FRAME_PERIOD); -SDL_ShowCursor(SDL_DISABLE); + SDL_ShowCursor(SDL_DISABLE); } MouseCursor::~MouseCursor() @@ -36,28 +36,42 @@ MouseCursor::~MouseCursor() int MouseCursor::state() { -return cur_state; + return cur_state; } void MouseCursor::set_state(int nstate) { -cur_state = nstate; + cur_state = nstate; } -void MouseCursor::draw(int x, int y) +void MouseCursor::draw() { -int w,h; -w = cursor.w / tot_frames; -h = cursor.h / MC_STATES_NB; + int x,y,w,h; + Uint8 ispressed = SDL_GetMouseState(&x,&y); + w = cursor.w / tot_frames; + h = cursor.h / MC_STATES_NB; + if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2)) + { + if(cur_state != MC_CLICK) + { + state_before_click = cur_state; + cur_state = MC_CLICK; + } + } + else + { + if(cur_state == MC_CLICK) + cur_state = state_before_click; + } -if(timer_get_left(&timer) < 0 && tot_frames > 1) - { - cur_frame++; - if(cur_frame++ >= tot_frames) - cur_frame = 0; + if(timer_get_left(&timer) < 0 && tot_frames > 1) + { + cur_frame++; + if(cur_frame++ >= tot_frames) + cur_frame = 0; - timer_start(&timer,MC_FRAME_PERIOD); - } + timer_start(&timer,MC_FRAME_PERIOD); + } -texture_draw_part(&cursor, w*cur_frame, h*cur_state , x, y, w, h); + texture_draw_part(&cursor, w*cur_frame, h*cur_state , x, y, w, h); } diff --git a/src/mousecursor.h b/src/mousecursor.h index 27787562c..7ef6e0153 100644 --- a/src/mousecursor.h +++ b/src/mousecursor.h @@ -33,9 +33,10 @@ class MouseCursor ~MouseCursor(); int state(); void set_state(int nstate); - void draw(int x, int y); + void draw(); private: + int state_before_click; int cur_state; int cur_frame, tot_frames; texture_type cursor; diff --git a/src/setup.cpp b/src/setup.cpp index 8b5a52dd0..60e5695ac 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -545,6 +545,9 @@ void st_general_setup(void) texture_load(&arrow_left, datadir + "/images/icons/left.png", USE_ALPHA); texture_load(&arrow_right, datadir + "/images/icons/right.png", USE_ALPHA); + /* Load the mouse-cursor */ + mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1); + } void st_general_free(void) @@ -567,6 +570,9 @@ void st_general_free(void) texture_free(&arrow_left); texture_free(&arrow_right); + /* Free mouse-cursor */ + delete mouse_cursor; + /* Free menus */ delete main_menu; delete game_menu; diff --git a/src/tile.h b/src/tile.h index 1d301e8dd..7ec80356c 100644 --- a/src/tile.h +++ b/src/tile.h @@ -55,6 +55,7 @@ struct Tile unsigned char alpha; }; + class TileManager { private: @@ -80,4 +81,6 @@ class TileManager } }; + + #endif diff --git a/src/title.cpp b/src/title.cpp index c1bda33be..a6f24fd67 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -326,6 +326,8 @@ int title(void) process_save_load_game_menu(false); } + mouse_cursor->draw(); + flipscreen(); /* Set the time of the last update and the time of the current update */ @@ -461,7 +463,7 @@ void display_credits() texture_draw_part(&bkg_title, 0, 0, 0, 0, 640, 130); - + flipscreen(); if(60+screen->h+(n*18)+(d*18)-scroll < 0 && 20+60+screen->h+(n*18)+(d*18)-scroll < 0)