void
InventoryImpl::update(float delta, const Controller& controller)
{
  float step_angle = (2.0f * math::pi) / static_cast<float>(items.size());
  if (fabsf(add_angle) > step_angle)
  {
    if (moving == 1)
      decr_current_item();
    else if (moving == -1)
      incr_current_item();

    moving = 0;
    add_angle = 0;
  }

  if (controller.get_axis_state(X_AXIS) < -0.5f)
  {
    if (moving == 1)
    {
      add_angle = -step_angle + add_angle;
      decr_current_item();
    }

    moving = -1;
  }
  else if (controller.get_axis_state(X_AXIS) > 0.5f)
  {
    if (moving == -1)
    {
      add_angle = step_angle + add_angle;
      incr_current_item();
    }

    moving =  1;
  }

  if (moving == -1)
  {
    add_angle -= 3 * delta;
  }
  else if (moving == 1)
  {
    add_angle += 3 * delta;
  }

  if (moving == 0)
  {
    if (controller.button_was_pressed(OK_BUTTON) ||
        controller.button_was_pressed(CANCEL_BUTTON) ||
        controller.button_was_pressed(INVENTORY_BUTTON))
    {
      GameSession::current()->set_control_state(GameSession::GAME);
    }
  }
}
void
GeometryTest::update(float delta, const Controller& controller)
{
  if (controller.button_was_pressed(ESCAPE_BUTTON) ||
      controller.button_was_pressed(PAUSE_BUTTON))
  {
    MenuManager::display_pause_menu();
  }

  cursor.x += controller.get_axis_state(X_AXIS) * 500.0f * delta;
  cursor.y += controller.get_axis_state(Y_AXIS) * 500.0f * delta;

  cursor2.x += controller.get_axis_state(X2_AXIS) * 500.0f * delta;
  cursor2.y += controller.get_axis_state(Y2_AXIS) * 500.0f * delta;

  if (controller.button_was_pressed(PRIMARY_BUTTON))
  {
    if (point_count == 0) {
      cursor  = line2.p1;
      cursor2 = line2.p2;
    } else {
      cursor  = line1.p1;
      cursor2 = line1.p2;
    }

    point_count += 1;
    if (point_count > 1)
      point_count = 0;
  }

  if (point_count == 0)
  {
    line1.p1 = cursor;
    line1.p2 = cursor2;
  }
  else if (point_count == 1)
  {
    line2.p1 = cursor;
    line2.p2 = cursor2;
  }

  if (line1.intersect(line2, collision_point))
  {
    if (!had_prev_collision)
      ConsoleLog << "Collision" << std::endl;
    had_prev_collision = true;
  }
  else
  {
    if (had_prev_collision)
      ConsoleLog << "No Collision" << std::endl;
    had_prev_collision = false;
    collision_point = Vector2f(32, 32);
  }
}
void
ParticleViewer::update(float delta, const Controller& controller)
{
  for(Systems::iterator i = systems.begin(); i != systems.end(); ++i)
    (*i)->update(delta);

  if (!show_gui)
    {
      pos.x += controller.get_axis_state(X_AXIS) * delta * 100.0f;
      pos.y += controller.get_axis_state(Y_AXIS) * delta * 100.0f;

      if (controller.button_was_pressed(OK_BUTTON))
        {
          // FIXME: Disable GUI, since its crashy
          // show_gui = true;
          // manager->get_root()->get_focus()->set_active(true);
        }
      else if (controller.button_was_pressed(CANCEL_BUTTON) ||
               controller.button_was_pressed(ESCAPE_BUTTON))
        {
          screen_manager.pop_screen();
        }
    }
  else
    {
      if (!manager->get_root()->is_active())
        {
          show_gui = false;
        }
      else
        {
          manager->update(delta, controller);
        }
    }

  for(ParticleSystemGUIs::iterator i = guis.begin(); i != guis.end(); ++i)
    {
      (*i)->update();
    }

  //systems[3]->set_count(count_slider->get_pos());
  //systems[3]->set_velocity(velocity_slider->get_pos(), velocity_slider->get_pos());
}
void
Conversation::update(float delta, const Controller& controller)
{
  time += delta;

  if (!active)
    return;

  grow = fabsf(sinf(time * 3.0f)) * 4.0f;

  direction = Vector2f(controller.get_axis_state(X_AXIS),
                       controller.get_axis_state(Y_AXIS));

  if (fabs(controller.get_axis_state(X_AXIS)) > 0.3f ||
      fabs(controller.get_axis_state(Y_AXIS)) > 0.3f)
  {
    float segment = 360.0f / static_cast<float>(choices.size());
    float angle = math::rad2deg(math::normalize_angle(atan2f(direction.y, direction.x) + math::pi/2.0f + math::deg2rad(segment/2.0f)));

    int new_selection = int(angle / segment);
    new_selection = math::mid(0, new_selection, int(choices.size()));

    if (new_selection != selection) {
      selection = new_selection;
      // FIXME: Might be a good idea to do the woople-size per button, not globaly
      grow = 0.0f;
      time = 0;
    }
  }
  else
  {
    selection = -1;
  }
  
  if (controller.button_was_pressed(OK_BUTTON) && selection != -1)
  {
    active = false;
    GameSession::current()->get_pda().add_dialog("Jane",
                                                 choices[selection].topic + " - " +
                                                 choices[selection].text);
    choices.clear();
    GameSession::current()->set_control_state(GameSession::GAME);
    ScriptManager::current()->fire_wakeup_event(ScriptManager::CONVERSATION_CLOSED);
  }
}
void
Sprite2DView::update(float delta, const Controller& controller)
{  
  if (ignore_delta)
  {
    ignore_delta = false;
    delta = 0.0f;
  }
  
  display_time += delta * 0.5f;

  switch(mode) {
    case SLIDESHOW:
      update_slideshow(delta, controller);
      break;
    case MANUAL:
      update_manual(delta, controller);
      break;
  }

  if (controller.button_was_pressed(PDA_BUTTON))
  {
    if (shuffle)
    {
      std::vector<Pathname>::iterator i = std::find(directory.begin(), directory.end(),
                                                    shuffle_directory[index]);
      if (i != directory.end())
      {
        index = i - directory.begin() ;
      }
    }
    else
    {
      std::vector<Pathname>::iterator i = std::find(shuffle_directory.begin(), shuffle_directory.end(),
                                                    directory[index]);
      if (i != shuffle_directory.end())
      {
        index = i - shuffle_directory.begin();
      }
    }

    shuffle = !shuffle;

    std::cout << shuffle << " " << index << std::endl;
  }

  //  if (controller.button_was_pressed(INVENTORY_BUTTON))
  //std::random_shuffle(shuffle_directory.begin(), shuffle_directory.end());
   
  if (controller.button_was_pressed(TERTIARY_BUTTON))
    show_thumbnail = !show_thumbnail;

  if (controller.button_was_pressed(AIM_BUTTON))
  {
    if (mode == SLIDESHOW) mode = MANUAL; 
    else if (mode == MANUAL) mode = SLIDESHOW; 
  }

  if (new_sprite)
  {
    fadein += delta;

    if (fadein > 1.0f)
    {
      sprite = new_sprite;
      sprite.set_alpha(1.0f);
      new_sprite = Sprite();
      offset = 0;
      display_time = 0;
    }
    else
    {
      new_sprite.set_alpha(fadein);
    }
  }
}
void
Sprite2DView::update_slideshow(float delta, const Controller& controller)
{
  if (!new_sprite)
  {
    width  = sprite.get_width();
    height = sprite.get_height();
    aspect = width/height;

    if (aspect > 4.0/3.0)
    { // expand vertical
      float s = DISPLAY_H/height;
      width  *= s;
      height *= s;
      sprite.set_scale(s);

      if (offset - (width - DISPLAY_W) > 0)
      {
        offset = (width - DISPLAY_W);

        if (auto_scroll && display_time > 3.0f)
          next_image();
      }
      else
      {
        offset += delta * 50.0f + controller.get_axis_state(X_AXIS) * 200.0f * delta;
      }

      offset +=  controller.get_ball_state(MOUSE_MOTION_Y);
    }
    else
    { // expand horizontal
      float s = 800.0f/width;
      width  *= s;
      height *= s;
      sprite.set_scale(s);

      if (offset - (height - DISPLAY_H) > 0)
      {
        offset = (width - DISPLAY_H);

        if (auto_scroll && display_time > 3.0f)
          next_image();
      }
      else
      {
        offset += delta * 50.0f +   controller.get_axis_state(X_AXIS) * 200.0f * delta;
      }

      offset += controller.get_ball_state(MOUSE_MOTION_Y);
    }
  }

  if (controller.button_was_pressed(PRIMARY_BUTTON))
  {
    next_image();
  }
  else if (controller.button_was_pressed(SECONDARY_BUTTON))
  {
    prev_image();
  }
}