CustomCheckpointWidget   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 139
ccs 0
cts 105
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A createManialink() 0 4 1
B createContent() 0 106 1
A updateContent() 0 4 1
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\Model\UserGroups\Group;
9
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
10
use eXpansion\Framework\Gui\Components\Label;
11
use FML\Controls\Frame;
12
use FML\Controls\Quad;
13
use FML\Script\ScriptLabel;
14
15
class CustomCheckpointWidget extends WidgetFactory
16
{
17
18
    public function __construct(
19
        $name,
20
        $sizeX,
21
        $sizeY,
22
        $posX,
23
        $posY,
24
        WidgetFactoryContext $context
25
    ) {
26
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    protected function createManialink(Group $group, $hideable = true)
33
    {
34
        return parent::createManialink($group, false);
35
    }
36
37
    /**
38
     * @param ManialinkInterface|Widget $manialink
39
     */
40
    protected function createContent(ManialinkInterface $manialink)
41
    {
42
        parent::createContent($manialink);
43
44
        $frame = Frame::create("Frame_Main");
45
        $manialink->addChild($frame);
46
47
        $lbl = $this->uiFactory->createLabel("0  \$bcc-:--.---", Label::TYPE_TITLE, "CurrentTime");
48
        $lbl->setPosition(0, -70)->setAlign("center", "center2")->setTextSize(3);
49
        $frame->addChild($lbl);
50
51
        $quad = Quad::create("quad_bg");
52
        $quad->setPosition(0, -70)->setSize(35, 6)->setBackgroundColor("000")
53
            ->setOpacity(0.5)->setAlign("center", "center");
54
        $frame->addChild($quad);
55
56
        $manialink->getFmlManialink()->getScript()->addScriptFunction("", <<<EOL
57
            Text FormatSec(Real sec, Text color, Text highlite) {
58
                if (sec > 10.) {
59
                    return highlite ^ TextLib::FormatReal(sec,3,False,False);
60
                } 
61
                return color ^ 0 ^ highlite ^ TextLib::FormatReal(sec,3,False,False);                                
62
            }
63
            
64
            Text TimeToText(Integer intime) {
65
                declare Text highlite = "\$eff";
66
                declare Text color = "\$bcc";
67
                declare time = MathLib::Abs(intime);                	
68
                declare Integer cent = time % 1000;	
69
                declare Integer sec2 = (time / 1000) % 60;
70
                declare Real sec = 1. * sec2 + cent * 0.001;
71
                declare Integer min = (time / 60000) % 60;                                                
72
                declare Integer hour = time / 3600000;
73
                declare Text sign = "";
74
                if (intime < 0)  {
75
                    sign = "-";
76
                }
77
                
78
                if (hour > 0) {
79
                    return highlite ^ sign ^ hour ^ "'" ^ TextLib::FormatInteger(min,2) ^ ":" ^ FormatSec(sec, highlite,highlite);
80
                }
81
                
82
                if (min == 0) {
83
                    return color ^ sign ^ "0:" ^ FormatSec(sec, color, highlite);
84
                }
85
                                                            
86
                if (min > 10)  {
87
                   return highlite ^ sign ^ min ^ ":" ^ FormatSec(sec, highlite, highlite);
88
                } 
89
                
90
                return color ^ sign ^ 0 ^ highlite ^ min ^ ":" ^ FormatSec(sec, highlite, highlite);                  
91
                                                                     
92
            }
93
94
EOL
95
        );
96
97
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit, <<<EOL
98
            declare CMlLabel CheckPointLabel = (Page.GetFirstChild("CurrentTime") as CMlLabel);        
99
            declare CMlQuad Bg = (Page.GetFirstChild("quad_bg") as CMlQuad);                                      
100
            declare CMlFrame Frame = (Page.GetFirstChild("Frame_Main") as CMlFrame);
101
            
102
            declare IsIntro = (
103
                UI.UISequence != CUIConfig::EUISequence::Playing                
104
            );              
105
            
106
EOL
107
        );
108
109
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop, <<<EOL
110
            if (Frame.Visible && IsIntro) {
111
                Frame.Visible = False;
112
              
113
            } else if (!Frame.Visible && !IsIntro) {            
114
                Frame.Visible = True;                        
115
            }                      
116
               foreach (RaceEvent in RaceEvents) {
117
                if (GUIPlayer == RaceEvent.Player && RaceEvent.Type == CTmRaceClientEvent::EType::Respawn) {
118
                     if (InputPlayer.RaceState == CTmMlPlayer::ERaceState::BeforeStart) {                     
119
                        CheckPointLabel.Value = "0  \$bcc-:--.---";
120
                        Bg.BgColor = <0., 0., 0.>;                        
121
                     }
122
                }             
123
                
124
                if (GUIPlayer == RaceEvent.Player && RaceEvent.Type == CTmRaceClientEvent::EType::WayPoint) {
125
                  
126
                    declare CTmResult Score <=> RaceEvent.Player.Score.BestLap;
127
                   // TopBg.Show();                    
128
                    if (Score.Checkpoints.existskey(RaceEvent.CheckpointInLap) ) {
129
                        CheckPointLabel.Value = (RaceEvent.CheckpointInLap+1) ^ "  " ^ TimeToText(RaceEvent
130
                        .LapTime - Score.Checkpoints[RaceEvent.CheckpointInLap]);
131
                        if (RaceEvent.LapTime < Score.Checkpoints[RaceEvent.CheckpointInLap]) {
132
                            Bg.BgColor = <0., 0., 1.>;                            
133
                        } else {
134
                            Bg.BgColor = <1., 0., 0.>;                            
135
                        }
136
                    } else {
137
                       CheckPointLabel.Value = (RaceEvent.CheckpointInLap+1) ^ "  " ^ TimeToText(RaceEvent
138
                        .LapTime);                     
139
                    }
140
                                                                                               
141
                }
142
          }
143
EOL
144
        );
145
    }
146
147
    protected function updateContent(ManialinkInterface $manialink)
148
    {
149
        parent::updateContent($manialink);
150
    }
151
152
153
}
154