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