/src/MenuItemStorage.cpp

http://github.com/clintbellanger/flare · C++ · 82 lines · 52 code · 11 blank · 19 comment · 9 complexity · 2badbeb7d423d222ed614de166a1d8ef MD5 · raw file

  1. /*
  2. Copyright 2011 Clint Bellanger
  3. This file is part of FLARE.
  4. FLARE is free software: you can redistribute it and/or modify it under the terms
  5. of the GNU General Public License as published by the Free Software Foundation,
  6. either version 3 of the License, or (at your option) any later version.
  7. FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  9. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. FLARE. If not, see http://www.gnu.org/licenses/
  12. */
  13. /**
  14. * class MenuItemStorage
  15. */
  16. #include "MenuItemStorage.h"
  17. using namespace std;
  18. void MenuItemStorage::init(int _slot_number, ItemManager *_items, SDL_Rect _area, int _icon_size, int _nb_cols) {
  19. ItemStorage::init( _slot_number, _items);
  20. area = _area;
  21. icon_size = _icon_size;
  22. nb_cols = _nb_cols;
  23. }
  24. void MenuItemStorage::render() {
  25. for (int i=0; i<slot_number; i++) {
  26. if (storage[i].item > 0) {
  27. items->renderIcon(storage[i], area.x + (i % nb_cols * icon_size), area.y + (i / nb_cols * icon_size), icon_size);
  28. }
  29. }
  30. }
  31. int MenuItemStorage::slotOver(Point mouse) {
  32. if( isWithin( area, mouse)) {
  33. return (mouse.x - area.x) / icon_size + (mouse.y - area.y) / icon_size * nb_cols;
  34. }
  35. else {
  36. return -1;
  37. }
  38. }
  39. TooltipData MenuItemStorage::checkTooltip(Point mouse, StatBlock *stats, bool vendor_view) {
  40. TooltipData tip;
  41. int slot = slotOver( mouse);
  42. if (slot > -1 && storage[slot].item > 0) {
  43. return items->getTooltip( storage[slot].item, stats, vendor_view);
  44. }
  45. return tip;
  46. }
  47. ItemStack MenuItemStorage::click(InputState * input) {
  48. ItemStack item;
  49. drag_prev_slot = slotOver(input->mouse);
  50. if( drag_prev_slot > -1) {
  51. item = storage[drag_prev_slot];
  52. if( input->pressing[SHIFT]) {
  53. item.quantity = 1;
  54. }
  55. substract( drag_prev_slot, item.quantity);
  56. return item;
  57. }
  58. else {
  59. item.item = 0;
  60. item.quantity = 0;
  61. return item;
  62. }
  63. }
  64. void MenuItemStorage::itemReturn(ItemStack stack) {
  65. add( stack, drag_prev_slot);
  66. drag_prev_slot = -1;
  67. }