-
Notifications
You must be signed in to change notification settings - Fork 5
/
AirConsole.gd
248 lines (180 loc) · 7.79 KB
/
AirConsole.gd
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
extends Node
const SCREEN = 0
const ORIENTATION_PORTRAIT = 'portrait'
const ORIENTATION_LANDSCAPE = 'landscape'
const PARSE_JS = true
export(bool) var print_signal = true
signal device_connected(device_id)
signal device_disconnected(device_id)
signal is_ready(join_code)
signal message_received(device_id, data)
signal custom_device_state_changed(device_id, custom_data)
signal device_state_changed(device_id, user_data)
signal device_profile_changed(device_id)
signal received_email_address(email_address)
signal received_profile_picture(device_id)
signal active_players_changed(player_number)
signal device_motion(data)
signal ad_completed(ad_was_shown)
signal ad_shown()
signal premium_connected(device_id)
signal persistent_data_loaded(data)
signal persistent_data_stored(uid)
signal highscores(highscores)
signal highscores_stored(highscore)
var _last_texture:ImageTexture
func get_controller_device_ids() -> Array:
return js('airconsole.getControllerDeviceIds()', PARSE_JS)
func get_device_id() -> int:
return js('airconsole.getDeviceId()')
func get_master_controller_device_id() -> int:
return js('airconsole.getMasterControllerDeviceId()')
func get_server_time() -> int:
return js('airconsole.getServerTime()')
func broadcast(data):
js('airconsole.broadcast(%s)' % to_json(data))
func message(device_id:int, data):
js('airconsole.message(%s, %s)' % [device_id, to_json(data)])
func get_custom_device_state(device_id):
return js('airconsole.getCustomDeviceState(%s)' % device_id, PARSE_JS)
func set_custom_device_state(data):
js('airconsole.setCustomDeviceState(%s)' % to_json(data))
func set_custom_device_state_property(key:String, value):
js('airconsole.setCustomDeviceStateProperty(%s, %s)' % [to_json(key), to_json(value)])
func edit_profile():
js('airconsole.editProfile()')
func get_nickname(device_id:int) -> String:
return js('airconsole.getNickname(%s)' % device_id)
func request_profile_picture(device_id_or_uid, size = 64) -> String:
var url = js('airconsole.getProfilePicture(%s, %s)' % [to_json(device_id_or_uid), size])
_download_profile_picture(url, device_id_or_uid)
return url
func _download_profile_picture(url, device_id):
$ImageRequest.request(url, device_id)
func get_profile_picture(device_id_or_uid):
return $ImageRequest.get_texture(device_id_or_uid)
func get_uid(device_id:int = get_device_id()) -> String:
return js('airconsole.getUID(%s)' % to_json(device_id))
func is_user_logged_in(device_id:int) -> bool:
return js('airconsole.isUserLoggedIn(%s)' % device_id)
func request_email_address():
js('airconsole.requestEmailAddress()')
func convert_device_id_to_player_number(device_id:int) -> int:
return js('airconsole.convertDeviceIdToPlayerNumber(%s)' % device_id)
func convert_player_number_to_device_id(player_number:int) -> int:
return js('airconsole.convertPlayerNumberToDeviceId(%s)' % player_number)
func get_active_player_device_ids() -> Array:
return js('airconsole.getActivePlayerDeviceIds()', PARSE_JS)
func set_active_players(max_players:int):
js('airconsole.setActivePlayers(%s)' % max_players)
func vibrate(miliseconds:int):
js('airconsole.vibrate(%s)' % miliseconds)
func show_ad():
js('airconsole.showAd()')
func get_premium():
js('airconsole.getPremium()')
func get_premium_device_ids() -> Array:
return js('airconsole.getPremiumDeviceIds()', PARSE_JS)
func is_premium(device_id:int) -> bool:
return js('airconsole.isPremium(%s)' % device_id)
func get_navigate_parameters():
return js('airconsole.getNavigateParameters()', PARSE_JS)
func navigate_home():
js('airconsole.navigateHome()')
func navigate_to(url:String, parameters):
js('airconsole.navigate_to(%s, %s)' % [to_json(url), to_json(parameters)])
func open_external_url(url:String):
js('airconsole.openExternalUrl(%s)' % to_json(url))
func set_orientation(orientation:String):
js('airconsole.setOrientation(%s)' % to_json(orientation))
func show_default_ui(visible:bool):
js('airconsole.showDefaultUI(%s)' % visible)
func request_persistent_data(uids:Array = []):
js('airconsole.requestPersistentData(%s)' % to_json(uids))
func store_persistent_data(key:String, value, uid:String = get_uid()):
js('airconsole.storePersistentData(%s, %s, %s)' % [to_json(key), to_json(value), to_json(uid)])
func request_highscores(level_name:String, level_version:String, uids:PoolStringArray, ranks:PoolStringArray, total:int = 8, top:int = 5):
js('airconsole.requestHighScores(%s, %s, %s, %s, %s, %s)' % [to_json(level_name), to_json(level_version), to_json(uids), to_json(ranks), total, top])
func store_highscore(level_name:String, level_version:String, score:int, uid:String, data, score_string:String):
js('airconsole.storeHighScore(%s, %s, %s, %s, %s, %s)' % [to_json(level_name), to_json(level_version), score, to_json(uid), to_json(data), to_json(score_string)])
func _enter_tree():
var image_request = load('res://addons/godot-airconsole/ImageRequest.gd').new()
image_request
image_request.name = 'ImageRequest'
image_request.connect('received_texture', self, '_forward_image_result')
add_child(image_request, true)
func _forward_image_result(device_id):
emit_signal('received_profile_picture', device_id)
func _ready():
_setup_signals()
if print_signal:
print('GodotAirConsole initialized.')
func _init():
js("""
function Signal(name, args) {
this.name = name
this.args = args
}
""")
js("airconsole = new AirConsole({ synchronize_time:true })")
js("signals = []")
js("""
function has_signals() {
return signals.length > 0
}
function get_signals() {
var value = JSON.stringify(signals)
signals = []
return value
}
""")
func _setup_signals():
_setup_signal('onConnect', 'device_connected', ['device_id'])
_setup_signal('onDisconnect', 'device_disconnected', ['device_id'])
_setup_signal('onReady', 'is_ready', ['join_code'])
_setup_signal('onMessage', 'message_received', ['device_id', 'data'])
_setup_signal('onCustomDeviceStateChange', 'custom_device_state_changed', ['device_id', 'custom_data'])
_setup_signal('onDeviceStateChange', 'device_state_changed', ['device_id', 'user_data'])
_setup_signal('onDeviceProfileChange', 'device_profile_changed', ['device_id'])
_setup_signal('onEmailAddress', 'received_email_address', ['email_address'])
_setup_signal('onActivePlayersChange', 'active_players_changed', ['player_number'])
_setup_signal('onDeviceMotion', 'device_motion', ['data'])
_setup_signal('onAdComplete', 'ad_completed', ['ad_was_shown'])
_setup_signal('onAdShow', 'ad_shown', [])
_setup_signal('onPremium', 'premium_connected', ['device_id'])
_setup_signal('onPersistentDataLoaded', 'persistent_data_loaded', ['data'])
_setup_signal('onPersistentDataStored', 'persistent_data_stored', ['uid'])
_setup_signal('onHighScores', 'highscores', ['highscores'])
_setup_signal('onHighScoreStored', 'highscores_stored', ['highscore'])
func _setup_signal(airconsoleMethod:String, signalName:String, parameters:PoolStringArray):
var parameters_string = parameters.join(',')
var template = \
"""airconsole.{airconsole} = function({parameters}) {
signals.push(new Signal('{signalName}', [{parameters}]))
}"""
var code = template.format({
'airconsole': airconsoleMethod,
'signalName': signalName,
'parameters': parameters_string
})
js(code)
func js(code:String, parse_js = false):
if parse_js:
code = 'JSON.stringify(%s)' % code
var value = JavaScript.eval(code, true)
if parse_js:
value = parse_json(value)
return value
func _physics_process(delta):
_poll_signals()
func _poll_signals():
if not js('has_signals()'): return
var signals = parse_json(js('get_signals()'))
for s in signals:
var arguments = s.args
if print_signal:
print_debug('GodotAirConsole: Signal %s(%s)' % [s.name, _to_argument_string(arguments)])
arguments.push_front(s.name)
callv('emit_signal', arguments)
func _to_argument_string(array:Array):
return str(array).replace('[', '').replace(']', '')