/
ButtonController.as
113 lines (92 loc) · 4.95 KB
/
ButtonController.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright 2011 Marek Standio.
This file is part of SaladoPlayer.
SaladoPlayer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
SaladoPlayer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SaladoPlayer. If not, see <http://www.gnu.org/licenses/>.
*/
package com.panozona.modules.imagebutton.controller{
import com.panozona.modules.imagebutton.model.structure.SubButton;
import com.panozona.modules.imagebutton.model.SubButtonData;
import com.panozona.modules.imagebutton.view.ButtonView;
import com.panozona.modules.imagebutton.view.SubButtonView;
import com.panozona.player.module.data.property.Size;
import com.panozona.player.module.Module;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
public class ButtonController {
private var _buttonView:ButtonView;
private var _module:Module;
private var subButtonControllers:Vector.<SubButtonController>;
public function ButtonController(buttonView:ButtonView, module:Module) {
_buttonView = buttonView;
_module = module;
var panoramaEventClass:Class = ApplicationDomain.currentDomain.getDefinition("com.panozona.player.manager.events.PanoramaEvent") as Class;
_module.saladoPlayer.manager.addEventListener(panoramaEventClass.PANORAMA_STARTED_LOADING, onPanoramaStartedLoading, false, 0, true);
if(_buttonView.windowData.button.action != null){
_buttonView.addEventListener(MouseEvent.CLICK, getMouseEventHandler(_buttonView.windowData.button.action));
}
if (_buttonView.windowData.button.path != null){
var buttonLoader:Loader = new Loader();
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
context.checkPolicyFile = true;
buttonLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, buttonImageLost);
buttonLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, buttonImageLoaded);
buttonLoader.load(new URLRequest(_buttonView.windowData.button.path), context);
}
subButtonControllers = new Vector.<SubButtonController>();
var subButtonView:SubButtonView;
var subButtonController:SubButtonController;
for each(var subButton:SubButton in buttonView.windowData.button.subButtons.getChildrenOfGivenClass(SubButton)) {
subButtonView = new SubButtonView(new SubButtonData(subButton), _buttonView.windowData);
_buttonView.subButtonsContainer.addChild(subButtonView);
subButtonController = new SubButtonController(subButtonView, _module);
subButtonControllers.push(subButtonController);
}
}
private function onPanoramaStartedLoading(loadPanoramaEvent:Object):void {
var panoramaEventClass:Class = ApplicationDomain.currentDomain.getDefinition("com.panozona.player.manager.events.PanoramaEvent") as Class;
_module.saladoPlayer.manager.removeEventListener(panoramaEventClass.PANORAMA_STARTED_LOADING, onPanoramaStartedLoading);
if (_buttonView.windowData.open) {
_module.saladoPlayer.manager.runAction(_buttonView.windowData.window.onOpen);
}else {
_module.saladoPlayer.manager.runAction(_buttonView.windowData.window.onClose);
}
}
public function buttonImageLost(e:IOErrorEvent):void {
(e.target as LoaderInfo).removeEventListener(IOErrorEvent.IO_ERROR, buttonImageLost);
(e.target as LoaderInfo).removeEventListener(Event.COMPLETE, buttonImageLoaded);
_module.printError(e.toString());
}
public function buttonImageLoaded(e:Event):void {
(e.target as LoaderInfo).removeEventListener(IOErrorEvent.IO_ERROR, buttonImageLost);
(e.target as LoaderInfo).removeEventListener(Event.COMPLETE, buttonImageLoaded);
_buttonView.addChildAt((e.target as LoaderInfo).content, 0);
_buttonView.windowData.size = new Size(_buttonView.width, _buttonView.height);
if (_buttonView.windowData.button.mouse.onOver != null) {
_buttonView.addEventListener(MouseEvent.ROLL_OVER, getMouseEventHandler(_buttonView.windowData.button.mouse.onOver));
}
if (_buttonView.windowData.button.mouse.onOut != null) {
_buttonView.addEventListener(MouseEvent.ROLL_OUT, getMouseEventHandler(_buttonView.windowData.button.mouse.onOut));
}
}
private function getMouseEventHandler(actionId:String):Function{
return function(e:MouseEvent):void {
_module.saladoPlayer.manager.runAction(actionId);
}
}
}
}