OpenBarnyard
 
Loading...
Searching...
No Matches
ABYardMenuState.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "ABYardMenuState.h"
3#include "GUI/AGUI2.h"
6#include "SoundBank/ui.h"
7
8//-----------------------------------------------------------------------------
9// Enables memory debugging.
10// Note: Should be the last include!
11//-----------------------------------------------------------------------------
12#include <Core/TMemoryDebugOn.h>
13
15
17
29
35
37{
39 {
40 m_oMenu.ProcessInputEvent( a_pInputEvent );
41 }
42
43 return BaseClass::ProcessInput( a_pInputEvent );
44}
45
46TBOOL ABYardMenuState::ProcessCommand( AInputCommand a_eInputCommand, const Toshi::TInputInterface::InputEvent* a_pInputEvent, TBOOL& a_rStopEvents )
47{
49 {
50 if ( a_eInputCommand == AInputCommand_Cancel )
52 else if ( !m_oMenu.ProcessInputCommand( a_eInputCommand, a_pInputEvent ) )
53 return TFALSE;
54
55 return TTRUE;
56 }
57
58 return TFALSE;
59}
60
61static TFLOAT s_fOscillatorPeriod = TMath::PI / 1.5f;
62static TFLOAT s_fOscillatorAmplitude = 2.0f - TMath::Sin( s_fOscillatorPeriod );
63
65{
66 m_fTotalTime += a_fDeltaTime;
67
68 switch ( m_eMenuState )
69 {
71 // Dialog appear animation
72 m_fOscillatorOffset += a_fDeltaTime * 6.0f;
73
74 if ( m_fOscillatorOffset <= s_fOscillatorPeriod )
75 {
76 TFLOAT fScale = s_fOscillatorAmplitude * TMath::Sin( m_fOscillatorOffset );
77 m_oDialog.GetTransform().SetFromScale( fScale, fScale );
78 }
79 else
80 {
81 m_fOscillatorOffset = s_fOscillatorPeriod;
84 m_oDialog.GetTransform().SetIdentity();
85 }
86
87 // Increase dialog alpha
88 SetDialogOpacity( m_oDialog.GetAlpha() + a_fDeltaTime * 5.0f );
89 break;
90
92 // Dialog disappear animation
93 if ( m_fOscillatorOffset >= 0.0f )
94 {
95 m_fOscillatorOffset -= a_fDeltaTime * 6.0f;
96
97 TFLOAT fScale = s_fOscillatorAmplitude * TMath::Sin( TMath::Max( m_fOscillatorOffset, 0.0f ) );
98 m_oDialog.GetTransform().SetFromScale( fScale, fScale );
99
100 if ( fScale < 0.5f )
101 {
102 // Decrease dialog alpha
103 SetDialogOpacity( m_oDialog.GetAlpha() - a_fDeltaTime * 5.0f );
104 }
105 }
106 else
107 {
110 m_fMenuOpacity = 1.0f;
111
112 m_oRootElement.Hide();
113 OnMenuClose();
114
115 return TTRUE;
116 }
117
118 break;
120 // Menu fade-in animation
121 m_fMenuOpacity += a_fDeltaTime * 5.0f;
122
123 if ( m_fMenuOpacity >= 1.0f )
124 {
125 m_fMenuOpacity = 1.0f;
127 m_fOscillatorOffset = 0.0f;
128
129 OnMenuOpen();
130 }
131
132 // Update menu opacity
133 UpdateMenuOpacity();
134 return BaseClass::OnUpdate( a_fDeltaTime );
135
137 // Menu fade-out animation
138 m_fMenuOpacity -= a_fDeltaTime * 5.0f;
139
140 if ( m_fMenuOpacity > 0.0f )
141 {
142 UpdateMenuOpacity();
143 return BaseClass::OnUpdate( a_fDeltaTime );
144 }
145 else
146 {
147 // Fade-out animation is over
148 m_fMenuOpacity = 0.0f;
149 m_oMenu.SetMenuAlpha( 0.0f, 0.0f );
150
151 if ( m_oDialog.IsVisible() )
152 {
153 // Can show dialog
155 m_fOscillatorOffset = s_fOscillatorPeriod;
156 return BaseClass::OnUpdate( a_fDeltaTime );
157 }
158 else
159 {
160 // Can't show dialog
161 m_oRootElement.Hide();
163 OnMenuClose();
164
165 return TTRUE;
166 }
167 }
168
169 break;
171 // Closed?
172 return BaseClass::OnUpdate( a_fDeltaTime );
174 // Menu is open and can be used
176 {
177 TFLOAT fScale = 1.0f + 0.05f * TMath::Sin( m_fTotalTime * ( TMath::PI * 2 ) );
178 m_oMenu.GetFocusedMenuItem()->GetTransform().SetScale( fScale, fScale );
179 }
180
181 m_oMenu.Update( a_fDeltaTime );
182 return BaseClass::OnUpdate( a_fDeltaTime );
183 }
184
185 return BaseClass::OnUpdate( a_fDeltaTime );
186}
187
188TPSTRING8_DECLARE( DialogInputMap );
189
191{
192 ms_fAnimationTime = 0.0f;
193
194 if ( m_bHasRectangle2 )
195 {
196 TFLOAT fWidth, fHeight;
197 AGUI2::GetSingleton()->GetDimensions( fWidth, fHeight );
198
199 m_oRectangle2.SetDimensions( fWidth, fHeight );
200 m_oRectangle2.SetColour( 0x7f000000 );
201
203 }
204
205 SetInputMap( TPS8( DialogInputMap ) );
208
209 TVALIDPTR( pFont );
210 TVALIDPTR( pPanelTexSec );
211
212 // Setup textbox
213 TFLOAT fTextBoxWidth = ( m_oDialogTitle.GetText() != TNULL ) ? pFont->GetTextWidth( m_oDialogTitle.GetText() ) : pPanelTexSec->GetWidth();
214 m_oDialogTitle.Create( pFont, fTextBoxWidth );
215 m_oDialogTitle.SetColour( 0xfffff0af );
216 m_oDialogTitle.SetTransform( 0.0f, 2.0f, 0.0f );
217
218 AGUI2Transform oTransform;
219 oTransform.m_aMatrixRows[ 0 ] = TVector2( 1.1f, 0.0f );
220 oTransform.m_aMatrixRows[ 1 ] = TVector2( 0.0f, 1.1f );
221 oTransform.m_vecTranslation.x = 0.0f;
222 m_oDialogTitle.GetTransform().PreMultiply( oTransform );
223
224 m_oDialogTitle.SetShadow( TTRUE, 0xa0000000 );
225 m_oDialogTitle.SetShadowAlpha( 0.6f );
226 m_oDialogTitle.SetShadowOffset( 5.0f, 5.0f );
227
228 // Setup textbox background
229 m_oDialogTitleBackground.SetTextureSection( pPanelTexSec );
230 m_oDialogTitleBackground.SetDimensions( TMath::Max( m_oDialogTitle.GetWidth() * 1.25f, pPanelTexSec->GetWidth() ), pPanelTexSec->GetHeight() );
233
234 // Setup menu
235 m_oMenu.SetFocus( TTRUE );
236 m_oMenu.SetItemActivationCallback( []( void* a_pUserData, AGUI2MenuItem* a_pItem ) {
237 TSTATICCAST( ABYardMenuState, a_pUserData )->OnMenuItemActivated( *a_pItem );
238 } );
239 m_oMenu.SetItemFocusCallback( []( void* a_pUserData, AGUI2MenuItem* a_pOldFocus, AGUI2MenuItem* a_pNewFocus ) {
240 TSTATICCAST( ABYardMenuState, a_pUserData )->OnMenuItemFocused( *a_pOldFocus, *a_pNewFocus );
241 } );
242 m_oMenu.SetCallbackUserData( this );
243 m_oMenu.SetFocusSound( soundbank::UI_MENUCLICK );
244 m_oMenu.SetBackSound( soundbank::UI_MENUBACK );
245 m_oMenu.SetSelectSound( soundbank::UI_MENUOK );
246 m_oMenu.SetNegativeSound( soundbank::UI_NEGATIVE );
247 m_oMenu.ReflowChildrenVertically();
248
249 // Setup dialog
250 TFLOAT fMenuWidth, fMenuHeight;
251 m_oMenu.GetDimensions( fMenuWidth, fMenuHeight );
252
253 if ( fMenuHeight < 128.0f )
254 fMenuHeight = 128.0f;
255
256 if ( fMenuWidth < 384.0f )
257 fMenuWidth = 384.0f;
258
259 m_oDialog.Create();
260 m_oDialog.SetDialogSize( fMenuWidth + 128.0f, fMenuHeight + 128.0f );
261
262 AGUI2Transform oDialogTransform;
263 oDialogTransform.SetZero();
264 m_oDialog.GetTransform().PreMultiply( oDialogTransform );
265
266 // Add the elements to the GUI root
267 m_oRootElement.AddChildTail( m_oDialog );
268
269 TFLOAT fGUIRootWidth, fGUIRootHeight;
270 AGUI2::GetRootElement()->GetDimensions( fGUIRootWidth, fGUIRootHeight );
271 m_oRootElement.SetDimensions( fGUIRootWidth, fGUIRootHeight );
272
273 m_oRootElement.AddChildTail( m_oMenu );
274 m_oDialog.AddChildTail( m_oDialogTitleBackground );
276
278}
279
281{
282 if ( m_bHasRectangle2 )
283 m_oRectangle2.RemoveSelf();
284
285 m_oDialogTitle.RemoveSelf();
286 m_oDialogTitleBackground.RemoveSelf();
287 m_oDialog.RemoveSelf();
288 m_oMenu.RemoveSelf();
289 m_oRootElement.RemoveSelf();
290
292}
293
295{
296 AGUI2::GetSingleton()->SetCursorVisible( TTRUE );
297
299 m_oRootElement.Show();
300
301 // Apply additional transform to the dialog window (apparently, no transform is applied)
303 oDialogTransform.SetZero();
304
305 m_oDialog.GetTransform().PreMultiply( oDialogTransform );
306
307 m_oMenu.SetMenuAlpha( 0.0f );
308 m_fMenuOpacity = 0;
309 m_fOscillatorOffset = 0.0f;
311
312 m_HUDParams.SetFlags( 64 );
313}
314
316{
317 AGUI2::GetSingleton()->SetCursorVisible( TFALSE );
318 m_oRootElement.Hide();
319}
320
324
326{
328 {
329 // Reset transform of the element saving it's translation
330 TVector2 vecTranslation = a_rOldFocus.GetTransform().GetTranslation();
331
332 a_rOldFocus.GetTransform().SetZero();
333 a_rOldFocus.GetTransform().Scale( 1.0f, 1.0f );
334 a_rOldFocus.GetTransform().SetTranslation( vecTranslation );
335 }
336}
337
341
342void ABYardMenuState::UpdateMenuOpacity()
343{
344 TMath::Clip( m_fMenuOpacity, 0.0f, 1.0f );
346}
347
348void ABYardMenuState::SetDialogOpacity( TFLOAT a_fOpacity )
349{
350 TMath::Clip( a_fOpacity, 0.0f, 1.0f );
351 m_oDialog.SetAlpha( a_fOpacity );
352}
353
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TOSHI_NAMESPACE_USING
Definition Defines.h:46
#define TVALIDPTR(PTR)
Definition Defines.h:139
#define TDEFINE_CLASS_NORUNTIME(...)
Definition TObject.h:138
#define TPSTRING8_DECLARE(STR)
Definition TPString8.h:15
#define TPS8(STR)
Definition TPString8.h:16
float TFLOAT
Definition Typedefs.h:4
#define TNULL
Definition Typedefs.h:23
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
@ AGUI2ATTACHMENT_TOPCENTER
#define AGUI2STYLE_FONT_PRIMARY
Definition AGUI2Style.h:4
AInputCommand
Definition AInputMap.h:12
@ AInputCommand_Cancel
Definition AInputMap.h:16
TFORCEINLINE void Clip(T &rVal, const T &Min, const T &Max)
TFORCEINLINE const T & Max(const T &a, const T &b)
constexpr TFLOAT PI
Definition TMathInline.h:35
TFORCEINLINE TFLOAT Sin(TFLOAT fVal)
Definition TMathInline.h:42
@ UI_MENUCLICK
Definition ui.h:28
@ UI_MENUOK
Definition ui.h:34
@ UI_MENUBACK
Definition ui.h:31
@ UI_NEGATIVE
Definition ui.h:19
virtual void OnMenuItemFocused(AGUI2MenuItem &a_rOldFocus, AGUI2MenuItem &a_rNewFocus)
static Toshi::TVector2 ms_vecActivatedButtonTranslation
virtual void OnMenuClose()
virtual void OnActivate() override
TBOOL m_bFocusedElementBloated
AGUI2TextBox m_oDialogTitle
virtual TBOOL OnUpdate(TFLOAT a_fDeltaTime) override
virtual void OnRemoval() override
static TFLOAT ms_fAnimationTime
virtual TBOOL ProcessCommand(AInputCommand a_eInputCommand, const Toshi::TInputInterface::InputEvent *a_pInputEvent, TBOOL &a_rStopEvents) override
virtual void OnMenuOpen()
static Toshi::TVector2 ms_vecActivatedButtonShadow
void OnButtonActivated(AGUI2Button *a_pButton)
MENUSTATE m_eMenuState
AGUI2Element m_oRootElement
AGUI2Dialog m_oDialog
TFLOAT * m_pButtonRotations
virtual TBOOL ProcessInput(const Toshi::TInputInterface::InputEvent *a_pInputEvent) override
AGUI2Rectangle m_oDialogTitleBackground
AGUI2Rectangle m_oRectangle2
virtual void OnDeactivate() override
virtual void OnInsertion() override
virtual TBOOL OnUpdate(TFLOAT a_fDeltaTime)
virtual void OnRemoval()
virtual TBOOL ProcessInput(const Toshi::TInputInterface::InputEvent *a_pInputEvent)
void SetInputMap(const Toshi::TPString8 &a_MapName)
virtual void OnInsertion()
HUDParams m_HUDParams
Definition AGameState.h:135
static AGUI2Element * GetRootElement()
Definition AGUI2.h:31
AGUI2TextBox & GetTextBox()
Definition AGUI2Button.h:36
AGUI2Transform & GetTransform()
void AddChildTail(AGUI2Element &a_rElement)
virtual void GetDimensions(TFLOAT &a_rWidth, TFLOAT &a_rHeight)
TFLOAT GetTextWidth(const TWCHAR *a_wszText, TFLOAT a_fScale=1.0f)
static AGUI2Font * FindFont(const TCHAR *a_szFontName)
void SetMenuAlpha(TFLOAT a_fAlpha, TFLOAT a_fShadowAlpha=-1.0f)
TFLOAT GetShadowOffsetX() const
TFLOAT GetShadowOffsetY() const
static AGUI2TextureSection * GetTextureSection(const TCHAR *a_szSectionName)
constexpr const Toshi::TVector2 & GetTranslation() const
Toshi::TVector2 m_vecTranslation
Toshi::TVector2 m_aMatrixRows[2]
constexpr void SetTranslation(TFLOAT a_fX, TFLOAT a_fY)
void Scale(TFLOAT a_fScaleX, TFLOAT a_fScaleY)
constexpr void SetZero()