contextPopup.ed.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. /// @class ContextPopup
  23. /// @brief Create a Popup Dialog that closes itself when signaled by an event.
  24. ///
  25. /// ContextPopup is a support class that offers a simple way of displaying
  26. /// reusable context sensitive GuiControl popups. These dialogs are created
  27. /// and shown to the user when the <b>show</b> method is used.
  28. ///
  29. /// Once a Popup is shown it will be dismissed if it meets one of a few
  30. /// criteria.
  31. ///
  32. /// 1. A user clicks anywhere outside the bounds of the GuiControl, specified by
  33. /// the 'dialog' field on the object.
  34. /// 2. Time Passes of (n)Milliseconds, specifed by the 'timeout' field on
  35. /// the object.
  36. ///
  37. /// For example, if you wished to create a context dialog with a dialog you held in
  38. /// a local variable named %myDialog you would create a new script object as such.
  39. ///
  40. ///
  41. /// @code
  42. /// %MyContextPopup = new ScriptObject()
  43. /// {
  44. /// class = ContextPopup;
  45. /// superClass= MyCallbackNamespace; // Only Necessary when you want to perform logic pre/post showing
  46. /// dialog = %%myDialog.getID();
  47. /// delay = 500; // Pop the Popup after 500 Milliseconds
  48. /// };
  49. /// @endcode
  50. ///
  51. /// Now, if you wanted to show the dialog %%myDialog and have it dismissed when anything in the
  52. /// background is clicked, simply call the following.
  53. ///
  54. ///
  55. /// @code
  56. /// %MyContextPopup.show( %positionX, %positionY );
  57. /// @endcode
  58. ///
  59. /// If you need to know more than show the dialog and hide it when clicked or time passes, ContextPopup
  60. /// Provides callback methods that you may override for doing intermediate processing on a dialog
  61. /// that is to be shown or is being hidden. For example, in the above script we created a Context Dialog Container
  62. /// called @%myContextDialog with a superClass of <b>MyCallbackNamespace</b>. If we wanted to hide the cursor when
  63. /// the dialog was shown, and show it when the dialog was hidden, we could implement the following functions.
  64. ///
  65. /// @code
  66. /// function MyCallbackNamespace::onContextActivate( %%this )
  67. /// {
  68. /// // Hide Cursor Here
  69. /// }
  70. /// function MyCallbackNamespace::onContextDeactivate( %%this )
  71. /// {
  72. /// // Show Cursor Here
  73. /// }
  74. /// @endcode
  75. ///
  76. /// @field GuiControl Dialog The GuiControl dialog to be shown when the context dialog is activated
  77. function ContextDialogContainer::onAdd(%this)
  78. {
  79. // Add to our cleanup group.
  80. $EditorClassesGroup.add( %this );
  81. %this.base = new GuiButtonBaseCtrl()
  82. {
  83. profile = ToolsGuiTransparentProfile;
  84. class = ContextDialogWatcher;
  85. parent = %this;
  86. modal = true;
  87. };
  88. // Flag not active.
  89. %this.isPushed = false;
  90. // Add to our cleanup group.
  91. $EditorClassesGroup.add( %this.base );
  92. return true;
  93. }
  94. function ContextDialogContainer::onRemove(%this)
  95. {
  96. %this.Hide();
  97. if( isObject( %this.base ) )
  98. %this.base.delete();
  99. }
  100. //-----------------------------------------------------------------------------
  101. /// (SimID this, int positionX, int positionY)
  102. /// Shows the GuiControl specified in the Dialog field at the coordinates passed
  103. /// to this function. If no coordinates are passed to this function, the Dialog control
  104. /// is shown using it's current position.
  105. ///
  106. /// @param this The ContextDialogContainer object
  107. /// @param positionX The X Position in Global Screen Coordinates to display the dialog
  108. /// @param positionY The Y Position in Global Screen Coordinates to display the dialog
  109. /// @param delay Optional delay before this popup is hidden that overrides that specified at construction time
  110. ///
  111. //-----------------------------------------------------------------------------
  112. function ContextDialogContainer::Show( %this, %positionX, %positionY, %delay )
  113. {
  114. if( %this.isPushed == true )
  115. return true;
  116. if( !isObject( %this.Dialog ) )
  117. return false;
  118. // Store old parent.
  119. %this.oldParent = %this.dialog.getParent();
  120. // Set new parent.
  121. %this.base.add( %this.Dialog );
  122. if( %positionX !$= "" && %positionY !$= "" )
  123. %this.Dialog.setPositionGlobal( %positionX, %positionY );
  124. Canvas.pushDialog( %this.base, 99 );
  125. // Setup Delay Schedule
  126. if( isEventPending( %this.popSchedule ) )
  127. cancel( %this.popSchedule );
  128. if( %delay !$= "" )
  129. %this.popSchedule = %this.schedule( %delay, hide );
  130. else if( %this.Delay !$= "" )
  131. %this.popSchedule = %this.schedule( %this.Delay, hide );
  132. }
  133. //-----------------------------------------------------------------------------
  134. /// (SimID this)
  135. /// Hides the GuiControl specified in the Dialog field if shown. This function
  136. /// is provided merely for more flexibility in when your dialog is shown. If you
  137. /// do not call this function, it will be called when the dialog is dismissed by
  138. /// a background click.
  139. ///
  140. /// @param this The ContextDialogContainer object
  141. ///
  142. //-----------------------------------------------------------------------------
  143. function ContextDialogContainer::Hide( %this )
  144. {
  145. if( %this.isPushed == true )
  146. Canvas.popDialog( %this.base );
  147. // Restore Old Parent;
  148. if( isObject( %this.Dialog ) && isObject( %this.oldParent ) )
  149. %this.oldParent.add( %this.Dialog );
  150. }
  151. // ContextDialogWatcher Class - JDD
  152. // CDW is a namespace link for the context background button to catch
  153. // event information and pass it back to the main class.
  154. //
  155. // onClick it will dismiss the parent
  156. // onDialogPop it will cleanup and notify user of deactivation
  157. // onDialogPush it will initialize state information and notify user of activation
  158. function ContextDialogWatcher::onClick( %this )
  159. {
  160. if( isObject( %this.parent ) )
  161. %this.parent.hide();
  162. }
  163. function ContextDialogWatcher::onDialogPop( %this )
  164. {
  165. if( !isObject( %this.parent ) )
  166. return;
  167. %this.parent.isPushed = false;
  168. if( %this.parent.isMethod( "onContextDeactivate" ) )
  169. %this.parent.onContextDeactivate();
  170. }
  171. function ContextDialogWatcher::onDialogPush( %this )
  172. {
  173. if( !isObject( %this.parent ) )
  174. return;
  175. %this.parent.isPushed = true;
  176. if( %this.parent.isMethod( "onContextActivate" ) )
  177. %this.parent.onContextActivate();
  178. }