aboutsummaryrefslogtreecommitdiff
path: root/Northstar.Client/mod/scripts/vscripts/ui/menu_mode_select.nut
blob: 109eed1291a94c803a23146e756f6dcdec1ce0af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
untyped
global function InitModesMenu
global function NSSetModeCategory

global enum eModeMenuModeCategory
{
	UNKNOWN = 0,
	PVPVE   = 1,
	PVE     = 2,
	PVP     = 3,
	FFA     = 4,
	TITAN   = 5,
	OTHER   = 6,
	CUSTOM  = 7

	SIZE
}

// List of blocked modes due to them being unfinished
const array<string> blockedModes =
[
	"fd_easy",
	"fd_normal",
	"fd_hard",
	"fd_master",
	"fd_insane"
]

struct ListEntry_t {
	string mode
	int category
}

// Slider mouse delta buffer
struct {
	int deltaX = 0
	int deltaY = 0
} mouseDeltaBuffer

struct {
	int scrollOffset
	var menu

	string searchString
	int searchEnum

	// Table of category overrides
	table<string,int> categoryOverrides

	// List of all modes we know
	array<ListEntry_t> modes

	// Sorted list of modes we want to show with categories included
	array<string> sortedModes
} file

const int MODES_PER_PAGE = 15

void function InitModesMenu()
{
	file.menu = GetMenu( "ModesMenu" )

	AddMouseMovementCaptureHandler( Hud_GetChild( file.menu, "MouseMovementCapture"), UpdateMouseDeltaBuffer )

	AddMenuEventHandler( file.menu, eUIEvent.MENU_CLOSE, OnCloseModesMenu )
	AddMenuEventHandler( file.menu, eUIEvent.MENU_OPEN, OnOpenModesMenu )
	AddButtonEventHandler( Hud_GetChild( file.menu, "BtnModeListUpArrow"), UIE_CLICK, OnUpArrowSelected )
	AddButtonEventHandler( Hud_GetChild( file.menu, "BtnModeListDownArrow"), UIE_CLICK, OnDownArrowSelected )

	AddButtonEventHandler( Hud_GetChild( file.menu, "BtnModeLabel"), UIE_CHANGE, FilterAndUpdateList )
	AddButtonEventHandler( Hud_GetChild( file.menu, "BtnModeSearch"), UIE_CHANGE, FilterAndUpdateList )
	AddButtonEventHandler( Hud_GetChild( file.menu, "SwtModeLabel"), UIE_CHANGE, FilterAndUpdateList )

	AddButtonEventHandler( Hud_GetChild( file.menu, "BtnModeFiltersClear"), UIE_CLICK, OnBtnFiltersClear_Activate )

	array<var> buttons = GetElementsByClassname( file.menu, "ModeSelectorPanel" )
	foreach ( var panel in buttons )
	{
		AddEventHandlerToButton( panel, "BtnMode", UIE_GET_FOCUS, ModeButton_GetFocus )
		AddEventHandlerToButton( panel, "BtnMode", UIE_CLICK, ModeButton_Click )
	}

	Hud_SetText( Hud_GetChild( file.menu, "SwtModeLabel" ), "#MODE_MENU_SWITCH" )
	SetButtonRuiText( Hud_GetChild( file.menu, "SwtModeLabel" ), "" )
	Hud_DialogList_AddListItem( Hud_GetChild( file.menu, "SwtModeLabel" ) , "#MODE_MENU_ALL", "-1" )
	for( int i = 0; i < eModeMenuModeCategory.SIZE; i++ )
	{
		Hud_DialogList_AddListItem( Hud_GetChild( file.menu, "SwtModeLabel" ) , GetCategoryStringFromEnum(i), string(i) )
	}

	AddMenuFooterOption( file.menu, BUTTON_A, "#A_BUTTON_SELECT" )
	AddMenuFooterOption( file.menu, BUTTON_B, "#B_BUTTON_BACK", "#BACK" )
}

