-Changed drawing model. Everything is handled through DrawingContext now and
[supertux.git] / src / mousecursor.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include "screen/drawing_context.h"
20 #include "mousecursor.h"
21
22 MouseCursor* MouseCursor::current_ = 0;
23
24 MouseCursor::MouseCursor(std::string cursor_file, int frames) : mid_x(0), mid_y(0)
25 {
26   cursor = new Surface(cursor_file, USE_ALPHA);
27   
28   cur_state = MC_NORMAL;
29   cur_frame = 0;
30   tot_frames = frames;
31
32   timer.init(false);
33   timer.start(MC_FRAME_PERIOD);
34   
35   SDL_ShowCursor(SDL_DISABLE);
36 }
37
38 MouseCursor::~MouseCursor()
39 {
40   delete cursor;
41
42   SDL_ShowCursor(SDL_ENABLE);
43 }
44
45 int MouseCursor::state()
46 {
47   return cur_state;
48 }
49
50 void MouseCursor::set_state(int nstate)
51 {
52   cur_state = nstate;
53 }
54
55 void MouseCursor::set_mid(int x, int y)
56 {
57   mid_x = x;
58   mid_y = y;
59 }
60
61 void MouseCursor::draw(DrawingContext& context)
62 {
63   int x,y,w,h;
64   Uint8 ispressed = SDL_GetMouseState(&x,&y);
65   w = cursor->w / tot_frames;
66   h = cursor->h / MC_STATES_NB;
67   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2))
68     {
69       if(cur_state != MC_CLICK)
70         {
71           state_before_click = cur_state;
72           cur_state = MC_CLICK;
73         }
74     }
75   else
76     {
77       if(cur_state == MC_CLICK)
78         cur_state = state_before_click;
79     }
80
81   if(timer.get_left() < 0 && tot_frames > 1)
82     {
83       cur_frame++;
84       if(cur_frame++ >= tot_frames)
85         cur_frame = 0;
86
87       timer.start(MC_FRAME_PERIOD);
88     }
89
90   context.push_transform();
91   context.set_translation(Vector(0, 0));
92   context.draw_surface_part(cursor, Vector(w*cur_frame, h*cur_state), Vector(w,
93         h), Vector(x-mid_x, y-mid_y), LAYER_FOREGROUND1+100);
94   context.pop_transform();
95 }