Completed
Push — master ( 4cc80e...c361a4 )
by De Cramer
13s
created

NotificationWidget   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 202
rs 10
c 0
b 0
f 0
ccs 0
cts 32
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B createContent() 0 137 2
B createToast() 0 25 1
1
<?php
2
3
namespace eXpansion\Framework\Notifications\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\Label;
11
use FML\Controls\Quad;
12
use FML\Script\ScriptLabel;
13
14
class NotificationWidget extends WidgetFactory
15
{
16
    const toastCount = 4;
17
18
    /** @var NotificationUpdater */
19
    protected $updaterWidgetFactory;
20
21
    /***
22
     * MenuFactory constructor.
23
     *
24
     * @param                      $name
25
     * @param                      $sizeX
26
     * @param                      $sizeY
27
     * @param null                 $posX
28
     * @param null                 $posY
29
     * @param WidgetFactoryContext $context
30
     * @param NotificationUpdater  $notificationUpdater
31
     */
32
    public function __construct(
33
        $name,
34
        $sizeX,
35
        $sizeY,
36
        $posX,
37
        $posY,
38
        WidgetFactoryContext $context,
39
        NotificationUpdater $notificationUpdater
40
    ) {
41
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
42
43
        $this->updaterWidgetFactory = $notificationUpdater;
44
    }
45
46
    /**
47
     * @param Widget|ManialinkInterface $manialink
48
     */
49
    protected function createContent(ManialinkInterface $manialink)
50
    {
51
        for ($x = 0; $x < self::toastCount; $x++) {
52
            $manialink->addChild($this->createToast($x));
53
        }
54
55
        $manialink->getFmlManialink()->getScript()->addScriptFunction("", /** @lang text */
56
            <<<EOL
57
58
            Text getMessage(Text[Text] Message) {     
59
                
60
                log(LocalUser.Language);       
61
                if (Message.existskey(LocalUser.Language)) {
62
                       return Message[LocalUser.Language];           
63
                }
64
                
65
                return Message["en"];
66
            }
67
            
68
            Void HideToast(CMlFrame Frame, Boolean Instant) {
69
                declare CMlFrame[] Frames for This;
70
                
71
                foreach(Element in Frame.Controls) {
72
                    if (Instant) {
73
                        if (Element is CMlLabel) {
74
                            (Element as CMlLabel).Opacity = 0.;
75
                        }
76
                        
77
                        if (Element is CMlQuad) {
78
                            (Element as CMlQuad).Opacity = 0.;
79
                        }	
80
                    } else {
81
                        AnimMgr.Add(Element, "<elem opacity=\"0\" />", 500, CAnimManager::EAnimManagerEasing::QuadOut);
82
                    }
83
                }
84
                
85
                if (Instant) {
86
                    Frames.remove(Frame);
87
                }
88
            }
89
            
90
            Void ShowToast(Text[Text][Text] _Notification) {
91
    
92
                declare CMlFrame[] Frames for This;
93
                declare CMlFrame Exp_Window <=> (Page.GetFirstChild("Window") as CMlFrame);
94
                
95
                if (Frames.count >= 4) {
96
                    HideToast(Frames[0], True);
97
                }
98
                Page.GetClassChildren("uiToast", Exp_Window, True);
99
                
100
                
101
                foreach (Read in Page.GetClassChildren_Result) {
102
                    
103
                    declare Frame = (Read as CMlFrame);
104
                    
105
                    // stop at first available toast
106
                    if ( (Frame.Controls[0] as CMlLabel).Opacity < 0.1) {
107
                    Frame.Show();
108
                    Frames.add(Frame);
109
                    (Frame.Controls[0] as CMlLabel).Opacity = 0.1;
110
                    declare Prefix = _Notification["params"]["prefix"];
111
                    
112
                    (Frame.Controls[1] as CMlLabel).Value = Prefix ^ getMessage(_Notification["title"]);
113
                    (Frame.Controls[2] as CMlLabel).Value = getMessage(_Notification["message"]);
114
                    declare Integer Duration =  TextLib::ToInteger(_Notification["params"]["duration"]);
115
                   
116
                        foreach(Element in Frame.Controls) {
117
                                AnimMgr.Add(Element, "<elem opacity=\"1\" />", 500, CAnimManager::EAnimManagerEasing::QuadIn);
118
                                if (Duration > 0) {
119
                                    AnimMgr.Add(Element, "<elem opacity=\"0\" />", (Now + Duration), 500, CAnimManager::EAnimManagerEasing::QuadOut);
120
                                }
121
                        }
122
                                    
123
                        return;
124
                    }
125
                }
126
            }
127
128
EOL
129
        );
130
131
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit, <<<EOL
132
                
133
                
134
                    declare CMlFrame[] Frames for This;
135
                    Frames.clear();
136
                    Page.GetClassChildren("uiToast", (Page.GetFirstChild("Window") as CMlFrame), True);
137
                  
138
                    foreach (Frame in Page.GetClassChildren_Result) {
139
140
                        foreach(Element in (Frame as CMlFrame).Controls) {
141
                            if (Element is CMlLabel) {
142
                                (Element as CMlLabel).Opacity = 0.;
143
                            }
144
                            
145
                            if (Element is CMlQuad) {
146
                                (Element as CMlQuad).Opacity = 0.;
147
                            }
148
                        }
149
                    }
150
                    {$this->updaterWidgetFactory->getScriptInitialization(true)}
151
EOL
152
        );
153
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop, <<<EOL
154
155
             {$this->updaterWidgetFactory->getScriptOnChange('
156
             if (notification.count > 0) {
157
                 ShowToast(notification);
158
                 }
159
             ')}
160
             
161
                declare Index = 0;
162
                foreach(Frame in Frames) {
163
                    declare Real posY = 70.;		
164
                    if ((Frame.Controls[0] as CMlLabel).Opacity > 0.09) {
165
                        Frame.RelativePosition_V3.Y = posY - (Index* 28. * 0.8);
166
                        Index +=1;
167
                    } else {
168
                        HideToast(Frame, True);
169
                        Frame.Hide();
170
                    }
171
                }
172
                  
173
EOL
174
        );
175
176
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::MouseClick, <<<EOL
177
         if (Event.Control.HasClass("toastClose")) {
178
			HideToast(Event.Control.Parent, False);
179
		}
180
        
181
EOL
182
        );
183
184
185
    }
186
187
188
    private function createToast($idx)
189
    {
190
        $frame = Frame::create()->setScale(0.75)->setPosition(-159, 80 - (21 * $idx))->addClass("uiToast");
191
        $closeButton = Label::create();
192
        $closeButton->setPosition(74, -4)->setSize(4, 4)->setText("✖")->setAreaColor("0000")->setAreaFocusColor("f00a")
193
            ->setAlign("center", "center2")->addClass("toastClose")->setScriptEvents(true)->setOpacity(0.5);
194
        $frame->addChild($closeButton);
195
196
197
        $title = Label::create()->setPosition(2, -2)->setSize(70, 5)->setTextSize(2)
198
            ->setTextColor("fff")->setText("n/a")->setOpacity(0);
199
        $frame->addChild($title);
200
201
202
        $message = Label::create()->setPosition(2, -8)->setSize(70,
203
            15)->setTextSize(2)->setMaxLines(3)->setOpacity(0.5)->setTextColor("fff")->setText("n/a");
204
        $frame->addChild($message);
205
206
        $quad = Quad::create();
207
        $quad->setSize(78, 27)->setBackgroundColor("000a");
208
        $frame->addChild($quad);
209
210
        return $frame;
211
212
    }
213
214
215
}
216