00001 /* 00002 * The Mana World 00003 * Copyright (C) 2007 The Mana World Development Team 00004 * 00005 * This file is part of The Mana World. 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00022 #include "configuration.h" 00023 #include "inventory.h" 00024 #include "item.h" 00025 #include "itemshortcut.h" 00026 #include "localplayer.h" 00027 00028 #include "utils/stringutils.h" 00029 00030 ItemShortcut::ItemShortcut *itemShortcut; 00031 00032 ItemShortcut::ItemShortcut(): 00033 mItemSelected(-1) 00034 { 00035 for (int i = 0; i < SHORTCUT_ITEMS; i++) 00036 mItems[i] = -1; 00037 00038 load(); 00039 } 00040 00041 ItemShortcut::~ItemShortcut() 00042 { 00043 save(); 00044 } 00045 00046 void ItemShortcut::load() 00047 { 00048 for (int i = 0; i < SHORTCUT_ITEMS; i++) 00049 { 00050 int itemId = (int) config.getValue("shortcut" + toString(i), -1); 00051 00052 if (itemId != -1) 00053 mItems[i] = itemId; 00054 } 00055 } 00056 00057 void ItemShortcut::save() 00058 { 00059 for (int i = 0; i < SHORTCUT_ITEMS; i++) 00060 { 00061 const int itemId = mItems[i] ? mItems[i] : -1; 00062 config.setValue("shortcut" + toString(i), itemId); 00063 } 00064 } 00065 00066 void ItemShortcut::useItem(int index) 00067 { 00068 if (mItems[index]) 00069 { 00070 Item *item = player_node->getInventory()->findItem(mItems[index]); 00071 if (item && item->getQuantity()) 00072 { 00073 if (item->isEquipment()) 00074 { 00075 #ifdef EATHENA_SUPPORT 00076 if (item->isEquipped()) 00077 player_node->unequipItem(item); 00078 else 00079 #endif 00080 player_node->equipItem(item); 00081 } 00082 else 00083 { 00084 player_node->useItem(item); 00085 } 00086 } 00087 } 00088 }