1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace ManiaLive\Gui\Controls;
13:
14: use ManiaLib\Gui\Component;
15: use ManiaLib\Gui\Elements\Icons64x64_1;
16: use ManiaLib\Gui\Elements\Label;
17: use ManiaLib\Gui\Layouts\Column;
18:
19: 20: 21: 22: 23: 24: 25:
26: class extends \ManiaLive\Gui\Control
27: {
28: private $buttonNext;
29: private $buttonPrev;
30: private $container;
31: private $label;
32:
33: private $actionNext;
34: private $actionPrev;
35:
36: private $items;
37: private $pages;
38: private $currentPage;
39:
40: private $stretchContentX;
41: private $needRefresh;
42:
43: function __construct()
44: {
45: $this->items = array();
46: $this->currentPage = 0;
47: $this->stretchContentX = false;
48: $this->needRefresh = false;
49:
50: $this->actionNext = $this->createAction(array($this, 'nextPage'));
51: $this->actionPrev = $this->createAction(array($this, 'previousPage'));
52:
53: $this->buttonNext = new Icons64x64_1(9);
54: $this->buttonNext->setSubStyle(Icons64x64_1::ArrowNext);
55: $this->addComponent($this->buttonNext);
56:
57: $this->buttonPrev = new Icons64x64_1(9);
58: $this->buttonPrev->setSubStyle(Icons64x64_1::ArrowPrev);
59: $this->addComponent($this->buttonPrev);
60:
61: $this->container = new Frame(2, 0, new Column(0, 0, Column::DIRECTION_DOWN));
62: $this->addComponent($this->container);
63:
64: $this->label = new Label();
65: $this->label->setHalign('center');
66: $this->addComponent($this->label);
67: }
68:
69: function addItem(Component $item)
70: {
71: $this->items[spl_object_hash($item)] = $item;
72: $this->needRefresh = true;
73: }
74:
75: function removeItem(Component $item)
76: {
77: unset($this->items[spl_object_hash($item)]);
78: $this->needRefresh = true;
79: }
80:
81: function clearItems()
82: {
83: $this->items = array();
84: $this->needRefresh = true;
85: }
86:
87: function orderfromTopToBottom()
88: {
89: $this->container->getLayout()->setDirection($this->direction);
90: $this->container->setPosY(0);
91: $this->needRefresh = true;
92: }
93:
94: function orderFromBottomToTop()
95: {
96: $this->container->getLayout()->setDirection($this->direction);
97: $this->container->setPosY(5.3 - $this->sizeY);
98: $this->needRefresh = true;
99: }
100:
101: function setStretchContentX($stretch)
102: {
103: $this->stretchContentX = $stretch;
104: }
105:
106: function getPages()
107: {
108: return count($this->pages);
109: }
110:
111: function nextPage($login)
112: {
113: $this->currentPage++;
114: $this->redraw();
115: }
116:
117: function previousPage($login)
118: {
119: $this->currentPage--;
120: $this->redraw();
121: }
122:
123: protected function onResize($oldX, $oldY)
124: {
125: $this->buttonPrev->setPosition(0, 5 - $this->sizeY);
126: $this->buttonNext->setPosition($this->sizeX - 10, 5 - $this->sizeY);
127: $this->label->setPosition($this->sizeX / 2, 1.5 - $this->sizeY);
128: if($this->container->getPosY())
129: $this->container->setPosY(5.3 - $this->sizeY);
130: }
131:
132: function onDraw()
133: {
134: if($this->needRefresh)
135: $this->refreshPages();
136:
137:
138: $this->container->clearComponents();
139:
140: if(isset($this->pages[$this->currentPage]))
141: {
142: foreach($this->pages[$this->currentPage] as $item)
143: {
144: if($this->stretchContentX)
145: $item->setSizeX($this->sizeX - 4);
146: $this->container->addComponent($item);
147: }
148: }
149:
150:
151: $this->label->setText(($this->currentPage + 1).' / '.count($this->pages));
152:
153:
154: if($this->currentPage <= 0)
155: {
156: $this->buttonPrev->setAction(null);
157: $this->buttonPrev->setSubStyle(Icons64x64_1::ClipPause);
158: }
159: else
160: {
161: $this->buttonPrev->setSubStyle(Icons64x64_1::ArrowPrev);
162: $this->buttonPrev->setAction($this->actionPrev);
163: }
164:
165: if($this->currentPage >= count($this->pages) - 1)
166: {
167: $this->buttonNext->setAction(null);
168: $this->buttonNext->setSubStyle(Icons64x64_1::ClipPause);
169: }
170: else
171: {
172: $this->buttonNext->setAction($this->actionNext);
173: $this->buttonNext->setSubStyle(Icons64x64_1::ArrowNext);
174: }
175: }
176:
177: private function refreshPages()
178: {
179: $current = null;
180: if(isset($this->pages[$this->currentPage]) && isset($this->pages[$this->currentPage][0]))
181: $current = $this->pages[$this->currentPage][0];
182:
183: $this->pages = array();
184: $currentSizeY = 0;
185: $maxSizeY = $this->sizeY - 5.3;
186: $pageCount = 0;
187:
188: foreach($this->items as $item)
189: {
190: $currentSizeY += $item->getSizeY();
191: if($currentSizeY > $maxSizeY)
192: {
193: $currentSizeY = $item->getSizeY();
194: ++$pageCount;
195: }
196: $this->pages[$pageCount][] = $item;
197:
198: if($current === $item)
199: $this->currentPage = $pageCount;
200: }
201: }
202:
203: function destroy()
204: {
205: $this->items = null;
206: $this->pages = null;
207:
208: parent::destroy();
209: }
210: }
211:
212: ?>