View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: ButtonDetaultDeviceKeyCodes.java 1441 2007-10-23 17:58:20Z vlads $
21   */
22  package org.microemu.device.impl;
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import javax.microedition.lcdui.Canvas;
29  
30  /**
31   * 
32   * This class defines default device key codes and game actions for buttons.
33   * 
34   * Key code is reported to MIDP application by Canvas.keyPressed()
35   * 
36   * Game action is reported to MIDP application by Canvas.getGameAction()
37   * 
38   * Use 'device.xml' to redefine codes for your device if required.
39   * 
40   * @author vlads
41   * 
42   */
43  public abstract class ButtonDetaultDeviceKeyCodes {
44  
45  	private static Map codes = new HashMap();
46  
47  	private static Map gameActions = new HashMap();
48  
49  	public static int getKeyCode(ButtonName name) {
50  		Integer code = (Integer) codes.get(name);
51  		if (code != null) {
52  			return code.intValue();
53  		}
54  		return 0;
55  	}
56  
57  	public static int getGameAction(ButtonName name) {
58  		Integer code = (Integer) gameActions.get(name);
59  		if (code != null) {
60  			return code.intValue();
61  		}
62  		return 0;
63  	}
64  
65  	public static ButtonName getButtonNameByGameAction(int gameAction) {
66  		Integer value = new Integer(gameAction);
67  		if (gameActions.containsValue(value)) {
68  			for (Iterator iterator = gameActions.entrySet().iterator(); iterator.hasNext();) {
69  				Map.Entry v = (Map.Entry) iterator.next();
70  				if (v.getValue().equals(value)) {
71  					return (ButtonName) v.getKey();
72  				}
73  			}
74  		}
75  		throw new IllegalArgumentException("Illegal action " + gameAction);
76  	}
77  
78  	static {
79  		code(ButtonName.SOFT1, -6);
80  		code(ButtonName.SOFT2, -7);
81  		code(ButtonName.SELECT, -5, Canvas.FIRE);
82  		code(ButtonName.UP, -1, Canvas.UP);
83  		code(ButtonName.DOWN, -2, Canvas.DOWN);
84  		code(ButtonName.LEFT, -3, Canvas.LEFT);
85  		code(ButtonName.RIGHT, -4, Canvas.RIGHT);
86  
87  		code(ButtonName.KEY_NUM0, Canvas.KEY_NUM0);
88  		code(ButtonName.KEY_NUM1, Canvas.KEY_NUM1, Canvas.GAME_A);
89  		code(ButtonName.KEY_NUM2, Canvas.KEY_NUM2);
90  		code(ButtonName.KEY_NUM3, Canvas.KEY_NUM3, Canvas.GAME_B);
91  		code(ButtonName.KEY_NUM4, Canvas.KEY_NUM4);
92  		code(ButtonName.KEY_NUM5, Canvas.KEY_NUM5);
93  		code(ButtonName.KEY_NUM6, Canvas.KEY_NUM6);
94  		code(ButtonName.KEY_NUM7, Canvas.KEY_NUM7, Canvas.GAME_C);
95  		code(ButtonName.KEY_NUM8, Canvas.KEY_NUM8);
96  		code(ButtonName.KEY_NUM9, Canvas.KEY_NUM9, Canvas.GAME_D);
97  		code(ButtonName.KEY_STAR, Canvas.KEY_STAR);
98  		code(ButtonName.KEY_POUND, Canvas.KEY_POUND);
99  	}
100 
101 	private static void code(ButtonName name, int code) {
102 		codes.put(name, new Integer(code));
103 	}
104 
105 	private static void code(ButtonName name, int code, int gameAction) {
106 		code(name, code);
107 		gameActions.put(name, new Integer(gameAction));
108 	}
109 }