forked from robotlegs/robotlegs-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandMappingList.as
147 lines (124 loc) · 4.62 KB
/
CommandMappingList.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//------------------------------------------------------------------------------
// Copyright (c) 2009-2013 the original author or authors. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//------------------------------------------------------------------------------
package robotlegs.bender.extensions.commandCenter.impl
{
import flash.utils.Dictionary;
import robotlegs.bender.extensions.commandCenter.api.ICommandMapping;
import robotlegs.bender.extensions.commandCenter.api.ICommandMappingList;
import robotlegs.bender.extensions.commandCenter.api.ICommandTrigger;
import robotlegs.bender.framework.api.ILogger;
public class CommandMappingList implements ICommandMappingList
{
/*============================================================================*/
/* Private Properties */
/*============================================================================*/
private const _mappingsByCommand:Dictionary = new Dictionary();
private var _mappings:Vector.<ICommandMapping> = new Vector.<ICommandMapping>;
private var _trigger:ICommandTrigger;
private var _logger:ILogger;
private var _compareFunction:Function;
private var _sorted:Boolean;
/*============================================================================*/
/* Constructor */
/*============================================================================*/
public function CommandMappingList(trigger:ICommandTrigger, logger:ILogger = null)
{
_trigger = trigger;
_logger = logger;
}
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
public function getList():Vector.<ICommandMapping>
{
_sorted || sortMappings();
return _mappings.concat();
}
public function withSortFunction(sorter:Function):ICommandMappingList
{
_sorted = false;
_compareFunction = sorter;
return this;
}
public function addMapping(mapping:ICommandMapping):void
{
_sorted = false;
const oldMapping:ICommandMapping = _mappingsByCommand[mapping.commandClass];
if (oldMapping)
{
overwriteMapping(oldMapping, mapping);
}
else
{
storeMapping(mapping);
_mappings.length == 1 && _trigger.activate();
}
}
public function removeMapping(mapping:ICommandMapping):void
{
_sorted = false;
if (_mappingsByCommand[mapping.commandClass])
{
deleteMapping(mapping);
_mappings.length == 0 && _trigger.deactivate();
}
}
public function removeMappingFor(commandClass:Class):void
{
const mapping:ICommandMapping = _mappingsByCommand[commandClass];
mapping && removeMapping(mapping);
}
public function removeAllMappings():void
{
const list:Vector.<ICommandMapping> = getList();
var length:int = list.length;
while (length--)
{
deleteMapping(list[length]);
}
_trigger.deactivate();
}
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
private function storeMapping(mapping:ICommandMapping):void
{
_mappingsByCommand[mapping.commandClass] = mapping;
_mappings.push(mapping);
_logger && _logger.debug('{0} mapped to {1}', [_trigger, mapping]);
}
private function deleteMapping(mapping:ICommandMapping):void
{
delete _mappingsByCommand[mapping.commandClass];
_mappings.splice(_mappings.indexOf(mapping), 1);
_logger && _logger.debug('{0} unmapped from {1}', [_trigger, mapping]);
}
private function overwriteMapping(oldMapping:ICommandMapping, newMapping:ICommandMapping):void
{
_logger && _logger.warn('{0} already mapped to {1}\n' +
'If you have overridden this mapping intentionally you can use "unmap()" ' +
'prior to your replacement mapping in order to avoid seeing this message.\n',
[_trigger, oldMapping]);
deleteMapping(oldMapping);
storeMapping(newMapping);
}
private function sortMappings():void
{
if (_compareFunction != null)
{
const mappings:Array = [];
var length:uint = _mappings.length;
for (var i:uint = 0; i < length; i++)
{
mappings[i] = _mappings[i];
}
_mappings = Vector.<ICommandMapping>(mappings.sort(_compareFunction));
}
_sorted = true;
}
}
}