aboutsummaryrefslogtreecommitdiff
path: root/Northstar.Client/mod/scripts/vscripts/ui/menu_ns_custom_match_settings.nut
blob: d39b7774836a02d5c1a5adc91d8881b08c5bda89 (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
global function AddNorthstarCustomMatchSettingsMenu
global function SetNextMatchSettingsCategory

const string SETTING_ITEM_TEXT = "                           " // this is long enough to be the same size as the textentry field

struct {
	string currentCategory
	
	table< int, int > enumRealValues
} file

void function AddNorthstarCustomMatchSettingsMenu()
{
	AddMenu( "CustomMatchSettingsMenu", $"resource/ui/menus/custom_match_settings.menu", InitNorthstarCustomMatchSettingsMenu, "#MENU_MATCH_SETTINGS" )
}

void function SetNextMatchSettingsCategory( string category )
{
	file.currentCategory = category
	print( "Category: " + category )
	
	file.enumRealValues.clear()
}

void function InitNorthstarCustomMatchSettingsMenu()
{
	AddMenuEventHandler( GetMenu( "CustomMatchSettingsMenu" ), eUIEvent.MENU_OPEN, OnNorthstarCustomMatchSettingsMenuOpened )
	AddMenuFooterOption( GetMenu( "CustomMatchSettingsMenu" ), BUTTON_B, "#B_BUTTON_BACK", "#BACK" )
	
	foreach ( var button in GetElementsByClassname( GetMenu( "CustomMatchSettingsMenu" ), "MatchSettingButton" ) )
	{	
		// it's not possible to clear dialoglists, so we have to hack together stuff that effectively recreates their functionality
		Hud_DialogList_AddListItem( button, SETTING_ITEM_TEXT, "prev" )
		Hud_DialogList_AddListItem( button, SETTING_ITEM_TEXT, "main" )
		Hud_DialogList_AddListItem( button, SETTING_ITEM_TEXT, "next" )
		
		AddButtonEventHandler( button, UIE_CHANGE, OnSettingButtonPressed  )
	}
	
	foreach ( var textPanel in GetElementsByClassname( GetMenu( "CustomMatchSettingsMenu" ), "MatchSettingTextEntry" ) )
		Hud_AddEventHandler( textPanel, UIE_LOSE_FOCUS, SendTextPanelChanges )
}

void function OnNorthstarCustomMatchSettingsMenuOpened()
{
	array<var> buttons = GetElementsByClassname( GetMenu( "CustomMatchSettingsMenu" ), "MatchSettingButton" )
	array<var> textPanels = GetElementsByClassname( GetMenu( "CustomMatchSettingsMenu" ), "MatchSettingTextEntry" )

	foreach ( var button in buttons )
	{
		Hud_SetEnabled( button, false )
		Hud_SetVisible( button, false )
	}
	
	foreach ( var textPanel in textPanels )
	{
		Hud_SetEnabled( textPanel, false )
		Hud_SetVisible( textPanel, false )
	}
	
	int i = 0;
	foreach ( CustomMatchSettingContainer setting in GetPrivateMatchCustomSettingsForCategory( file.currentCategory ) )
	{
		Hud_SetEnabled( buttons[ i ], true )
		Hud_SetVisible( buttons[ i ], true )
		Hud_SetText( buttons[ i ], setting.localizedName )
		Hud_SetDialogListSelectionValue( buttons[ i ], "main" )
		
		Hud_SetEnabled( textPanels[ i ], true )
		Hud_SetVisible( textPanels[ i ], true )
		
		// manually resolve default gamemode/playlist vars since game won't do it for us if we aren't using GetCurrentPlaylistVar
		string gamemode = PrivateMatch_GetSelectedMode()
		if ( gamemode != "speedball" ) // hack since lf is weird
			gamemode = GetPlaylistGamemodeByIndex( gamemode, 0 )
		
		string gamemodeVar = GetGamemodeVarOrUseValue( PrivateMatch_GetSelectedMode(), setting.playlistVar, setting.defaultValue )
		string playlistVar = GetPlaylistVarOrUseValue( PrivateMatch_GetSelectedMode(), setting.playlistVar, setting.defaultValue )
		
		if ( playlistVar != gamemodeVar && playlistVar == setting.defaultValue )
			playlistVar = gamemodeVar

		if ( setting.isEnumSetting )
		{
			// setup internal state for enums 
			int enumIndex = int ( max( 0, setting.enumValues.find( playlistVar ) ) )
			
			file.enumRealValues[ int( Hud_GetScriptID( textPanels[ i ] ) ) ] <- enumIndex
			Hud_SetText( textPanels[ i ], setting.enumNames[ enumIndex ] )
		}
		else
			Hud_SetText( textPanels[ i ], playlistVar )
					
		i++
	}
}

void function OnSettingButtonPressed( var button )
{
	CustomMatchSettingContainer setting = GetPrivateMatchCustomSettingsForCategory( file.currentCategory )[ int( Hud_GetScriptID( button ) ) ]
	var textPanel = GetElementsByClassname( GetMenu( "CustomMatchSettingsMenu" ), "MatchSettingTextEntry" )[ int( Hud_GetScriptID( button ) ) ]
	
	if ( setting.isEnumSetting )
	{
		string selectionVal = Hud_GetDialogListSelectionValue( button )
		if ( selectionVal == "main" )
			return
						
		int enumVal = file.enumRealValues[ int( Hud_GetScriptID( button ) ) ]
		if ( selectionVal == "next" ) // enum val += 1
			 enumVal = ( enumVal + 1 ) % setting.enumValues.len()
		else // enum val -= 1
		{
			enumVal--
			if ( enumVal == -1 )
				enumVal = setting.enumValues.len() - 1
		}
		
		file.enumRealValues[ int( Hud_GetScriptID( button ) ) ] = enumVal
		Hud_SetText( textPanel, setting.enumNames[ enumVal ] )
		
		ClientCommand( "PrivateMatchSetPlaylistVarOverride " + setting.playlistVar + " " + setting.enumValues[ enumVal ] )
	}
	else
	{
		// this doesn't work for some reason
		Hud_SetFocused( textPanel )
	}
	
	Hud_SetDialogListSelectionValue( button, "main" )
}

void function SendTextPanelChanges( var textPanel ) 
{
	CustomMatchSettingContainer setting = GetPrivateMatchCustomSettingsForCategory( file.currentCategory )[ int( Hud_GetScriptID( textPanel ) ) ]
	
	// enums don't need to do this
	if ( !setting.isEnumSetting )
		ClientCommand( "PrivateMatchSetPlaylistVarOverride " + setting.playlistVar + " " + Hud_GetUTF8Text( textPanel ) )
}