|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\WidgetBestCheckpoints\Plugins\Gui; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface; |
|
6
|
|
|
use eXpansion\Framework\Core\Model\Gui\Widget; |
|
7
|
|
|
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext; |
|
8
|
|
|
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory; |
|
9
|
|
|
use eXpansion\Framework\Gui\Builders\WidgetBackground; |
|
10
|
|
|
use eXpansion\Framework\Gui\Ui\Factory; |
|
11
|
|
|
use FML\Controls\Frame; |
|
12
|
|
|
use FML\Script\ScriptInclude; |
|
13
|
|
|
use FML\Script\ScriptLabel; |
|
14
|
|
|
|
|
15
|
|
|
class BestCheckpointsWidgetFactory extends WidgetFactory |
|
16
|
|
|
{ |
|
17
|
|
|
const rowCount = 3; |
|
18
|
|
|
const columnCount = 6; |
|
19
|
|
|
|
|
20
|
|
|
/*** |
|
21
|
|
|
* MenuFactory constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param $name |
|
24
|
|
|
* @param $sizeX |
|
25
|
|
|
* @param $sizeY |
|
26
|
|
|
* @param null $posX |
|
27
|
|
|
* @param null $posY |
|
28
|
|
|
* @param WidgetFactoryContext $context |
|
29
|
|
|
* @param Factory $uiFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
$name, |
|
33
|
|
|
$sizeX, |
|
34
|
|
|
$sizeY, |
|
35
|
|
|
$posX, |
|
36
|
|
|
$posY, |
|
37
|
|
|
WidgetFactoryContext $context, |
|
38
|
|
|
Factory $uiFactory |
|
39
|
|
|
) { |
|
40
|
|
|
parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context); |
|
41
|
|
|
|
|
42
|
|
|
$this->uiFactory = $uiFactory; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Widget|ManialinkInterface $manialink |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function createContent(ManialinkInterface $manialink) |
|
49
|
|
|
{ |
|
50
|
|
|
$elementCount = 0; |
|
51
|
|
|
$rows = $this->uiFactory->createLayoutRow(0, 0, [], -1); |
|
52
|
|
|
|
|
53
|
|
|
for ($i = 0; $i < self::rowCount; $i++) { |
|
54
|
|
|
$elements = []; |
|
55
|
|
|
for ($c = 0; $c < self::columnCount; $c++) { |
|
56
|
|
|
$elements[] = $this->createColumnBox($elementCount); |
|
57
|
|
|
$elementCount++; |
|
58
|
|
|
} |
|
59
|
|
|
$line = $this->uiFactory->createLayoutLine(0, 0, $elements, 1); |
|
60
|
|
|
|
|
61
|
|
|
$rows->addChild($line); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$manialink->getFmlManialink()->getScript()->setScriptInclude(ScriptInclude::TextLib); |
|
|
|
|
|
|
65
|
|
|
$manialink->getFmlManialink()->getScript()->addScriptFunction("", |
|
|
|
|
|
|
66
|
|
|
<<<EOL |
|
67
|
|
|
|
|
68
|
|
|
Void UpdateCp(Integer _Index, Integer _Score, Text _Nickname, Boolean _Animate) { |
|
69
|
|
|
declare CMlLabel Label <=> (Page.GetFirstChild("Cp_"^ _Index) as CMlLabel); |
|
70
|
|
|
|
|
71
|
|
|
if (_Score == -1) { |
|
72
|
|
|
Label.Value = "\$fff\$o" ^ (_Index+1); |
|
73
|
|
|
} else { |
|
74
|
|
|
Label.Value = "\$fff\$o" ^ (_Index+1) ^ " \$o\$ff3" ^ TextLib::TimeToText(_Score, True) ^" \$fff "^ TextLib::StripFormatting(_Nickname); |
|
75
|
|
|
} |
|
76
|
|
|
if (_Animate) { |
|
77
|
|
|
AnimMgr.Add (Label, "<elem scale=\"1.5\" />", 350, CAnimManager::EAnimManagerEasing::ElasticOut); |
|
78
|
|
|
AnimMgr.AddChain (Label, "<elem scale=\"1.0\" />", 350, CAnimManager::EAnimManagerEasing::ElasticIn); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
Void HideCp(Integer _Index) { |
|
83
|
|
|
(Page.GetFirstChild("Cp_"^ _Index) as CMlLabel).Hide(); |
|
84
|
|
|
(Page.GetFirstChild("Bg_"^ _Index) as CMlFrame).Hide(); |
|
85
|
|
|
} |
|
86
|
|
|
EOL |
|
87
|
|
|
|
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit, |
|
|
|
|
|
|
91
|
|
|
<<<EOL |
|
92
|
|
|
declare Integer ElementCount for Page = $elementCount; |
|
93
|
|
|
declare Integer[Integer] BestCheckpoints for Page = Integer[Integer]; |
|
94
|
|
|
|
|
95
|
|
|
// clear |
|
96
|
|
|
for (i,0, (ElementCount-1)) { |
|
97
|
|
|
BestCheckpoints[i] = 99999999; |
|
98
|
|
|
UpdateCp(i, -1, "", False); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// hide checkpoints not needed |
|
102
|
|
|
for (i, (MapCheckpointPos.count+1), (ElementCount-1)) { |
|
103
|
|
|
HideCp(i); |
|
104
|
|
|
} |
|
105
|
|
|
EOL |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop, |
|
|
|
|
|
|
110
|
|
|
<<<EOL |
|
111
|
|
|
foreach (RaceEvent in RaceEvents) { |
|
112
|
|
|
if (RaceEvent.Type == CTmRaceClientEvent::EType::WayPoint) { |
|
113
|
|
|
if (RaceEvent.CheckpointInLap < ElementCount) { |
|
114
|
|
|
if (RaceEvent.LapTime < BestCheckpoints[RaceEvent.CheckpointInLap]) { |
|
115
|
|
|
BestCheckpoints[RaceEvent.CheckpointInLap] = RaceEvent.LapTime; |
|
116
|
|
|
UpdateCp(RaceEvent.CheckpointInLap, RaceEvent.LapTime, RaceEvent.Player.User.Name, True); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
EOL |
|
122
|
|
|
); |
|
123
|
|
|
$manialink->addChild($rows); |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param int $index |
|
130
|
|
|
* @return Frame |
|
131
|
|
|
*/ |
|
132
|
|
|
private function createColumnBox($index) |
|
133
|
|
|
{ |
|
134
|
|
|
$width = 40; |
|
135
|
|
|
$height = 5; |
|
136
|
|
|
|
|
137
|
|
|
$frame = Frame::create(); |
|
138
|
|
|
|
|
139
|
|
|
$label = $this->uiFactory->createLabel(); |
|
140
|
|
|
$label->setAlign("left", "center"); |
|
141
|
|
|
$label->setTextSize(1)->setPosition(1, -($height / 2)); |
|
142
|
|
|
$label->setSize($width, $height) |
|
143
|
|
|
->setId("Cp_".$index); |
|
144
|
|
|
$frame->addChild($label); |
|
145
|
|
|
|
|
146
|
|
|
$background = new WidgetBackground($width, $height); |
|
147
|
|
|
$background->setId("Bg_".$index); |
|
148
|
|
|
$frame->addChild($background); |
|
149
|
|
|
|
|
150
|
|
|
$frame->setSize($width, $height); |
|
151
|
|
|
|
|
152
|
|
|
return $frame; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
protected function updateContent(ManialinkInterface $manialink) |
|
156
|
|
|
{ |
|
157
|
|
|
parent::updateContent($manialink); // TODO: Change the autogenerated stub |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: