~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to app-tutorial/tutorial/ModalPopupMenu.hx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
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.
 
4
 * </license> 
 
5
 */
 
6
package tutorial;
 
7
 
 
8
#if neash
 
9
import neash.events.MouseEvent;
 
10
#elseif flash
 
11
import flash.events.MouseEvent;
 
12
#end
 
13
 
 
14
import ui.MenuEvent;
 
15
import ui.Menu;
 
16
import ui.Action;
 
17
 
 
18
/**
 
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.
 
22
 *
 
23
 * == 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:
 
28
 *
 
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)
 
32
 *
 
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.
 
39
 *
 
40
 * == Modal/Stage  ==
 
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
 
45
 *
 
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.
 
50
 * 
 
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.
 
56
 */
 
57
class ModalPopupMenu extends ui.StageLayout
 
58
{
 
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";
 
63
        
 
64
        public function new()
 
65
        {
 
66
                super();
 
67
                
 
68
                //add a local mouse down listener, to popup the menu
 
69
                addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
 
70
                
 
71
                //add a global listener to listen for menu events
 
72
                ui.Events.addListener( menuEventStr1, onMenuEvent1 );
 
73
                ui.Events.addListener( menuEventStr2, onMenuEvent2 );
 
74
        }
 
75
        
 
76
        function onMouseDown( me : MouseEvent )
 
77
        {
 
78
                //for simplicity we are creating the menu here, but note that
 
79
                //Popup's can be used more than once.
 
80
                
 
81
                //Create a popup menu
 
82
                var menu = Menu.popup();
 
83
                
 
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" ) );
 
89
                
 
90
                //add a visual separator
 
91
                menu.addSeparator();
 
92
                
 
93
                //Add an item which calls a function when selected
 
94
                menu.addItem( "Action", Action.bind( menuCall ) );
 
95
                
 
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" ) );
 
100
                
 
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" ) );
 
104
                menu.addSeparator();
 
105
                menu.addSubMenu( "Sub Menu", sub );
 
106
                
 
107
                //now show the popup in a typical fashion
 
108
                menu.modalAtMouse();
 
109
        }
 
110
        
 
111
        function onMenuEvent1( me : MenuEvent )
 
112
        {
 
113
                //Trace that the menu event was caught
 
114
                trace( "Menu Event 1" );
 
115
        }
 
116
        
 
117
        function onMenuEvent2( me : MenuEvent )
 
118
        {
 
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 );
 
122
        }
 
123
        
 
124
        function menuCall( )
 
125
        {
 
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" );
 
129
        }
 
130
        
 
131
        function menuCallParam( param : String )
 
132
        {
 
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 );
 
137
        }
 
138
        
 
139
        override function _resize( w : Float, h : Float )
 
140
        {
 
141
                //something has to be drawn, otherwise clicks won't be registered,
 
142
                //plus _resize has to be overridden in all cases anyways
 
143
                gfx.clear();
 
144
                gfx.use( draw.Brush.solid( draw.Color.rgb( 0.5, 0.5, 0.5 ) ) );
 
145
                gfx.drawRect( 0, 0, w, h );
 
146
        }
 
147
        
 
148
        static public function main()
 
149
        {
 
150
                ui.StageLayout.setup( ModalPopupMenu );
 
151
        }
 
152
}