|
1
|
|
|
# |
|
2
|
|
|
# Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/] |
|
3
|
|
|
# |
|
4
|
|
|
# This file is part of Pygame.UI. |
|
5
|
|
|
# |
|
6
|
|
|
# Pygame.UI is free software; you can redistribute it and/or modify |
|
7
|
|
|
# it under the terms of the Lesser GNU General Public License as published by |
|
8
|
|
|
# the Free Software Foundation; either version 2.1 of the License, or |
|
9
|
|
|
# (at your option) any later version. |
|
10
|
|
|
# |
|
11
|
|
|
# Pygame.UI is distributed in the hope that it will be useful, |
|
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
# Lesser GNU General Public License for more details. |
|
15
|
|
|
# |
|
16
|
|
|
# You should have received a copy of the Lesser GNU General Public License |
|
17
|
|
|
# along with Pygame.UI; if not, write to the Free Software |
|
18
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
19
|
|
|
# |
|
20
|
|
|
import pygame |
|
21
|
|
|
|
|
22
|
|
|
import Const |
|
23
|
|
|
from Widget import registerWidget |
|
24
|
|
|
from MetaWidget import MetaWidget |
|
25
|
|
|
from Scrollbar import Scrollbar |
|
26
|
|
|
from Button import Button |
|
27
|
|
|
|
|
28
|
|
|
class ButtonArray(MetaWidget): |
|
29
|
|
|
|
|
30
|
|
|
def __init__(self, parent, **kwargs): |
|
31
|
|
|
MetaWidget.__init__(self, parent) |
|
32
|
|
|
# data |
|
33
|
|
|
self.items = [] |
|
34
|
|
|
self.buttons = [] |
|
35
|
|
|
self.action = None |
|
36
|
|
|
self.rmbAction = None |
|
37
|
|
|
self.hoverAction = None |
|
38
|
|
|
self.selected = None |
|
39
|
|
|
self.selectedButton = None |
|
40
|
|
|
self.highlighted = None |
|
41
|
|
|
self.buttonSize = (1, 1) |
|
42
|
|
|
self.rows = 0 |
|
43
|
|
|
self.columns = 0 |
|
44
|
|
|
self.showSlider = 1 |
|
45
|
|
|
# flags |
|
46
|
|
|
self.processKWArguments(kwargs) |
|
47
|
|
|
parent.registerWidget(self) |
|
48
|
|
|
# create widgets |
|
49
|
|
|
self.vertScrollbar = Scrollbar(self, action = 'onScroll') |
|
50
|
|
|
self.vertScrollbar.subscribeAction('*', self) |
|
51
|
|
|
|
|
52
|
|
|
def layoutWidgets(self): |
|
53
|
|
|
gx, gy = self.theme.getGridParams() |
|
54
|
|
|
r = self.rect |
|
55
|
|
|
self.vertScrollbar.rect = pygame.Rect(r.width - gx, 0, gx, r.height) |
|
56
|
|
|
self.vertScrollbar.visible = self.showSlider |
|
57
|
|
|
self.labels = [] |
|
58
|
|
|
bwidth, bheight = self.buttonSize |
|
59
|
|
|
self.rows = r.height / gy / bheight |
|
60
|
|
|
if self.showSlider: |
|
61
|
|
|
self.columns = (r.width - gx) / gx / bwidth |
|
62
|
|
|
else: |
|
63
|
|
|
self.columns = r.width / gx / bwidth |
|
64
|
|
|
for row in xrange(0, self.rows): |
|
|
|
|
|
|
65
|
|
|
for column in xrange(0, self.columns): |
|
66
|
|
|
x = column * gx * bwidth |
|
67
|
|
|
y = row * gy * bheight |
|
68
|
|
|
button = Button(self, action = 'onButtonPressed', rmbAction = 'onRButtonPressed', hoverAction = 'onButtonHighlighted', toggle = 1) |
|
69
|
|
|
button.subscribeAction('*', self) |
|
70
|
|
|
button.rect = pygame.Rect(x, y, bwidth * gx, bheight * gy) |
|
71
|
|
|
self.buttons.append(button) |
|
72
|
|
|
self.vertScrollbar.slider.position = 0 |
|
73
|
|
|
self.vertScrollbar.slider.min = 0 |
|
74
|
|
|
self.vertScrollbar.slider.shown = self.rows |
|
75
|
|
|
self.itemsChanged() |
|
76
|
|
|
|
|
77
|
|
|
def onScroll(self, widget, action, data): |
|
78
|
|
|
self.itemsChanged() |
|
79
|
|
|
|
|
80
|
|
|
def clearSelection(self): |
|
81
|
|
|
if self.selectedButton: |
|
82
|
|
|
self.selectedButton.pressed = 0 |
|
83
|
|
|
self.selectedButton = None |
|
84
|
|
|
|
|
85
|
|
|
def selectItem(self, item): |
|
86
|
|
|
if not item: |
|
87
|
|
|
self.clearSelection() |
|
88
|
|
|
elif item != self.selected: |
|
89
|
|
|
self.clearSelection() |
|
90
|
|
|
self.selected = item |
|
91
|
|
|
self.itemsChanged() |
|
92
|
|
|
|
|
93
|
|
|
def onButtonPressed(self, widget, action, data): |
|
94
|
|
|
self.clearSelection() |
|
95
|
|
|
if widget.pressed: |
|
96
|
|
|
self.selected = widget.data |
|
97
|
|
|
self.selectedButton = widget |
|
98
|
|
|
else: |
|
99
|
|
|
self.selected = None |
|
100
|
|
|
self.processAction(self.action, self.selected) |
|
101
|
|
|
|
|
102
|
|
|
def onRButtonPressed(self, widget, action, data): |
|
103
|
|
|
self.processAction(self.rmbAction, widget.data) |
|
104
|
|
|
|
|
105
|
|
|
def onButtonHighlighted(self, widget, action, data): |
|
106
|
|
|
self.highlighted = widget.data |
|
107
|
|
|
self.processAction(self.hoverAction, self.highlighted if data else None) |
|
108
|
|
|
|
|
109
|
|
|
# redirect mouse wheel events to the scrollbar |
|
110
|
|
|
def processMWUp(self, evt): |
|
111
|
|
|
if self.vertScrollbar: |
|
112
|
|
|
return self.vertScrollbar.processMWUp(evt) |
|
113
|
|
|
|
|
114
|
|
|
def processMWDown(self, evt): |
|
115
|
|
|
if self.vertScrollbar: |
|
116
|
|
|
return self.vertScrollbar.processMWDown(evt) |
|
117
|
|
|
|
|
118
|
|
|
def itemsChanged(self): |
|
119
|
|
|
if self.columns == 0 or self.rows == 0: |
|
120
|
|
|
return |
|
121
|
|
|
if self.items: |
|
122
|
|
|
self.vertScrollbar.slider.max = len(self.items) / self.columns + 1 |
|
123
|
|
|
else: |
|
124
|
|
|
self.vertScrollbar.slider.max = 1 |
|
125
|
|
|
|
|
126
|
|
|
index = 0 |
|
127
|
|
|
pos = int(self.vertScrollbar.slider.position) * self.columns |
|
128
|
|
|
# TODO should be changed |
|
129
|
|
|
if pos >= len(self.items): pos = len(self.items) - 1 |
|
130
|
|
|
for item in self.items[pos:]: |
|
131
|
|
|
if index < len(self.buttons): |
|
132
|
|
|
button = self.buttons[index] |
|
133
|
|
|
button.text = item.text |
|
134
|
|
|
button.align = item.align |
|
135
|
|
|
button.icons = item.icons |
|
136
|
|
|
button.tooltip = item.tooltip |
|
137
|
|
|
button.tooltipTitle = item.tooltipTitle |
|
138
|
|
|
button.statustip = item.statustip |
|
139
|
|
|
button.font = item.font |
|
140
|
|
|
button.data = item |
|
141
|
|
|
button.foreground = item.foreground |
|
142
|
|
|
button.background = item.background |
|
143
|
|
|
button.enabled = item.enabled |
|
144
|
|
|
if not button.visible: button.visible = 1 |
|
145
|
|
|
if item == self.selected: |
|
146
|
|
|
button.pressed = 1 |
|
147
|
|
|
# do not trigger auto update |
|
148
|
|
|
self.__dict__['selectedButton'] = button |
|
149
|
|
|
else: |
|
150
|
|
|
button.pressed = 0 |
|
151
|
|
|
else: |
|
152
|
|
|
break |
|
153
|
|
|
index += 1 |
|
154
|
|
|
while index < len(self.buttons): |
|
155
|
|
|
button = self.buttons[index] |
|
156
|
|
|
button.text = None |
|
157
|
|
|
button.icons = None |
|
158
|
|
|
button.data = None |
|
159
|
|
|
button.tooltip = None |
|
160
|
|
|
button.statustip = None |
|
161
|
|
|
button.pressed = 0 |
|
162
|
|
|
button.enabled = 0 |
|
163
|
|
|
button.foreground = None |
|
164
|
|
|
button.background = None |
|
165
|
|
|
if button.visible: button.visible = 0 |
|
166
|
|
|
index += 1 |
|
167
|
|
|
self.parent.redraw(self) |
|
168
|
|
|
|
|
169
|
|
|
def drawMetaWidget(self, surface): |
|
170
|
|
|
return self.theme.drawListbox(surface, self) |
|
171
|
|
|
|
|
172
|
|
|
registerWidget(ButtonArray, 'buttonarray') |
|
173
|
|
|
|