/
controller_widget.py
243 lines (190 loc) · 7.04 KB
/
controller_widget.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
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
#!/usr/bin/env python2
"""
SC-Controller - Controller Widget
Button that user can click to choose emulated action for physical button, axis
or pad.
Wraps around actual button defined in glade file.
"""
from __future__ import unicode_literals
from scc.tools import _
from gi.repository import Gtk, Gdk, Pango
from scc.constants import SCButtons, STICK, GYRO, LEFT, RIGHT
from scc.actions import Action, XYAction, MultiAction
from scc.gui.ae.gyro_action import is_gyro_enable
from scc.profile import Profile
import os, sys, logging
log = logging.getLogger("ControllerWidget")
TRIGGERS = [ Profile.LEFT, Profile.RIGHT ]
PADS = [ "LPAD", "RPAD" ]
STICKS = [ STICK ]
GYROS = [ GYRO ]
PRESSABLE = [ SCButtons.LPAD, SCButtons.RPAD, SCButtons.STICK ]
_NOT_BUTTONS = PADS + STICKS + GYROS + [ "LT", "RT" ]
_NOT_BUTTONS += [ x + "TOUCH" for x in PADS ]
BUTTONS = [ b for b in SCButtons if b.name not in _NOT_BUTTONS ]
LONG_TEXT = 12
class ControllerWidget:
ACTION_CONTEXT = None
def __init__(self, app, id, use_icon, widget):
self.app = app
self.id = id
self.name = id if type(id) in (str, unicode) else id.name
self.widget = widget
self.label = Gtk.Label()
self.label.set_ellipsize(Pango.EllipsizeMode.END)
self.icon = Gtk.Image.new_from_file(self.get_image()) if use_icon else None
self.update()
self.widget.connect('enter', self.on_cursor_enter)
self.widget.connect('leave', self.on_cursor_leave)
self.widget.connect('clicked', self.on_click)
self.widget.connect('button-release-event', self.on_button_release)
def get_image(self):
return os.path.join(self.app.imagepath, self.name + ".svg")
def update(self):
self.label.set_label(_("(no action)"))
def on_click(self, *a):
self.app.show_editor(self.id)
def on_button_release(self, bt, event):
if event.button == 3:
# Rightclick
self.app.show_context_menu(self.id)
def on_cursor_enter(self, *a):
self.app.hilight(self.name)
def on_cursor_leave(self, *a):
self.app.hilight(None)
class ControllerButton(ControllerWidget):
ACTION_CONTEXT = Action.AC_BUTTON
def __init__(self, app, name, use_icon, widget):
ControllerWidget.__init__(self, app, name, use_icon, widget)
if use_icon:
vbox = Gtk.Box(Gtk.Orientation.HORIZONTAL)
separator = Gtk.Separator(orientation = Gtk.Orientation.VERTICAL)
vbox.pack_start(self.icon, False, False, 1)
vbox.pack_start(separator, False, False, 1)
vbox.pack_start(self.label, False, True, 1)
self.widget.add(vbox)
else:
self.widget.add(self.label)
self.widget.show_all()
self.label.set_max_width_chars(12)
if name == "C":
self.label.set_max_width_chars(10)
def update(self):
if self.id in SCButtons and self.id in self.app.current.buttons:
txt = self.app.current.buttons[self.id].describe(self.ACTION_CONTEXT)
if len(txt) > LONG_TEXT or "\n" in txt:
txt = "\n".join(txt.split("\n")[0:2])
txt = txt.replace("<", "<").replace(">", ">")
self.label.set_markup("<small>%s</small>" % (txt,))
else:
txt = txt.replace("<", "<").replace(">", ">")
self.label.set_markup(txt)
else:
self.label.set_label(_("(no action)"))
class ControllerStick(ControllerWidget):
ACTION_CONTEXT = Action.AC_STICK
def __init__(self, app, name, use_icon, enable_press, widget):
self.pressed = Gtk.Label() if enable_press else None
ControllerWidget.__init__(self, app, name, use_icon, widget)
grid = Gtk.Grid()
self.widget.set_events(Gdk.EventMask.POINTER_MOTION_MASK)
self.widget.connect('motion-notify-event', self.on_cursor_motion)
self.label.set_property("vexpand", True)
self.label.set_property("hexpand", True)
if self.pressed:
self.label.set_xalign(0.0); self.label.set_yalign(0.5)
self.pressed.set_property("hexpand", True)
self.pressed.set_xalign(0.0); self.pressed.set_yalign(1.0)
grid.attach(self.pressed, 2, 2, 1, 1)
else:
self.label.set_xalign(0.5); self.label.set_yalign(0.5)
self.pressed = None
if self.icon:
self.icon.set_margin_right(5)
grid.attach(self.icon, 1, 1, 1, 2)
grid.attach(self.label, 2, 1, 1, 1)
self.over_icon = False
self.enable_press = enable_press
self.widget.add(grid)
self.widget.show_all()
def on_cursor_enter(self, *a):
return
def on_click(self, *a):
if self.over_icon and self.enable_press:
self.app.show_editor(getattr(SCButtons, self.id))
else:
self.app.show_editor(self.id)
def on_cursor_motion(self, trash, event):
# self.icon.get_allocation().x + self.icon.get_allocation().width # yields nonsense
ix2 = 74
# Check if cursor is placed on icon
if event.x < ix2:
self.app.hilight(self.name + "_press")
self.over_icon = True
else:
self.app.hilight(self.name)
self.over_icon = False
def _set_label(self, action):
self.label.set_label(action.describe(self.ACTION_CONTEXT))
def update(self):
action = self.app.current.buttons[SCButtons.STICK]
self._set_label(self.app.current.stick)
txt = action.describe(self.ACTION_CONTEXT)
txt = txt.replace("<", "<").replace(">", ">")
if self.pressed:
self.pressed.set_markup("<small>Pressed: %s</small>" % (txt,))
class ControllerTrigger(ControllerButton):
ACTION_CONTEXT = Action.AC_TRIGGER
def update(self):
if self.id in TRIGGERS and self.id in self.app.current.triggers:
self.label.set_label(self.app.current.triggers[self.id].describe(self.ACTION_CONTEXT))
else:
self.label.set_label(_("(no action)"))
class ControllerPad(ControllerStick):
ACTION_CONTEXT = Action.AC_PAD
def update(self):
if self.id == "LPAD":
action = self.app.current.pads[Profile.LEFT]
pressed = self.app.current.buttons[SCButtons.LPAD]
else:
action = self.app.current.pads[Profile.RIGHT]
pressed = self.app.current.buttons[SCButtons.RPAD]
self._set_label(action)
txt = pressed.describe(self.ACTION_CONTEXT)
txt = txt.replace("<", "<").replace(">", ">")
if self.pressed:
self.pressed.set_markup("<small>Pressed: %s</small>" % (txt,))
class ControllerGyro(ControllerWidget):
ACTION_CONTEXT = Action.AC_GYRO
def __init__(self, app, name, use_icon, widget):
self.pressed = Gtk.Label()
ControllerWidget.__init__(self, app, name, use_icon, widget)
grid = Gtk.Grid()
self.label.set_property("vexpand", True)
self.label.set_property("hexpand", True)
self.label.set_xalign(0.0); self.label.set_yalign(0.5)
self.pressed.set_property("hexpand", True)
self.pressed.set_xalign(0.0); self.pressed.set_yalign(1.0)
if self.icon:
self.icon.set_margin_right(5)
grid.attach(self.icon, 1, 1, 1, 2)
grid.attach(self.label, 2, 1, 1, 1)
grid.attach(self.pressed, 2, 2, 1, 1)
self.over_icon = False
self.widget.add(grid)
self.widget.show_all()
def on_click(self, *a):
self.app.show_editor(self.id)
def _set_label(self, action):
if is_gyro_enable(action):
action = action.mods[action.order[0]]
if isinstance(action, MultiAction):
rv = []
for a in action.actions:
d = a.describe(self.ACTION_CONTEXT)
if not d in rv : rv.append(d)
self.label.set_label("\n".join(rv))
return
self.label.set_label(action.describe(self.ACTION_CONTEXT))
def update(self):
self._set_label(self.app.current.gyro)