forked from mart1nro/joycontrol
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoycontrol_gui.py
198 lines (198 loc) · 8.52 KB
/
joycontrol_gui.py
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
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" # you saw nothing
from joycontrol_wrapper import joycontrolWrapper
import asyncio
import pygame
import config
from tkinter.filedialog import askopenfilename # using tkinter for file chooser
from tkinter import Tk
pygame.init()
window_wdith = 200
window_height = 200
gameDisp = pygame.display.set_mode((window_wdith,window_height))
pygame.display.set_caption("joycontrol gui")
def maxMinCap(value,max,min):
if value < min:
return min
elif value > max:
return max
else:
return value
def text_object(text, font):
textSurface = font.render(text, True , (0,0,0))
return textSurface,textSurface.get_rect()
def statusText(content):
txtSize = pygame.font.SysFont('arial',22)
textSurf,textRect = text_object((content),txtSize)
textRect = (10,0)
gameDisp.blit(textSurf,textRect)
def nfcText(content):
txtSize = pygame.font.SysFont('arial',22)
textSurf,textRect = text_object((content),txtSize)
textRect = (10,22)
gameDisp.blit(textSurf,textRect)
async def main():
filepath = ''
nfcKeyOnce = False
clock = pygame.time.Clock()
xMoveCheck = False
yMoveCheck = False
cmToggled = False
alt = False
eToggle = False
catchMouse = config.catchMouse
gameDisp.fill((255,255,255))
statusText("connecting...")
pygame.display.update()
wrapper = joycontrolWrapper()
await wrapper.init(config.switchMac)
running = True
while running:
if xMoveCheck:
await wrapper.setRsitckH(2048)
xMoveCheck = False
if yMoveCheck:
await wrapper.setRsitckV(2048)
yMoveCheck = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == config.lstickKeyUp:
await wrapper.setLsitckV(3840)
if event.key == config.lstickKeyDown:
await wrapper.setLsitckV(256)
if event.key == config.lstickKeyLeft:
await wrapper.setLsitckH(256)
if event.key == config.lstickKeyRight:
await wrapper.setLsitckH(3840)
if event.key == config.lstickKeyClick:
await wrapper.pushButton('l_stick')
if event.key == config.rstickKeyClick:
await wrapper.pushButton('r_stick')
if event.key == config.aKey:
await wrapper.pushButton('a')
if event.key == config.bKey:
await wrapper.pushButton('b')
if event.key == config.xKey:
await wrapper.pushButton('x')
if event.key == config.yKey:
await wrapper.pushButton('y')
if event.key == config.dpadUp:
await wrapper.pushButton('up')
if event.key == config.dpadDown:
await wrapper.pushButton('down')
if event.key == config.dpadLeft:
await wrapper.pushButton('left')
if event.key == config.dpadRight:
await wrapper.pushButton('right')
if event.key == config.lKey:
await wrapper.pushButton('l')
if event.key == config.rKey:
await wrapper.pushButton('r')
if event.key == config.zlKey:
await wrapper.pushButton('zl')
if event.key == config.zrKey:
await wrapper.pushButton('zr')
if event.key == config.minusKey:
await wrapper.pushButton('minus')
if event.key == config.plusKey:
await wrapper.pushButton('plus')
if event.key == pygame.K_LALT:
alt = True
if event.key == pygame.K_e:
eToggle = True
if event.key == config.nfcKey:
if not nfcKeyOnce:
if wrapper.nfcIsEmpty():
_loop = asyncio.get_event_loop()
Tk().withdraw() # without this a blank window will appear after choosing the
filepath = askopenfilename(initialdir="./", title = "Select Amiibo Dump",
filetypes=(("Binary","*.bin"),("All Files", "*.*")))
if not filepath == '' and not filepath == ():
with open(filepath, 'rb') as nfcFile:
content = await _loop.run_in_executor(None, nfcFile.read)
await wrapper.setNfc(content)
else:
await wrapper.setNfc(None)
nfcKeyOnce = True
if event.type == pygame.KEYUP:
if event.key == config.lstickKeyUp:
await wrapper.setLsitckV(2048)
if event.key == config.lstickKeyDown:
await wrapper.setLsitckV(2048)
if event.key == config.lstickKeyLeft:
await wrapper.setLsitckH(2048)
if event.key == config.lstickKeyRight:
await wrapper.setLsitckH(2048)
if event.key == config.lstickKeyClick:
await wrapper.releaseButton('l_stick')
if event.key == config.rstickKeyClick:
await wrapper.releaseButton('r_stick')
if event.key == config.aKey:
await wrapper.releaseButton('a')
if event.key == config.bKey:
await wrapper.releaseButton('b')
if event.key == config.xKey:
await wrapper.releaseButton('x')
if event.key == config.yKey:
await wrapper.releaseButton('y')
if event.key == config.lKey:
await wrapper.releaseButton('l')
if event.key == config.dpadUp:
await wrapper.releaseButton('up')
if event.key == config.dpadDown:
await wrapper.releaseButton('down')
if event.key == config.dpadLeft:
await wrapper.releaseButton('left')
if event.key == config.dpadRight:
await wrapper.releaseButton('right')
if event.key == config.rKey:
await wrapper.releaseButton('r')
if event.key == config.zlKey:
await wrapper.releaseButton('zl')
if event.key == config.zrKey:
await wrapper.releaseButton('zr')
if event.key == config.minusKey:
await wrapper.releaseButton('minus')
if event.key == config.plusKey:
await wrapper.releaseButton('plus')
if event.key == pygame.KMOD_ALT:
cmToggled = False
alt = False
if event.key == pygame.K_e:
cmToggled = False
eToggle = False
if event.key == config.nfcKey:
nfcKeyOnce = False
if event.type == pygame.MOUSEMOTION:
if config.mouseAsRstick:
dx, dy = event.rel
"""
calculating what is worth to send to the switch
if the value is too low wont send anything
if this check didn't exist then the program would lag a lot
"""
if dx > 50 or dx < -50:
xMoveCheck = True
await wrapper.setRsitckH(maxMinCap((dx * config.rstickSenstivity * -1) + 2048,4095,0))
if dy > 50 or dy < -50:
yMoveCheck = True
await wrapper.setRsitckV(maxMinCap((dy * config.rstickSenstivity) + 2048,4095,0))
if alt and eToggle and not cmToggled:
catchMouse = not catchMouse
cmToggled = True
if catchMouse:
pygame.mouse.set_pos([100,100])
gameDisp.fill((255,255,255))
if wrapper.connected:
statusText("Connected!")
if not wrapper.nfcIsEmpty() and not filepath == '':
nfcText("nfc: "+filepath)
else:
nfcText("nfc: None")
pygame.display.update()
clock.tick(30)
pygame.quit()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())