Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef DIALOGMANAGER_H
  8. #define DIALOGMANAGER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <utllinkedlist.h>
  13. #include <KeyValues.h>
  14. #include <vgui_controls/PHandle.h>
  15. namespace vgui
  16. {
  17. //-----------------------------------------------------------------------------
  18. // Purpose: utility class, maps a set of ID's to dialogs
  19. // used to manage sets of similar dialogs (object property dialogs, etc.)
  20. //-----------------------------------------------------------------------------
  21. template <class TDialog, class I = int>
  22. class DialogManager
  23. {
  24. public:
  25. // new dialog factory function
  26. typedef TDialog *(*CreateNewDialogFunc_t)(I dialogID);
  27. // constructor
  28. DialogManager(CreateNewDialogFunc_t createDialogFunc);
  29. // finds the dialog by the specified ID
  30. TDialog *FindDialog(I dialogID, bool bCreate);
  31. // opens the dialog; creating it if specified
  32. TDialog *ActivateDialog(I dialogID, bool bCreate);
  33. // closes all the dialogs
  34. void CloseAll();
  35. // closes and deletes all the dialogs
  36. void CloseAndDeleteAll();
  37. // returns number of active dialogs
  38. int Count();
  39. // sets parent to use
  40. void SetParent( vgui::VPANEL parent );
  41. private:
  42. // checks if an index in the dialog list is valid; if it has been deleted, removes the entry
  43. bool ValidateIndex(int index);
  44. struct DialogItem_t
  45. {
  46. I id;
  47. DHANDLE<TDialog> dlg;
  48. };
  49. CUtlLinkedList<DialogItem_t, int> m_Dialogs;
  50. CreateNewDialogFunc_t m_CreateFunc;
  51. vgui::VPANEL m_pVGUIParentPanel;
  52. };
  53. // constructor
  54. template <class TDialog, class I>
  55. inline DialogManager<TDialog, I>::DialogManager(CreateNewDialogFunc_t createDialogFunc)
  56. {
  57. m_CreateFunc = createDialogFunc;
  58. m_pVGUIParentPanel = NULL;
  59. }
  60. // finds the dialog; creating it if necessary
  61. template <class TDialog, class I>
  62. inline TDialog *DialogManager<TDialog, I>::FindDialog(I dialogID, bool bCreate)
  63. {
  64. for (int i = 0; i < m_Dialogs.MaxElementIndex(); i++)
  65. {
  66. if (ValidateIndex(i) && m_Dialogs[i].id == dialogID)
  67. {
  68. return m_Dialogs[i].dlg;
  69. }
  70. }
  71. if (bCreate)
  72. {
  73. int newIndex = m_Dialogs.AddToTail();
  74. if (m_CreateFunc)
  75. {
  76. m_Dialogs[newIndex].dlg = m_CreateFunc(dialogID);
  77. }
  78. else
  79. {
  80. m_Dialogs[newIndex].dlg = new TDialog(NULL, dialogID);
  81. }
  82. Assert(m_pVGUIParentPanel);
  83. m_Dialogs[newIndex].dlg->SetParent( m_pVGUIParentPanel );
  84. m_Dialogs[newIndex].id = dialogID;
  85. return m_Dialogs[newIndex].dlg;
  86. }
  87. // dlg not found, not created
  88. return NULL;
  89. }
  90. // opens the dialog; creating it if necessary
  91. template <class TDialog, class I>
  92. inline TDialog *DialogManager<TDialog, I>::ActivateDialog(I dialogID, bool bCreate)
  93. {
  94. TDialog *dlg = FindDialog(dialogID, bCreate);
  95. if (dlg)
  96. {
  97. dlg->Activate();
  98. }
  99. return dlg;
  100. }
  101. // count
  102. template <class TDialog, class I>
  103. inline int DialogManager<TDialog, I>::Count()
  104. {
  105. // validate all the indexes first
  106. for (int i = 0; i < m_Dialogs.MaxElementIndex(); i++)
  107. {
  108. if (ValidateIndex(i))
  109. {
  110. }
  111. }
  112. // return the (remaining) count
  113. return m_Dialogs.Count();
  114. }
  115. // closes all the dialogs
  116. template <class TDialog, class I>
  117. inline void DialogManager<TDialog, I>::CloseAll()
  118. {
  119. for (int i = 0; i < m_Dialogs.MaxElementIndex(); i++)
  120. {
  121. if (ValidateIndex(i))
  122. {
  123. m_Dialogs[i].dlg->PostMessage(m_Dialogs[i].dlg, new KeyValues("Close"));
  124. }
  125. }
  126. }
  127. // closes and deletes all the dialogs
  128. template <class TDialog, class I>
  129. inline void DialogManager<TDialog, I>::CloseAndDeleteAll()
  130. {
  131. CloseAll();
  132. for (int i = 0; i < m_Dialogs.MaxElementIndex(); i++)
  133. {
  134. if (ValidateIndex(i))
  135. {
  136. m_Dialogs[i].dlg->MarkForDeletion();
  137. }
  138. }
  139. m_Dialogs.RemoveAll();
  140. }
  141. // checks if a dialog is valid; if it has been deleted, removes the entry
  142. template <class TDialog, class I>
  143. inline bool DialogManager<TDialog, I>::ValidateIndex(int index)
  144. {
  145. if (m_Dialogs.IsValidIndex(index))
  146. {
  147. if (m_Dialogs[index].dlg.Get())
  148. {
  149. return true;
  150. }
  151. else
  152. {
  153. // entry has been deleted; removed
  154. m_Dialogs.Remove(index);
  155. }
  156. }
  157. return false;
  158. }
  159. template <class TDialog, class I>
  160. inline void DialogManager<TDialog, I>::SetParent( vgui::VPANEL parent )
  161. {
  162. m_pVGUIParentPanel = parent;
  163. }
  164. } // namespace vgui
  165. #endif // DIALOGMANAGER_H