void function NSSetModeCategory( string mode, int category )
{
	if( mode in file.categoryOverrides )
	{
		file.categoryOverrides[mode] = category
		printt( "Overwriting category for mode:", mode )
		return
	}

	file.categoryOverrides[mode] <- category
}

void function OnBtnFiltersClear_Activate( var b )
{
	file.searchString = ""
	file.searchEnum = -1

	SetConVarInt( "modemenu_mode_filter", -1 )
	Hud_SetText( Hud_GetChild( file.menu, "BtnModeSearch"), "" )

	file.scrollOffset = 0

	BuildSortedModesArray()
	UpdateListSliderHeight(float(file.sortedModes.len()))
	UpdateListSliderPosition(file.sortedModes.len())
	UpdateVisibleModes()
}

void function FilterAndUpdateList( var n )
{
	file.searchString = Hud_GetUTF8Text( Hud_GetChild( file.menu, "BtnModeSearch" ) )
	file.searchEnum = GetConVarInt( "modemenu_mode_filter" )

	file.scrollOffset = 0

	BuildSortedModesArray()
	UpdateListSliderHeight(float(file.sortedModes.len()))
	UpdateListSliderPosition(file.sortedModes.len())
	UpdateVisibleModes()
}

void function OnOpenModesMenu()
{
	RegisterButtonPressedCallback( MOUSE_WHEEL_UP , OnScrollUp )
	RegisterButtonPressedCallback( MOUSE_WHEEL_DOWN , OnScrollDown )

	// Reset filters
	file.searchString = ""
	file.searchEnum = -1

	// We rebuild the modes array on open menu to make sure
	// all modes get listed
	BuildModesArray()
	BuildSortedModesArray()

	UpdateListSliderHeight(float(file.sortedModes.len()))
	UpdateListSliderPosition(file.sortedModes.len())
	UpdateVisibleModes()

	// Set to the first mode if there's no mode focused
	if ( level.ui.privatematch_mode == 0 )
	{
		array<var> panels = GetElementsByClassname( file.menu, "ModeSelectorPanel" )
		foreach( var panel in panels )
		{
			if( Hud_IsEnabled( Hud_GetChild( panel, "BtnMode") ) )
			{
				Hud_SetFocused( Hud_GetChild( panel, "BtnMode") )
				break
			}
		}
	}
}

void function OnCloseModesMenu()
{
	try
	{
		DeregisterButtonPressedCallback( MOUSE_WHEEL_UP , OnScrollUp )
		DeregisterButtonPressedCallback( MOUSE_WHEEL_DOWN , OnScrollDown )
	}
	catch ( ex ) {}
}

string function GetCategoryStringFromEnum( int category )
{
	switch( category )
	{
		case eModeMenuModeCategory.PVPVE: return "#MODE_MENU_PVPVE"
		case eModeMenuModeCategory.PVE: return "#MODE_MENU_PVE"
		case eModeMenuModeCategory.PVP: return "#MODE_MENU_PVP"
		case eModeMenuModeCategory.FFA: return "#MODE_MENU_FFA"
		case eModeMenuModeCategory.TITAN: return "#MODE_MENU_TITAN_ONLY"
		case eModeMenuModeCategory.OTHER: return "#MODE_MENU_OTHER"
		case eModeMenuModeCategory.CUSTOM: return "#MODE_MENU_CUSTOM"
	}

	return "#MODE_MENU_UNKNOWN"
}

