- Trampoline test level
[supertux.git] / src / button.cpp
index ff0c8d2..8bb8e50 100644 (file)
@@ -69,7 +69,7 @@ void Button::add_icon(std::string icon_file, int mw, int mh)
   }
   else
     icon.push_back(new Surface(filename,USE_ALPHA));
-    
+
 }
 
 void Button::draw()
@@ -82,11 +82,11 @@ void Button::draw()
   fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
 
   for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
-  (*it)->draw(rect.x,rect.y);
-  
+    (*it)->draw(rect.x,rect.y);
+
   if(game_object != NULL)
   {
-    game_object->draw_on_screen();
+    game_object->draw_on_screen(rect.x,rect.y);
   }
 
   if(show_info)
@@ -102,7 +102,7 @@ void Button::draw()
     sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
     white_small_text->draw(str, i + rect.x - strlen(str) * white_small_text->w, rect.y + white_small_text->h+2, 1);
   }
-  if(state == BUTTON_PRESSED)
+  if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE)
     fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
   else if(state == BUTTON_HOVER)
     fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
@@ -111,13 +111,16 @@ void Button::draw()
 Button::~Button()
 {
   for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
-  delete (*it);
+    delete (*it);
   icon.clear();
   delete game_object;
 }
 
 void Button::event(SDL_Event &event)
 {
+  if(state == BUTTON_DEACTIVE)
+    return;
+
   SDLKey key = event.key.keysym.sym;
 
   if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)
@@ -126,26 +129,41 @@ void Button::event(SDL_Event &event)
         event.button.y < rect.y || event.button.y >= rect.y + rect.h)
       return;
 
-    if(event.button.button != SDL_BUTTON_LEFT)
+    if(event.button.button == SDL_BUTTON_RIGHT)
     {
       show_info = true;
       return;
     }
+    else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
+    {
+      state = BUTTON_WHEELUP;
+      return;
+    }
+    else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */
+    {
+      state = BUTTON_WHEELDOWN;
+      return;
+    }
 
-    if(event.type == SDL_MOUSEBUTTONDOWN)
-      state = BUTTON_PRESSED;
-    else
-      state = BUTTON_CLICKED;
+    if(event.button.button == SDL_BUTTON_LEFT)
+      if(event.type == SDL_MOUSEBUTTONDOWN)
+        state = BUTTON_PRESSED;
+      else
+        state = BUTTON_CLICKED;
   }
   else if(event.type == SDL_MOUSEMOTION)
   {
     if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w ||
         event.motion.y < rect.y || event.motion.y >= rect.y + rect.h)
+    {
       state = BUTTON_NONE;
+    }
     else
+    {
       state = BUTTON_HOVER;
+      popup_timer.start(1500);
+    }
 
-    popup_timer.start(1500);
     if(show_info)
     {
       show_info = false;
@@ -166,14 +184,15 @@ void Button::event(SDL_Event &event)
 int Button::get_state()
 {
   int rstate;
-  if(state == BUTTON_CLICKED)
+  switch(state)
   {
+  case BUTTON_CLICKED:
+  case BUTTON_WHEELUP:
+  case BUTTON_WHEELDOWN:
     rstate = state;
     state = BUTTON_NONE;
     return rstate;
-  }
-  else
-  {
+  default:
     return state;
   }
 }
@@ -194,17 +213,18 @@ Button* ButtonPanel::event(SDL_Event& event)
 {
   if(!hidden)
   {
+  Button* ret = NULL;
     for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
     {
       (*it)->event(event);
       if((*it)->state != BUTTON_NONE)
       {
         if(hlast && (*it)->state == BUTTON_CLICKED)
-       last_clicked = it;
-        return (*it);
-       }
+          last_clicked = it;
+       ret = (*it);
+      }
     }
-    return NULL;
+    return ret;
   }
   else
   {
@@ -232,7 +252,7 @@ void ButtonPanel::draw()
       (*it)->draw();
       if(hlast && it == last_clicked)
       {
-      fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
+        fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
       }
     }
   }
@@ -273,7 +293,7 @@ Button* ButtonPanel::manipulate_button(int i)
 
 void ButtonPanel::highlight_last(bool b)
 {
-hlast = b;
+  hlast = b;
 }