Completed
Pull Request — master (#175)
by
unknown
05:05
created

CustomSpeedWidget::updateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Bundle\CustomUi\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 FML\Controls\Frame;
10
use FML\Controls\Quad;
11
use FML\Script\ScriptLabel;
12
13
class CustomSpeedWidget extends WidgetFactory
14
{
15
16
    public function __construct(
17
        $name,
18
        $sizeX,
19
        $sizeY,
20
        $posX,
21
        $posY,
22
        WidgetFactoryContext $context
23
    ) {
24
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
25
    }
26
27
    /**
28
     * @param ManialinkInterface|Widget $manialink
29
     */
30
    protected function createContent(ManialinkInterface $manialink)
31
    {
32
        parent::createContent($manialink);
33
34
        $frame = Frame::create("Frame_Main");
35
        $frame->setScale(1);
36
        $manialink->addChild($frame);
37
38
        $lbl = $this->uiFactory->createLabel("0:00");
39
        $lbl->setPosition(0, 6)->setAlign("center", "center")
40
            ->setSize(20, 4)->setTextSize(2)->setTextFont("RajdhaniMono")->setId("Label_Countdown");
41
        $frame->addChild($lbl);
42
43
        $lbl = $this->uiFactory->createLabel("100");
44
        $lbl->setPosition(2, 0)->setAlign("right", "center")
45
            ->setSize(20, 4)->setTextSize(2)->setTextFont("RajdhaniMono")->setId("Label_Speed");
46
        $frame->addChild($lbl);
47
48
        $lbl = $this->uiFactory->createLabel("km/h");
49
        $lbl->setPosition(5, 0)->setAlign("center", "center")
50
            ->setSize(5, 4)->setTextSize(1);
51
        $frame->addChild($lbl);
52
53
        $lbl = $this->uiFactory->createLabel("1");
54
        $lbl->setPosition(-6, -8)->setAlign("right", "center")
55
            ->setSize(20, 4)->setTextSize(1)->setTextFont("RajdhaniMono")->setId("Label_Gear");
56
        $frame->addChild($lbl);
57
58
        $lbl = $this->uiFactory->createLabel("gear");
59
        $lbl->setPosition(-7, -5)->setAlign("center", "center")
60
            ->setSize(5, 4)->setTextSize(1);
61
        $frame->addChild($lbl);
62
63
        $lbl = $this->uiFactory->createLabel("0");
64
        $lbl->setPosition(10, -8)->setAlign("right", "center")
65
            ->setSize(20, 5)->setTextSize(1)->setTextFont("RajdhaniMono")->setId("Label_Distance");
66
        $frame->addChild($lbl);
67
68
        $lbl = $this->uiFactory->createLabel("distance");
69
        $lbl->setPosition(5, -5)->setAlign("center", "center")
70
            ->setSize(15, 4)->setTextSize(1);
71
        $frame->addChild($lbl);
72
73
        $rpm1 = Frame::create();
74
        $rpm1->setSize(21, 42)->setAlign("right", "center");
75
        $quad = Quad::create();
76
        $quad->setSize(21, 42)->setAlign("right", "center")->setId("Quad_RPM1");
77
        $quad->setImageUrl('file://Media/MEDIABROWSER_HiddenResources/Common/Images/Ingame/NewSpeed-gauge1.dds');
78
        $quad->setColorize("fff")->setRotation(-180);
79
        $rpm1->addChild($quad);
80
        $frame->addChild($rpm1);
81
82
        $rpm2 = Frame::create();
83
        $rpm2->setSize(21, 42)->setAlign("left", "center");
84
        $quad = Quad::create();
85
        $quad->setSize(21, 42)->setAlign("right", "center")->setId("Quad_RPM2");
86
        $quad->setImageUrl('file://Media/MEDIABROWSER_HiddenResources/Common/Images/Ingame/NewSpeed-gauge1.dds');
87
        $quad->setColorize("fff")->setRotation(0);
88
        $rpm2->addChild($quad);
89
        $frame->addChild($rpm2);
90
91
92
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit, <<<EOL
93
            declare CMlFrame Frame = (Page.GetFirstChild("Frame_Main") as CMlFrame);
94
            declare CMlQuad RPM1 = (Page.GetFirstChild("Quad_RPM1") as CMlQuad);
95
            declare CMlQuad RPM2 = (Page.GetFirstChild("Quad_RPM2") as CMlQuad);
96
            declare CMlLabel Speed = (Page.GetFirstChild("Label_Speed") as CMlLabel);
97
            declare CMlLabel Distance = (Page.GetFirstChild("Label_Distance") as CMlLabel);
98
            declare CMlLabel Gear = (Page.GetFirstChild("Label_Gear") as CMlLabel);
99
            declare CMlLabel Countdown = (Page.GetFirstChild("Label_Countdown") as CMlLabel);
100
            
101
            declare netread Integer Net_LibUI_SettingsUpdate for Teams[0];
102
            declare netread Text[Text] Net_LibUI_Settings for Teams[0];
103
  
104
            declare PrevSettingsUpdate = -1;
105
            declare CutOffTimeLimit = -1;
106
              
107
            declare IsIntro = (
108
                UI.UISequence == CUIConfig::EUISequence::Intro ||
109
                UI.UISequence == CUIConfig::EUISequence::RollingBackgroundIntro ||
110
                UI.UISequence == CUIConfig::EUISequence::Outro
111
            );        
112
                                           
113
EOL
114
        );
115
116
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop,
117
            <<<EOL
118
    
119
            if (Frame.Visible && IsIntro) {
120
                Frame.Visible = False;
121
            } else if (!Frame.Visible && !IsIntro) {            
122
                Frame.Visible = True;                        
123
            }
124
            if (PrevSettingsUpdate != Net_LibUI_SettingsUpdate) {
125
                  PrevSettingsUpdate = Net_LibUI_SettingsUpdate;
126
                  foreach (SettingName => SettingValue in Net_LibUI_Settings) {
127
                        switch (SettingName) {                  
128
                              case "TMUIModule_Countdown_CutOffTimeLimit": {
129
                                  CutOffTimeLimit = TextLib::ToInteger(SettingValue);
130
                              }
131
                        }
132
                  }              
133
            }
134
           
135
            if (CutOffTimeLimit > 0) {
136
                if (!Frame.Visible) Frame.Visible = True;
137
            } else if (Frame.Visible) {
138
                Frame.Visible = False;
139
            }
140
           
141
           if (CutOffTimeLimit >= GameTime) Countdown.Value = TextLib::TimeToText(CutOffTimeLimit - GameTime + 1, False);
142
           else Countdown.Value = TextLib::TimeToText(0);
143
144
           if (CutOffTimeLimit - GameTime > 30000) { 
145
                Countdown.TextColor = <1., 1., 1.>;
146
           } else {
147
                Countdown.TextColor = <1., 0.12, 0.12>;
148
           }
149
           
150
     
151
            if (GUIPlayer != Null && GUIPlayer.IsSpawned) {
152
                declare Real speed = GUIPlayer.DisplaySpeed / 1000.;		                
153
                Gear.Value= "" ^ GUIPlayer.EngineCurGear;
154
                Distance.Value= "" ^ MathLib::NearestInteger(GUIPlayer.Distance);		
155
                Speed.Value = "" ^ GUIPlayer.DisplaySpeed;
156
                declare Real rpm = GUIPlayer.EngineRpm / 11000;
157
                RPM1.RelativeRotation  = -180 + (MathLib::Clamp(rpm * 2. , 0., 1.) * 180);
158
                RPM2.RelativeRotation  = (MathLib::Clamp((rpm -0.5)*2. ,0., 1.) * 180);
159
            } else {
160
                Speed.Value = "0";
161
                Gear.Value= "N";
162
                Distance.Value= "0";		
163
                RPM1.RelativeRotation  = -180.;
164
                RPM2.RelativeRotation  = 0.;
165
            }                        
166
167
EOL
168
        );
169
170
    }
171
172
    protected function updateContent(ManialinkInterface $manialink)
173
    {
174
        parent::updateContent($manialink); // TODO: Change the autogenerated stub
175
    }
176
177
178
}
179