void function BuildModesArray()
{
	file.modes.clear()

	foreach( string mode in GetPrivateMatchModes() )
	{
		ListEntry_t entry
		entry.mode = mode
		entry.category = eModeMenuModeCategory.UNKNOWN

		switch( mode )
		{
			case "aitdm":
			case "at":
				entry.category = eModeMenuModeCategory.PVPVE
				break
			case "fd_easy":
			case "fd_normal":
			case "fd_hard":
			case "fd_master":
			case "fd_insane":
				entry.category = eModeMenuModeCategory.PVE
				break
			case "tdm":
			case "ctf":
			case "mfd":
			case "ps":
			case "cp":
			case "speedball":
			case "rocket_lf":
			case "holopilot_lf":
				entry.category = eModeMenuModeCategory.PVP
				break
			case "ffa":
			case "fra":
				entry.category = eModeMenuModeCategory.FFA
				break
			case "lts":
			case "ttdm":
			case "attdm":
			case "turbo_ttdm":
			case "alts":
			case "turbo_lts":
				entry.category = eModeMenuModeCategory.TITAN
				break
			case "coliseum":
			case "sp_coop":
				entry.category = eModeMenuModeCategory.OTHER
				break
			case "chamber":
			case "hidden":
			case "sns":
			case "fw":
			case "gg":
			case "tt":
			case "inf":
			case "kr":
			case "fastball":
			case "hs":
			case "ctf_comp":
			case "tffa":
				entry.category = eModeMenuModeCategory.CUSTOM
				break
		}

		file.modes.append(entry)
	}
}

int function SortModesAlphabetize( string a, string b )
{
	a = Localize( GetGameModeDisplayName( a ) )
	b = Localize( GetGameModeDisplayName( b ) )

	if ( a > b )
		return 1

	if ( a < b )
		return -1

	return 0
}

void function BuildSortedModesArray()
{
	file.sortedModes.clear()

	// Build sorted list of categories
	array<string> categories
	for( int i = 0; i < eModeMenuModeCategory.SIZE; i++ )
	{
		if( file.searchEnum != -1 && file.searchEnum != i )
			continue

		categories.append( GetCategoryStringFromEnum( i ) )
	}

	categories.sort( SortStringAlphabetize )

	// Build final list of mixed modes and categories
	foreach( string category in categories )
	{
		// Build sorted list of modes in category
		array<string> modes
		foreach( ListEntry_t entry in file.modes )
		{
			int iCategory = entry.category
			if( entry.mode in file.categoryOverrides )
				iCategory = file.categoryOverrides[entry.mode]
			
			if( GetCategoryStringFromEnum( iCategory ) != category )
				continue

			string mode = entry.mode

			if( file.searchString != "" && Localize(GetGameModeDisplayName(mode)).tolower().find(file.searchString.tolower()) == null )
				continue

			if( !modes.contains(mode) )
				modes.append( mode )
		}

		modes.sort( SortModesAlphabetize )

		if( modes.len() == 0 )
			continue

		// Add to final list we then display
		file.sortedModes.append( category )
		foreach( string mode in modes )
			file.sortedModes.append( mode )
	}
}

////////////////////////////
// Slider
////////////////////////////
void function UpdateMouseDeltaBuffer( int x, int y )
{
	mouseDeltaBuffer.deltaX += x
	mouseDeltaBuffer.deltaY += y

	SliderBarUpdate()
}

void function FlushMouseDeltaBuffer()
{
	mouseDeltaBuffer.deltaX = 0
	mouseDeltaBuffer.deltaY = 0
}


void function SliderBarUpdate()
{
	if( file.sortedModes.len() < MODES_PER_PAGE )
		return

	var sliderButton = Hud_GetChild( file.menu , "BtnModeListSlider" )
	var sliderPanel = Hud_GetChild( file.menu , "BtnModeListSliderPanel" )
	var movementCapture = Hud_GetChild( file.menu , "MouseMovementCapture" )

	Hud_SetFocused( sliderButton )

	int[2] screenSize = GetScreenSize()
	float minYPos = -40.0 * ( screenSize[1] / 1080.0 )
	float maxHeight = 596.0  * ( screenSize[1] / 1080.0 )
	float maxYPos = minYPos - ( maxHeight - Hud_GetHeight( sliderPanel ) )
	float useableSpace = maxHeight - Hud_GetHeight( sliderPanel )

	float jump = minYPos - ( useableSpace / ( float( file.sortedModes.len() ) ) )

	// got local from official respaw scripts, without untyped throws an error
	local pos =	Hud_GetPos( sliderButton )[1]
	local newPos = pos - mouseDeltaBuffer.deltaY
	FlushMouseDeltaBuffer()

	if ( newPos < maxYPos ) newPos = maxYPos
	if ( newPos > minYPos ) newPos = minYPos

	Hud_SetPos( sliderButton , 2, newPos )
	Hud_SetPos( sliderPanel , 2, newPos )
	Hud_SetPos( movementCapture , 2, newPos )

	file.scrollOffset = -int( ( ( newPos - minYPos ) / useableSpace ) * ( file.sortedModes.len() - MODES_PER_PAGE ) )
	UpdateVisibleModes()
}

