efpopup.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "pch.h"
  2. /////////////////////////////////////////////////////////////////////////////
  3. //
  4. // MessageBox
  5. //
  6. /////////////////////////////////////////////////////////////////////////////
  7. class MessageBoxImpl :
  8. public IMessageBox,
  9. public IEventSink
  10. {
  11. TRef<Modeler> m_pmodeler;
  12. TRef<Pane> m_ppane;
  13. TRef<Pane> m_ppaneWrapper;
  14. TRef<IntegerEventSourceImpl> m_psource;
  15. int m_delay;
  16. TRef<ButtonPane> m_pbuttonOK;
  17. TRef<ButtonPane> m_pbuttonCancel;
  18. TRef<WrapImage> m_pwrapImage;
  19. TRef<IEventSink> m_peventSink;
  20. TRef<ITimerEventSource> m_ptimerEventSource;
  21. public:
  22. MessageBoxImpl(
  23. ITimerEventSource* ptimerEventSource,
  24. Modeler* pmodeler,
  25. const ZString& str,
  26. ButtonPane* pbuttonIn,
  27. bool fAddDefaultButton,
  28. ButtonPane* pbuttonCancel,
  29. float paintDelay
  30. ) :
  31. m_pmodeler(pmodeler)
  32. {
  33. m_pwrapImage = new WrapImage(Image::GetEmpty());
  34. m_ppane = new ImagePane(pmodeler->LoadImage("dialogbkgndbmp", false));
  35. TRef<Image> pstringImage =
  36. CreateStringImage(
  37. JustifyLeft(),
  38. TrekResources::LargeFont(),
  39. new ColorValue(Color::White()),
  40. 403,
  41. new StringValue(str)
  42. );
  43. float bright = 1.0f / 16.0f;
  44. TRef<Image> pstringImage2 =
  45. CreateStringImage(
  46. JustifyLeft(),
  47. TrekResources::LargeFont(),
  48. new ColorValue(Color(bright)),
  49. 403,
  50. new StringValue(str)
  51. );
  52. TRef<AnimatedImagePane> panimatedImagePane =
  53. new AnimatedImagePane(
  54. new GroupImage(
  55. pstringImage,
  56. new TranslateImage(pstringImage2, Point(1, -1))
  57. )
  58. );
  59. panimatedImagePane->SetOffset(WinPoint(50, 50));
  60. m_ppane->InsertAtBottom(panimatedImagePane);
  61. TRef<ButtonPane> pbutton = pbuttonIn;
  62. if (!pbutton && fAddDefaultButton) {
  63. TRef<Surface> psurfaceButton = pmodeler->LoadImage("btnokbmp", true)->GetSurface();
  64. m_pbuttonOK =
  65. CreateButton(
  66. CreateButtonFacePane(psurfaceButton, ButtonNormal, 0, psurfaceButton->GetSize().X()),
  67. false, 0, 1.0
  68. );
  69. pbutton = m_pbuttonOK;
  70. } else if (pbutton) {
  71. m_pbuttonOK = pbutton;
  72. }
  73. if (pbuttonCancel) {
  74. m_pbuttonCancel = pbuttonCancel;
  75. if (pbutton == NULL) {
  76. pbutton = m_pbuttonCancel;
  77. }
  78. }
  79. if (pbutton) {
  80. m_ppane->InsertAtBottom(pbutton);
  81. pbutton->GetEventSource()->AddSink(IEventSink::CreateDelegate(this));
  82. pbutton->SetOffset(WinPoint(140, 170));
  83. if (m_pbuttonCancel != NULL && m_pbuttonCancel != pbutton) {
  84. m_ppane->InsertAtBottom(m_pbuttonCancel);
  85. m_pbuttonCancel->GetEventSource()->AddSink(IEventSink::CreateDelegate(this));
  86. pbutton->SetOffset(WinPoint(110, 170));
  87. pbuttonCancel->SetOffset(WinPoint(290, 170));
  88. }
  89. }
  90. m_psource = new IntegerEventSourceImpl();
  91. if (paintDelay == 0) {
  92. m_ppaneWrapper = m_ppane;
  93. m_pwrapImage->SetImage(IPopup::GetImage(m_pmodeler->GetEngine()));
  94. } else {
  95. m_ppaneWrapper = new AnimatedImagePane(m_pwrapImage);
  96. m_peventSink = IEventSink::CreateDelegate(this);
  97. m_ptimerEventSource = ptimerEventSource;
  98. ptimerEventSource->AddSink(m_peventSink, paintDelay);
  99. }
  100. }
  101. ~MessageBoxImpl()
  102. {
  103. if (m_peventSink != NULL) {
  104. m_ptimerEventSource->RemoveSink(m_peventSink);
  105. }
  106. }
  107. void Close(int id)
  108. {
  109. // an event may delete the last reference to this, so keep a reference
  110. // to ourselves for the duration of the function.
  111. TRef<MessageBoxImpl> pthis = this;
  112. if (m_ppopupOwner) {
  113. m_ppopupOwner->ClosePopup(this);
  114. } else {
  115. m_pcontainer->ClosePopup(this);
  116. }
  117. m_psource->Trigger(id);
  118. }
  119. //
  120. // IEventSync
  121. //
  122. bool OnEvent(IEventSource* pevent)
  123. {
  124. if (m_pbuttonOK && pevent == m_pbuttonOK->GetEventSource()) {
  125. Close(IDOK);
  126. } else if (m_pbuttonCancel && pevent == m_pbuttonCancel->GetEventSource()) {
  127. Close(IDCANCEL);
  128. } else {
  129. //
  130. // Timer
  131. //
  132. ZAssert(m_peventSink != NULL);
  133. m_peventSink = NULL;
  134. m_pwrapImage->SetImage(IPopup::GetImage(m_pmodeler->GetEngine()));
  135. }
  136. return false;
  137. }
  138. //
  139. // IKeyboardInput
  140. //
  141. bool OnChar(IInputProvider* pprovider, const KeyState& ks)
  142. {
  143. return true;
  144. }
  145. bool OnKey(IInputProvider* pprovider, const KeyState& ks, bool& fForceTranslate)
  146. {
  147. if (ks.bDown) {
  148. if (ks.vk == VK_ESCAPE) {
  149. if (m_pbuttonCancel) {
  150. Close(IDCANCEL);
  151. } else if (m_pbuttonOK) {
  152. Close(IDOK);
  153. }
  154. } else {
  155. if (ks.vk == VK_RETURN) {
  156. if (m_pbuttonOK) {
  157. Close(IDOK);
  158. }
  159. }
  160. }
  161. }
  162. return true;
  163. }
  164. //
  165. // IPopup methods
  166. //
  167. Pane* GetPane()
  168. {
  169. return m_ppaneWrapper;
  170. }
  171. Image* GetImage()
  172. {
  173. return m_pwrapImage;
  174. }
  175. IntegerEventSourceImpl* GetEventSource()
  176. {
  177. return m_psource;
  178. }
  179. };
  180. TRef<IMessageBox> CreateMessageBox(
  181. ITimerEventSource* ptimerEventSource,
  182. Modeler* pmodeler,
  183. const ZString& str,
  184. ButtonPane* pbuttonIn,
  185. bool fAddDefaultButton,
  186. ButtonPane* pbuttonCancel,
  187. float paintDelay
  188. ) {
  189. return new MessageBoxImpl(ptimerEventSource, pmodeler, str, pbuttonIn, fAddDefaultButton, pbuttonCancel, paintDelay);
  190. }