src/gui/mousecursor.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "gui/mousecursor.hpp"
00018 
00019 #include <SDL.h>
00020 
00021 #include "supertux/globals.hpp"
00022 #include "video/drawing_context.hpp"
00023 
00024 MouseCursor* MouseCursor::current_ = 0;
00025 
00026 MouseCursor::MouseCursor(std::string cursor_file) : 
00027   mid_x(0), 
00028   mid_y(0),
00029   state_before_click(),
00030   cur_state(),
00031   cursor()
00032 {
00033   cursor = Surface::create(cursor_file);
00034 
00035   cur_state = MC_NORMAL;
00036 }
00037 
00038 MouseCursor::~MouseCursor()
00039 {
00040 }
00041 
00042 int MouseCursor::state()
00043 {
00044   return cur_state;
00045 }
00046 
00047 void MouseCursor::set_state(int nstate)
00048 {
00049   cur_state = nstate;
00050 }
00051 
00052 void MouseCursor::set_mid(int x, int y)
00053 {
00054   mid_x = x;
00055   mid_y = y;
00056 }
00057 
00058 void MouseCursor::draw(DrawingContext& context)
00059 {
00060   if(cur_state == MC_HIDE)
00061     return;
00062 
00063   int x,y,w,h;
00064   Uint8 ispressed = SDL_GetMouseState(&x,&y);
00065 
00066   x = int(x * float(SCREEN_WIDTH)/g_screen->w);
00067   y = int(y * float(SCREEN_HEIGHT)/g_screen->h);
00068 
00069   w = (int) cursor->get_width();
00070   h = (int) (cursor->get_height() / MC_STATES_NB);
00071   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2)) {
00072     if(cur_state != MC_CLICK) {
00073       state_before_click = cur_state;
00074       cur_state = MC_CLICK;
00075     }
00076   } else {
00077     if(cur_state == MC_CLICK)
00078       cur_state = state_before_click;
00079   }
00080 
00081   context.draw_surface_part(cursor, Vector(0, h*cur_state),
00082                             Vector(w, h), Vector(x-mid_x, y-mid_y), LAYER_GUI+100);
00083 }
00084 
00085 /* EOF */

Generated on Mon Jun 9 03:38:18 2014 for SuperTux by  doxygen 1.5.1