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.

153 lines
2.9 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef PANELPTR_H
  6. #define PANELPTR_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "panelhandle.h"
  11. #include "../iuiengine.h"
  12. #include "../iuipanelclient.h"
  13. #include "panorama/panoramacxx.h"
  14. #include "generichash.h"
  15. namespace panorama
  16. {
  17. class CPanel2D;
  18. //
  19. // Safe pointer to a panel
  20. //
  21. template < class T >
  22. class CPanelPtr
  23. {
  24. public:
  25. CPanelPtr() { Clear(); }
  26. CPanelPtr( const CPanelPtr &rhs ) { *this = rhs; }
  27. CPanelPtr( const IUIPanel *pPanel ) { Set( pPanel ); }
  28. CPanelPtr( const IUIPanelClient *pPanel ) { Set( pPanel ? pPanel->UIPanel() : (IUIPanel*)NULL ); }
  29. CPanelPtr< T > &operator=( const CPanelPtr< T > &ptr ) { m_handle = ptr.m_handle; return *this; }
  30. T *operator=( T *pPanel ) { Set( pPanel ); return pPanel; }
  31. void Clear()
  32. {
  33. m_handle = PanelHandle_t::InvalidHandle();
  34. }
  35. void Set( const IUIPanel *pPanel )
  36. {
  37. if( pPanel )
  38. m_handle = UIEngine()->GetPanelHandle( pPanel );
  39. else
  40. Clear();
  41. }
  42. void Set( const IUIPanelClient *pPanel )
  43. {
  44. if( pPanel )
  45. m_handle = UIEngine()->GetPanelHandle( pPanel->UIPanel() );
  46. else
  47. Clear();
  48. }
  49. T * Get() const
  50. {
  51. if ( m_handle == PanelHandle_t::InvalidHandle() )
  52. return NULL;
  53. // allow us to be called to return NULL pointers early
  54. if( UIEngine() == NULL )
  55. {
  56. return NULL;
  57. }
  58. if( panorama_is_base_of< IUIPanel, T >::value )
  59. {
  60. T* pPanel = (T *)UIEngine()->GetPanelPtr( m_handle );
  61. if ( pPanel )
  62. {
  63. return pPanel;
  64. }
  65. else
  66. {
  67. m_handle = PanelHandle_t::InvalidHandle();
  68. return NULL;
  69. }
  70. }
  71. else
  72. {
  73. IUIPanel *pPanel = UIEngine()->GetPanelPtr( m_handle );
  74. if( pPanel )
  75. return (T*)(pPanel->ClientPtr());
  76. else
  77. {
  78. m_handle = PanelHandle_t::InvalidHandle();
  79. return NULL;
  80. }
  81. }
  82. }
  83. T *operator->() const
  84. {
  85. return Get();
  86. }
  87. template < class U >
  88. bool operator==( const CPanelPtr< U > &rhs ) const
  89. {
  90. return ( m_handle == rhs.GetPanelHandle() );
  91. }
  92. template < class U >
  93. bool operator!=( const CPanelPtr< U > &rhs ) const
  94. {
  95. return !operator==( rhs );
  96. }
  97. template < class U >
  98. bool operator<( const CPanelPtr< U > &rhs) const
  99. {
  100. return m_handle < rhs.m_handle;
  101. }
  102. uint64 GetHandleAsUInt64() const
  103. {
  104. uint64 val = 0;
  105. val = ((uint64)m_handle.m_unSerialNumber)<<32 | m_handle.m_iPanelIndex;
  106. return val;
  107. }
  108. void SetFromUInt64( uint64 ulValue )
  109. {
  110. m_handle.m_unSerialNumber = ulValue>>32;
  111. m_handle.m_iPanelIndex = 0xFFFFFFFF & ulValue;
  112. }
  113. const PanelHandle_t &GetPanelHandle() const { return m_handle; }
  114. bool BPreviouslySet() { return ( m_handle != PanelHandle_t::InvalidHandle() ); }
  115. private:
  116. mutable PanelHandle_t m_handle;
  117. };
  118. template< class T >
  119. inline uint32 HashItem( const CPanelPtr<T> &item )
  120. {
  121. #if defined( SOURCE2_PANORAMA )
  122. return ::HashItem( item );
  123. #else
  124. return HashItemAsBytes( item );
  125. #endif
  126. }
  127. } // namespace panorama
  128. #endif // PANELPTR_H