Source code of Windows XP (NT5)
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.

142 lines
3.8 KiB

  1. /*************************************************
  2. * spritlst.cpp *
  3. * *
  4. * Copyright (C) 1995-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. // spritlst.cpp : implementation file
  8. //
  9. #include "stdafx.h"
  10. #include "dib.h"
  11. #include "spriteno.h"
  12. #include "sprite.h"
  13. #include "splstno.h"
  14. #include "spritlst.h"
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char BASED_CODE THIS_FILE[] = __FILE__;
  18. #endif
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CSpriteList
  21. IMPLEMENT_SERIAL(CSpriteList, CObList, 0 /* schema number*/ )
  22. CSpriteList::CSpriteList()
  23. {
  24. // give the sprite notification object
  25. // a pointer to the list object
  26. m_NotifyObj.SetList(this);
  27. }
  28. CSpriteList::~CSpriteList()
  29. {
  30. }
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CSpriteList serialization
  33. void CSpriteList::Serialize(CArchive& ar)
  34. {
  35. // let the base class create the set of objects
  36. CObList::Serialize(ar);
  37. // If we just loaded, initialize each sprite
  38. if (ar.IsLoading()) {
  39. for (POSITION pos = GetHeadPosition(); pos != NULL;) {
  40. CSprite *pSprite = GetNext(pos); // inc pos
  41. pSprite->SetNotificationObject(&m_NotifyObj);
  42. }
  43. }
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CSpriteList commands
  47. // Remove everything from the list deleting all the sprites we remove
  48. void CSpriteList::RemoveAll()
  49. {
  50. // Walk down the list deleting objects as we go.
  51. // We need to do this here because the base class
  52. // simply deletes the pointers.
  53. POSITION pos;
  54. CSprite *pSprite;
  55. for (pos = GetHeadPosition(); pos != NULL;) {
  56. pSprite = GetNext(pos); // inc pos
  57. if (pSprite) {
  58. ASSERT(pSprite->IsKindOf(RUNTIME_CLASS(CSprite)));
  59. delete pSprite;
  60. }
  61. }
  62. // now call the base class to remove the pointers
  63. CObList::RemoveAll();
  64. }
  65. // Add a sprite to the list, placing it according to its z-order value
  66. BOOL CSpriteList::Insert(CSprite *pNewSprite)
  67. {
  68. // Set the notification object pointer in the sprite
  69. // to the list's notifiaction object
  70. pNewSprite->SetNotificationObject(&m_NotifyObj);
  71. // Walk down the list until we either get to the end
  72. // or we find a sprite with the same or higher z order
  73. // in which case we insert just before that one.
  74. POSITION pos, posThis;
  75. CSprite *pSprite;
  76. for (pos = GetHeadPosition(); pos != NULL;) {
  77. posThis = pos;
  78. pSprite = GetNext(pos); // inc pos
  79. if (pSprite->GetZ() >= pNewSprite->GetZ()) {
  80. InsertBefore(posThis, pNewSprite);
  81. return TRUE;
  82. }
  83. }
  84. // nothing with same or higher z-order so add it to the end
  85. AddTail(pNewSprite);
  86. return TRUE;
  87. }
  88. // remove a sprite from the list, but do not delete it
  89. CSprite *CSpriteList::Remove(CSprite *pSprite)
  90. {
  91. POSITION pos = Find(pSprite);
  92. if (pos == NULL) {
  93. return NULL;
  94. }
  95. RemoveAt(pos);
  96. return pSprite;
  97. }
  98. // Move a sprite to its correct z-order position
  99. void CSpriteList::Reorder(CSprite *pSprite)
  100. {
  101. // Remove the sprite from the list
  102. if (!Remove(pSprite)) {
  103. TRACE("Unable to find sprite");
  104. return; // not there
  105. }
  106. // Now insert it again in the right place
  107. Insert(pSprite);
  108. }
  109. // Test for a mouse hit on any sprite in the list
  110. CSprite *CSpriteList::HitTest(CPoint point)
  111. {
  112. // Walk the list top down
  113. POSITION pos;
  114. CSprite *pSprite;
  115. for (pos = GetHeadPosition(); pos != NULL;) {
  116. pSprite = GetNext(pos); // inc pos
  117. if (pSprite->HitTest(point)) {
  118. return pSprite;
  119. }
  120. }
  121. return NULL;
  122. }