MenuWidget_Scrollbar.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_ScrollBar::Initialize( idMenuHandler * data ) {
  24. idMenuWidget::Initialize( data );
  25. AddEventAction( WIDGET_EVENT_DRAG_START ).Set( new (TAG_SWF) idWidgetActionHandler( this, WIDGET_ACTION_EVENT_DRAG_START, WIDGET_EVENT_DRAG_START ) );
  26. AddEventAction( WIDGET_EVENT_DRAG_STOP ).Set( new (TAG_SWF) idWidgetActionHandler( this, WIDGET_ACTION_EVENT_DRAG_STOP, WIDGET_EVENT_DRAG_STOP ) );
  27. }
  28. /*
  29. ========================
  30. idMenuWidget_ScrollBar::Update
  31. ========================
  32. */
  33. void idMenuWidget_ScrollBar::Update() {
  34. if ( GetSWFObject() == NULL ) {
  35. return;
  36. }
  37. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  38. if ( !BindSprite( root ) || GetSprite() == NULL ) {
  39. return;
  40. }
  41. if ( GetParent() == NULL ) {
  42. return;
  43. }
  44. CalcTopAndBottom();
  45. idSWFScriptObject * node = GetSprite()->GetScriptObject()->GetNestedObj( "node" );
  46. idSWFSpriteInstance * nodeSprite = GetSprite()->GetScriptObject()->GetNestedSprite( "node" );
  47. if ( node != NULL && nodeSprite != NULL ) {
  48. node->Set( "onDrag", new ( TAG_SWF ) WrapWidgetSWFEvent( this, WIDGET_EVENT_DRAG_START, 0 ) );
  49. node->Set( "onRelease", new ( TAG_SWF ) WrapWidgetSWFEvent( this, WIDGET_EVENT_DRAG_STOP, 0 ) );
  50. const idMenuWidget_DynamicList * const list = dynamic_cast< const idMenuWidget_DynamicList * const >( GetParent() );
  51. if ( list != NULL ) {
  52. float percent = 0.0f;
  53. if ( ( list->GetTotalNumberOfOptions() - list->GetNumVisibleOptions() ) > 0 ) {
  54. percent = (float)list->GetViewOffset() / ( (float)list->GetTotalNumberOfOptions() - list->GetNumVisibleOptions() );
  55. float range = yBot - yTop;
  56. nodeSprite->SetVisible( true );
  57. nodeSprite->SetYPos( percent * range );
  58. //GetSprite()->StopFrame( int( ( percent * 100.0f ) + 2.0f ) );
  59. } else {
  60. nodeSprite->SetVisible( 0 );
  61. }
  62. }
  63. idMenuWidget_InfoBox * const infoBox = dynamic_cast< idMenuWidget_InfoBox * const >( GetParent() );
  64. if ( infoBox != NULL ) {
  65. float percent = 0.0f;
  66. if ( infoBox->GetMaxScroll() == 0 ) {
  67. nodeSprite->SetVisible( 0 );
  68. } else {
  69. percent = (float)infoBox->GetScroll() / (float)infoBox->GetMaxScroll();
  70. float range = yBot - yTop;
  71. nodeSprite->SetVisible( true );
  72. nodeSprite->SetYPos( percent * range );
  73. //GetSprite()->StopFrame( int( ( percent * 100.0f ) + 2.0f ) );
  74. }
  75. }
  76. }
  77. }
  78. /*
  79. ========================
  80. idMenuWidget_ScrollBar::CalcTopAndBottom
  81. ========================
  82. */
  83. void idMenuWidget_ScrollBar::CalcTopAndBottom() {
  84. if ( GetSWFObject() == NULL ) {
  85. return;
  86. }
  87. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  88. if ( !BindSprite( root ) || GetSprite() == NULL ) {
  89. return;
  90. }
  91. int tempPos = 0.0f;
  92. idSWFSpriteInstance * curMC = GetSprite()->GetScriptObject()->GetNestedSprite( "top" );
  93. if ( curMC != NULL ) {
  94. tempPos = curMC->GetYPos();
  95. while ( curMC->parent != NULL ) {
  96. curMC = curMC->parent;
  97. tempPos += curMC->GetYPos();
  98. }
  99. }
  100. yTop = tempPos;
  101. tempPos = 0.0f;
  102. curMC = GetSprite()->GetScriptObject()->GetNestedSprite( "bottom" );
  103. if ( curMC != NULL ) {
  104. tempPos = curMC->GetYPos();
  105. while ( curMC->parent != NULL ) {
  106. curMC = curMC->parent;
  107. tempPos += curMC->GetYPos();
  108. }
  109. }
  110. yBot = tempPos;
  111. }
  112. /*
  113. ========================
  114. idMenuWidget_ScrollBar::CalculatePosition
  115. ========================
  116. */
  117. void idMenuWidget_ScrollBar::CalculatePosition( float x, float y ) {
  118. if ( GetSprite() == NULL ) {
  119. return;
  120. }
  121. if ( y >= yTop && y <= yBot ) {
  122. float range = yBot - yTop;
  123. float val = y - yTop;
  124. float percent = val / range;
  125. idSWFSpriteInstance * node = GetSprite()->GetScriptObject()->GetNestedSprite( "node" );
  126. if ( node != NULL ) {
  127. node->SetYPos( percent * range );
  128. }
  129. idMenuWidget_DynamicList * const list = dynamic_cast< idMenuWidget_DynamicList * const >( GetParent() );
  130. if ( list != NULL ) {
  131. float maxScroll = list->GetTotalNumberOfOptions() - list->GetNumVisibleOptions();
  132. int offset = list->GetViewOffset();
  133. float segment = ( maxScroll + 0.5f ) / 100.0f;
  134. int newOffset = ( int )( ( ( percent * segment ) * 100.0f ) );
  135. if ( newOffset >= maxScroll ) {
  136. int i = 1;
  137. i = i;
  138. }
  139. if ( newOffset != offset ) {
  140. int viewIndex = list->GetViewIndex();
  141. list->SetViewOffset( newOffset );
  142. idLib::Printf( "newOffset = %d\n", newOffset );
  143. if ( viewIndex < newOffset ) {
  144. viewIndex = newOffset;
  145. list->SetViewIndex( viewIndex );
  146. } else if ( viewIndex > ( newOffset + list->GetNumVisibleOptions() - 1 ) ) {
  147. viewIndex = ( newOffset + list->GetNumVisibleOptions() - 1 );
  148. list->SetViewIndex( viewIndex );
  149. }
  150. idLib::Printf( "newView = %d\n", list->GetViewIndex() );
  151. int newFocus = viewIndex - newOffset;
  152. if ( newFocus >= 0 ) {
  153. list->SetFocusIndex( newFocus );
  154. }
  155. list->Update();
  156. }
  157. }
  158. idMenuWidget_InfoBox * const infoBox = dynamic_cast< idMenuWidget_InfoBox * const >( GetParent() );
  159. if ( infoBox != NULL ) {
  160. float maxScroll = infoBox->GetMaxScroll();
  161. int scroll = infoBox->GetScroll();
  162. float segment = ( maxScroll + 1.0f ) / 100.0f;
  163. int newScroll = (int)( ( ( percent * segment ) * 100.0f ) );
  164. if ( newScroll != scroll ) {
  165. infoBox->SetScroll( newScroll );
  166. }
  167. }
  168. }
  169. }
  170. /*
  171. ========================
  172. idMenuWidget_ScrollBar::HandleAction
  173. ========================
  174. */
  175. bool idMenuWidget_ScrollBar::HandleAction( idWidgetAction & action, const idWidgetEvent & event, idMenuWidget * widget, bool forceHandled ) {
  176. widgetAction_t actionType = action.GetType();
  177. switch ( actionType ) {
  178. case WIDGET_ACTION_SCROLL_DRAG: {
  179. if ( event.parms.Num() != 3 ) {
  180. return true;
  181. }
  182. dragging = true;
  183. float x = event.parms[0].ToFloat();
  184. float y = event.parms[1].ToFloat();
  185. bool initial = event.parms[2].ToBool();
  186. if ( initial ) {
  187. CalcTopAndBottom();
  188. }
  189. CalculatePosition( x, y );
  190. return true;
  191. }
  192. case WIDGET_ACTION_EVENT_DRAG_STOP: {
  193. dragging = false;
  194. return true;
  195. }
  196. }
  197. return idMenuWidget::HandleAction( action, event, widget, forceHandled );
  198. }
  199. /*
  200. ========================
  201. idMenuWidget_Help::ObserveEvent
  202. ========================
  203. */
  204. void idMenuWidget_ScrollBar::ObserveEvent( const idMenuWidget & widget, const idWidgetEvent & event ) {
  205. switch ( event.type ) {
  206. case WIDGET_EVENT_SCROLL_UP:
  207. case WIDGET_EVENT_SCROLL_DOWN:
  208. case WIDGET_EVENT_SCROLL_UP_LSTICK:
  209. case WIDGET_EVENT_SCROLL_UP_RSTICK:
  210. case WIDGET_EVENT_SCROLL_DOWN_LSTICK:
  211. case WIDGET_EVENT_SCROLL_DOWN_RSTICK:
  212. case WIDGET_EVENT_FOCUS_ON: {
  213. if ( !dragging ) {
  214. Update();
  215. }
  216. break;
  217. }
  218. }
  219. }