MenuWidget_Carousel.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../../idLib/precompiled.h"
  22. #include "../Game_local.h"
  23. void idMenuWidget_Carousel::Initialize( idMenuHandler * data ) {
  24. idMenuWidget::Initialize( data );
  25. class idCarouselRefresh : public idSWFScriptFunction_RefCounted {
  26. public:
  27. idCarouselRefresh( idMenuWidget_Carousel * _widget ) :
  28. widget( _widget ) {
  29. }
  30. idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ) {
  31. if ( widget == NULL ) {
  32. return idSWFScriptVar();
  33. }
  34. if ( widget->GetMoveDiff() != 0 ) {
  35. int diff = widget->GetMoveDiff();
  36. diff--;
  37. if ( widget->GetScrollLeft() ) {
  38. widget->SetViewIndex( widget->GetViewIndex() - 1 );
  39. } else {
  40. widget->SetViewIndex( widget->GetViewIndex() + 1 );
  41. }
  42. if ( diff > 0 ) {
  43. if ( widget->GetScrollLeft() ) {
  44. widget->MoveToIndex( ( widget->GetNumVisibleOptions() / 2 ) + diff );
  45. } else {
  46. widget->MoveToIndex( diff );
  47. }
  48. } else {
  49. widget->SetMoveDiff( 0 );
  50. }
  51. }
  52. widget->Update();
  53. return idSWFScriptVar();
  54. }
  55. private:
  56. idMenuWidget_Carousel * widget;
  57. };
  58. if ( GetSWFObject() != NULL ) {
  59. GetSWFObject()->SetGlobal( "refreshCarousel", new idCarouselRefresh( this ) );
  60. }
  61. }
  62. /*
  63. ========================
  64. idMenuWidget_Carousel::Update
  65. ========================
  66. */
  67. void idMenuWidget_Carousel::Update() {
  68. if ( GetSWFObject() == NULL ) {
  69. return;
  70. }
  71. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  72. if ( !BindSprite( root ) ) {
  73. return;
  74. }
  75. int midPoint = GetNumVisibleOptions() / 2 + 1;
  76. for ( int optionIndex = 0; optionIndex < GetNumVisibleOptions(); ++optionIndex ) {
  77. int listIndex = viewIndex + optionIndex;
  78. if ( optionIndex >= midPoint ) {
  79. listIndex = viewIndex - ( optionIndex - ( midPoint - 1 ) );
  80. }
  81. idMenuWidget & child = GetChildByIndex( optionIndex );
  82. child.SetSpritePath( GetSpritePath(), va( "item%d", optionIndex ) );
  83. if ( child.BindSprite( root ) ) {
  84. if ( listIndex < 0 || listIndex >= GetTotalNumberOfOptions() ) {
  85. child.SetState( WIDGET_STATE_HIDDEN );
  86. } else {
  87. idMenuWidget_Button * const button = dynamic_cast< idMenuWidget_Button * >( &child );
  88. button->SetImg( imgList[ listIndex ] );
  89. child.Update();
  90. if ( optionIndex == focusIndex ) {
  91. child.SetState( WIDGET_STATE_SELECTING );
  92. } else {
  93. child.SetState( WIDGET_STATE_NORMAL );
  94. }
  95. }
  96. }
  97. }
  98. }
  99. /*
  100. ========================
  101. idMenuWidget_Carousel::SetListImages
  102. ========================
  103. */
  104. void idMenuWidget_Carousel::SetListImages( idList<const idMaterial *> & list ) {
  105. imgList.Clear();
  106. for ( int i = 0; i < list.Num(); ++i ) {
  107. imgList.Append( list[ i ] );
  108. }
  109. }
  110. /*
  111. ========================
  112. idMenuWidget_Carousel::Update
  113. ========================
  114. */
  115. bool idMenuWidget_Carousel::HandleAction( idWidgetAction & action, const idWidgetEvent & event, idMenuWidget * widget, bool forceHandled ) {
  116. return idMenuWidget::HandleAction( action, event, widget, forceHandled );
  117. }
  118. /*
  119. ========================
  120. idMenuWidget_Carousel::MoveToFirstItem
  121. ========================
  122. */
  123. void idMenuWidget_Carousel::MoveToFirstItem( bool instant ) {
  124. if ( instant ) {
  125. moveDiff = 0;
  126. viewIndex = 0;
  127. moveToIndex = 0;
  128. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  129. if ( BindSprite( root ) ) {
  130. GetSprite()->StopFrame( 1 );
  131. }
  132. Update();
  133. }
  134. }
  135. /*
  136. ========================
  137. idMenuWidget_Carousel::MoveToLastItem
  138. ========================
  139. */
  140. void idMenuWidget_Carousel::MoveToLastItem( bool instant ) {
  141. if ( instant ) {
  142. moveDiff = 0;
  143. viewIndex = GetTotalNumberOfOptions() - 1;
  144. moveToIndex = GetTotalNumberOfOptions() - 1;
  145. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  146. if ( BindSprite( root ) ) {
  147. GetSprite()->StopFrame( 1 );
  148. }
  149. Update();
  150. }
  151. }
  152. /*
  153. ========================
  154. idMenuWidget_Carousel::Update
  155. ========================
  156. */
  157. void idMenuWidget_Carousel::MoveToIndex( int index, bool instant ) {
  158. idLib::Printf( "moveToIndex %i\n", index );
  159. if ( instant ) {
  160. viewIndex = index;
  161. moveDiff = 0;
  162. moveToIndex = viewIndex;
  163. idLib::Printf( "moveDiff = %i\n", moveDiff );
  164. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  165. if ( BindSprite( root ) ) {
  166. GetSprite()->StopFrame( 1 );
  167. }
  168. Update();
  169. return;
  170. }
  171. if ( index == 0 ) {
  172. fastScroll = false;
  173. moveDiff = 0;
  174. idLib::Printf( "moveDiff = %i\n", moveDiff );
  175. viewIndex = moveToIndex;
  176. return;
  177. }
  178. int midPoint = GetNumVisibleOptions() / 2;
  179. scrollLeft = false;
  180. if ( index > midPoint ) {
  181. moveDiff = index - midPoint;
  182. scrollLeft = true;
  183. } else {
  184. moveDiff = index;
  185. }
  186. idLib::Printf( "moveDiff = %i\n", moveDiff );
  187. if ( scrollLeft ) {
  188. moveToIndex = viewIndex - moveDiff;
  189. if ( moveToIndex < 0 ) {
  190. moveToIndex = 0;
  191. int diff = 0 - moveToIndex;
  192. moveDiff -= diff;
  193. }
  194. } else {
  195. moveToIndex = viewIndex + moveDiff;
  196. if ( moveToIndex >= GetTotalNumberOfOptions() ) {
  197. moveDiff = GetTotalNumberOfOptions() - GetViewIndex() - 1;
  198. moveToIndex = GetTotalNumberOfOptions() - 1;
  199. }
  200. }
  201. idLib::Printf( "moveDiff = %i\n", moveDiff );
  202. if ( moveDiff != 0 ) {
  203. if ( moveDiff > 1 ) {
  204. if ( scrollLeft ) {
  205. GetSprite()->PlayFrame( "leftFast" );
  206. } else {
  207. GetSprite()->PlayFrame( "rightFast" );
  208. }
  209. } else {
  210. if ( scrollLeft ) {
  211. GetSprite()->PlayFrame( "left" );
  212. } else {
  213. GetSprite()->PlayFrame( "right" );
  214. }
  215. }
  216. }
  217. idLib::Printf( "moveDiff = %i\n", moveDiff );
  218. }