void function UpdateListSliderHeight( float modes )
{
	var sliderButton = Hud_GetChild( file.menu , "BtnModeListSlider" )
	var sliderPanel = Hud_GetChild( file.menu , "BtnModeListSliderPanel" )
	var movementCapture = Hud_GetChild( file.menu , "MouseMovementCapture" )

	int[2] screenSize = GetScreenSize()
	float maxHeight = 596.0 * ( screenSize[1] / 1080.0 )
	float minHeight = 80.0 * ( screenSize[1] / 1080.0 )

	float height = maxHeight * ( MODES_PER_PAGE / modes )

	if ( height > maxHeight ) height = maxHeight
	if ( height < minHeight ) height = minHeight

	Hud_SetHeight( sliderButton, height )
	Hud_SetHeight( sliderPanel, height )
	Hud_SetHeight( movementCapture, height )
}


void function UpdateListSliderPosition( int modes )
{
	if( modes < MODES_PER_PAGE )
		return

	var sliderButton = Hud_GetChild( file.menu, "BtnModeListSlider" )
	var sliderPanel = Hud_GetChild( file.menu, "BtnModeListSliderPanel" )
	var movementCapture = Hud_GetChild( file.menu, "MouseMovementCapture" )

	float minYPos = -40.0 * ( GetScreenSize()[1] / 1080.0 )
	float useableSpace = (596.0 * ( GetScreenSize()[1] / 1080.0 ) - Hud_GetHeight( sliderPanel ) )

	float jump = minYPos - ( useableSpace / ( float( modes ) - MODES_PER_PAGE ) * file.scrollOffset )

	if ( jump > minYPos ) jump = minYPos

	Hud_SetPos( sliderButton, 2, jump )
	Hud_SetPos( sliderPanel, 2, jump )
	Hud_SetPos( movementCapture, 2, jump )
}

void function OnScrollDown( var button )
{
	if (file.sortedModes.len() <= MODES_PER_PAGE) return
	file.scrollOffset += 5
	if (file.scrollOffset + MODES_PER_PAGE > file.sortedModes.len()) {
		file.scrollOffset = file.sortedModes.len() - MODES_PER_PAGE
	}
	UpdateVisibleModes()
	UpdateListSliderPosition( file.sortedModes.len() )
}

void function OnScrollUp( var button )
{
	file.scrollOffset -= 5
	if ( file.scrollOffset < 0 ) {
		file.scrollOffset = 0
	}
	UpdateVisibleModes()
	UpdateListSliderPosition( file.sortedModes.len() )
}

void function OnDownArrowSelected( var button )
{
	if ( file.sortedModes.len() <= MODES_PER_PAGE ) return
	file.scrollOffset += 1
	if ( file.scrollOffset + MODES_PER_PAGE > file.sortedModes.len() )
	{
		file.scrollOffset = file.sortedModes.len() - MODES_PER_PAGE
	}

	UpdateVisibleModes()
	UpdateListSliderPosition( file.sortedModes.len() )
}


void function OnUpArrowSelected( var button )
{
	file.scrollOffset -= 1
	if ( file.scrollOffset < 0 )
	{
		file.scrollOffset = 0
	}

	UpdateVisibleModes()
	UpdateListSliderPosition( file.sortedModes.len() )
}

bool function IsStringCategory( string str )
{
	return GetGameModeDisplayName( str ) == ""
}

/////////////////////////////
// LIST
/////////////////////////////

