//
// mainmenubutton.cpp
//
// button from program's main menu
//
//
// Author: Tomi Belan <tomi.belan@gmail.com>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <iostream> // @debug

#include <SDL.h>

#include "mainmenubutton.h"
#include "mainmenu.h"

MainMenuButton::MainMenuButton(MainMenu* menu, int id)
{
  this->menu = menu;
  this->id = id;
  type = MainMenu::buttonTypes[id];
  buf = createSurface(MainMenu::firstButton.w, MainMenu::firstButton.h);
  focusLevel = 0;
//  SDL_SetAlpha(this->buf, SDL_SRCALPHA | SDL_RLEACCEL, SDL_ALPHA_OPAQUE);
}

MainMenuButton::~MainMenuButton()
{
  SDL_FreeSurface(buf);
}

void MainMenuButton::update()
{
  int oneStep = 70; // bigger value = faster effect
  if(menu->getFocusedButton() == id) {
    if(focusLevel < 255)
      focusLevel += oneStep;
    if(focusLevel > 255)
      focusLevel = 255;
  }
  else {
    if(focusLevel > 0)
      focusLevel -= oneStep;
    if(focusLevel < 0)
      focusLevel = 0;
  }
}

void MainMenuButton::redraw(SDL_Surface* where)
{
  SDL_Surface *buttons = imagePool["tlacitka.png"];
  SDL_Rect buttonPos = getPosition();
  
  SDL_Rect normalPos, focusPos;
  normalPos.x = MainMenu::firstButton.x;
  normalPos.y = MainMenu::firstButton.y + type*MainMenu::firstButton.h;
  normalPos.w = MainMenu::firstButton.w; normalPos.h = MainMenu::firstButton.h;
  focusPos.x = normalPos.x + MainMenu::focusModifier.x;
  focusPos.y = normalPos.y + MainMenu::focusModifier.y;
  focusPos.w = normalPos.w; focusPos.h = normalPos.h;
  
  SDL_SetAlpha(buf, SDL_SRCALPHA | SDL_RLEACCEL, focusLevel);
  SDL_BlitSurface(where, &buttonPos, buf, NULL);
  SDL_BlitSurface(buttons, &normalPos, where, &buttonPos);
  SDL_BlitSurface(buttons, &focusPos, buf, NULL);
  SDL_BlitSurface(buf, NULL, where, &buttonPos);
}

SDL_Rect MainMenuButton::getPosition() const
{
  SDL_Rect pos;
  pos.w = MainMenu::firstButton.w; pos.h = MainMenu::firstButton.h;
  pos.x = (screen->w-MainMenu::firstButton.w)/2;
  pos.y = MainMenu::getAbsoluteMenuYOffset()+(MainMenu::getMenuHeight()+pos.h*(2*id-MainMenu::numButtons))/2;
//  pos.y = (screen->h-MainMenu::firstButton.h*(MainMenu::numButtons-2*id)+MainMenu::logoYOffset)/2;
  return pos;
}

