summaryrefslogtreecommitdiff
path: root/SDL_Android/LivioSdlUtilities/src/com/livio/sdl/menu/MenuManager.java
blob: b6fef256f54a44241a1bae14c48f607d7858d43e (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.livio.sdl.menu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import android.util.Log;
import android.util.SparseArray;

/**
 * Manages SDL menu items (commands and submenus), keeping an up-to-date list of available
 * menu items.
 *
 * @author Mike Burke
 *
 */
public class MenuManager {
	/**
	 * Allows iterating over the items in a menu manager object.
	 *
	 * @author Mike Burke
	 *
	 */
	public static class MenuIterator implements Iterator<MenuItem>{

		private final MenuManager items;
		private int currentIndex = 0;
		
		private MenuIterator(MenuManager items){
			this.items = items;
		}
		
		@Override
		public boolean hasNext() {
			return (currentIndex < (items.size()));
		}

		@Override
		public MenuItem next() {
			return items.getItemAt(currentIndex++);
		}

		@Override
		public void remove() {
			// don't allow removing items through the iterator
			throw new UnsupportedOperationException();
		}
	}

	private static boolean debug = false;
	private SparseArray<MenuItem> menuItems;
	
	// constructors
	public MenuManager() {
		menuItems = new SparseArray<MenuItem>();
	}
	
	public MenuManager(int startSize){
		menuItems = new SparseArray<MenuItem>(startSize);
	}
	
	/**
	 * Adds the input menu item to the list.  If the input item is the child of a submenu,
	 * also adds the item to the parent's list of children.
	 * 
	 * @param item The item to add
	 */
	public void addItem(MenuItem item){
		log(new StringBuilder().append("Adding item: ").append(item.toString()).toString());
		menuItems.put(item.getId(), item);
		
		// if not a top-level menu, let's check if it needs to be added to a submenu as well
		if(!item.isMenu()){
			CommandButton button = (CommandButton) item;
			int parentId = button.getParentId();
			if(parentId != -1){
				// button has a valid parent.  let's find it and add it to the list
				addCommandToParent(button, parentId);
			}
		}
	}
	
	/**
	 * Adds the input command button to the parent represented by the parent id.
	 * 
	 * @param button The button to add
	 * @param parentId The id of the button's parent
	 */
	private void addCommandToParent(CommandButton button, int parentId){
		MenuItem parent = menuItems.get(parentId);
		log(new StringBuilder().append("Adding command: ").append(button.toString()).append(" to parent: ").append(parent.toString()).toString());
		
		if(parent.isMenu()){
			SubmenuButton parentButton = (SubmenuButton) parent;
			parentButton.addChild(button);
		}
	}
	
	/**
	 * Removes the item with the input id.  If the item to remove is a submenu which contains children,
	 * this method removes the children commands from the menu manager as well.  If the item to remove is
	 * a command which belongs to a registered submenu, this method will also remove the child from its
	 * parent's list of children.
	 * 
	 * @param id The id of the item to remove
	 */
	public void removeItem(int id){
		MenuItem itemToRemove = menuItems.get(id);
		if(itemToRemove == null){
			return;
		}

		log(new StringBuilder().append("Removing item: ").append(itemToRemove.toString()).toString());
		
		if(!itemToRemove.isMenu()){
			// command button
			final int parentId = ((CommandButton)itemToRemove).getParentId(); 
			if(parentId != -1){
				// we have a command button with a valid parent - let's remove it from the parent list
				SubmenuButton parent = (SubmenuButton) menuItems.get(parentId);
				if(parent != null){
					log(new StringBuilder().append("Removing child: ").append(itemToRemove.toString()).append(" from parent: ").append(parent.toString()).toString());
					parent.removeChild(itemToRemove.getId());
				}
			}
		}
		else{
			// submenu button is being deleted - remove all children as well
			removeChildren((SubmenuButton) itemToRemove);
		}
		menuItems.remove(id);
	}
	
	/**
	 * Removes any children that belong to the input submenu.
	 * 
	 * @param parent The parent whose children should be removed
	 */
	private void removeChildren(SubmenuButton parent){
		List<MenuItem> children = parent.getChildren();
		if(children != null && children.size() > 0){
			for(MenuItem child : children){
				removeItem(child.getId());
			}
		}
	}
	
	/**
	 * Makes a copy of all registered submenus and returns it.
	 * 
	 * @return A list of all registered submenus
	 */
	public List<MenuItem> getSubmenus(){
		if(size() == 0){
			return Collections.emptyList();
		}
		
		log("Making a copy of all submenus");
		
		List<MenuItem> result = new ArrayList<MenuItem>();
		
		// iterate through all items
		Iterator<MenuItem> iterator = iterator();
		while(iterator.hasNext()){
			MenuItem current = iterator.next();
			if(current.isMenu()){
				// if this item is a submenu, make a copy of the item and add it to the result list
				result.add(new SubmenuButton((SubmenuButton) current));
			}
		}
		
		return result;
	}

	/**
	 * Makes a copy of all registered commands and returns it.
	 * 
	 * @return A list of all registered commands
	 */
	public List<MenuItem> getCommands(){
		if(size() == 0){
			return Collections.emptyList();
		}
		
		log("Making a copy of all commands");
		
		List<MenuItem> result = new ArrayList<MenuItem>();
		
		// iterate through all items
		Iterator<MenuItem> iterator = iterator();
		while(iterator.hasNext()){
			MenuItem current = iterator.next();
			if(!current.isMenu()){
				// if this item is a command, make a copy of the item and add it to the result list
				result.add(new CommandButton((CommandButton) current));
			}
		}
		
		return result;
	}
	
	/**
	 * Makes a copy of all items and returns it.
	 * 
	 * @return A list of all registered menu items
	 */
	public List<MenuItem> getAllItems(){
		if(size() == 0){
			return Collections.emptyList();
		}
		
		log("Making a copy of all menu items");
		
		List<MenuItem> result = new ArrayList<MenuItem>(size());
		
		// iterate through all items
		Iterator<MenuItem> iterator = iterator();
		while(iterator.hasNext()){
			MenuItem current = iterator.next();

			// make a copy of the item and add it to the list
			if(current.isMenu()){
				result.add(new SubmenuButton((SubmenuButton) current));
			}
			else{
				result.add(new CommandButton((CommandButton) current));
			}
		}
		
		return result;
	}
	
	/**
	 * Returns the item with the given id.
	 * 
	 * @param id The id of the item to return
	 * @return The item with the input id, or null if the id doesn't exist
	 */
	public MenuItem get(int id){
		return menuItems.get(id);
	}
	
	/**
	 * Returns the item at the input index.
	 * 
	 * @param index The index of the item to return
	 * @return The item at the given index
	 */
	public MenuItem getItemAt(int index){
		return menuItems.valueAt(index);
	}
	
	/**
	 * Returns the item with the given name.
	 * 
	 * @param name The name of the item to return
	 * @return The item with the given name, or null if the name doesn't exist
	 */
	public MenuItem get(String name){
		// iterate through all items
		Iterator<MenuItem> iterator = iterator();
		while(iterator.hasNext()){
			MenuItem current = iterator.next();

			// if the current item matches the input name, return it
			if(name.equals(current.getName())){
				return current;
			}
		}
		
		return null;
	}
	
	/**
	 * Returns the number of items being managed by the menu manager.
	 * 
	 * @return The number of items being managed by the menu manager
	 */
	public int size(){
		return menuItems.size();
	}
	
	/**
	 * Removes all menu items from the menu manager.
	 */
	public void clear(){
		menuItems.clear();
	}
	
	/**
	 * Dispatches a click event to the button with the input id.  If the button with the
	 * input id isn't found in the menu manager, this method does nothing.
	 * 
	 * @param buttonId The id of the button that was clicked
	 */
	public void dispatchClick(int buttonId){
		MenuItem item = menuItems.get(buttonId);
		
		if(item == null || item.isMenu()){
			log("Button with selected ID hasn't been added to this menu manager.");
		}
		else{
			CommandButton itemCmd = (CommandButton) item;
			itemCmd.dispatchClickEvent();
		}
	}
	
	/**
	 * Creates a new iterator object for this MenuManager object.
	 * 
	 * @return The new iterator instance
	 */
	public Iterator<MenuItem> iterator(){
		log("Creating new iterator object");
		Iterator<MenuItem> iterator = new MenuIterator(this);
		return iterator;
	}

	/**
	 * Enables or disables debug mode for log messages.
	 * 
	 * @param enable True to enable debug logs, false to disable
	 */
	public static void setDebug(boolean enable){
		debug = enable;
	}
	
	private static void log(String msg){
		if(debug){
			Log.d("MenuManager", msg);
		}
	}
}