Tweaked scores.
[supertux.git] / src / button.cpp
index 91ce1de..2201808 100644 (file)
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
 
-#include <string.h>
-#include <stdlib.h>
+#include <cstring>
+#include <cstdlib>
+
 #include "setup.h"
-#include "screen.h"
+#include "screen/screen.h"
+#include "screen/drawing_context.h"
 #include "globals.h"
 #include "button.h"
+#include "camera.h"
 
 Timer Button::popup_timer;
 
-Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x, int y, int mw, int mh)
+Button::Button(Surface* button_image, const std::string& ninfo,
+    SDLKey nshortcut, int x, int y, int mw, int mh)
 {
   popup_timer.init(false);
 
-  add_icon(icon_file,mw,mh);
+  if(button_image)
+    icon.push_back(button_image);
 
   info = ninfo;
 
@@ -44,10 +49,29 @@ Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x
   tag = -1;
   state = BUTTON_NONE;
   show_info = false;
-  game_object = NULL;
 }
 
-void Button::add_icon(std::string icon_file, int mw, int mh)
+Button::Button(const std::string& imagefilename, const std::string& ninfo,
+    SDLKey nshortcut, int x, int y, int mw, int mh)
+{
+  popup_timer.init(false);
+
+  add_icon(imagefilename, mw, mh);
+  
+  info = ninfo;
+
+  shortcut = nshortcut;
+
+  rect.x = x;
+  rect.y = y;
+  rect.w = icon[0]->w;
+  rect.h = icon[0]->h;
+  tag = -1;
+  state = BUTTON_NONE;
+  show_info = false;
+}
+
+void Button::add_icon(const std::string& icon_file, int mw, int mh)
 {
   char filename[1024];
 
@@ -64,15 +88,15 @@ void Button::add_icon(std::string icon_file, int mw, int mh)
 
   if(mw != -1 || mh != -1)
   {
-    icon.push_back(new Surface(filename,USE_ALPHA));
+    icon.push_back(new Surface(filename,true));
     icon.back()->resize(mw,mh);
   }
   else
-    icon.push_back(new Surface(filename,USE_ALPHA));
-    
+    icon.push_back(new Surface(filename,true));
+
 }
 
-void Button::draw()
+void Button::draw(DrawingContext& context)
 {
   if(state == BUTTON_HOVER)
     if(!popup_timer.check())
@@ -82,27 +106,29 @@ 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);
-  
-  if(game_object != NULL)
+    context.draw_surface(*it, Vector(rect.x,rect.y), LAYER_GUI);
+
+/*  if(drawable)
   {
-    game_object->draw_on_screen(rect.x,rect.y);
-  }
+    Camera viewport;
+    viewport.set_translation(Vector(rect.x, rect.y));
+    drawable->draw(viewport, 0);
+  }*/
 
   if(show_info)
   {
     char str[80];
     int i = -32;
 
-    if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text->w)
-      i = rect.w + strlen(info.c_str()) * white_small_text->w;
+    if(0 > rect.x - white_small_text->get_text_width(info))
+      i = rect.w + (int)white_small_text->get_text_width(info);
 
     if(!info.empty())
-      white_small_text->draw(info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text->w, rect.y, 1);
+      context.draw_text(white_small_text, info, Vector(i + rect.x - white_small_text->get_text_width(info), rect.y), LAYER_GUI);
     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);
+    context.draw_text(white_small_text, str, Vector(i + rect.x -  white_small_text->get_text_width(str), rect.y + white_small_text->get_height()+2), LAYER_GUI);
   }
-  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 +137,18 @@ 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;
+  // FIXME TODO XXX: commenting this out fixes the leveleditor quit crash
+  //   probably should be deleted somehow, though
+  //delete drawable;
 }
 
 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)
@@ -131,31 +162,36 @@ void Button::event(SDL_Event &event)
       show_info = true;
       return;
     }
-    else if(event.button.button == 4) /* Mouse wheel up. */
+    else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
     {
       state = BUTTON_WHEELUP;
       return;
     }
-    else if(event.button.button == 5) /* Mouse wheel down. */
+    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;
@@ -176,14 +212,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;
   }
 }
@@ -204,17 +241,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
   {
@@ -231,7 +269,7 @@ ButtonPanel::~ButtonPanel()
   item.clear();
 }
 
-void ButtonPanel::draw()
+void ButtonPanel::draw(DrawingContext& context)
 {
 
   if(hidden == false)
@@ -239,10 +277,10 @@ void ButtonPanel::draw()
     fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
     for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
     {
-      (*it)->draw();
+      (*it)->draw(context);
       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);
       }
     }
   }
@@ -283,7 +321,5 @@ Button* ButtonPanel::manipulate_button(int i)
 
 void ButtonPanel::highlight_last(bool b)
 {
-hlast = b;
+  hlast = b;
 }
-
-