00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/npclistdialog.h"
00023
00024 #include "gui/npc_text.h"
00025
00026 #include "gui/widgets/button.h"
00027 #include "gui/widgets/layout.h"
00028 #include "gui/widgets/listbox.h"
00029 #include "gui/widgets/scrollarea.h"
00030
00031 #include "npc.h"
00032
00033 #include "net/net.h"
00034 #include "net/npchandler.h"
00035
00036 #include "utils/gettext.h"
00037 #include "utils/strprintf.h"
00038
00039 #include <sstream>
00040
00041 NpcListDialog::NpcListDialog()
00042 : Window("NPC")
00043 {
00044 setWindowName("NPCList");
00045 setResizable(true);
00046
00047 setMinWidth(200);
00048 setMinHeight(150);
00049
00050 setDefaultSize(260, 200, ImageRect::CENTER);
00051
00052 mItemList = new ListBox(this);
00053 mItemList->setWrappingEnabled(true);
00054
00055 gcn::ScrollArea *scrollArea = new ScrollArea(mItemList);
00056
00057 gcn::Button *okButton = new Button(_("OK"), "ok", this);
00058 gcn::Button *cancelButton = new Button(_("Cancel"), "cancel", this);
00059
00060 setContentSize(260, 175);
00061 scrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
00062
00063 place(0, 0, scrollArea, 5).setPadding(3);
00064 place(3, 1, cancelButton);
00065 place(4, 1, okButton);
00066
00067 Layout &layout = getLayout();
00068 layout.setRowHeight(0, Layout::AUTO_SET);
00069
00070 center();
00071 loadWindowState();
00072 }
00073
00074 int NpcListDialog::getNumberOfElements()
00075 {
00076 return mItems.size();
00077 }
00078
00079 std::string NpcListDialog::getElementAt(int i)
00080 {
00081 return mItems[i];
00082 }
00083
00084 void NpcListDialog::addItem(const std::string &item)
00085 {
00086 mItems.push_back(item);
00087 }
00088
00089 void NpcListDialog::parseItems(const std::string &itemString)
00090 {
00091 std::istringstream iss(itemString);
00092
00093 std::string tmp;
00094 while (getline(iss, tmp, ':'))
00095 mItems.push_back(tmp);
00096 }
00097
00098 void NpcListDialog::reset()
00099 {
00100 NPC::isTalking = false;
00101 mItemList->setSelected(-1);
00102 mItems.clear();
00103 }
00104
00105 void NpcListDialog::action(const gcn::ActionEvent &event)
00106 {
00107 int choice = 0;
00108 if (event.getId() == "ok")
00109 {
00110
00111 int selectedIndex = mItemList->getSelected();
00112
00113 if (selectedIndex > -1)
00114 {
00115 choice = selectedIndex + 1;
00116 npcTextDialog->addText(strprintf("\n> \"%s\"\n",
00117 mItems[selectedIndex].c_str()));
00118 }
00119 }
00120 else if (event.getId() == "cancel")
00121 {
00122 choice = 0xff;
00123 npcTextDialog->addText(_("\n> Cancel\n"));
00124 npcTextDialog->showCloseButton();
00125 }
00126
00127 if (choice)
00128 {
00129 setVisible(false);
00130 saveWindowState();
00131 reset();
00132
00133 Net::getNpcHandler()->listInput(current_npc, choice);
00134 }
00135 }
00136
00137 void NpcListDialog::setVisible(bool visible)
00138 {
00139 if (visible) {
00140 npcTextDialog->setVisible(true);
00141 }
00142
00143 Window::setVisible(visible);
00144 }
00145
00146 void NpcListDialog::requestFocus()
00147 {
00148 mItemList->requestFocus();
00149 mItemList->setSelected(0);
00150 }