javauis/lcdui_qt/src/javax/microedition/lcdui/CanvasKeypad.java
changeset 57 59b3b4473dc8
parent 26 dc7c549001d5
child 78 71ad690e91f5
equal deleted inserted replaced
56:abc41079b313 57:59b3b4473dc8
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5  * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:
    14  * Description:
    15 *
    15  *
    16 */
    16  */
    17 package javax.microedition.lcdui;
    17 package javax.microedition.lcdui;
    18 
    18 
    19 import java.util.Vector;
       
    20 import java.util.Timer;
    19 import java.util.Timer;
    21 import java.util.TimerTask;
    20 import java.util.TimerTask;
    22 
    21 
    23 import org.eclipse.ercp.swt.mobile.MobileShell;
       
    24 import org.eclipse.swt.SWT;
    22 import org.eclipse.swt.SWT;
    25 import org.eclipse.swt.events.*;
    23 import org.eclipse.swt.events.*;
    26 import org.eclipse.swt.widgets.*;
    24 import org.eclipse.swt.widgets.*;
    27 import org.eclipse.swt.widgets.Display;
    25 import org.eclipse.swt.widgets.Display;
    28 import org.eclipse.swt.graphics.*;
    26 import org.eclipse.swt.graphics.*;
       
    27 import org.eclipse.swt.internal.extension.ImageUtil;
    29 import org.eclipse.swt.internal.qt.*;
    28 import org.eclipse.swt.internal.qt.*;
    30 import com.nokia.mj.impl.rt.support.ApplicationInfo;
       
    31 
    29 
    32 import org.eclipse.swt.layout.FormAttachment;
    30 import org.eclipse.swt.layout.FormAttachment;
    33 import org.eclipse.swt.layout.FormData;
    31 import org.eclipse.swt.layout.FormData;
    34 import org.eclipse.swt.layout.FormLayout;
    32 import org.eclipse.swt.layout.FormLayout;
    35 
    33 
    36 import javax.microedition.lcdui.EventDispatcher.LCDUIEvent;
       
    37 
       
    38 
       
    39 /**
    34 /**
    40  * The CanvasKeypad class is designed to generate the on screen keypad
    35  * The CanvasKeypad class is designed to generate the on screen keypad and
    41  * and handle graphical operations as well as low-level pointer events.
    36  * handle graphical operations as well as low-level pointer events. CanvasKeypad
    42  * CanvasKeypad maps the pointer events to key events that are needed by the Canvas.
    37  * maps the pointer events to key events that are needed by the Canvas.
    43  *
    38  * 
    44  */
    39  */
    45 final class CanvasKeypad implements MouseListener, MouseMoveListener
    40 final class CanvasKeypad implements MouseListener, MouseMoveListener
    46 {
    41 {
    47 
    42     /**
    48     /**
    43      * Constant for <code>UP</code> game action.
    49              *  Constant for <code>UP</code> game action.
    44      */
    50              */
       
    51     private static final int UP = -1;
    45     private static final int UP = -1;
    52 
    46 
    53     /**
    47     /**
    54              * Constant for <code>DOWN</code> game action.
    48      * Constant for <code>DOWN</code> game action.
    55              */
    49      */
    56     private static final int DOWN = -2;
    50     private static final int DOWN = -2;
    57 
    51 
    58     /**
    52     /**
    59              * Constant for <code>LEFT</code> game action.
    53      * Constant for <code>LEFT</code> game action.
    60              */
    54      */
    61     private static final int LEFT = -3;
    55     private static final int LEFT = -3;
    62 
    56 
    63     /**
    57     /**
    64              * Constant for <code>RIGHT</code> game action.
    58      * Constant for <code>RIGHT</code> game action.
    65              */
    59      */
    66     private static final int RIGHT = -4;
    60     private static final int RIGHT = -4;
    67 
    61 
    68     /**
    62     /**
    69              * Constant for <code>FIRE</code> game action.
    63      * Constant for <code>FIRE</code> game action.
    70              */
    64      */
    71     private static final int FIRE = -5;
    65     private static final int FIRE = -5;
    72 
    66 
    73     /**
    67     /**
    74              * Constant for <code>SLK</code> game action.
    68      * Constant for <code>SLK</code> game action.
    75              */
    69      */
    76     private static final int SLK = -6;
    70     private static final int SLK = -6;
    77 
    71 
    78     /**
    72     /**
    79              * Constant for <code>SRK</code> game action.
    73      * Constant for <code>SRK</code> game action.
    80              */
    74      */
    81     private static final int SRK = -7;
    75     private static final int SRK = -7;
    82 
    76 
    83     /**
    77     /**
    84              * Constant for general "<code>A</code>" game action.
    78      * Constant for general "<code>A</code>" game action.
    85              */
    79      */
    86     private static final int GAME_A = 55;
    80     private static final int GAME_A = 55;
    87 
    81 
    88     /**
    82     /**
    89              * Constant for general "<code>B</code>" game action.
    83      * Constant for general "<code>B</code>" game action.
    90              */
    84      */
    91     private static final int GAME_B = 57;
    85     private static final int GAME_B = 57;
    92 
    86 
    93     /**
    87     /**
    94              * Constant for general "<code>C</code>" game action.
    88      * Constant for general "<code>C</code>" game action.
    95              */
    89      */
    96     private static final int GAME_C = 42;
    90     private static final int GAME_C = 42;
    97 
    91 
    98     /**
    92     /**
    99              * Constant for general "<code>D</code>" game action.
    93      * Constant for general "<code>D</code>" game action.
   100              */
    94      */
   101     private static final int GAME_D = 35;
    95     private static final int GAME_D = 35;
   102 
    96 
   103     /**
    97     /**
   104              * Constant for general invalid key code.
    98      * Constant for general invalid key code.
   105              */
    99      */
   106     private static final int INVALID_CODE = -99;
   100     private static final int INVALID_CODE = -99;
   107 
   101 
   108     /**
   102     /**
   109             * Constant for long keypress timeout.
   103      * Constant for long keypress timeout.
   110             */
   104      */
   111     private static final int KEYREPEAT_INTERVAL = 500;
   105     private static final int KEYREPEAT_INTERVAL = 500;
   112 
   106 
   113     /**
   107     /**
   114              * alpha value of a transparent pixel.
   108      * Alpha value of a transparent pixel.
   115              */
   109      */
   116     private static final int TRANSPARENT = 0;
   110     private static final int TRANSPARENT = 0;
   117 
   111 
   118     /**
   112     /**
   119              * keypad and game keys composites.
   113      * Keypad and game keys composites.
   120              */
   114      */
   121     private Composite keypadComposite;
   115     private Composite keypadComposite;
   122     private Composite gameKeysComposite;
   116     private Composite gameKeysComposite;
   123     private Canvas canvas;
   117     private Canvas canvas;
   124 
   118 
   125     /**
   119     /**
   126              * CSS engine instance
   120      * CSS engine instance
   127              */
   121      */
   128     private BaseCSSEngine cssEngine;
   122     private BaseCSSEngine cssEngine;
   129 
   123 
   130     /**
   124     /**
   131             * Canvas keypad buttons.
   125      * Canvas keypad buttons.
   132             */
   126      */
   133 
       
   134     private Label center;
   127     private Label center;
   135     private Label up;
   128     private Label up;
   136     private Label down;
   129     private Label down;
   137     private Label left;
   130     private Label left;
   138     private Label right;
   131     private Label right;
   167 
   160 
   168     private int keyPressed;
   161     private int keyPressed;
   169     private Label rockerKeyPressed;
   162     private Label rockerKeyPressed;
   170 
   163 
   171     /**
   164     /**
   172              * Timer for handling long key press and mouse move events.
   165      * Timer for handling long key press and mouse move events.
   173              */
   166      */
   174     private Timer timer = new Timer();
   167     private Timer timer = new Timer();
   175     private CanvasKeypadTimerTask timerTask;
   168     private CanvasKeypadTimerTask timerTask;
   176 
   169 
   177     /**
   170     /**
   178              * Path of the osk graphics
   171      * Path of the osk graphics
   179              */
   172      */
   180     private static final String RESOURCE_PATH = "c:/data/images/";
   173     private static final String RESOURCE_PATH = "c:/data/images/";
   181 
   174 
   182     /**
   175     /**
   183              * offset on the composite in pixels where the osk rocker keys are layed out.
   176      * Offset on the composite in pixels where the osk rocker keys are layed
   184              */
   177      * out.
       
   178      */
   185     private int image_offset;
   179     private int image_offset;
   186 
   180 
   187 
   181     /**
   188     /**
   182      * Constructs a canvas kepad based on the JAD attribute
   189              * Constructs a canvas kepad based on the JAD attribute
   183      * 
   190              *
   184      * @param canvas
   191              * @param canvas - canvas for which the keypad is required.
   185      *            - canvas for which the keypad is required.
   192              * @param canvasComp - Composite corresponding to the canvas.
   186      * @param mode
   193              * @param mode- the jad attribute as a string.
   187      *            - the jad attribute as a string.
   194              */
   188      */
   195     CanvasKeypad(Canvas canvas, Composite canvasComp, String mode)
   189     CanvasKeypad(Canvas canvas, String mode)
   196     {
   190     {
   197 
   191 
   198         this.canvas = canvas;
   192         this.canvas = canvas;
   199         display = ESWTUIThreadRunner.getInstance().getDisplay();
   193         display = ESWTUIThreadRunner.getInstance().getDisplay();
   200 
   194 
   201         cssEngine = new BaseCSSEngine(display);
   195         cssEngine = new BaseCSSEngine(display);
   202 
   196 
   203 
   197         // Load the images
   204         //load the images
   198         center_normal = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_normal_c");
   205         center_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_c.svg");
   199         center_pressed = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_pressed_c");
   206         center_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_c.svg");
   200         left_normal = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_normal_l");
   207         left_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_l.svg");
   201         left_pressed = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_pressed_l");
   208         left_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_l.svg");
   202         right_normal = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_normal_r");
   209         right_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_r.svg");
   203         right_pressed = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_pressed_r");
   210         right_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_r.svg");
   204         up_normal = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_normal_t");
   211         up_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_t.svg");
   205         up_pressed = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_pressed_t");
   212         up_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_t.svg");
   206         down_normal = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_normal_b");
   213         down_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_b.svg");
   207         down_pressed = ImageUtil.createImageFromTheme(display, "qtg_fr_rocker_pressed_b");
   214         down_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_b.svg");
   208 
   215 
   209         image_offset = (display.getBounds().width / 2) - (center_normal.getBounds().width / 2);
   216         image_offset = (display.getBounds().width/2) - (center_normal.getBounds().width/2);
   210 
   217 
   211         // Load image data required to determine the osk button pressed on mouse events
   218         //Load image data required to determine the osk button pressed on mouse events
       
   219         center_data = center_normal.getImageData();
   212         center_data = center_normal.getImageData();
   220         left_data = left_normal.getImageData();
   213         left_data = left_normal.getImageData();
   221         right_data = right_normal.getImageData();
   214         right_data = right_normal.getImageData();
   222         up_data = up_normal.getImageData();
   215         up_data = up_normal.getImageData();
   223         down_data = down_normal.getImageData();
   216         down_data = down_normal.getImageData();
   224 
   217 
   225 
   218         // Set form layout for the shell containing the canvas.
   226         //Set form layout for the shell containing the canvas.
       
   227         canvas.getShell().setLayout(new FormLayout());
   219         canvas.getShell().setLayout(new FormLayout());
   228 
   220 
   229         //Create the Navigation keypad
   221         // Create the Navigation keypad
   230         createNavKeypad();
   222         createNavKeypad();
   231 
   223 
   232         //check if game keys are needed
   224         // Check if game keys are needed
   233         if(mode.equalsIgnoreCase("gameactions"))
   225         if (mode.equalsIgnoreCase("gameactions"))
   234         {
   226         {
   235             //create the game key pad
       
   236             createGamePad();
   227             createGamePad();
   237         }
   228         }
   238 
   229 
   239 
   230         // Layout the game keys, navigation keys and the canvas composites on
   240         //Layout the game keys, navigation keys and the canvas composites on the shell using form layout
   231         // the shell using form layout
   241 
       
   242         FormData canvasData = new FormData();
   232         FormData canvasData = new FormData();
   243         canvasData.right = new FormAttachment(100);
   233         canvasData.right = new FormAttachment(100);
   244         canvasData.left = new FormAttachment(0);
   234         canvasData.left = new FormAttachment(0);
   245         canvasData.top = new FormAttachment(0);
   235         canvasData.top = new FormAttachment(0);
   246         canvasData.bottom = new FormAttachment(keypadComposite);
   236         canvasData.bottom = new FormAttachment(keypadComposite);
   247         canvasComp.setLayoutData(canvasData);
   237         canvas.getContentComp().setLayoutData(canvasData);
   248 
   238 
   249         FormData navKeyData = new FormData();
   239         FormData navKeyData = new FormData();
   250         navKeyData.right = new FormAttachment(100);
   240         navKeyData.right = new FormAttachment(100);
   251         navKeyData.left = new FormAttachment(0);
   241         navKeyData.left = new FormAttachment(0);
   252 
   242 
   253         if(gameKeysComposite != null)
   243         if (gameKeysComposite != null)
   254         {
   244         {
   255             //game keypad was created above, so layout the game keys.
   245             // Game keypad was created above, so layout the game keys.
   256             navKeyData.bottom = new FormAttachment(gameKeysComposite);
   246             navKeyData.bottom = new FormAttachment(gameKeysComposite);
   257         }
   247         }
   258         else
   248         else
   259         {
   249         {
   260             //game keys are not needed
   250             // Game keys are not needed
   261             navKeyData.bottom = new FormAttachment(100);
   251             navKeyData.bottom = new FormAttachment(100);
   262         }
   252         }
   263 
   253 
   264         keypadComposite.setLayoutData(navKeyData);
   254         keypadComposite.setLayoutData(navKeyData);
   265 
   255 
   266 
   256         if (gameKeysComposite != null)
   267         if(gameKeysComposite != null)
   257         {
   268         {
   258             // Game keys were created, layout the game keys
   269             //game keys were created, layout the game keys
       
   270             FormData gameKeyData = new FormData();
   259             FormData gameKeyData = new FormData();
   271             gameKeyData.right = new FormAttachment(100);
   260             gameKeyData.right = new FormAttachment(100);
   272             gameKeyData.left = new FormAttachment(0);
   261             gameKeyData.left = new FormAttachment(0);
   273             gameKeyData.bottom = new FormAttachment(100);
   262             gameKeyData.bottom = new FormAttachment(100);
   274             gameKeysComposite.setLayoutData(gameKeyData);
   263             gameKeysComposite.setLayoutData(gameKeyData);
   275         }
   264         }
   276 
   265 
   277         //layout the shell
       
   278         canvas.getShell().layout();
   266         canvas.getShell().layout();
   279 
   267     }
   280     }
   268 
   281 
   269     /**
   282     /**
   270      * creates the navigation keypad - the directional keys and selctio key.
   283              * creates the navigation keypad - the directional keys and selctio key.
   271      */
   284              */
       
   285     private void createNavKeypad()
   272     private void createNavKeypad()
   286     {
   273     {
   287 
   274         // Create the navigation key composite
   288         //Create the navigation key composite
       
   289 
       
   290         keypadComposite = new Composite(canvas.getShell(), SWT.NONE);
   275         keypadComposite = new Composite(canvas.getShell(), SWT.NONE);
   291 
   276 
   292         center = new Label(keypadComposite, SWT.NONE);
   277         center = new Label(keypadComposite, SWT.NONE);
   293         left = new Label(keypadComposite, SWT.NONE);
   278         left = new Label(keypadComposite, SWT.NONE);
   294         right = new Label(keypadComposite, SWT.NONE);
   279         right = new Label(keypadComposite, SWT.NONE);
   304         up.setImage(up_normal);
   289         up.setImage(up_normal);
   305         up.pack();
   290         up.pack();
   306         down.setImage(down_normal);
   291         down.setImage(down_normal);
   307         down.pack();
   292         down.pack();
   308 
   293 
   309         //layout the navigation keys
   294         // layout the navigation keys
   310         keypadComposite.setLayout(new FormLayout());
   295         keypadComposite.setLayout(new FormLayout());
   311 
   296 
   312 
       
   313         FormData navkeysFormData = new FormData();
   297         FormData navkeysFormData = new FormData();
   314         navkeysFormData.top=new FormAttachment(0);
   298         navkeysFormData.top = new FormAttachment(0);
   315         navkeysFormData.left = new FormAttachment(0,image_offset);
   299         navkeysFormData.left = new FormAttachment(0, image_offset);
   316         center.setLayoutData(navkeysFormData);
   300         center.setLayoutData(navkeysFormData);
   317         left.setLayoutData(navkeysFormData);
   301         left.setLayoutData(navkeysFormData);
   318         right.setLayoutData(navkeysFormData);
   302         right.setLayoutData(navkeysFormData);
   319         up.setLayoutData(navkeysFormData);
   303         up.setLayoutData(navkeysFormData);
   320         down.setLayoutData(navkeysFormData);
   304         down.setLayoutData(navkeysFormData);
   321 
   305 
   322         keypadComposite.pack();
   306         keypadComposite.pack(); // will also layout
   323 
   307 
   324         keypadComposite.layout();
   308         // The down Label will catch all mouse down events as it is covering
   325 
   309         // all other rocker Labels, with the assumption that they all have 
       
   310         // the same bounds!
       
   311         down.addMouseListener(this);
       
   312         down.addMouseMoveListener(this);
       
   313         
       
   314         // We want to catch all drag events occurring on top of the rocker 
       
   315         // including those starting outside. 
   326         keypadComposite.addMouseListener(this);
   316         keypadComposite.addMouseListener(this);
   327         keypadComposite.addMouseMoveListener(this);
   317         keypadComposite.addMouseMoveListener(this);
   328 
   318     }
   329     }
   319 
   330 
   320     /**
   331 
   321      * creates the game keypad - the game action keys A, B, C and D.
   332     /**
   322      */
   333              * creates the game keypad - the game action keys A, B, C and D.
       
   334              */
       
   335     private void createGamePad()
   323     private void createGamePad()
   336     {
   324     {
   337         //create the game pad composite
   325         // create the game pad composite
   338         gameKeysComposite = new Composite(canvas.getShell(), SWT.NONE);
   326         gameKeysComposite = new Composite(canvas.getShell(), SWT.NONE);
   339         gameKeysComposite.setLayout(new FormLayout());
   327         gameKeysComposite.setLayout(new FormLayout());
   340 
   328 
   341         //Add the game buttons
   329         // Add the game buttons
   342         gameA = new Button(gameKeysComposite, SWT.PUSH);
   330         gameA = new Button(gameKeysComposite, SWT.PUSH);
   343         gameA.addMouseListener(this);
   331         gameA.addMouseListener(this);
   344 
   332 
   345         gameB = new Button(gameKeysComposite, SWT.PUSH);
   333         gameB = new Button(gameKeysComposite, SWT.PUSH);
   346         gameB.addMouseListener(this);
   334         gameB.addMouseListener(this);
   349         gameC.addMouseListener(this);
   337         gameC.addMouseListener(this);
   350 
   338 
   351         gameD = new Button(gameKeysComposite, SWT.PUSH);
   339         gameD = new Button(gameKeysComposite, SWT.PUSH);
   352         gameD.addMouseListener(this);
   340         gameD.addMouseListener(this);
   353 
   341 
   354         //Set the game key style sheets
   342         // Set the game key style sheets
   355         cssEngine.applyCSS(gameA,"QPushButton" +
   343         cssEngine.applyCSS(gameA, "QPushButton" + "{" + "background-color:white;"
   356                            "{" +
   344             + "border-style: outset;" + "border-width: 1px; " + "border-color: black;"
   357                            "background-color:white;" +
   345             + "border-radius: 2px;" + "font: bold 14px;" + "min-height: 2em;" + "min-width: 5.1em;"
   358                            "border-style: outset;" +
   346             + "image: url(" + RESOURCE_PATH + "qtg_mono_game_a.svg);" + "}"
   359                            "border-width: 1px; " +
   347             + "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_a.svg);"
   360                            "border-color: black;" +
   348             + " border-style: inset;" + "}");
   361                            "border-radius: 2px;" +
   349         cssEngine.applyCSS(gameB, "QPushButton" + "{" + "background-color:white;"
   362                            "font: bold 14px;" +
   350             + "border-style: outset;" + "border-width: 1px; " + "border-color: black;"
   363                            "min-height: 2em;" +
   351             + "border-radius: 2px;" + "font: bold 14px;" + "min-height: 2em;" + "min-width: 5.1em;"
   364                            "min-width: 5.1em;" +
   352             + "image: url(" + RESOURCE_PATH + "qtg_mono_game_b.svg);" + "}"
   365                            "image: url(" + RESOURCE_PATH + "qtg_mono_game_a.svg);" +
   353             + "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_b.svg);"
   366                            "}" +
   354             + " border-style: inset;" + "}");
   367                            "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_a.svg);" +
   355         cssEngine.applyCSS(gameC, "QPushButton" + "{" + "background-color:white;"
   368                            " border-style: inset;" +
   356             + "border-style: outset;" + "border-width: 1px; " + "border-color: black;"
   369                            "}");
   357             + "border-radius: 2px;" + "font: bold 14px;" + "min-height: 2em;" + "min-width: 5.1em;"
   370         cssEngine.applyCSS(gameB,"QPushButton" +
   358             + "image: url(" + RESOURCE_PATH + "qtg_mono_game_c.svg);" + "}"
   371                            "{" +
   359             + "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_c.svg);"
   372                            "background-color:white;" +
   360             + " border-style: inset;" + "}");
   373                            "border-style: outset;" +
   361         cssEngine.applyCSS(gameD, "QPushButton" + "{" + "background-color:white;"
   374                            "border-width: 1px; " +
   362             + "border-style: outset;" + "border-width: 1px; " + "border-color: black;"
   375                            "border-color: black;" +
   363             + "border-radius: 2px;" + "font: bold 14px;" + "min-height: 2em;" + "min-width: 5.1em;"
   376                            "border-radius: 2px;" +
   364             + "image: url(" + RESOURCE_PATH + "qtg_mono_game_d.svg);" + "}"
   377                            "font: bold 14px;" +
   365             + "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_d.svg);"
   378                            "min-height: 2em;" +
   366             + " border-style: inset;" + "}");
   379                            "min-width: 5.1em;" +
   367 
   380                            "image: url(" + RESOURCE_PATH + "qtg_mono_game_b.svg);" +
   368         // layout the game Keys
   381                            "}" +
       
   382                            "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_b.svg);" +
       
   383                            " border-style: inset;" +
       
   384                            "}");
       
   385         cssEngine.applyCSS(gameC,"QPushButton" +
       
   386                            "{" +
       
   387                            "background-color:white;" +
       
   388                            "border-style: outset;" +
       
   389                            "border-width: 1px; " +
       
   390                            "border-color: black;" +
       
   391                            "border-radius: 2px;" +
       
   392                            "font: bold 14px;" +
       
   393                            "min-height: 2em;" +
       
   394                            "min-width: 5.1em;" +
       
   395                            "image: url(" + RESOURCE_PATH + "qtg_mono_game_c.svg);" +
       
   396                            "}" +
       
   397                            "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_c.svg);" +
       
   398                            " border-style: inset;" +
       
   399                            "}");
       
   400         cssEngine.applyCSS(gameD,"QPushButton" +
       
   401                            "{" +
       
   402                            "background-color:white;" +
       
   403                            "border-style: outset;" +
       
   404                            "border-width: 1px; " +
       
   405                            "border-color: black;" +
       
   406                            "border-radius: 2px;" +
       
   407                            "font: bold 14px;" +
       
   408                            "min-height: 2em;" +
       
   409                            "min-width: 5.1em;" +
       
   410                            "image: url(" + RESOURCE_PATH + "qtg_mono_game_d.svg);" +
       
   411                            "}" +
       
   412                            "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_d.svg);" +
       
   413                            " border-style: inset;" +
       
   414                            "}");
       
   415 
       
   416 
       
   417         //layout the game Keys
       
   418         FormData gameAFormData = new FormData();
   369         FormData gameAFormData = new FormData();
   419         gameAFormData.bottom=new FormAttachment(100,0);
   370         gameAFormData.bottom = new FormAttachment(100, 0);
   420         gameAFormData.left=new FormAttachment(0,0);
   371         gameAFormData.left = new FormAttachment(0, 0);
   421         gameA.setLayoutData(gameAFormData);
   372         gameA.setLayoutData(gameAFormData);
   422 
   373 
   423         FormData gameBFormData = new FormData();
   374         FormData gameBFormData = new FormData();
   424         gameBFormData.bottom=new FormAttachment(100,0);
   375         gameBFormData.bottom = new FormAttachment(100, 0);
   425         gameBFormData.left=new FormAttachment(gameA,0);
   376         gameBFormData.left = new FormAttachment(gameA, 0);
   426         gameB.setLayoutData(gameBFormData);
   377         gameB.setLayoutData(gameBFormData);
   427 
   378 
   428         FormData gameCFormData = new FormData();
   379         FormData gameCFormData = new FormData();
   429         gameCFormData.bottom=new FormAttachment(100,0);
   380         gameCFormData.bottom = new FormAttachment(100, 0);
   430         gameCFormData.left=new FormAttachment(gameB,0);
   381         gameCFormData.left = new FormAttachment(gameB, 0);
   431         gameC.setLayoutData(gameCFormData);
   382         gameC.setLayoutData(gameCFormData);
   432 
   383 
   433         FormData gameDFormData = new FormData();
   384         FormData gameDFormData = new FormData();
   434         gameDFormData.bottom=new FormAttachment(100,0);
   385         gameDFormData.bottom = new FormAttachment(100, 0);
   435         gameDFormData.left=new FormAttachment(gameC,0);
   386         gameDFormData.left = new FormAttachment(gameC, 0);
   436         gameD.setLayoutData(gameDFormData);
   387         gameD.setLayoutData(gameDFormData);
   437 
   388 
   438         gameKeysComposite.layout();
   389         gameKeysComposite.layout();
   439     }
   390     }
   440 
   391 
   441     /**
   392     /**
   442              * Handles orientation changes.
   393      * Handles orientation changes.
   443              */
   394      */
   444     void handleOrientationChange(int mode)
   395     void handleOrientationChange(int mode)
   445     {
   396     {
   446         //change the layout as per orientation mode
   397         // change the layout as per orientation mode
   447     }
   398     }
   448 
   399 
   449 
   400     /**
   450     /**
   401      * Sets the currect Canvas for Key events.
   451              * Adds LSk and RSK keys to navigation keypad if the canvas is set to full screen mode.
   402      * 
   452              * @param mode- Indicates if the canvas is in fullscreen mode or not. True if fullscreen, False otherwise.
   403      * @param canvas
   453              */
   404      *            - Current Canvas to which the key events needs to be
       
   405      *            delivered.
       
   406      */
       
   407     void setCurrentCanvas(Canvas canvas)
       
   408     {
       
   409         this.canvas = canvas;
       
   410     }
       
   411 
       
   412     /**
       
   413      * Adds LSk and RSK keys to navigation keypad if the canvas is set to full
       
   414      * screen mode.
       
   415      * 
       
   416      * @param mode
       
   417      *            - Indicates if the canvas is in fullscreen mode or not. True
       
   418      *            if fullscreen, False otherwise.
       
   419      */
   454     void setFullScreenMode(boolean mode)
   420     void setFullScreenMode(boolean mode)
   455     {
   421     {
   456 
   422 
   457         //add the soft keys if the canvas is set to full screen mode/remove otherwise
   423         // add the soft keys if the canvas is set to full screen mode/remove
   458         if(mode == true)
   424         // otherwise
   459         {
   425         if (mode == true)
   460 
   426         {
   461             if(keypadComposite != null)
   427 
       
   428             if (keypadComposite != null)
   462             {
   429             {
   463 
   430 
   464                 Color black = display.getSystemColor(SWT.COLOR_BLACK);
   431                 Color black = display.getSystemColor(SWT.COLOR_BLACK);
   465                 keypadComposite.setBackground(black);
   432                 keypadComposite.setBackground(black);
   466 
   433 
   468                 skright.addMouseListener(this);
   435                 skright.addMouseListener(this);
   469 
   436 
   470                 skleft = new Button(keypadComposite, SWT.PUSH);
   437                 skleft = new Button(keypadComposite, SWT.PUSH);
   471                 skleft.addMouseListener(this);
   438                 skleft.addMouseListener(this);
   472 
   439 
   473                 //Set the style sheet
   440                 // Set the style sheet
   474                 cssEngine.applyCSS(skright,"QPushButton" +
   441                 cssEngine.applyCSS(skright, "QPushButton" + "{" + "background-color:white;"
   475                                    "{" +
   442                     + "border-style: outset;" + "border-width: 1px; "
   476                                    "background-color:white;" +
   443                     + "border-bottom-left-radius: 22px;" + "border-color: black;"
   477                                    "border-style: outset;" +
   444                     + "font: bold 14px;" + "min-width: 3em;" + "min-height: 2em;" + "image: url("
   478                                    "border-width: 1px; " +
   445                     + RESOURCE_PATH + "qtg_mono_rsk_horizontal.svg);" + "}"
   479                                    "border-bottom-left-radius: 22px;"+
   446                     + "QPushButton:pressed { image: url(" + RESOURCE_PATH
   480                                    "border-color: black;" +
   447                     + "qtg_mono_rsk_horizontal.svg);" + " border-style: inset;" + "}");
   481                                    "font: bold 14px;" +
   448 
   482                                    "min-width: 3em;" +
   449                 cssEngine.applyCSS(skleft, "QPushButton" + "{" + "background-color:white;"
   483                                    "min-height: 2em;" +
   450                     + "border-style: outset;" + "border-width: 1px; "
   484                                    "image: url(" + RESOURCE_PATH + "qtg_mono_rsk_horizontal.svg);" +
   451                     + "border-bottom-right-radius: 22px;" + "border-color: black;"
   485                                    "}" +
   452                     + "font: bold 14px;" + "min-width: 3em;" + "min-height: 2em;" + "image: url("
   486                                    "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_rsk_horizontal.svg);" +
   453                     + RESOURCE_PATH + "qtg_mono_lsk_horizontal.svg);" + "}"
   487                                    " border-style: inset;" +
   454                     + "QPushButton:pressed { image: url(" + RESOURCE_PATH
   488                                    "}");
   455                     + "qtg_mono_lsk_horizontal.svg);" + " border-style: inset;" + "}");
   489 
       
   490                 cssEngine.applyCSS(skleft,"QPushButton" +
       
   491                                    "{" +
       
   492                                    "background-color:white;" +
       
   493                                    "border-style: outset;" +
       
   494                                    "border-width: 1px; " +
       
   495                                    "border-bottom-right-radius: 22px;"+
       
   496                                    "border-color: black;" +
       
   497                                    "font: bold 14px;" +
       
   498                                    "min-width: 3em;" +
       
   499                                    "min-height: 2em;" +
       
   500                                    "image: url(" + RESOURCE_PATH + "qtg_mono_lsk_horizontal.svg);" +
       
   501                                    "}" +
       
   502                                    "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_lsk_horizontal.svg);" +
       
   503                                    " border-style: inset;" +
       
   504                                    "}");
       
   505 
       
   506 
   456 
   507                 FormData sklFormData = new FormData();
   457                 FormData sklFormData = new FormData();
   508                 sklFormData.top=new FormAttachment(0, 0);
   458                 sklFormData.top = new FormAttachment(0, 0);
   509                 sklFormData.left=new FormAttachment(0, 0);
   459                 sklFormData.left = new FormAttachment(0, 0);
   510                 skleft.setLayoutData(sklFormData);
   460                 skleft.setLayoutData(sklFormData);
   511 
   461 
   512                 FormData skrFormData = new FormData();
   462                 FormData skrFormData = new FormData();
   513                 skrFormData.top=new FormAttachment(0, 0);
   463                 skrFormData.top = new FormAttachment(0, 0);
   514                 skrFormData.right=new FormAttachment(100, 0);
   464                 skrFormData.right = new FormAttachment(100, 0);
   515                 skright.setLayoutData(skrFormData);
   465                 skright.setLayoutData(skrFormData);
   516 
   466 
   517                 keypadComposite.layout();
   467                 keypadComposite.layout();
   518             }
   468             }
   519         }
   469         }
   520         else
   470         else
   521         {
   471         {
   522             //LSK and RSK buttons are not needed, remove the buttons from navigation keypad
   472             // LSK and RSK buttons are not needed, remove the buttons from
   523             if(skright != null)
   473             // navigation keypad
       
   474             if (skright != null)
   524                 skright.dispose();
   475                 skright.dispose();
   525             if(skleft != null)
   476             if (skleft != null)
   526                 skleft.dispose();
   477                 skleft.dispose();
   527 
   478 
   528             keypadComposite.setBackground(null);
   479             keypadComposite.setBackground(null);
   529         }
   480         }
   530 
   481 
   531     }
   482     }
   532 
   483 
   533     /**
   484     /**
   534              * returns the height of the canvas keypad in pixels.
   485      * returns the height of the canvas keypad in pixels.
   535              * @return Height of the canvas keypad in pixels.
   486      * 
   536              */
   487      * @return Height of the canvas keypad in pixels.
       
   488      */
   537     int getHeight()
   489     int getHeight()
   538     {
   490     {
   539 
   491 
   540         int navKeyPadHeight = 0;
   492         int navKeyPadHeight = 0;
   541         int gameKeyPadHeight = 0;
   493         int gameKeyPadHeight = 0;
   542 
   494 
   543         if(keypadComposite != null)
   495         if (keypadComposite != null)
   544         {
   496         {
   545             //calculate the navigation keypad height
   497             // calculate the navigation keypad height
   546             navKeyPadHeight = keypadComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
   498             navKeyPadHeight = keypadComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
   547         }
   499         }
   548 
   500 
   549         if(gameKeysComposite != null)
   501         if (gameKeysComposite != null)
   550         {
   502         {
   551             //calculate the game keypad height
   503             // calculate the game keypad height
   552             gameKeyPadHeight = gameKeysComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
   504             gameKeyPadHeight = gameKeysComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
   553         }
   505         }
   554 
   506 
   555         //return the total height
   507         // return the total height
   556         return (navKeyPadHeight + gameKeyPadHeight);
   508         return (navKeyPadHeight + gameKeyPadHeight);
   557     }
   509     }
   558 
   510 
   559     /**
   511     /**
   560              * returns the keycode corresponding to the canvas keypad button.
   512      * returns the keycode corresponding to the canvas keypad button.
   561              * @return keycode.
   513      * 
   562              */
   514      * @return keycode.
       
   515      */
   563     private int getKeyCode(Widget w)
   516     private int getKeyCode(Widget w)
   564     {
   517     {
   565 
       
   566         int keyCode = INVALID_CODE;
   518         int keyCode = INVALID_CODE;
   567 
   519 
   568         // Find out which osk button was pressed
   520         // Find out which osk button was pressed
   569         if(w == up)
   521         if (w == up)
   570         {
   522         {
   571             keyCode = UP;
   523             keyCode = UP;
   572         }
   524         }
   573         else if(w == down)
   525         else if (w == down)
   574         {
   526         {
   575             keyCode = DOWN;
   527             keyCode = DOWN;
   576         }
   528         }
   577         else if(w == left)
   529         else if (w == left)
   578         {
   530         {
   579             keyCode = LEFT;
   531             keyCode = LEFT;
   580         }
   532         }
   581         else if(w == right)
   533         else if (w == right)
   582         {
   534         {
   583             keyCode = RIGHT;
   535             keyCode = RIGHT;
   584         }
   536         }
   585         else if(w == center)
   537         else if (w == center)
   586         {
   538         {
   587             keyCode = FIRE;
   539             keyCode = FIRE;
   588         }
   540         }
   589         else if(w == skleft)
   541         else if (w == skleft)
   590         {
   542         {
   591             keyCode = SLK;
   543             keyCode = SLK;
   592         }
   544         }
   593         else if(w == skright)
   545         else if (w == skright)
   594         {
   546         {
   595             keyCode = SRK;
   547             keyCode = SRK;
   596         }
   548         }
   597         else if(w == gameA)
   549         else if (w == gameA)
   598         {
   550         {
   599             keyCode = GAME_A;
   551             keyCode = GAME_A;
   600         }
   552         }
   601         else if(w == gameB)
   553         else if (w == gameB)
   602         {
   554         {
   603             keyCode = GAME_B;
   555             keyCode = GAME_B;
   604         }
   556         }
   605         else if(w == gameC)
   557         else if (w == gameC)
   606         {
   558         {
   607             keyCode = GAME_C;
   559             keyCode = GAME_C;
   608         }
   560         }
   609         else if(w == gameD)
   561         else if (w == gameD)
   610         {
   562         {
   611             keyCode = GAME_D;
   563             keyCode = GAME_D;
   612         }
   564         }
   613         else
   565         else
   614         {
   566         {
   615             keyCode = INVALID_CODE;;
   567             keyCode = INVALID_CODE;
   616         }
   568         }
   617 
   569 
   618         return keyCode;
   570         return keyCode;
   619     }
   571     }
   620 
   572 
   621 
   573     /**
   622     /**
   574      * Mouse listeners.
   623              * Mouse listeners.
   575      */
   624              */
       
   625 
       
   626     public void mouseDown(MouseEvent e)
   576     public void mouseDown(MouseEvent e)
   627     {
   577     {
   628 
       
   629         //handle the mouse down event
       
   630         handleMouseDown(e);
   578         handleMouseDown(e);
   631 
       
   632     }
   579     }
   633 
   580 
   634     public void mouseUp(MouseEvent e)
   581     public void mouseUp(MouseEvent e)
   635     {
   582     {
   636 
       
   637         //handle the mouse up event
       
   638         handleMouseUp(e);
   583         handleMouseUp(e);
   639 
       
   640     }
   584     }
   641 
   585 
   642     public void mouseMove(MouseEvent e)
   586     public void mouseMove(MouseEvent e)
   643     {
   587     {
   644 
       
   645         //handle the mouse move event
       
   646         handleMouseMove(e);
   588         handleMouseMove(e);
   647 
       
   648     }
   589     }
   649 
   590 
   650     public void mouseDoubleClick(MouseEvent event)
   591     public void mouseDoubleClick(MouseEvent event)
   651     {
   592     {
   652         //do nothing
   593         // Do nothing
   653     }
   594     }
   654 
   595 
   655 
   596     /**
   656 
   597      * Handles the mouse down event
   657 
   598      * Note that e.x and e.y are relative to the widget that was pressed.
   658 
   599      */
   659     /**
       
   660              * Handles the mouse down event
       
   661              *
       
   662              */
       
   663     private void handleMouseDown(MouseEvent e)
   600     private void handleMouseDown(MouseEvent e)
   664     {
   601     {
   665 
   602         if (e.widget == down) // highest in the z-order
   666         if(e.widget == keypadComposite)
   603         {
   667         {
       
   668             //the mouse down was grabbed by the keypad composite, map the key code based on the on rocker images.
       
   669             keyPressed = handleRockerPressed(e);
   604             keyPressed = handleRockerPressed(e);
   670 
       
   671         }
   605         }
   672         else
   606         else
   673         {
   607         {
   674             //get the keycode of the game or soft keys based on the button that was clicked
       
   675             keyPressed = getKeyCode(e.widget);
   608             keyPressed = getKeyCode(e.widget);
   676         }
   609         }
   677 
   610 
   678         if(keyPressed != INVALID_CODE)
   611         if (keyPressed != INVALID_CODE)
   679         {
   612         {
   680 
       
   681             //notify the canvas
       
   682             canvas.doKeyPressed(keyPressed);
   613             canvas.doKeyPressed(keyPressed);
   683 
   614 
   684             // Create and Schedule the long keypress timer
       
   685             timerTask = new CanvasKeypadTimerTask();
   615             timerTask = new CanvasKeypadTimerTask();
   686             timer.schedule(timerTask, KEYREPEAT_INTERVAL);
   616             timer.schedule(timerTask, KEYREPEAT_INTERVAL);
   687         }
   617         }
   688     }
   618     }
   689 
   619 
   690     /**
   620     /**
   691              * Handles the mouse up event
   621      * Handles the mouse up event
   692              *
   622      * Note that e.x and e.y are relative to the widget that was pressed.
   693              */
   623      */
   694     private void handleMouseUp(MouseEvent e)
   624     private void handleMouseUp(MouseEvent e)
   695     {
   625     {
   696         int keyReleased;
   626         int keyReleased;
   697 
   627 
   698         if(timerTask != null)
   628         if (timerTask != null)
   699         {
   629         {
   700             //cancel the long keypress timer if it is running
       
   701             timerTask.cancel();
   630             timerTask.cancel();
   702             timerTask = null;
   631             timerTask = null;
   703         }
   632         }
   704 
   633 
   705         if(e.widget == keypadComposite)
   634         if (e.widget == down || e.widget == keypadComposite)
   706         {
   635         {
   707             //the mouse up was grabbed by the keypad composite, map the key code based on the on rocker images.
       
   708             keyReleased = handleRockerReleased(e);
   636             keyReleased = handleRockerReleased(e);
   709 
       
   710         }
   637         }
   711         else
   638         else
   712         {
   639         {
   713             //get the keycode of the game or soft keys based on the button that was clicked
       
   714             keyReleased = getKeyCode(e.widget);
   640             keyReleased = getKeyCode(e.widget);
   715         }
   641         }
   716 
   642 
   717         if(keyReleased != INVALID_CODE)
   643         if (keyReleased != INVALID_CODE)
   718         {
   644         {
   719 
       
   720             //notify the canvas
       
   721             canvas.doKeyReleased(keyReleased);
   645             canvas.doKeyReleased(keyReleased);
   722         }
   646         }
   723     }
   647     }
   724 
   648 
   725 
   649     /**
   726     /**
   650      * Handles the mouse move event
   727              * Handles the mouse move event
   651      * Note that e.x and e.y are relative to the widget that was pressed.
   728              *
   652      */
   729              */
       
   730     private void handleMouseMove(MouseEvent e)
   653     private void handleMouseMove(MouseEvent e)
   731     {
   654     {
   732 
   655         if (timerTask != null)
   733         if(timerTask != null)
   656         {
   734         {
       
   735             //cancel the long keypress timer if it is running
       
   736             timerTask.cancel();
   657             timerTask.cancel();
   737             timerTask = null;
   658             timerTask = null;
   738         }
   659         }
   739 
   660         
   740         if(e.widget == keypadComposite)
   661         if (e.widget == down || e.widget == keypadComposite)
   741         {
   662         {
   742             //the mouse move was grabbed by the keypad composite, handle the rocker key swipe
   663             handleRockerMoved(e);    
   743             handleRockerMoved(e);
       
   744 
       
   745         }
   664         }
   746         else
   665         else
   747         {
   666         {
   748             //get the keycode of the game or soft keys based on the button that was clicked
       
   749             keyPressed = getKeyCode(e.widget);
   667             keyPressed = getKeyCode(e.widget);
   750 
       
   751             //notify the canvas
       
   752             canvas.doKeyPressed(keyPressed);
   668             canvas.doKeyPressed(keyPressed);
   753 
       
   754             // Create and Schedule Timer
       
   755             timerTask = new CanvasKeypadTimerTask();
   669             timerTask = new CanvasKeypadTimerTask();
   756             timer.schedule(timerTask, KEYREPEAT_INTERVAL);
   670             timer.schedule(timerTask, KEYREPEAT_INTERVAL);
   757         }
   671         }
   758 
   672     }
   759 
   673 
   760     }
   674     /**
   761 
   675      * Handles the rocker key press
   762 
   676      */
   763     /**
       
   764              * Handles the rocker key press
       
   765              *
       
   766              */
       
   767     private int handleRockerPressed(MouseEvent e)
   677     private int handleRockerPressed(MouseEvent e)
   768     {
   678     {
   769 
       
   770         keyPressed = INVALID_CODE;
   679         keyPressed = INVALID_CODE;
   771 
   680         
   772         Rectangle image_bounds = center.getBounds();
   681         Point rockerSize = center.getSize();
   773 
   682         if (e.x >= 0 && e.x <= rockerSize.x && e.y >= 0 && e.y <= rockerSize.y)
   774         // Find out which rocker button was pressed
   683         {
   775         if(image_bounds.contains(e.x,e.y) == true)
   684             if (center_data.getAlpha(e.x, e.y) != TRANSPARENT)
   776         {
   685             {
   777             //check each of the button images  to check which osk navkey button was pressed
       
   778             if(center_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
       
   779             {
       
   780                 //center button was pressed
       
   781                 keyPressed = FIRE;
   686                 keyPressed = FIRE;
   782                 rockerKeyPressed = center;
   687                 rockerKeyPressed = center;
   783                 center.setImage(center_pressed);
   688                 center.setImage(center_pressed);
   784             }
   689             }
   785             else if(left_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
   690             else if (left_data.getAlpha(e.x, e.y) != TRANSPARENT)
   786             {
   691             {
   787                 //left button was pressed
       
   788                 keyPressed = LEFT;
   692                 keyPressed = LEFT;
   789                 rockerKeyPressed = left;
   693                 rockerKeyPressed = left;
   790                 left.setImage(left_pressed);
   694                 left.setImage(left_pressed);
   791             }
   695             }
   792             else if(right_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
   696             else if (right_data.getAlpha(e.x, e.y) != TRANSPARENT)
   793             {
   697             {
   794                 //right button was pressed
       
   795                 keyPressed = RIGHT;
   698                 keyPressed = RIGHT;
   796                 rockerKeyPressed = right;
   699                 rockerKeyPressed = right;
   797                 right.setImage(right_pressed);
   700                 right.setImage(right_pressed);
   798             }
   701             }
   799             else if(up_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
   702             else if (up_data.getAlpha(e.x, e.y) != TRANSPARENT)
   800             {
   703             {
   801                 //up button was pressed
       
   802                 keyPressed = UP;
   704                 keyPressed = UP;
   803                 rockerKeyPressed = up;
   705                 rockerKeyPressed = up;
   804                 up.setImage(up_pressed);
   706                 up.setImage(up_pressed);
   805             }
   707             }
   806             else if(down_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
   708             else if (down_data.getAlpha(e.x, e.y) != TRANSPARENT)
   807             {
   709             {
   808                 //down button was pressed
       
   809                 keyPressed = DOWN;
   710                 keyPressed = DOWN;
   810                 rockerKeyPressed = down;
   711                 rockerKeyPressed = down;
   811                 down.setImage(down_pressed);
   712                 down.setImage(down_pressed);
   812             }
   713             }
   813             else
   714             else
   814             {
   715             {
   815                 keyPressed = INVALID_CODE;;
   716                 keyPressed = INVALID_CODE;
   816             }
   717             }
   817 
       
   818         }
   718         }
   819 
   719 
   820         return keyPressed;
   720         return keyPressed;
   821 
   721     }
   822     }
   722 
   823 
   723     /**
   824 
   724      * Handles the mouse released on the rocker keys
   825     /**
   725      */
   826              * Handles the mouse released on the rocker keys
       
   827              *
       
   828              */
       
   829     private int handleRockerReleased(MouseEvent e)
   726     private int handleRockerReleased(MouseEvent e)
   830     {
   727     {
   831 
       
   832         int keyReleased = INVALID_CODE;
   728         int keyReleased = INVALID_CODE;
   833 
   729 
   834         Rectangle image_bounds = center.getBounds();
   730         Point rockerSize = center.getSize();
   835 
   731         Point rockerLoc = center.getLocation();
   836         // Find out which rocker button was released
   732         
   837         if(image_bounds.contains(e.x,e.y) == true)
   733         if (e.widget == keypadComposite)
   838         {
   734         {
   839             //check each of the button images  to check which osk navkey button was released
   735             // Drag started outside rocker
   840             if((rockerKeyPressed == center) &&
   736             e.x -= rockerLoc.x;
   841                     (center_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
   737             e.y -= rockerLoc.y;
   842             {
   738         }
   843                 //center button was released
   739 
       
   740         if (e.x >= 0 && e.x <= rockerSize.x && e.y >= 0 && e.y <= rockerSize.y)
       
   741         {
       
   742             if ((rockerKeyPressed == center)
       
   743                 && (center_data.getAlpha(e.x, e.y) != TRANSPARENT))
       
   744             {
   844                 keyReleased = FIRE;
   745                 keyReleased = FIRE;
   845                 center.setImage(center_normal);
   746                 center.setImage(center_normal);
   846             }
   747             }
   847             else if((rockerKeyPressed == left) &&
   748             else if ((rockerKeyPressed == left)
   848                     (left_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
   749                 && (left_data.getAlpha(e.x, e.y) != TRANSPARENT))
   849             {
   750             {
   850                 //left button was released
       
   851                 keyReleased = LEFT;
   751                 keyReleased = LEFT;
   852                 left.setImage(left_normal);
   752                 left.setImage(left_normal);
   853             }
   753             }
   854             else if((rockerKeyPressed == right) &&
   754             else if ((rockerKeyPressed == right)
   855                     (right_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
   755                 && (right_data.getAlpha(e.x, e.y) != TRANSPARENT))
   856             {
   756             {
   857                 //right button was released
       
   858                 keyReleased = RIGHT;
   757                 keyReleased = RIGHT;
   859                 right.setImage(right_normal);
   758                 right.setImage(right_normal);
   860             }
   759             }
   861             else if((rockerKeyPressed == up) &&
   760             else if ((rockerKeyPressed == up)
   862                     (up_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
   761                 && (up_data.getAlpha(e.x, e.y) != TRANSPARENT))
   863             {
   762             {
   864                 //up button was released
       
   865                 keyReleased = UP;
   763                 keyReleased = UP;
   866                 up.setImage(up_normal);
   764                 up.setImage(up_normal);
   867             }
   765             }
   868             else if((rockerKeyPressed == down) &&
   766             else if ((rockerKeyPressed == down)
   869                     (down_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
   767                 && (down_data.getAlpha(e.x, e.y) != TRANSPARENT))
   870             {
   768             {
   871                 //down button was released
       
   872                 keyReleased = DOWN;
   769                 keyReleased = DOWN;
   873                 down.setImage(down_normal);
   770                 down.setImage(down_normal);
   874             }
   771             }
   875             else
   772             else
   876             {
   773             {
   877                 keyReleased = INVALID_CODE;;
   774                 keyReleased = INVALID_CODE;
   878             }
   775             }
   879 
       
   880         }
   776         }
   881 
   777 
   882         return keyReleased;
   778         return keyReleased;
   883 
   779     }
   884     }
   780 
   885 
   781     /**
   886 
   782      * Handles the mouse move on the rocker keys
   887     /**
   783      */
   888              * Handles the mouse move on the rocker keys
       
   889              *
       
   890              */
       
   891     private void handleRockerMoved(MouseEvent e)
   784     private void handleRockerMoved(MouseEvent e)
   892     {
   785     {
   893 
       
   894         keyPressed = INVALID_CODE;
   786         keyPressed = INVALID_CODE;
   895 
   787         Point size = center.getSize();
   896         Rectangle image_bounds = center.getBounds();
   788         Point rockerLoc = center.getLocation();
   897 
   789         
   898         // Find out which rocker button was traversed to
   790         if (e.widget == keypadComposite)
   899         if(image_bounds.contains(e.x,e.y) == true)
   791         {
   900         {
   792             // Drag started outside rocker
   901             //check each of the button images  to check which osk navkey button was traversed to
   793             e.x -= rockerLoc.x;
   902             if((center_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
   794             e.y -= rockerLoc.y;
   903                     (rockerKeyPressed != center))
   795         }
   904             {
   796 
   905                 //Mouse moved to center rocker key from another rocker key, release the other(source) rocker key
   797         if (e.x >= 0 && e.x <= size.x && e.y >= 0 && e.y <= size.y)
       
   798         {
       
   799             if ((center_data.getAlpha(e.x, e.y) != TRANSPARENT)
       
   800                 && (rockerKeyPressed != center))
       
   801             {
       
   802                 // Mouse moved to center rocker key from another rocker key,
       
   803                 // release the other(source) rocker key
   906                 releaseRockerKey(rockerKeyPressed);
   804                 releaseRockerKey(rockerKeyPressed);
   907             }
   805             }
   908             else if((left_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
   806             else if ((left_data.getAlpha(e.x, e.y) != TRANSPARENT)
   909                     (rockerKeyPressed != left))
   807                 && (rockerKeyPressed != left))
   910             {
   808             {
   911                 //Mouse moved to left rocker key from another rocker key, release the other(source) rocker key
   809                 // Mouse moved to left rocker key from another rocker key,
       
   810                 // release the other(source) rocker key
   912                 // and press the left key
   811                 // and press the left key
   913                 releaseRockerKey(rockerKeyPressed);
   812                 releaseRockerKey(rockerKeyPressed);
   914                 keyPressed = LEFT;
   813                 keyPressed = LEFT;
   915                 left.setImage(left_pressed);
   814                 left.setImage(left_pressed);
   916                 rockerKeyPressed = left;
   815                 rockerKeyPressed = left;
   917                 canvas.doKeyPressed(keyPressed);
   816                 canvas.doKeyPressed(keyPressed);
   918                 pressedKeyData = left_data;
   817                 pressedKeyData = left_data;
   919             }
   818             }
   920             else if((right_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
   819             else if ((right_data.getAlpha(e.x, e.y) != TRANSPARENT)
   921                     (rockerKeyPressed != right))
   820                 && (rockerKeyPressed != right))
   922             {
   821             {
   923                 //Mouse moved to right rocker key from another rocker key, release the other(source) rocker key
   822                 // Mouse moved to right rocker key from another rocker key,
       
   823                 // release the other(source) rocker key
   924                 // and press the right key
   824                 // and press the right key
   925                 releaseRockerKey(rockerKeyPressed);
   825                 releaseRockerKey(rockerKeyPressed);
   926                 keyPressed = RIGHT;
   826                 keyPressed = RIGHT;
   927                 right.setImage(right_pressed);
   827                 right.setImage(right_pressed);
   928                 rockerKeyPressed = right;
   828                 rockerKeyPressed = right;
   929                 canvas.doKeyPressed(keyPressed);
   829                 canvas.doKeyPressed(keyPressed);
   930                 pressedKeyData = right_data;
   830                 pressedKeyData = right_data;
   931             }
   831             }
   932             else if((up_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
   832             else if ((up_data.getAlpha(e.x, e.y) != TRANSPARENT)
   933                     (rockerKeyPressed != up))
   833                 && (rockerKeyPressed != up))
   934             {
   834             {
   935                 //Mouse moved to up rocker key from another rocker key, release the other(source) rocker key
   835                 // Mouse moved to up rocker key from another rocker key, release
       
   836                 // the other(source) rocker key
   936                 // and press the up key
   837                 // and press the up key
   937                 releaseRockerKey(rockerKeyPressed);
   838                 releaseRockerKey(rockerKeyPressed);
   938                 keyPressed = UP;
   839                 keyPressed = UP;
   939                 up.setImage(up_pressed);
   840                 up.setImage(up_pressed);
   940                 canvas.doKeyPressed(keyPressed);
   841                 canvas.doKeyPressed(keyPressed);
   941                 rockerKeyPressed = up;
   842                 rockerKeyPressed = up;
   942                 pressedKeyData = up_data;
   843                 pressedKeyData = up_data;
   943             }
   844             }
   944             else if((down_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
   845             else if ((down_data.getAlpha(e.x, e.y) != TRANSPARENT)
   945                     (rockerKeyPressed != down))
   846                 && (rockerKeyPressed != down))
   946             {
   847             {
   947                 //Mouse moved to down rocker key from another rocker key, release the other(source) rocker key
   848                 // Mouse moved to down rocker key from another rocker key,
       
   849                 // release the other(source) rocker key
   948                 // and press the down key
   850                 // and press the down key
   949                 releaseRockerKey(rockerKeyPressed);
   851                 releaseRockerKey(rockerKeyPressed);
   950                 keyPressed = DOWN;
   852                 keyPressed = DOWN;
   951                 down.setImage(down_pressed);
   853                 down.setImage(down_pressed);
   952                 rockerKeyPressed = down;
   854                 rockerKeyPressed = down;
   953                 canvas.doKeyPressed(keyPressed);
   855                 canvas.doKeyPressed(keyPressed);
   954                 pressedKeyData = down_data;
   856                 pressedKeyData = down_data;
   955             }
   857             }
   956             else
   858             else
   957             {
   859             {
   958                 if((pressedKeyData != null) &&
   860                 if ((pressedKeyData != null)
   959                         (pressedKeyData.getAlpha(e.x-image_offset,e.y) == TRANSPARENT))
   861                     && (pressedKeyData.getAlpha(e.x, e.y) == TRANSPARENT))
   960                 {
   862                 {
   961                     //Mouse moved to out of all rocker keys from another rocker key, release the other(source) rocker key
   863                     // Mouse moved to out of all rocker keys from another rocker
       
   864                     // key, release the other(source) rocker key
   962                     releaseRockerKey(rockerKeyPressed);
   865                     releaseRockerKey(rockerKeyPressed);
   963                     pressedKeyData = null;
   866                     pressedKeyData = null;
   964                     rockerKeyPressed = null;
   867                     rockerKeyPressed = null;
   965 
   868 
   966                 }
   869                 }
   967             }
   870             }
   968 
       
   969         }
   871         }
   970         else
   872         else
   971         {
   873         {
   972             //Mouse moved to out of all rocker keys, release the other(source) rocker key
   874             // Mouse moved to out of all rocker keys, release the other(source)
       
   875             // rocker key
   973             releaseRockerKey(rockerKeyPressed);
   876             releaseRockerKey(rockerKeyPressed);
   974         }
   877         }
   975 
   878     }
   976 
   879 
   977     }
   880     /**
   978 
   881      * Releases a rocker key
   979 
   882      */
   980 
       
   981     /**
       
   982              * Releases a rocker key
       
   983              *
       
   984              */
       
   985     private void releaseRockerKey(Label rockerKeyPressed)
   883     private void releaseRockerKey(Label rockerKeyPressed)
   986     {
   884     {
   987 
   885         if (rockerKeyPressed == center)
   988         if(rockerKeyPressed == center)
       
   989         {
   886         {
   990             center.setImage(center_normal);
   887             center.setImage(center_normal);
   991             canvas.doKeyReleased(FIRE);
   888             canvas.doKeyReleased(FIRE);
   992         }
   889         }
   993         else if(rockerKeyPressed == left)
   890         else if (rockerKeyPressed == left)
   994         {
   891         {
   995             left.setImage(left_normal);
   892             left.setImage(left_normal);
   996             canvas.doKeyReleased(LEFT);
   893             canvas.doKeyReleased(LEFT);
   997         }
   894         }
   998         else if(rockerKeyPressed == right)
   895         else if (rockerKeyPressed == right)
   999         {
   896         {
  1000             right.setImage(right_normal);
   897             right.setImage(right_normal);
  1001             canvas.doKeyReleased(RIGHT);
   898             canvas.doKeyReleased(RIGHT);
  1002         }
   899         }
  1003         else if(rockerKeyPressed == up)
   900         else if (rockerKeyPressed == up)
  1004         {
   901         {
  1005             up.setImage(up_normal);
   902             up.setImage(up_normal);
  1006             canvas.doKeyReleased(UP);
   903             canvas.doKeyReleased(UP);
  1007         }
   904         }
  1008         else if(rockerKeyPressed == down)
   905         else if (rockerKeyPressed == down)
  1009         {
   906         {
  1010             down.setImage(down_normal);
   907             down.setImage(down_normal);
  1011             canvas.doKeyReleased(DOWN);
   908             canvas.doKeyReleased(DOWN);
  1012         }
   909         }
  1013 
   910     }
  1014     }
   911 
  1015 
   912     /**
  1016 
   913      * Canvas Keypad timer task
  1017 
   914      */
  1018     /**
       
  1019              * Canvas Keypad timer task
       
  1020              *
       
  1021              */
       
  1022     class CanvasKeypadTimerTask extends TimerTask
   915     class CanvasKeypadTimerTask extends TimerTask
  1023     {
   916     {
  1024 
       
  1025         public void run()
   917         public void run()
  1026         {
   918         {
  1027 
       
  1028             ESWTUIThreadRunner.syncExec(new Runnable()
   919             ESWTUIThreadRunner.syncExec(new Runnable()
  1029             {
   920             {
  1030                 public void run()
   921                 public void run()
  1031                 {
   922                 {
  1032                     canvas.doKeyPressed(keyPressed);
   923                     canvas.doKeyPressed(keyPressed);
  1033                 }
   924                 }
  1034             });
   925             });
  1035 
   926         }
  1036 
   927     }
  1037         }
       
  1038     }
       
  1039 
       
  1040 
       
  1041 
       
  1042 }
   928 }