1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace ManiaLive\Gui\Windows;
13:
14: use ManiaLib\Gui\Elements\Quad;
15:
16: final class Shortkey extends \ManiaLive\Gui\Window
17: {
18: private $onKey = array();
19:
20: const F5 = 1;
21: const F6 = 2;
22: const F7 = 3;
23: const F8 = 4;
24:
25: protected function onConstruct()
26: {
27: $this->onKey = array(
28: self::F5 => null,
29: self::F6 => null,
30: self::F7 => null,
31: self::F8 => null
32: );
33:
34: $ui = new Quad();
35: $ui->setPosition(400, 400);
36: $ui->setStyle(null);
37: $ui->setActionKey(self::F5);
38: $ui->setAction($this->createAction(array($this, 'onKey'), self::F5));
39: $this->addComponent($ui);
40:
41: $ui = new Quad();
42: $ui->setPosition(400, 400);
43: $ui->setStyle(null);
44: $ui->setActionKey(self::F6);
45: $ui->setAction($this->createAction(array($this, 'onKey'), self::F6));
46: $this->addComponent($ui);
47:
48: $ui = new Quad();
49: $ui->setPosition(400, 400);
50: $ui->setStyle(null);
51: $ui->setActionKey(self::F7);
52: $ui->setAction($this->createAction(array($this, 'onKey'), self::F7));
53: $this->addComponent($ui);
54:
55: $ui = new Quad();
56: $ui->setPosition(400, 400);
57: $ui->setStyle(null);
58: $ui->setActionKey(self::F8);
59: $ui->setAction($this->createAction(array($this, 'onKey'), self::F8));
60: $this->addComponent($ui);
61: }
62:
63: function addCallback($key, $callback)
64: {
65: if(!is_array($callback) || !is_callable($callback))
66: throw new \InvalidArgumentException('Invalid callback');
67: if(isset($this->onKey[$key]) && $this->onKey[$key] != null)
68: throw new \Exception('This key already has a callback');
69: $this->onKey[$key] = $callback;
70: }
71:
72:
73:
74:
75: function removeCallback($key)
76: {
77: if($key != self::F8)
78: $this->onKey[$key] = null;
79: }
80:
81: function onKey($login, $key)
82: {
83: if(isset($this->onKey[$key]) && $this->onKey[$key] != null)
84: call_user_func($this->onKey[$key], $login);
85: }
86: }