1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace ManiaLive\Gui;
13:
14: use ManiaLib\Gui\Drawable;
15: use ManiaLib\Gui\Manialink;
16: use ManiaLib\Gui\Layouts\AbstractLayout;
17:
18: 19: 20: 21:
22: abstract class Control extends Container implements Drawable, Containable
23: {
24: protected $layout = null;
25:
26: private $parents = array();
27: private $linksDisabled = false;
28:
29: function disableLinks()
30: {
31: $this->linksDisabled = true;
32: }
33:
34: function enableLinks()
35: {
36: $this->linksDisabled = false;
37: }
38:
39: function areLinksDisabled()
40: {
41: return $this->linksDisabled;
42: }
43:
44: 45: 46: 47:
48: function getLayout()
49: {
50: return $this->layout;
51: }
52:
53: 54: 55: 56:
57: function setLayout(AbstractLayout $layout)
58: {
59: $this->layout = $layout;
60: }
61:
62: 63: 64: 65:
66: function onIsAdded(Container $target)
67: {
68: $this->parents[spl_object_hash($target)] = $target;
69: }
70:
71: 72: 73: 74:
75: function onIsRemoved(Container $target)
76: {
77: unset($this->parents[spl_object_hash($target)]);
78: }
79:
80: 81: 82:
83: final function redraw()
84: {
85: foreach($this->parents as $parent)
86: $parent->redraw();
87: }
88:
89: 90: 91: 92:
93: protected function onDraw() {}
94:
95: 96: 97: 98:
99: final function save()
100: {
101: if(!$this->visible)
102: return;
103:
104: $posX = $this->posX;
105: $posY = $this->posY;
106:
107:
108: $layout = end(Manialink::$parentLayouts);
109: if($layout instanceof AbstractLayout)
110: {
111: $layout->preFilter($this);
112:
113: $posX += $layout->xIndex;
114: $posY += $layout->yIndex;
115: }
116: $this->onDraw();
117:
118:
119: if($this->halign == 'right')
120: $posX -= $this->getRealSizeX();
121: else if($this->halign == 'center')
122: $posX -= $this->getRealSizeX() / 2;
123:
124:
125: if($this->valign == 'bottom')
126: $posY += $this->getRealSizeY();
127: else if($this->valign == 'center')
128: $posY += $this->getRealSizeY() / 2;
129:
130:
131:
132: Manialink::beginFrame($posX, $posY, $this->posZ, $this->scale);
133: if($this->layout)
134: Manialink::beginFrame(0, 0, 0, null, $this->layout ? clone $this->layout : null);
135: if($this->linksDisabled)
136: Manialink::disableLinks();
137:
138:
139: $zCur = 0;
140: foreach($this->getComponents() as $component)
141: {
142: $component->setPosZ($zCur);
143: if($component instanceof Control)
144: $zCur += $component->save();
145: else
146: {
147:
148: if($this->layout)
149: {
150: $oldX = $component->getPosX();
151: $oldY = $component->getPosY();
152: $component->save();
153: $component->setPosition($oldX, $oldY);
154: }
155: else
156: $component->save();
157: $zCur += Window::Z_OFFSET;
158: }
159: }
160:
161: if($this->linksDisabled)
162: Manialink::enableLinks();
163: if($this->layout)
164: Manialink::endFrame();
165: Manialink::endFrame();
166:
167:
168: if($layout instanceof AbstractLayout)
169: $layout->postFilter($this);
170:
171: return $zCur;
172: }
173:
174: 175: 176: 177:
178: function destroy()
179: {
180: $this->clearComponents();
181: foreach($this->parents as $parent)
182: $parent->removeComponent($this);
183: $this->parents = array();
184: $this->layout = null;
185:
186: foreach($this->actions as $action)
187: ActionHandler::getInstance()->deleteAction($action);
188: }
189:
190:
191: private $actions = array();
192:
193: function createAction($callback)
194: {
195: $action = call_user_func_array(array(ActionHandler::getInstance(), 'createAction'), func_get_args());
196: $this->actions[] = $action;
197: return $action;
198: }
199: }
200:
201: ?>