2
* This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
3
* For full copyright and license information please refer to doc/license.txt.
9
import neash.events.MouseEvent;
11
import flash.events.MouseEvent;
19
* This class introduces the popup menu and the related
20
* Action items -- since a popup would not be useful if
21
* there were no actions.
24
* Several different methods are used here to identify what items in
25
* the menu had been clicked. Which manner you use depends on
26
* where the menu is created, and what your application is trying to
27
* do. A quick overview between the basic types:
29
* Message: The MenuEvent message will likely be used when the
30
* menu item is of global use, and may change its meaning depending
31
* on application state (such as a Copy or Paste action)
33
* Function Binding: The function will be called directly, and thus
34
* the exact function must be specified at menu setup time. This is
35
* used in cases where the menu action is very specific, and is
36
* not context sensitive.
37
* As they also require less setup overhead, they are also just
38
* generally useful whenever a Message is not strictly needed.
41
* This class does the popup as a global menu, which is
42
* a typical case for use. To do so it uses the "Modal" class, which
43
* additionally indicates that when the popup is active, no events
44
* outside of the popup are sent to the underlying windows
46
* In this mode, all selected events are posted to the Stage, thus listeners
47
* for such events should listen on the stage (using ui.Events.addListener).
48
* This is also a normal use, since the popup commands could likely be
49
* used anywhere in the program, not just in widget that created it.
51
* It is possible to use a local popup, and one that is not Modal, and while
52
* the code to do so is quite simple, there are odd caveats and thus it is
53
* beyond the scope of this tutorial.
54
* Refer to ui.test.MenuTest for an example of how a popup menu can be
55
* non-modal and clicking outside dismisses (or recreates) the menu.
57
class ModalPopupMenu extends ui.StageLayout
59
//choose an event string, must be unique in the Flash namespace, otherwise
60
//it would trigger some other event incorrectly.
61
static var menuEventStr1 = "menu_event_one";
62
static var menuEventStr2 = "menu_event_two";
68
//add a local mouse down listener, to popup the menu
69
addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
71
//add a global listener to listen for menu events
72
ui.Events.addListener( menuEventStr1, onMenuEvent1 );
73
ui.Events.addListener( menuEventStr2, onMenuEvent2 );
76
function onMouseDown( me : MouseEvent )
78
//for simplicity we are creating the menu here, but note that
79
//Popup's can be used more than once.
82
var menu = Menu.popup();
84
//Add the first items, which posts an event when selected
85
menu.addItem( "Event One", new MenuEvent( menuEventStr1 ) );
86
//the add two more showing how to pass dynamic value along with an event
87
menu.addItem( "Event Two : ABC", new MenuEvent( menuEventStr2, "abc" ) );
88
menu.addItem( "Event Two : XYZ", new MenuEvent( menuEventStr2, "xyz" ) );
90
//add a visual separator
93
//Add an item which calls a function when selected
94
menu.addItem( "Action", Action.bind( menuCall ) );
96
//Using bind again, add two items which call the same function,
97
//but with different parameters
98
menu.addItem( "Bind One", Action.bind1( menuCallParam, "One" ) );
99
menu.addItem( "Bind Two", Action.bind1( menuCallParam, "Two" ) );
101
//and finally, create a simple sub-menu, just to show how it is done
102
var sub = Menu.popup();
103
sub.addItem( "Event in Sub", new MenuEvent( menuEventStr2, "event in sub-menu" ) );
105
menu.addSubMenu( "Sub Menu", sub );
107
//now show the popup in a typical fashion
111
function onMenuEvent1( me : MenuEvent )
113
//Trace that the menu event was caught
114
trace( "Menu Event 1" );
117
function onMenuEvent2( me : MenuEvent )
119
//Trace that the menu event was caught, and display what dynamic
120
//item was associated with this event.
121
trace( "Menu Event 2: " + me.ext );
126
//this function is called directly by the menu when the "Action" item
127
//is selected. This method saves having to setup an event listener
128
trace( "Action clicked" );
131
function menuCallParam( param : String )
133
//this is called as like menuCall, but is passed the parameter setup
134
//during menu construction -- this provide simple selections in popups
135
//all backed by the same function
136
trace( "Param Action: " + param );
139
override function _resize( w : Float, h : Float )
141
//something has to be drawn, otherwise clicks won't be registered,
142
//plus _resize has to be overridden in all cases anyways
144
gfx.use( draw.Brush.solid( draw.Color.rgb( 0.5, 0.5, 0.5 ) ) );
145
gfx.drawRect( 0, 0, w, h );
148
static public function main()
150
ui.StageLayout.setup( ModalPopupMenu );