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.bar = Scrollbar(self, action = 'onScroll') |
||
50 | self.bar.subscribeAction('*', self) |
||
51 | |||
52 | def layoutWidgets(self): |
||
53 | gx, gy = self.theme.getGridParams() |
||
54 | r = self.rect |
||
55 | self.bar.rect = pygame.Rect(r.width - gx, 0, gx, r.height) |
||
56 | self.bar.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): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
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.bar.slider.position = 0 |
||
73 | self.bar.slider.min = 0 |
||
74 | self.bar.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 | def itemsChanged(self): |
||
110 | if self.columns == 0 or self.rows == 0: |
||
111 | return |
||
112 | if self.items: |
||
113 | self.bar.slider.max = len(self.items) / self.columns + 1 |
||
114 | else: |
||
115 | self.bar.slider.max = 1 |
||
116 | |||
117 | index = 0 |
||
118 | pos = int(self.bar.slider.position) * self.columns |
||
119 | # TODO should be changed |
||
120 | if pos >= len(self.items): pos = len(self.items) - 1 |
||
121 | for item in self.items[pos:]: |
||
122 | if index < len(self.buttons): |
||
123 | button = self.buttons[index] |
||
124 | button.text = item.text |
||
125 | button.align = item.align |
||
126 | button.icons = item.icons |
||
127 | button.tooltip = item.tooltip |
||
128 | button.tooltipTitle = item.tooltipTitle |
||
129 | button.statustip = item.statustip |
||
130 | button.font = item.font |
||
131 | button.data = item |
||
132 | button.foreground = item.foreground |
||
133 | button.background = item.background |
||
134 | button.enabled = item.enabled |
||
135 | if not button.visible: button.visible = 1 |
||
136 | if item == self.selected: |
||
137 | button.pressed = 1 |
||
138 | # do not trigger auto update |
||
139 | self.__dict__['selectedButton'] = button |
||
140 | else: |
||
141 | button.pressed = 0 |
||
142 | else: |
||
143 | break |
||
144 | index += 1 |
||
145 | while index < len(self.buttons): |
||
146 | button = self.buttons[index] |
||
147 | button.text = None |
||
148 | button.icons = None |
||
149 | button.data = None |
||
150 | button.tooltip = None |
||
151 | button.statustip = None |
||
152 | button.pressed = 0 |
||
153 | button.enabled = 0 |
||
154 | button.foreground = None |
||
155 | button.background = None |
||
156 | if button.visible: button.visible = 0 |
||
157 | index += 1 |
||
158 | self.parent.redraw(self) |
||
159 | |||
160 | def drawMetaWidget(self, surface): |
||
161 | return self.theme.drawListbox(surface, self) |
||
162 | |||
163 | registerWidget(ButtonArray, 'buttonarray') |
||
164 |