llfloaterbuycurrency.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "llfloaterbuycurrency.h"
00035 
00036 // viewer includes
00037 #include "llcurrencyuimanager.h"
00038 #include "llfloater.h"
00039 #include "llstatusbar.h"
00040 #include "lltextbox.h"
00041 #include "llviewchildren.h"
00042 #include "llviewerwindow.h"
00043 #include "llvieweruictrlfactory.h"
00044 #include "llweb.h"
00045 #include "llwindow.h"
00046 #include "viewer.h"
00047 
00048 static const S32 STANDARD_BUY_AMOUNT = 1000;
00049 static const S32 MINIMUM_BALANCE_AMOUNT = 0;
00050 
00051 class LLFloaterBuyCurrencyUI
00052 :       public LLFloater
00053 {
00054 public:
00055         static LLFloaterBuyCurrencyUI* soleInstance(bool createIfNeeded);
00056 
00057 private:
00058         static LLFloaterBuyCurrencyUI* sInstance;
00059 
00060         LLFloaterBuyCurrencyUI();
00061         virtual ~LLFloaterBuyCurrencyUI();
00062 
00063 
00064 public:
00065         LLViewChildren          mChildren;
00066         LLCurrencyUIManager     mManager;
00067         
00068         bool            mHasTarget;
00069         std::string     mTargetName;
00070         S32                     mTargetPrice;
00071         
00072 public:
00073         void noTarget();
00074         void target(const std::string& name, S32 price);
00075         
00076         virtual BOOL postBuild();
00077         
00078         void updateUI();
00079 
00080         virtual void draw();
00081         virtual BOOL canClose();
00082         virtual void onClose(bool app_quitting);
00083 
00084         static void onClickBuy(void* data);
00085         static void onClickCancel(void* data);
00086         static void onClickErrorWeb(void* data);
00087 };
00088 
00089 
00090 // static
00091 LLFloaterBuyCurrencyUI* LLFloaterBuyCurrencyUI::sInstance = NULL;
00092 
00093 // static
00094 LLFloaterBuyCurrencyUI* LLFloaterBuyCurrencyUI::soleInstance(bool createIfNeeded)
00095 {
00096         if (!sInstance  &&  createIfNeeded)
00097         {
00098                 sInstance = new LLFloaterBuyCurrencyUI();
00099 
00100                 gUICtrlFactory->buildFloater(sInstance, "floater_buy_currency.xml");
00101                 sInstance->center();
00102         }
00103         
00104         return sInstance;
00105 }
00106 
00107 
00108 #if LL_WINDOWS
00109 // passing 'this' during construction generates a warning. The callee
00110 // only uses the pointer to hold a reference to 'this' which is
00111 // already valid, so this call does the correct thing. Disable the
00112 // warning so that we can compile without generating a warning.
00113 #pragma warning(disable : 4355)
00114 #endif 
00115 LLFloaterBuyCurrencyUI::LLFloaterBuyCurrencyUI()
00116 :       LLFloater("Buy Currency"),
00117         mChildren(*this),
00118         mManager(*this)
00119 {
00120 }
00121 
00122 LLFloaterBuyCurrencyUI::~LLFloaterBuyCurrencyUI()
00123 {
00124         if (sInstance == this)
00125         {
00126                 sInstance = NULL;
00127         }
00128 }
00129 
00130 
00131 void LLFloaterBuyCurrencyUI::noTarget()
00132 {
00133         mHasTarget = false;
00134         mManager.setAmount(STANDARD_BUY_AMOUNT);
00135 }
00136 
00137 void LLFloaterBuyCurrencyUI::target(const std::string& name, S32 price)
00138 {
00139         mHasTarget = true;
00140         mTargetName = name;
00141         mTargetPrice = price;
00142         
00143         S32 balance = gStatusBar->getBalance();
00144         S32 need = price - balance;
00145         if (need < 0)
00146         {
00147                 need = 0;
00148         }
00149         
00150         mManager.setAmount(need + MINIMUM_BALANCE_AMOUNT);
00151 }
00152 
00153 
00154 // virtual
00155 BOOL LLFloaterBuyCurrencyUI::postBuild()
00156 {
00157         mManager.prepare();
00158         
00159         childSetAction("buy_btn", onClickBuy, this);
00160         childSetAction("cancel_btn", onClickCancel, this);
00161         childSetAction("error_web", onClickErrorWeb, this);
00162 
00163         updateUI();
00164         
00165         return TRUE;
00166 }
00167 
00168 void LLFloaterBuyCurrencyUI::draw()
00169 {
00170         if (mManager.process())
00171         {
00172                 if (mManager.bought())
00173                 {
00174                         close();
00175                         return;
00176                 }
00177                 
00178                 updateUI();
00179         }
00180 
00181         LLFloater::draw();
00182 }
00183 
00184 BOOL LLFloaterBuyCurrencyUI::canClose()
00185 {
00186         return mManager.canCancel();
00187 }
00188 
00189 void LLFloaterBuyCurrencyUI::onClose(bool app_quitting)
00190 {
00191         LLFloater::onClose(app_quitting);
00192         destroy();
00193 }
00194 
00195 void LLFloaterBuyCurrencyUI::updateUI()
00196 {
00197         bool hasError = mManager.hasError();
00198         mManager.updateUI(!hasError && !mManager.buying());
00199 
00200         // section zero: title area
00201         {
00202                 childSetVisible("info_buying", false);
00203                 childSetVisible("info_cannot_buy", false);
00204                 childSetVisible("info_need_more", false);
00205                 if (hasError)
00206                 {
00207                         childSetVisible("info_cannot_buy", true);
00208                 }
00209                 else if (mHasTarget)
00210                 {
00211                         childSetVisible("info_need_more", true);
00212                 }
00213                 else
00214                 {
00215                         childSetVisible("info_buying", true);
00216                 }
00217         }
00218         
00219         // error section
00220         if (hasError)
00221         {
00222                 mChildren.setBadge("step_error", LLViewChildren::BADGE_ERROR);
00223                 
00224                 LLTextBox* message = LLUICtrlFactory::getTextBoxByName(this, "error_message");
00225                 if (message)
00226                 {
00227                         message->setVisible(true);
00228                         message->setWrappedText(mManager.errorMessage());
00229                 }
00230 
00231                 childSetVisible("error_web", !mManager.errorURI().empty());
00232         }
00233         else
00234         {
00235                 childHide("step_error");
00236                 childHide("error_message");
00237                 childHide("error_web");
00238         }
00239         
00240         
00241         //  currency
00242         childSetVisible("contacting", false);
00243         childSetVisible("buy_action", false);
00244         childSetVisible("buy_action_unknown", false);
00245         
00246         if (!hasError)
00247         {
00248                 mChildren.setBadge("step_1", LLViewChildren::BADGE_NOTE);
00249 
00250                 if (mManager.buying())
00251                 {
00252                         childSetVisible("contacting", true);
00253                 }
00254                 else
00255                 {
00256                         if (mHasTarget)
00257                         {
00258                                 childSetVisible("buy_action", true);
00259                                 childSetTextArg("buy_action", "[NAME]", mTargetName);
00260                                 childSetTextArg("buy_action", "[PRICE]", llformat("%d",mTargetPrice));
00261                         }
00262                         else
00263                         {
00264                                 childSetVisible("buy_action_unknown", true);
00265                         }
00266                 }
00267                 
00268                 S32 balance = gStatusBar->getBalance();
00269                 childShow("balance_label");
00270                 childShow("balance_amount");
00271                 childSetTextArg("balance_amount", "[AMT]", llformat("%d", balance));
00272                 
00273                 S32 buying = mManager.getAmount();
00274                 childShow("buying_label");
00275                 childShow("buying_amount");
00276                 childSetTextArg("buying_amount", "[AMT]", llformat("%d", buying));
00277                 
00278                 S32 total = balance + buying;
00279                 childShow("total_label");
00280                 childShow("total_amount");
00281                 childSetTextArg("total_amount", "[AMT]", llformat("%d", total));
00282 
00283                 childSetVisible("purchase_warning_repurchase", false);
00284                 childSetVisible("purchase_warning_notenough", false);
00285                 if (mHasTarget)
00286                 {
00287                         if (total >= mTargetPrice)
00288                         {
00289                                 childSetVisible("purchase_warning_repurchase", true);
00290                         }
00291                         else
00292                         {
00293                                 childSetVisible("purchase_warning_notenough", true);
00294                         }
00295                 }
00296         }
00297         else
00298         {
00299                 childHide("step_1");
00300                 childHide("balance_label");
00301                 childHide("balance_amount");
00302                 childHide("buying_label");
00303                 childHide("buying_amount");
00304                 childHide("total_label");
00305                 childHide("total_amount");
00306                 childHide("purchase_warning_repurchase");
00307                 childHide("purchase_warning_notenough");
00308         }
00309         
00310         childSetEnabled("buy_btn", mManager.canBuy());
00311 }
00312 
00313 // static
00314 void LLFloaterBuyCurrencyUI::onClickBuy(void* data)
00315 {
00316         LLFloaterBuyCurrencyUI* self = LLFloaterBuyCurrencyUI::soleInstance(false);
00317         if (self)
00318         {
00319                 self->mManager.buy(self->childGetText("buy_currency"));
00320                 self->updateUI();
00321                 // JC: updateUI() doesn't get called again until progress is made
00322                 // with transaction processing, so the "Purchase" button would be
00323                 // left enabled for some time.  Pre-emptively disable.
00324                 self->childSetEnabled("buy_btn", false);
00325         }
00326 }
00327 
00328 // static
00329 void LLFloaterBuyCurrencyUI::onClickCancel(void* data)
00330 {
00331         LLFloaterBuyCurrencyUI* self = LLFloaterBuyCurrencyUI::soleInstance(false);
00332         if (self)
00333         {
00334                 self->close();
00335         }
00336 }
00337 
00338 // static
00339 void LLFloaterBuyCurrencyUI::onClickErrorWeb(void* data)
00340 {
00341         LLFloaterBuyCurrencyUI* self = LLFloaterBuyCurrencyUI::soleInstance(false);
00342         if (self)
00343         {
00344                 LLWeb::loadURLExternal(self->mManager.errorURI());
00345                 self->close();
00346         }
00347 }
00348 
00349 // static
00350 void LLFloaterBuyCurrency::buyCurrency()
00351 {
00352         if (gHideLinks)
00353         {
00354                 return;
00355         }
00356 
00357         LLFloaterBuyCurrencyUI* ui = LLFloaterBuyCurrencyUI::soleInstance(true);
00358         ui->noTarget();
00359         ui->updateUI();
00360         ui->open();
00361 }
00362 
00363 void LLFloaterBuyCurrency::buyCurrency(const std::string& name, S32 price)
00364 {
00365         if (gHideLinks)
00366         {
00367                 LLStringBase<char>::format_map_t args;
00368                 args["[NAME]"] = name.c_str();
00369                 args["[PRICE]"] = price;
00370                 gViewerWindow->alertXml("NotEnoughCurrency", args);
00371                 return;
00372         }
00373         
00374         LLFloaterBuyCurrencyUI* ui = LLFloaterBuyCurrencyUI::soleInstance(true);
00375         ui->target(name, price);
00376         ui->updateUI();
00377         ui->open();
00378 }
00379 
00380 

Generated on Thu Jul 1 06:08:32 2010 for Second Life Viewer by  doxygen 1.4.7