00001 /* 00002 * Extended support for activating emotes 00003 * Copyright (C) 2009 Aethyra 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 "gui/emotecontainer.h" 00023 00024 #include "animatedsprite.h" 00025 #include "configuration.h" 00026 #include "emoteshortcut.h" 00027 #include "graphics.h" 00028 #include "localplayer.h" 00029 #include "log.h" 00030 00031 #include "resources/emotedb.h" 00032 #include "resources/image.h" 00033 #include "resources/iteminfo.h" 00034 #include "resources/resourcemanager.h" 00035 00036 #include "utils/dtor.h" 00037 #include "utils/gettext.h" 00038 #include "utils/stringutils.h" 00039 00040 #include <guichan/mouseinput.hpp> 00041 #include <guichan/selectionlistener.hpp> 00042 00043 const int EmoteContainer::gridWidth = 34; // emote icon width + 4 00044 const int EmoteContainer::gridHeight = 36; // emote icon height + 4 00045 00046 static const int NO_EMOTE = -1; 00047 00048 EmoteContainer::EmoteContainer(): 00049 mSelectedEmoteIndex(NO_EMOTE) 00050 { 00051 ResourceManager *resman = ResourceManager::getInstance(); 00052 00053 // Setup emote sprites 00054 for (int i = 0; i <= EmoteDB::getLast(); i++) 00055 { 00056 mEmoteImg.push_back(EmoteDB::getAnimation(i)); 00057 } 00058 00059 mSelImg = resman->getImage("graphics/gui/selection.png"); 00060 if (!mSelImg) 00061 logger->error(_("Unable to load selection.png")); 00062 00063 mSelImg->setAlpha(config.getValue("guialpha", 0.8)); 00064 00065 mMaxEmote = EmoteDB::getLast() + 1; 00066 00067 addMouseListener(this); 00068 addWidgetListener(this); 00069 } 00070 00071 EmoteContainer::~EmoteContainer() 00072 { 00073 if (!mSelImg) 00074 { 00075 mSelImg->decRef(); 00076 mSelImg = NULL; 00077 } 00078 } 00079 00080 void EmoteContainer::draw(gcn::Graphics *graphics) 00081 { 00082 int columns = getWidth() / gridWidth; 00083 00084 // Have at least 1 column 00085 if (columns < 1) 00086 columns = 1; 00087 00088 for (int i = 0; i < mMaxEmote ; i++) 00089 { 00090 const int emoteX = ((i) % columns) * gridWidth; 00091 const int emoteY = ((i) / columns) * gridHeight; 00092 00093 // Draw selection image below selected item 00094 if (mSelectedEmoteIndex == i) 00095 { 00096 static_cast<Graphics*>(graphics)->drawImage( 00097 mSelImg, emoteX, emoteY + 4); 00098 } 00099 00100 // Draw emote icon 00101 mEmoteImg[i]->draw(static_cast<Graphics*>(graphics), emoteX, emoteY); 00102 } 00103 } 00104 00105 void EmoteContainer::widgetResized(const gcn::Event &event) 00106 { 00107 recalculateHeight(); 00108 } 00109 00110 void EmoteContainer::recalculateHeight() 00111 { 00112 int cols = getWidth() / gridWidth; 00113 00114 if (cols < 1) 00115 cols = 1; 00116 00117 const int rows = (mMaxEmote / cols) + (mMaxEmote % cols > 0 ? 1 : 0); 00118 const int height = rows * gridHeight + 8; 00119 if (height != getHeight()) 00120 setHeight(height); 00121 } 00122 00123 int EmoteContainer::getSelectedEmote() 00124 { 00125 if (mSelectedEmoteIndex == NO_EMOTE) 00126 return 0; 00127 00128 return 1 + mSelectedEmoteIndex; 00129 } 00130 00131 void EmoteContainer::selectNone() 00132 { 00133 setSelectedEmoteIndex(NO_EMOTE); 00134 } 00135 00136 void EmoteContainer::setSelectedEmoteIndex(int index) 00137 { 00138 if (index < 0 || index >= mMaxEmote ) 00139 mSelectedEmoteIndex = NO_EMOTE; 00140 else 00141 mSelectedEmoteIndex = index; 00142 } 00143 00144 void EmoteContainer::distributeValueChangedEvent() 00145 { 00146 gcn::SelectionEvent event(this); 00147 std::list<gcn::SelectionListener*>::iterator i_end = mListeners.end(); 00148 std::list<gcn::SelectionListener*>::iterator i; 00149 00150 for (i = mListeners.begin(); i != i_end; ++i) 00151 { 00152 (*i)->valueChanged(event); 00153 } 00154 } 00155 00156 void EmoteContainer::mousePressed(gcn::MouseEvent &event) 00157 { 00158 int button = event.getButton(); 00159 if (button == gcn::MouseEvent::LEFT || button == gcn::MouseEvent::RIGHT) 00160 { 00161 int columns = getWidth() / gridWidth; 00162 int mx = event.getX(); 00163 int my = event.getY(); 00164 int index = mx / gridWidth + ((my / gridHeight) * columns); 00165 if (index < mMaxEmote) 00166 { 00167 setSelectedEmoteIndex(index); 00168 emoteShortcut->setEmoteSelected(index + 1); 00169 } 00170 } 00171 }