void function UpdateVisibleModes()
{
	// ensures that we only ever show enough buttons for the number of modes we have
	array<var> buttons = GetElementsByClassname( GetMenu( "ModesMenu" ), "ModeSelectorPanel" )
	foreach ( var panel in buttons )
	{
		Hud_SetEnabled( panel, false )
		Hud_SetVisible( panel, false )
		Hud_SetText( Hud_GetChild( panel, "Header" ), "" )
		Hud_SetText( Hud_GetChild( panel, "BtnMode" ), "" )
		SetButtonRuiText( Hud_GetChild( panel, "BtnMode" ), "" )
	}

	for ( int i = 0; i < MODES_PER_PAGE; i++ )
	{
		if ( i + file.scrollOffset >= file.sortedModes.len() )
			break

		// Setup locals
		var panel = buttons[i]
		var button = Hud_GetChild( panel, "BtnMode" )
		var header = Hud_GetChild( panel, "Header" )

		int modeIndex = i + file.scrollOffset
		string mode = file.sortedModes[ modeIndex ]

		bool bIsCategory = IsStringCategory( mode )
		mode = bIsCategory ? mode : GetGameModeDisplayName( mode )

		// Show the panel
		Hud_SetEnabled( panel, true )
		Hud_SetVisible( panel, true )
		Hud_SetLocked( button, false )

		if( bIsCategory )
		{
			Hud_SetText( header, mode )
			Hud_SetEnabled( button, false )
		}
		else
		{
			Hud_SetEnabled( button, true )
			SetButtonRuiText( button, mode )

			if( blockedModes.contains( file.sortedModes[ modeIndex ] ) )
				Hud_SetLocked( button, true )

			if ( PrivateMatch_IsValidMapModeCombo( PrivateMatch_GetSelectedMap(), mode ) )
			{
				Hud_SetLocked( button, true )
				SetButtonRuiText( button, mode )
			}
		}
	}
}

void function ModeButton_GetFocus( var button )
{
	int modeId = int( Hud_GetScriptID( Hud_GetParent( button ) ) ) + file.scrollOffset - 1

	var nextModeImage = Hud_GetChild( file.menu, "NextModeImage" )
	var nextModeIcon = Hud_GetChild( file.menu, "ModeIconImage" )
	var nextModeName = Hud_GetChild( file.menu, "NextModeName" )
	var nextModeDesc = Hud_GetChild( file.menu, "NextModeDesc" )

	if ( modeId > file.sortedModes.len() )
		return

	string modeName = file.sortedModes[modeId]

	asset playlistImage = GetPlaylistImage( modeName )
	RuiSetImage( Hud_GetRui( nextModeImage ), "basicImage", playlistImage )
	RuiSetImage( Hud_GetRui( nextModeIcon ), "basicImage", GetPlaylistThumbnailImage( modeName ) )
	Hud_SetText( nextModeName, GetGameModeDisplayName( modeName ) )

	string mapName = PrivateMatch_GetSelectedMap()
	bool mapSupportsMode = PrivateMatch_IsValidMapModeCombo( mapName, modeName )
	if ( IsFDMode( modeName ) ) // HACK!
		Hud_SetText( nextModeDesc, Localize( "#FD_PLAYERS_DESC", Localize( GetGameModeDisplayHint( modeName ) ) ) )
	else
		Hud_SetText( nextModeDesc, GetGameModeDisplayHint( modeName ) )
}

void function ModeButton_Click( var button )
{
	// this never activates on custom servers, but keeping it for parity with official
	if ( !AmIPartyLeader() && GetPartySize() > 1 )
		return

	if ( Hud_IsLocked( button ) )
		return

	int modeID = int( Hud_GetScriptID( Hud_GetParent( button ) ) ) + file.scrollOffset - 1

	string modeName = file.sortedModes[ modeID ]

	// on modded servers set us to the first map for that mode automatically
	// need this for coliseum mainly which is literally impossible to select without this
 	if ( !PrivateMatch_IsValidMapModeCombo( PrivateMatch_GetSelectedMap(), modeName ) )
	{
		ClientCommand( "SetCustomMap " + GetPrivateMatchMapsForMode( modeName )[ 0 ] )
	}
	// set it
	ClientCommand( "PrivateMatchSetMode " + modeName )
	CloseActiveMenu()
}