00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/npc_text.h"
00023
00024 #include "gui/widgets/button.h"
00025 #include "gui/widgets/layout.h"
00026 #include "gui/widgets/scrollarea.h"
00027 #include "gui/widgets/textbox.h"
00028
00029 #include "npc.h"
00030
00031 #include "net/net.h"
00032 #include "net/npchandler.h"
00033
00034 #include "utils/gettext.h"
00035
00036 NpcTextDialog::NpcTextDialog()
00037 : Window(_("NPC"))
00038 , mState(NPC_TEXT_STATE_WAITING)
00039 {
00040 setWindowName("NPCText");
00041 setResizable(true);
00042
00043 setMinWidth(200);
00044 setMinHeight(150);
00045
00046 setDefaultSize(260, 200, ImageRect::CENTER);
00047
00048 mTextBox = new TextBox;
00049 mTextBox->setEditable(false);
00050 mTextBox->setOpaque(false);
00051
00052 mScrollArea = new ScrollArea(mTextBox);
00053 mButton = new Button(_("Waiting for server"), "ok", this);
00054
00055 mScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
00056 mScrollArea->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS);
00057
00058 place(0, 0, mScrollArea, 5).setPadding(3);
00059 place(4, 1, mButton);
00060
00061 Layout &layout = getLayout();
00062 layout.setRowHeight(0, Layout::AUTO_SET);
00063
00064 center();
00065 loadWindowState();
00066 }
00067
00068 void NpcTextDialog::clearText()
00069 {
00070 NPC::isTalking = false;
00071 setText("");
00072 }
00073
00074 void NpcTextDialog::setText(const std::string &text)
00075 {
00076 mText = text;
00077 mTextBox->setTextWrapped(mText, mScrollArea->getWidth() - 15);
00078 }
00079
00080 void NpcTextDialog::addText(const std::string &text)
00081 {
00082 setText(mText + text + "\n");
00083 mScrollArea->setVerticalScrollAmount(mScrollArea->getVerticalMaxScroll());
00084 }
00085
00086 void NpcTextDialog::showNextButton()
00087 {
00088 mButton->setCaption(_("Next"));
00089 mState = NPC_TEXT_STATE_NEXT;
00090 mButton->setEnabled(true);
00091 }
00092
00093 void NpcTextDialog::showCloseButton()
00094 {
00095 mButton->setCaption(_("Close"));
00096 mState = NPC_TEXT_STATE_CLOSE;
00097 mButton->setEnabled(true);
00098 }
00099
00100 void NpcTextDialog::action(const gcn::ActionEvent &event)
00101 {
00102 if (event.getId() == "ok")
00103 {
00104 if (mState == NPC_TEXT_STATE_NEXT && current_npc) {
00105 nextDialog();
00106 addText("\n> Next\n");
00107 } else if (mState == NPC_TEXT_STATE_CLOSE ||
00108 (mState == NPC_TEXT_STATE_NEXT && !current_npc)) {
00109 setText("");
00110 if (current_npc) nextDialog();
00111 setVisible(false);
00112 current_npc = 0;
00113 NPC::isTalking = false;
00114 } else return;
00115 }
00116 else return;
00117
00118 mButton->setEnabled(false);
00119 mButton->setCaption(_("Waiting for server"));
00120 mState = NPC_TEXT_STATE_WAITING;
00121 }
00122
00123 void NpcTextDialog::nextDialog(int npcID)
00124 {
00125 Net::getNpcHandler()->nextDialog(npcID);
00126 }
00127
00128 void NpcTextDialog::closeDialog(int npcID)
00129 {
00130 Net::getNpcHandler()->closeDialog(npcID);
00131 }
00132
00133 void NpcTextDialog::widgetResized(const gcn::Event &event)
00134 {
00135 Window::widgetResized(event);
00136
00137 setText(mText);
00138 }