00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "gui/guildwindow.h"
00024
00025 #include "gui/confirm_dialog.h"
00026 #include "gui/guildlistbox.h"
00027 #include "gui/textdialog.h"
00028
00029 #include "gui/widgets/button.h"
00030 #include "gui/widgets/chattab.h"
00031 #include "gui/widgets/layout.h"
00032 #include "gui/widgets/scrollarea.h"
00033 #include "gui/widgets/tabbedarea.h"
00034 #include "gui/widgets/windowcontainer.h"
00035
00036 #include "guild.h"
00037 #include "log.h"
00038 #include "localplayer.h"
00039
00040 #include "net/tmwserv/chatserver/guild.h"
00041
00042 #include "utils/dtor.h"
00043 #include "utils/gettext.h"
00044
00045 #include <algorithm>
00046
00047 #include <guichan/widgets/tab.hpp>
00048
00049 GuildWindow::GuildWindow():
00050 Window(_("Guild")),
00051 mFocus(false)
00052 {
00053 setWindowName("Guild");
00054 setCaption(_("Guild"));
00055 setResizable(false);
00056 setCloseButton(true);
00057 setSaveVisible(true);
00058 setMinWidth(200);
00059 setMinHeight(280);
00060 setDefaultSize(124, 41, 288, 330);
00061
00062
00063 mGuildButton[0] = new Button(_("Create Guild"), "CREATE_GUILD", this);
00064 mGuildButton[1] = new Button(_("Invite User"), "INVITE_USER", this);
00065 mGuildButton[2] = new Button(_("Quit Guild"), "QUIT_GUILD", this);
00066 mGuildButton[1]->setEnabled(false);
00067 mGuildButton[2]->setEnabled(false);
00068
00069 mGuildTabs = new TabbedArea;
00070
00071 place(0, 0, mGuildButton[0]);
00072 place(1, 0, mGuildButton[1]);
00073 place(2, 0, mGuildButton[2]);
00074 place(0, 1, mGuildTabs);
00075 Layout &layout = getLayout();
00076 layout.setColWidth(0, 48);
00077 layout.setColWidth(1, 65);
00078
00079 loadWindowState();
00080 }
00081
00082 GuildWindow::~GuildWindow()
00083 {
00084 }
00085
00086 void GuildWindow::update()
00087 {
00088 updateTab();
00089
00090 if (mGuildButton[2]->isEnabled() && mGuildTabs->getNumberOfTabs() <= 0)
00091 {
00092 mGuildButton[2]->setEnabled(false);
00093 mGuildButton[1]->setEnabled(false);
00094 }
00095 }
00096
00097 void GuildWindow::draw(gcn::Graphics *g)
00098 {
00099 update();
00100
00101 Window::draw(g);
00102 }
00103
00104 void GuildWindow::action(const gcn::ActionEvent &event)
00105 {
00106 const std::string &eventId = event.getId();
00107
00108
00109 if (eventId == "CREATE_GUILD")
00110 {
00111
00112 mFocus = true;
00113 guildDialog = new TextDialog("Guild Name", "Choose your guild's name", this);
00114 guildDialog->setOKButtonActionId("CREATE_GUILD_OK");
00115 guildDialog->addActionListener(this);
00116 }
00117 else if (eventId == "INVITE_USER")
00118 {
00119
00120 mFocus = true;
00121 inviteDialog = new TextDialog("Member Invite", "Who would you like to invite?", this);
00122 inviteDialog->setOKButtonActionId("INVITE_USER_OK");
00123 inviteDialog->addActionListener(this);
00124 }
00125 else if (eventId == "QUIT_GUILD")
00126 {
00127 short guild = getSelectedGuild();
00128 if (guild)
00129 {
00130 Net::ChatServer::Guild::quitGuild(guild);
00131 localChatTab->chatLog("Guild " + mGuildTabs->getSelectedTab()->getCaption() + " quit", BY_SERVER);
00132 }
00133 }
00134 else if (eventId == "CREATE_GUILD_OK")
00135 {
00136 std::string name = guildDialog->getText();
00137 if(name.size() > 16)
00138 {
00139
00140 return;
00141 }
00142
00143 Net::ChatServer::Guild::createGuild(name);
00144
00145
00146 mFocus = false;
00147 localChatTab->chatLog("Creating Guild called " + name, BY_SERVER);
00148 guildDialog->scheduleDelete();
00149 }
00150 else if (eventId == "INVITE_USER_OK")
00151 {
00152 std::string name = inviteDialog->getText();
00153 short selectedGuild = getSelectedGuild();
00154
00155
00156 Net::ChatServer::Guild::invitePlayer(name, selectedGuild);
00157
00158
00159 mFocus = false;
00160 localChatTab->chatLog("Invited user " + name, BY_SERVER);
00161 inviteDialog->scheduleDelete();
00162 }
00163 else if (eventId == "yes")
00164 {
00165 logger->log("Sending invitation acceptance.");
00166 Net::ChatServer::Guild::acceptInvite(invitedGuild);
00167 }
00168 }
00169
00170 void GuildWindow::newGuildTab(const std::string &guildName)
00171 {
00172
00173 GuildListBox *list = new GuildListBox;
00174 list->setListModel(player_node->getGuild(guildName));
00175 ScrollArea *sa = new ScrollArea(list);
00176 sa->setDimension(gcn::Rectangle(5, 5, 135, 250));
00177
00178
00179 mGuildLists.insert(std::pair<std::string, GuildListBox*>(guildName, list));
00180
00181 mGuildTabs->addTab(guildName, sa);
00182 mGuildTabs->setDimension(gcn::Rectangle(28,35,140,250));
00183
00184 updateTab();
00185 }
00186
00187 void GuildWindow::updateTab()
00188 {
00189 gcn::Tab *tab = mGuildTabs->getSelectedTab();
00190 if (tab)
00191 {
00192 setTab(tab->getCaption());
00193 }
00194 mGuildTabs->logic();
00195 }
00196
00197 void GuildWindow::setTab(const std::string &guildName)
00198 {
00199
00200 if(player_node->checkInviteRights(guildName))
00201 {
00202 mGuildButton[1]->setEnabled(true);
00203 }
00204 else
00205 {
00206 mGuildButton[1]->setEnabled(false);
00207 }
00208
00209 mGuildButton[2]->setEnabled(true);
00210 }
00211
00212 bool GuildWindow::isWindowFocused()
00213 {
00214 return mFocus;
00215 }
00216
00217 short GuildWindow::getSelectedGuild()
00218 {
00219 if (mGuildTabs->getNumberOfTabs() > 0)
00220 {
00221
00222 Guild *guild = player_node->getGuild(mGuildTabs->getSelectedTab()->getCaption());
00223
00224 if (guild)
00225 {
00226 return guild->getId();
00227 }
00228 }
00229
00230 return 0;
00231 }
00232
00233 void GuildWindow::openAcceptDialog(const std::string &inviterName,
00234 const std::string &guildName)
00235 {
00236 std::string msg = inviterName + " has invited you to join the guild " + guildName;
00237 localChatTab->chatLog(msg, BY_SERVER);
00238
00239 acceptDialog = new ConfirmDialog("Accept Guild Invite", msg, this);
00240 acceptDialog->addActionListener(this);
00241
00242 invitedGuild = guildName;
00243 }
00244
00245 void GuildWindow::requestMemberList(short guildId)
00246 {
00247
00248 Net::ChatServer::Guild::getGuildMembers(guildId);
00249 }
00250
00251 void GuildWindow::removeTab(int guildId)
00252 {
00253 Guild* guild = player_node->getGuild(guildId);
00254 if (guild)
00255 {
00256 Tab *tab = mGuildTabs->getTab(guild->getName());
00257 if (tab)
00258 {
00259 mGuildTabs->removeTab(tab);
00260 }
00261 updateTab();
00262 }
00263 mGuildTabs->logic();
00264 }
00265
00266 void GuildWindow::setOnline(const std::string &guildName, const std::string &member,
00267 bool online)
00268 {
00269 GuildListMap::iterator itr = mGuildLists.find(guildName);
00270 if (itr != mGuildLists.end())
00271 {
00272 itr->second->setOnlineStatus(member, online);
00273 }
00274 }