Fixed creating level-subset again.
[supertux.git] / src / button.cpp
index 3fcfc70..e40332f 100644 (file)
@@ -31,26 +31,7 @@ Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x
 {
   popup_timer.init(false);
 
-  char filename[1024];
-
-  if(!icon_file.empty())
-  {
-    snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
-    if(!faccessible(filename))
-      snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
-  }
-  else
-  {
-    snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
-  }
-
-  if(mw != -1 || mh != -1)
-  {
-    icon = new Surface(filename,USE_ALPHA);
-    icon->resize(mw,mh);
-  }
-  else
-    icon = new Surface(filename,USE_ALPHA);
+  add_icon(icon_file,mw,mh);
 
   info = ninfo;
 
@@ -58,16 +39,15 @@ Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x
 
   rect.x = x;
   rect.y = y;
-  rect.w = icon->w;
-  rect.h = icon->h;
+  rect.w = icon[0]->w;
+  rect.h = icon[0]->h;
   tag = -1;
   state = BUTTON_NONE;
   show_info = false;
-  bkgd = NULL;
   game_object = NULL;
 }
 
-void Button::change_icon(std::string icon_file, int /*mw*/, int /*mh*/)
+void Button::add_icon(std::string icon_file, int mw, int mh)
 {
   char filename[1024];
 
@@ -82,8 +62,14 @@ void Button::change_icon(std::string icon_file, int /*mw*/, int /*mh*/)
     snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
   }
 
-  delete icon;
-  icon = new Surface(filename,USE_ALPHA);
+  if(mw != -1 || mh != -1)
+  {
+    icon.push_back(new Surface(filename,USE_ALPHA));
+    icon.back()->resize(mw,mh);
+  }
+  else
+    icon.push_back(new Surface(filename,USE_ALPHA));
+    
 }
 
 void Button::draw()
@@ -94,14 +80,13 @@ void Button::draw()
 
   fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
   fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
-  if(bkgd != NULL)
-  {
-    bkgd->draw(rect.x,rect.y);
-  }
-  icon->draw(rect.x,rect.y);
+
+  for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
+  (*it)->draw(rect.x,rect.y);
+  
   if(game_object != NULL)
   {
-    game_object->draw();
+    game_object->draw_on_screen(rect.x,rect.y);
   }
 
   if(show_info)
@@ -125,7 +110,9 @@ void Button::draw()
 
 Button::~Button()
 {
-  delete icon;
+  for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
+  delete (*it);
+  icon.clear();
   delete game_object;
 }
 
@@ -139,12 +126,23 @@ 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.button.button == SDL_BUTTON_LEFT)
     if(event.type == SDL_MOUSEBUTTONDOWN)
       state = BUTTON_PRESSED;
     else
@@ -179,14 +177,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;
   }
 }
@@ -200,6 +199,7 @@ ButtonPanel::ButtonPanel(int x, int y, int w, int h)
   rect.w = w;
   rect.h = h;
   hidden = false;
+  hlast = false;
 }
 
 Button* ButtonPanel::event(SDL_Event& event)
@@ -210,7 +210,11 @@ Button* ButtonPanel::event(SDL_Event& event)
     {
       (*it)->event(event);
       if((*it)->state != BUTTON_NONE)
+      {
+        if(hlast && (*it)->state == BUTTON_CLICKED)
+       last_clicked = it;
         return (*it);
+       }
     }
     return NULL;
   }
@@ -238,6 +242,10 @@ void ButtonPanel::draw()
     for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
     {
       (*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);
+      }
     }
   }
 }
@@ -274,3 +282,10 @@ Button* ButtonPanel::manipulate_button(int i)
   else
     return item[i];
 }
+
+void ButtonPanel::highlight_last(bool b)
+{
+hlast = b;
+}
+
+