-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlideWidget.py
219 lines (186 loc) · 6.79 KB
/
SlideWidget.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
from WindowElements import *
from ChatCommandButton import *
from os import linesep
"""
\file SlideWidget.py
\brief Contains the SlideWidget plugin
"""
class SlideWidget(Widget):
"""
\class SlideWidget
\brief The SlideWidgetPlugin
This plugin holds severall ChatCommandButtons and displays them on demand
"""
def __init__(self):
"""
\brief Construct the widget
\param pipes The communication pipes to the PluginManager
\param args Additional startup arguments
"""
super(SlideWidget, self).__init__()
self.__commandButtons = []
def initialize(self, buttonList):
"""
\brief Initialize the widget
\param buttonList A list of dictionaries that define buttons
"""
if isinstance(buttonList, list):
for i in buttonList:
try:
commandLine = i['command']
except KeyError:
commandLine = None
newButton = ChatCommandButton(commandLine)
try:
newButton.setIcon(i['icon'])
except KeyError:
pass
try:
newButton.setText(i['text'])
except KeyError:
pass
try:
newButton.setImage(i['image'], i.get('imagefocus', i['image']))
except KeyError:
pass
try:
newButton.setManialink(i['manialink'])
except KeyError:
pass
self.__commandButtons.append(newButton)
def setUser(self, name):
"""
\brief Set the user of this window
"""
super(SlideWidget, self).setUser(name)
for b in self.__commandButtons:
b.setUser(name)
def addButton(self, button):
"""
\brief Add a new button to the SlideWidget
\param button The new button
"""
button.setName(str(len(self.__commandButtons)))
self.__commandButtons.append(button)
def getCallbackAddress(self, login, windowName, functionName):
"""
\brief Get the callback address for the window
\param login The login of the user to display to
"""
return self.getWindowManager().getCallbackAddress(login,
self.getName(),
windowName + '.' + functionName)
def callMethod(self, *args):
self.getWindowManager().callMethod(*args)
def callFunction(self, *args):
return self.getWindowManager().callFunction(*args)
def signalEvent(self, *args):
self.getWindowManager().signalEvent(*args)
def __getattr__(self, name):
"""
\brief Get the target of the calls from other plugins
\param name The name of the target
"""
if not '.' in name:
return getattr(super(SlideWidget, self), name)
try:
splitList = name.split('.')
return getattr(self.__commandButtons[int(splitList[0])], '.'.join(splitList[1:]))
except TypeError:
raise AttributeError
except KeyError:
print('Trying to access non existing button ', splitList[0])
raise
def getManialink(self):
"""
\brief Return the manialink hierarchie of this widget
"""
size = self.getSize()
oldSize = size
mainFrame = Frame()
mainFrame['id'] = 'mainFrame'
mainFrame['posn'] = '60 10 1'
contentFrame = Frame()
contentFrame['posn'] = '{:d} {:d} 1'.format(1, size[1] // 2 - 1)
x = 0
y = 0
for c in self.__commandButtons:
c.setWindowManager(self)
buttonSize = c.getSize()
#buttonFrame = Frame()
buttonFrame = c.getManialink()
buttonFrame['posn'] = '{:d} {:d} 1'.format(x + buttonSize[0],
y - buttonSize[1] // 2)
y -= buttonSize[1]
if -y > size[1] + buttonSize[1]:
y = 0
x += buttonSize[0]
#ml = c.getManialink()
#buttonFrame.addChild(ml)
#print(ml.getXML())
contentFrame.addChild(buttonFrame)
if x != 0:
size = (x + 10, size[1])
else:
size = (x + 10, 2 -y)
mainFrame.addChild(contentFrame)
bgQuad = Quad()
bgQuad['sizen'] = '{:d} {:d}'.format(size[0], size[1] + 5)
bgQuad['posn'] = '{:d} {:d} {:d}'.format(2, 2 + oldSize[1] // 2, 0)
bgQuad['style'] = 'BgsPlayerCard'
bgQuad['substyle'] = 'BgPlayerCardSmall'
bgQuad['ScriptEvents'] = '1'
mainFrame.addChild(bgQuad)
ms = ManiaScript()
variables = [{'name' : 'Integer windowWidth', 'value' : str(int(size[0] * 160 / 64))}
]
ms.setContent(self.getManiaScript(variables))
mainFrame.addChild(ms)
return mainFrame
def getManiaScript(self, variables):
"""
\brief Return the maniascript code with global variables
"""
globalVariables = ''
for v in variables:
globalVariables += ('declare ' + v['name']
+ ' = ' + v['value']
+ ';' + linesep)
return '''
main()
{
''' + globalVariables + '''
declare Boolean moveOut;
declare CGameManialinkFrame mainFrame <=> (Page.MainFrame.Controls["mainFrame"] as CGameManialinkFrame);
while(True)
{
foreach(Event in PendingEvents)
{
switch(Event.Type)
{
case CGameManialinkScriptEvent::Type::MouseClick:
{
moveOut = !moveOut;
}
default:
{
//log(Event.Type);
//log(Event.ControlId);
}
}
}
if(moveOut)
{
if(mainFrame.PosnX > 160 - windowWidth)
mainFrame.PosnX -= 1;
}
else
{
if(mainFrame.PosnX < 150)
mainFrame.PosnX += 1;
}
yield;
//sleep(20);
}
}
'''