passworddialog.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Password Dialog
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. class PasswordDialogImpl : public IPopup,
  8. public EventTargetContainer<PasswordDialogImpl>,
  9. public TrekClientEventSink
  10. {
  11. private:
  12. TRef<Pane> m_ppane;
  13. TRef<ButtonPane> m_pbuttonOK;
  14. TRef<ButtonPane> m_pbuttonCancel;
  15. TRef<EditPane> m_peditPane;
  16. MissionInfo* m_pMission;
  17. PasswordDialogSink* m_pparent;
  18. TRef<IKeyboardInput> m_pkeyboardInputOldFocus;
  19. public:
  20. PasswordDialogImpl(PasswordDialogSink* pparent, const ZString& strPassword, MissionInfo* pmission)
  21. {
  22. m_pMission = NULL;
  23. m_pparent = pparent;
  24. TRef<INameSpace> pns = GetModeler()->GetNameSpace("passworddialog");
  25. CastTo(m_ppane, pns->FindMember("PasswordDialog"));
  26. CastTo(m_pbuttonOK, pns->FindMember("passwordOkButtonPane"));
  27. CastTo(m_pbuttonCancel, pns->FindMember("passwordCancelButtonPane"));
  28. CastTo(m_peditPane, (Pane*)pns->FindMember("passwordEditPane"));
  29. m_peditPane->SetType(EditPane::Password);
  30. m_peditPane->SetString(strPassword);
  31. m_peditPane->SetMaxLength(c_cbGamePassword - 1);
  32. AddEventTarget(OnButtonOK, m_pbuttonOK->GetEventSource());
  33. AddEventTarget(OnButtonCancel, m_pbuttonCancel->GetEventSource());
  34. }
  35. void SetMission(MissionInfo* pmission)
  36. {
  37. ZAssert(m_pMission == NULL);
  38. m_pMission = pmission;
  39. }
  40. //
  41. // IPopup methods
  42. //
  43. Pane* GetPane()
  44. {
  45. return m_ppane;
  46. }
  47. virtual void OnClose()
  48. {
  49. if (m_pkeyboardInputOldFocus)
  50. GetWindow()->SetFocus(m_pkeyboardInputOldFocus);
  51. m_pkeyboardInputOldFocus = NULL;
  52. IPopup::OnClose();
  53. }
  54. virtual void SetContainer(IPopupContainer* pcontainer)
  55. {
  56. m_pkeyboardInputOldFocus = GetWindow()->GetFocus();
  57. GetWindow()->SetFocus(m_peditPane);
  58. m_peditPane->SetString("");
  59. IPopup::SetContainer(pcontainer);
  60. }
  61. bool OnKey(IInputProvider* pprovider, const KeyState& ks, bool& fForceTranslate)
  62. {
  63. // we need to make sure we get OnChar calls to pass on to the edit box
  64. fForceTranslate = true;
  65. return false;
  66. }
  67. bool OnChar(IInputProvider* pprovider, const KeyState& ks)
  68. {
  69. if (ks.vk == 13)
  70. {
  71. OnButtonOK();
  72. return true;
  73. }
  74. else
  75. return ((IKeyboardInput*)m_peditPane)->OnChar(pprovider, ks);
  76. }
  77. bool OnButtonOK()
  78. {
  79. GetWindow()->SetFocus(m_pkeyboardInputOldFocus);
  80. m_pkeyboardInputOldFocus = NULL;
  81. PasswordDialogSink* pparent = m_pparent;
  82. ZString strPassword = m_peditPane->GetString();
  83. m_pMission = NULL;
  84. if (m_ppopupOwner) {
  85. m_ppopupOwner->ClosePopup(this);
  86. } else {
  87. m_pcontainer->ClosePopup(this);
  88. }
  89. pparent->OnPassword(strPassword);
  90. return true;
  91. }
  92. bool OnButtonCancel()
  93. {
  94. GetWindow()->SetFocus(m_pkeyboardInputOldFocus);
  95. m_pkeyboardInputOldFocus = NULL;
  96. m_pMission = NULL;
  97. PasswordDialogSink* pparent = m_pparent;
  98. if (m_ppopupOwner) {
  99. m_ppopupOwner->ClosePopup(this);
  100. } else {
  101. m_pcontainer->ClosePopup(this);
  102. }
  103. pparent->OnCancelPassword();
  104. return true;
  105. }
  106. void OnDelMission(MissionInfo* pmission)
  107. {
  108. if (m_pMission == pmission)
  109. {
  110. // our mission has ended - bail.
  111. GetWindow()->SetFocus(m_pkeyboardInputOldFocus);
  112. m_pkeyboardInputOldFocus = NULL;
  113. if (m_ppopupOwner) {
  114. m_ppopupOwner->ClosePopup(this);
  115. } else {
  116. m_pcontainer->ClosePopup(this);
  117. }
  118. m_pMission = NULL;
  119. TRef<IMessageBox> pmsgBox =
  120. CreateMessageBox("The mission you were trying to join has ended.");
  121. GetWindow()->GetPopupContainer()->OpenPopup(pmsgBox, false);
  122. m_pparent->OnCancelPassword();
  123. }
  124. }
  125. };
  126. TRef<IPopup> CreatePasswordPopup(PasswordDialogSink* psink, const ZString& strPassword, MissionInfo* pmission)
  127. {
  128. return new PasswordDialogImpl(psink, strPassword, pmission);
  129. }