Completed
Pull Request — master (#175)
by De Cramer
02:53
created

CustomCheckpointWidget::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 99
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 8.3103
c 0
b 0
f 0
ccs 0
cts 23
cp 0
cc 1
eloc 25
nc 1
nop 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\uiLabel;
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("Cp: 0  00:00.000", uiLabel::TYPE_TITLE, "CurrentTime");
48
        $lbl->setPosition(0, -70)->setAlign("center", "center2")->setTextSize(4);
49
50
        $frame->addChild($lbl);
51
52
        $quad = Quad::create("quad_bg");
53
        $quad->setPosition(0, -70)->setSize(44, 8)->setBackgroundColor("000")
54
            ->setOpacity(0.5)->setAlign("center", "center2");
55
        $frame->addChild($quad);
56
57
        $quad = Quad::create("quad_top");
58
        $quad->setPosition(0, -80)->setSize(400, 33)
59
            ->setStyles("BgsPlayerCard", "BgRacePlayerLine")
60
            ->setColorize("777")->setAlign("center", "center")->setRotation(180);
61
        $frame->addChild($quad);
62
63
64
        $manialink->getFmlManialink()->getScript()->addScriptFunction("", <<<EOL
65
        Text TimeToText(Integer intime) {
66
                declare time = MathLib::Abs(intime);
67
                declare Integer cent = time % 1000;	
68
                declare Integer sec = (time / 1000) % 60;
69
                declare Integer min = time / 60000;
70
                declare Integer hour = time / 3600000;
71
                declare Text sign = "";
72
                if (intime < 0)  {
73
                    sign = "-";
74
                }
75
                
76
                if (hour > 0) {
77
                    return sign ^ hour ^ ":" ^ TextLib::FormatInteger(min,2) ^ ":" ^ TextLib::FormatInteger(sec,2) ^ "." ^ TextLib::FormatInteger(cent,3);
78
                } 
79
                return sign ^ TextLib::FormatInteger(min,2) ^ ":" ^ TextLib::FormatInteger(sec,2) ^ "." ^ TextLib::FormatInteger(cent,3);                                
80
            }
81
EOL
82
        );
83
84
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit, <<<EOL
85
            declare CMlLabel CheckPointLabel = (Page.GetFirstChild("CurrentTime") as CMlLabel);
86
            declare CMlQuad TopBg = (Page.GetFirstChild("quad_top") as CMlQuad);
87
            declare CMlQuad Bg = (Page.GetFirstChild("quad_bg") as CMlQuad);                                      
88
            declare CMlFrame Frame = (Page.GetFirstChild("Frame_Main") as CMlFrame);
89
            
90
            declare IsIntro = (
91
                UI.UISequence == CUIConfig::EUISequence::Intro ||
92
                UI.UISequence == CUIConfig::EUISequence::RollingBackgroundIntro ||
93
                UI.UISequence == CUIConfig::EUISequence::Outro
94
            );              
95
EOL
96
        );
97
98
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop, <<<EOL
99
            if (Frame.Visible && IsIntro) {
100
                Frame.Visible = False;
101
              
102
            } else if (!Frame.Visible && !IsIntro) {            
103
                Frame.Visible = True;                        
104
            }                      
105
          foreach (RaceEvent in RaceEvents) {
106
                if (GUIPlayer == RaceEvent.Player && RaceEvent.Type == CTmRaceClientEvent::EType::Respawn) {
107
                     if (InputPlayer.RaceState == CTmMlPlayer::ERaceState::BeforeStart) {                     
108
                        CheckPointLabel.Value = "Cp: 0  00:00.000";
109
                        Bg.BgColor = <0., 0., 0.>;
110
                        TopBg.Hide();
111
                     }
112
                }             
113
                
114
                if (GUIPlayer == RaceEvent.Player && RaceEvent.Type == CTmRaceClientEvent::EType::WayPoint) {
115
                  
116
                    declare CTmResult Score <=> RaceEvent.Player.Score.BestLap;
117
                   // TopBg.Show();                    
118
                    if (Score.Checkpoints.existskey(RaceEvent.CheckpointInLap) ) {
119
                        CheckPointLabel.Value = "Cp: " ^(RaceEvent.CheckpointInLap+1) ^ "  " ^ TimeToText(RaceEvent
120
                        .LapTime - Score.Checkpoints[RaceEvent.CheckpointInLap]);
121
                        if (RaceEvent.LapTime < Score.Checkpoints[RaceEvent.CheckpointInLap]) {
122
                            Bg.BgColor = <0., 0., 1.>;
123
                            TopBg.Colorize = <0., 0., 1.>;
124
                        } else {
125
                            Bg.BgColor = <1., 0., 0.>;
126
                            TopBg.Colorize = <1., 0., 0.>;
127
                        }
128
                    } else {
129
                       CheckPointLabel.Value = "Cp: " ^(RaceEvent.CheckpointInLap+1) ^ "  " ^ TimeToText(RaceEvent
130
                        .LapTime);
131
                       TopBg.Colorize = <0.7, 0.7, 0.7>;
132
                    }
133
                                                                                               
134
                }
135
          }
136
EOL
137
        );
138
    }
139
140
    protected function updateContent(ManialinkInterface $manialink)
141
    {
142
        parent::updateContent($manialink); // TODO: Change the autogenerated stub
143
    }
144
145
146
}
147