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

CustomCheckpointWidget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
eloc 8
nc 1
nop 6
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 eXpansion\Framework\Gui\Components\uiLabel;
10
use FML\Controls\Frame;
11
use FML\Controls\Quad;
12
use FML\Script\ScriptLabel;
13
14
class CustomCheckpointWidget extends WidgetFactory
15
{
16
17
    public function __construct(
18
        $name,
19
        $sizeX,
20
        $sizeY,
21
        $posX,
22
        $posY,
23
        WidgetFactoryContext $context
24
    ) {
25
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
26
    }
27
28
    /**
29
     * @param ManialinkInterface|Widget $manialink
30
     */
31
    protected function createContent(ManialinkInterface $manialink)
32
    {
33
        parent::createContent($manialink);
34
35
        $frame = Frame::create("Frame_Main");
36
        $manialink->addChild($frame);
37
38
        $lbl = $this->uiFactory->createLabel("Cp: 0  00:00.000", uiLabel::TYPE_TITLE, "CurrentTime");
39
        $lbl->setPosition(0, -70)->setAlign("center", "center2")->setTextSize(4);
40
41
        $frame->addChild($lbl);
42
43
        $quad = Quad::create("quad_bg");
44
        $quad->setPosition(0, -70)->setSize(44, 8)->setBackgroundColor("000")
45
            ->setOpacity(0.5)->setAlign("center", "center2");
46
        $frame->addChild($quad);
47
48
        $quad = Quad::create("quad_top");
49
        $quad->setPosition(0, -80)->setSize(400, 33)
50
            ->setStyles("BgsPlayerCard", "BgRacePlayerLine")
51
            ->setColorize("777")->setAlign("center", "center")->setRotation(180);
52
        $frame->addChild($quad);
53
54
55
        $manialink->getFmlManialink()->getScript()->addScriptFunction("", <<<EOL
56
        Text TimeToText(Integer intime) {
57
                declare time = MathLib::Abs(intime);
58
                declare Integer cent = time % 1000;	
59
                declare Integer sec = (time / 1000) % 60;
60
                declare Integer min = time / 60000;
61
                declare Integer hour = time / 3600000;
62
                declare Text sign = "";
63
                if (intime < 0)  {
64
                    sign = "-";
65
                }
66
                
67
                if (hour > 0) {
68
                    return sign ^ hour ^ ":" ^ TextLib::FormatInteger(min,2) ^ ":" ^ TextLib::FormatInteger(sec,2) ^ "." ^ TextLib::FormatInteger(cent,3);
69
                } 
70
                return sign ^ TextLib::FormatInteger(min,2) ^ ":" ^ TextLib::FormatInteger(sec,2) ^ "." ^ TextLib::FormatInteger(cent,3);                                
71
            }
72
EOL
73
        );
74
75
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit, <<<EOL
76
            declare CMlLabel CheckPointLabel = (Page.GetFirstChild("CurrentTime") as CMlLabel);
77
            declare CMlQuad TopBg = (Page.GetFirstChild("quad_top") as CMlQuad);
78
            declare CMlQuad Bg = (Page.GetFirstChild("quad_bg") as CMlQuad);                                      
79
            declare CMlFrame Frame = (Page.GetFirstChild("Frame_Main") as CMlFrame);
80
            
81
            declare IsIntro = (
82
                UI.UISequence == CUIConfig::EUISequence::Intro ||
83
                UI.UISequence == CUIConfig::EUISequence::RollingBackgroundIntro ||
84
                UI.UISequence == CUIConfig::EUISequence::Outro
85
            );              
86
EOL
87
        );
88
89
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop, <<<EOL
90
            if (Frame.Visible && IsIntro) {
91
                Frame.Visible = False;
92
              
93
            } else if (!Frame.Visible && !IsIntro) {            
94
                Frame.Visible = True;                        
95
            }                      
96
          foreach (RaceEvent in RaceEvents) {
97
                if (GUIPlayer == RaceEvent.Player && RaceEvent.Type == CTmRaceClientEvent::EType::Respawn) {
98
                     if (InputPlayer.RaceState == CTmMlPlayer::ERaceState::BeforeStart) {                     
99
                        CheckPointLabel.Value = "Cp: 0  00:00.000";
100
                        Bg.BgColor = <0., 0., 0.>;
101
                        TopBg.Hide();
102
                     }
103
                }             
104
                
105
                if (GUIPlayer == RaceEvent.Player && RaceEvent.Type == CTmRaceClientEvent::EType::WayPoint) {
106
                  
107
                    declare CTmResult Score <=> RaceEvent.Player.Score.BestLap;
108
                   // TopBg.Show();                    
109
                    if (Score.Checkpoints.existskey(RaceEvent.CheckpointInLap) ) {
110
                        CheckPointLabel.Value = "Cp: " ^(RaceEvent.CheckpointInLap+1) ^ "  " ^ TimeToText(RaceEvent
111
                        .LapTime - Score.Checkpoints[RaceEvent.CheckpointInLap]);
112
                        if (RaceEvent.LapTime < Score.Checkpoints[RaceEvent.CheckpointInLap]) {
113
                            Bg.BgColor = <0., 0., 1.>;
114
                            TopBg.Colorize = <0., 0., 1.>;
115
                        } else {
116
                            Bg.BgColor = <1., 0., 0.>;
117
                            TopBg.Colorize = <1., 0., 0.>;
118
                        }
119
                    } else {
120
                       CheckPointLabel.Value = "Cp: " ^(RaceEvent.CheckpointInLap+1) ^ "  " ^ TimeToText(RaceEvent
121
                        .LapTime);
122
                       TopBg.Colorize = <0.7, 0.7, 0.7>;
123
                    }
124
                                                                                               
125
                }
126
          }
127
EOL
128
        );
129
    }
130
131
    protected function updateContent(ManialinkInterface $manialink)
132
    {
133
        parent::updateContent($manialink); // TODO: Change the autogenerated stub
134
    }
135
136
137
}
138