//
// SlowdownButton.cpp
//

// Engine includes.
#include "WorldManager.h"

// Game includes.
#include "main.h"
#include "SlowdownButton.h"
#include "utility.h"

SlowdownButton::SlowdownButton(df::Sprite *p_sprite) {
  m_p_sprite = p_sprite;
  setBorder(true);
  setLocation(df::BOTTOM_RIGHT);
  setColor(df::WHITE);
  setDefaultColor(df::WHITE);
  std::string s = "slow ";
  s += df::toString(m_p_sprite -> getSlowdown());
  setViewString(s);
  setType("SlowdownButton");
}

// On callback, change sprite slowdown.
void SlowdownButton::callback() {
  df::ObjectList ol = WM.objectsOfType(PLAYER_TYPE);
  df::Object *p_o = ol[0];
  df::Animation a = p_o -> getAnimation();
  int slowdown = m_p_sprite -> getSlowdown();

  slowdown += 1;
  if (slowdown > 8)
    slowdown = -1;
  m_p_sprite -> setSlowdown(slowdown);
  if (slowdown == -1) {
    setViewString("stop");
    a.setSlowdownCount(-1);
    p_o -> setAnimation(a);
  } else {
    std::string s = "slow ";
    s += df::toString(slowdown);
    setViewString(s);
    if (slowdown == 0) {
      a.setSlowdownCount(0);
      p_o -> setAnimation(a);
    }
  }
}
