javauis/lcdui_qt/src/javax/microedition/lcdui/CanvasKeypad.java
changeset 26 dc7c549001d5
parent 23 98ccebc37403
child 47 f40128debb5d
child 57 59b3b4473dc8
equal deleted inserted replaced
23:98ccebc37403 26:dc7c549001d5
     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;
    19 import java.util.Vector;
    34 import org.eclipse.swt.layout.FormLayout;
    34 import org.eclipse.swt.layout.FormLayout;
    35 
    35 
    36 import javax.microedition.lcdui.EventDispatcher.LCDUIEvent;
    36 import javax.microedition.lcdui.EventDispatcher.LCDUIEvent;
    37 
    37 
    38 
    38 
    39 
    39 /**
    40 final class CanvasKeypad
    40  * The CanvasKeypad class is designed to generate the on screen keypad
       
    41  * and handle graphical operations as well as low-level pointer events.
       
    42  * CanvasKeypad maps the pointer events to key events that are needed by the Canvas.
       
    43  *
       
    44  */
       
    45 final class CanvasKeypad implements MouseListener, MouseMoveListener
    41 {
    46 {
    42 
    47 
    43     /**
    48     /**
    44      *  Constant for <code>UP</code> game action.
    49              *  Constant for <code>UP</code> game action.
    45      */
    50              */
    46     private static final int UP = -1;
    51     private static final int UP = -1;
    47 
    52 
    48     /**
    53     /**
    49      * Constant for <code>DOWN</code> game action.
    54              * Constant for <code>DOWN</code> game action.
    50      */
    55              */
    51     private static final int DOWN = -2;
    56     private static final int DOWN = -2;
    52 
    57 
    53     /**
    58     /**
    54      * Constant for <code>LEFT</code> game action.
    59              * Constant for <code>LEFT</code> game action.
    55      */
    60              */
    56     private static final int LEFT = -3;
    61     private static final int LEFT = -3;
    57 
    62 
    58     /**
    63     /**
    59      * Constant for <code>RIGHT</code> game action.
    64              * Constant for <code>RIGHT</code> game action.
    60      */
    65              */
    61     private static final int RIGHT = -4;
    66     private static final int RIGHT = -4;
    62 
    67 
    63     /**
    68     /**
    64      * Constant for <code>FIRE</code> game action.
    69              * Constant for <code>FIRE</code> game action.
    65      */
    70              */
    66     private static final int FIRE = -5;
    71     private static final int FIRE = -5;
    67 
    72 
    68     /**
    73     /**
    69      * Constant for <code>SLK</code> game action.
    74              * Constant for <code>SLK</code> game action.
    70      */
    75              */
    71     private static final int SLK = -6;
    76     private static final int SLK = -6;
    72 
    77 
    73     /**
    78     /**
    74      * Constant for <code>SRK</code> game action.
    79              * Constant for <code>SRK</code> game action.
    75      */
    80              */
    76     private static final int SRK = -7;
    81     private static final int SRK = -7;
    77 
    82 
    78     /**
    83     /**
    79      * Constant for general "<code>A</code>" game action.
    84              * Constant for general "<code>A</code>" game action.
    80      */
    85              */
    81     private static final int GAME_A = 55;
    86     private static final int GAME_A = 55;
    82 
    87 
    83     /**
    88     /**
    84      * Constant for general "<code>B</code>" game action.
    89              * Constant for general "<code>B</code>" game action.
    85      */
    90              */
    86     private static final int GAME_B = 57;
    91     private static final int GAME_B = 57;
    87 
    92 
    88     /**
    93     /**
    89      * Constant for general "<code>C</code>" game action.
    94              * Constant for general "<code>C</code>" game action.
    90      */
    95              */
    91     private static final int GAME_C = 42;
    96     private static final int GAME_C = 42;
    92 
    97 
    93     /**
    98     /**
    94      * Constant for general "<code>D</code>" game action.
    99              * Constant for general "<code>D</code>" game action.
    95      */
   100              */
    96     private static final int GAME_D = 35;
   101     private static final int GAME_D = 35;
    97 
   102 
    98     /**
   103     /**
    99              * Constant for general invalid key code.
   104              * Constant for general invalid key code.
   100              */
   105              */
   103     /**
   108     /**
   104             * Constant for long keypress timeout.
   109             * Constant for long keypress timeout.
   105             */
   110             */
   106     private static final int KEYREPEAT_INTERVAL = 500;
   111     private static final int KEYREPEAT_INTERVAL = 500;
   107 
   112 
   108     private BaseCSSEngine cssEngine;
   113     /**
   109 
   114              * alpha value of a transparent pixel.
   110     /**
   115              */
   111             * keypad and game keys composites.
   116     private static final int TRANSPARENT = 0;
   112             */
   117 
       
   118     /**
       
   119              * keypad and game keys composites.
       
   120              */
   113     private Composite keypadComposite;
   121     private Composite keypadComposite;
   114     private Composite gameKeysComposite;
   122     private Composite gameKeysComposite;
   115     private Canvas canvas;
   123     private Canvas canvas;
   116 
   124 
   117     /**
   125     /**
   118             * Listener for receiving mouse events on the keypad buttons.
   126              * CSS engine instance
   119             */
   127              */
   120     private CanvasKeypadMouseListener mouseListener = new CanvasKeypadMouseListener();
   128     private BaseCSSEngine cssEngine;
   121 
   129 
   122     /**
   130     /**
   123             * Canvas keypad buttons.
   131             * Canvas keypad buttons.
   124             */
   132             */
   125     private Button up;
   133 
   126     private Button down;
   134     private Label center;
   127     private Button left;
   135     private Label up;
   128     private Button right;
   136     private Label down;
   129     private Button center;
   137     private Label left;
       
   138     private Label right;
       
   139 
       
   140     private org.eclipse.swt.graphics.Image center_normal;
       
   141     private org.eclipse.swt.graphics.Image center_pressed;
       
   142     private org.eclipse.swt.graphics.Image left_normal;
       
   143     private org.eclipse.swt.graphics.Image left_pressed;
       
   144     private org.eclipse.swt.graphics.Image right_normal;
       
   145     private org.eclipse.swt.graphics.Image right_pressed;
       
   146     private org.eclipse.swt.graphics.Image up_normal;
       
   147     private org.eclipse.swt.graphics.Image up_pressed;
       
   148     private org.eclipse.swt.graphics.Image down_normal;
       
   149     private org.eclipse.swt.graphics.Image down_pressed;
       
   150 
       
   151     private ImageData center_data;
       
   152     private ImageData left_data;
       
   153     private ImageData right_data;
       
   154     private ImageData up_data;
       
   155     private ImageData down_data;
       
   156 
       
   157     private ImageData pressedKeyData;
       
   158 
   130     private Button skright;
   159     private Button skright;
   131     private Button skleft;
   160     private Button skleft;
   132     private Button gameA;
   161     private Button gameA;
   133     private Button gameB;
   162     private Button gameB;
   134     private Button gameC;
   163     private Button gameC;
   135     private Button gameD;
   164     private Button gameD;
   136 
   165 
       
   166     private Display display;
       
   167 
   137     private int keyPressed;
   168     private int keyPressed;
   138 
   169     private Label rockerKeyPressed;
       
   170 
       
   171     /**
       
   172              * Timer for handling long key press and mouse move events.
       
   173              */
   139     private Timer timer = new Timer();
   174     private Timer timer = new Timer();
   140     private CanvasKeypadTimerTask timerTask;
   175     private CanvasKeypadTimerTask timerTask;
       
   176 
       
   177     /**
       
   178              * Path of the osk graphics
       
   179              */
       
   180     private static final String RESOURCE_PATH = "c:/data/images/";
       
   181 
       
   182     /**
       
   183              * offset on the composite in pixels where the osk rocker keys are layed out.
       
   184              */
       
   185     private int image_offset;
       
   186 
   141 
   187 
   142     /**
   188     /**
   143              * Constructs a canvas kepad based on the JAD attribute
   189              * Constructs a canvas kepad based on the JAD attribute
   144              *
   190              *
   145              * @param canvas - canvas for which the keypad is required.
   191              * @param canvas - canvas for which the keypad is required.
   148              */
   194              */
   149     CanvasKeypad(Canvas canvas, Composite canvasComp, String mode)
   195     CanvasKeypad(Canvas canvas, Composite canvasComp, String mode)
   150     {
   196     {
   151 
   197 
   152         this.canvas = canvas;
   198         this.canvas = canvas;
   153         cssEngine = new BaseCSSEngine(Display.getCurrent());
   199         display = ESWTUIThreadRunner.getInstance().getDisplay();
   154 
   200 
   155         //Set form layout for the shell conatining the canvas.
   201         cssEngine = new BaseCSSEngine(display);
       
   202 
       
   203 
       
   204         //load the images
       
   205         center_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_c.svg");
       
   206         center_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_c.svg");
       
   207         left_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_l.svg");
       
   208         left_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_l.svg");
       
   209         right_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_r.svg");
       
   210         right_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_r.svg");
       
   211         up_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_t.svg");
       
   212         up_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_t.svg");
       
   213         down_normal = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_normal_b.svg");
       
   214         down_pressed = new org.eclipse.swt.graphics.Image(display,RESOURCE_PATH + "qtg_fr_rocker_pressed_b.svg");
       
   215 
       
   216         image_offset = (display.getBounds().width/2) - (center_normal.getBounds().width/2);
       
   217 
       
   218         //Load image data required to determine the osk button pressed on mouse events
       
   219         center_data = center_normal.getImageData();
       
   220         left_data = left_normal.getImageData();
       
   221         right_data = right_normal.getImageData();
       
   222         up_data = up_normal.getImageData();
       
   223         down_data = down_normal.getImageData();
       
   224 
       
   225 
       
   226         //Set form layout for the shell containing the canvas.
   156         canvas.getShell().setLayout(new FormLayout());
   227         canvas.getShell().setLayout(new FormLayout());
   157 
   228 
   158         //Create the Navigation keypad
   229         //Create the Navigation keypad
   159         createNavKeypad();
   230         createNavKeypad();
   160 
   231 
   164             //create the game key pad
   235             //create the game key pad
   165             createGamePad();
   236             createGamePad();
   166         }
   237         }
   167 
   238 
   168 
   239 
   169         //Layout the game keys, navigation keys and the canvas on the shell using form layout
   240         //Layout the game keys, navigation keys and the canvas composites on the shell using form layout
   170 
   241 
   171         FormData canvasData = new FormData();
   242         FormData canvasData = new FormData();
   172         canvasData.right = new FormAttachment(100);
   243         canvasData.right = new FormAttachment(100);
   173         canvasData.left = new FormAttachment(0);
   244         canvasData.left = new FormAttachment(0);
   174         canvasData.top = new FormAttachment(0);
   245         canvasData.top = new FormAttachment(0);
   176         canvasComp.setLayoutData(canvasData);
   247         canvasComp.setLayoutData(canvasData);
   177 
   248 
   178         FormData navKeyData = new FormData();
   249         FormData navKeyData = new FormData();
   179         navKeyData.right = new FormAttachment(100);
   250         navKeyData.right = new FormAttachment(100);
   180         navKeyData.left = new FormAttachment(0);
   251         navKeyData.left = new FormAttachment(0);
       
   252 
   181         if(gameKeysComposite != null)
   253         if(gameKeysComposite != null)
   182         {
   254         {
   183             //game keypad was created above, so layout the game keys.
   255             //game keypad was created above, so layout the game keys.
   184             navKeyData.bottom = new FormAttachment(gameKeysComposite);
   256             navKeyData.bottom = new FormAttachment(gameKeysComposite);
   185         }
   257         }
   186         else
   258         else
   187         {
   259         {
   188             //game keys are not needed
   260             //game keys are not needed
   189             navKeyData.bottom = new FormAttachment(100);
   261             navKeyData.bottom = new FormAttachment(100);
   190         }
   262         }
       
   263 
   191         keypadComposite.setLayoutData(navKeyData);
   264         keypadComposite.setLayoutData(navKeyData);
   192 
   265 
   193 
   266 
   194         if(gameKeysComposite != null)
   267         if(gameKeysComposite != null)
   195         {
   268         {
   196             //layout the game keys
   269             //game keys were created, layout the game keys
   197             FormData gameKeyData = new FormData();
   270             FormData gameKeyData = new FormData();
   198             gameKeyData.right = new FormAttachment(100);
   271             gameKeyData.right = new FormAttachment(100);
   199             gameKeyData.left = new FormAttachment(0);
   272             gameKeyData.left = new FormAttachment(0);
   200             gameKeyData.bottom = new FormAttachment(100);
   273             gameKeyData.bottom = new FormAttachment(100);
   201             gameKeysComposite.setLayoutData(gameKeyData);
   274             gameKeysComposite.setLayoutData(gameKeyData);
   202         }
   275         }
   203 
   276 
   204         //layout the shell
   277         //layout the shell
   205         canvas.getShell().layout();
   278         canvas.getShell().layout();
   206 
   279 
   207 
       
   208 
       
   209     }
   280     }
   210 
   281 
   211     /**
   282     /**
   212              * creates the navigation keypad - the directional keys and selctio key.
   283              * creates the navigation keypad - the directional keys and selctio key.
   213              */
   284              */
   216 
   287 
   217         //Create the navigation key composite
   288         //Create the navigation key composite
   218 
   289 
   219         keypadComposite = new Composite(canvas.getShell(), SWT.NONE);
   290         keypadComposite = new Composite(canvas.getShell(), SWT.NONE);
   220 
   291 
   221         //Create the buttons of the navigation keypad
   292         center = new Label(keypadComposite, SWT.NONE);
   222 
   293         left = new Label(keypadComposite, SWT.NONE);
   223         up = new Button(keypadComposite, SWT.PUSH);
   294         right = new Label(keypadComposite, SWT.NONE);
   224         up.addMouseListener(mouseListener);
   295         up = new Label(keypadComposite, SWT.NONE);
   225 
   296         down = new Label(keypadComposite, SWT.NONE);
   226         left = new Button(keypadComposite, SWT.PUSH);
   297 
   227         left.addMouseListener(mouseListener);
   298         center.setImage(center_normal);
   228 
   299         center.pack();
   229         center = new Button(keypadComposite, SWT.PUSH);
   300         left.setImage(left_normal);
   230         center.addMouseListener(mouseListener);
   301         left.pack();
   231 
   302         right.setImage(right_normal);
   232         right = new Button(keypadComposite, SWT.PUSH);
   303         right.pack();
   233         right.addMouseListener(mouseListener);
   304         up.setImage(up_normal);
   234 
   305         up.pack();
   235         down = new Button(keypadComposite, SWT.PUSH);
   306         down.setImage(down_normal);
   236         down.addMouseListener(mouseListener);
   307         down.pack();
   237 
       
   238         //up.setStyleSheet(
       
   239         cssEngine.applyCSS(up,"QPushButton" +
       
   240                            "{" +
       
   241                            "background-color:blue;" +
       
   242                            "border-style: outset;" +
       
   243                            "border-width: 2px; " +
       
   244                            "border-radius: 5px;" +
       
   245                            "border-top-right-radius: 27px;"+
       
   246                            "border-top-left-radius: 27px;"+
       
   247                            "border-color: beige;" +
       
   248                            "font: bold 14px;" +
       
   249                            "min-width: 3em;" +
       
   250                            "min-height: 2em;" +
       
   251                            "}" +
       
   252                            "QPushButton:pressed { background-color: rgb(224, 0, 0);" +
       
   253                            " border-style: inset;" +
       
   254                            "}");
       
   255 
       
   256         cssEngine.applyCSS(down,"QPushButton" +
       
   257                            "{" +
       
   258                            "background-color:blue;" +
       
   259                            "border-style: outset;" +
       
   260                            "border-width: 2px; " +
       
   261                            "border-radius: 5px;" +
       
   262                            "border-bottom-right-radius: 27px;"+
       
   263                            "border-bottom-left-radius: 27px;"+
       
   264                            "border-color: beige;" +
       
   265                            "font: bold 14px;" +
       
   266                            "min-width: 3em;" +
       
   267                            "min-height: 2em;" +
       
   268                            "}" +
       
   269                            "QPushButton:pressed { background-color: rgb(224, 0, 0);" +
       
   270                            " border-style: inset;" +
       
   271                            "}");
       
   272 
       
   273         cssEngine.applyCSS(left,"QPushButton" +
       
   274                            "{" +
       
   275                            "background-color:blue;" +
       
   276                            "border-style: outset;" +
       
   277                            "border-width: 2px; " +
       
   278                            "border-radius: 5px;" +
       
   279                            "border-top-left-radius: 27px;"+
       
   280                            "border-bottom-left-radius: 27px;"+
       
   281                            "border-color: beige;" +
       
   282                            "font: bold 14px;" +
       
   283                            "min-height: 3em;" +
       
   284                            "min-width: 2em;" +
       
   285                            "}" +
       
   286                            "QPushButton:pressed { background-color: rgb(224, 0, 0);" +
       
   287                            " border-style: inset;" +
       
   288                            "}");
       
   289 
       
   290         cssEngine.applyCSS(right,"QPushButton" +
       
   291                            "{" +
       
   292                            "background-color:blue;" +
       
   293                            "border-style: outset;" +
       
   294                            "border-width: 2px; " +
       
   295                            "border-radius: 5px;" +
       
   296                            "border-top-right-radius: 27px;"+
       
   297                            "border-bottom-right-radius: 27px;"+
       
   298                            "border-color: beige;" +
       
   299                            "font: bold 14px;" +
       
   300                            "min-height: 3em;" +
       
   301                            "min-width: 2em;" +
       
   302                            "}" +
       
   303                            "QPushButton:pressed { background-color: rgb(224, 0, 0);" +
       
   304                            " border-style: inset;" +
       
   305                            "}");
       
   306 
       
   307         cssEngine.applyCSS(center, "QPushButton" +
       
   308                            "{" +
       
   309                            "background-color:blue;" +
       
   310                            "border-style: outset;" +
       
   311                            "border-width: 2px; " +
       
   312                            "border-radius: 27px;" +
       
   313                            "border-color: beige;" +
       
   314                            "font: bold 14px;" +
       
   315                            "min-height: 3em;" +
       
   316                            "min-width: 3em;" +
       
   317                            "}" +
       
   318                            "QPushButton:pressed { background-color: rgb(224, 0, 0);" +
       
   319                            " border-style: inset;" +
       
   320                            "}");
       
   321 
       
   322         up.setText("^");
       
   323         center.setText("*");
       
   324         left.setText("<");
       
   325         right.setText(">");
       
   326         down.setText("v");
       
   327 
       
   328 
   308 
   329         //layout the navigation keys
   309         //layout the navigation keys
   330         keypadComposite.setLayout(new FormLayout());
   310         keypadComposite.setLayout(new FormLayout());
   331 
   311 
   332 
   312 
   333         FormData upFormData = new FormData();
   313         FormData navkeysFormData = new FormData();
   334         upFormData.top=new FormAttachment(0,10);
   314         navkeysFormData.top=new FormAttachment(0);
   335         upFormData.left=new FormAttachment(40);
   315         navkeysFormData.left = new FormAttachment(0,image_offset);
   336         up.setLayoutData(upFormData);
   316         center.setLayoutData(navkeysFormData);
   337 
   317         left.setLayoutData(navkeysFormData);
   338         FormData centerFormData = new FormData();
   318         right.setLayoutData(navkeysFormData);
   339         centerFormData.top=new FormAttachment(up,0,SWT.CENTER|SWT.TRAIL);
   319         up.setLayoutData(navkeysFormData);
   340         centerFormData.left=new FormAttachment(40);
   320         down.setLayoutData(navkeysFormData);
   341         center.setLayoutData(centerFormData);
   321 
   342 
   322         keypadComposite.pack();
   343         FormData downFormData = new FormData();
       
   344         downFormData.top = new FormAttachment(center,0,SWT.TRAIL);
       
   345         downFormData.left=new FormAttachment(left);
       
   346         down.setLayoutData(downFormData);
       
   347 
       
   348         FormData rightFormData = new FormData();
       
   349         rightFormData.top=new FormAttachment(up,0,SWT.TRAIL);
       
   350         rightFormData.left=new FormAttachment(center);
       
   351         right.setLayoutData(rightFormData);
       
   352 
       
   353         FormData leftFormData = new FormData();
       
   354         leftFormData.top= new FormAttachment(up);
       
   355         leftFormData.right = new FormAttachment(center);
       
   356         left.setLayoutData(leftFormData);
       
   357 
   323 
   358         keypadComposite.layout();
   324         keypadComposite.layout();
       
   325 
       
   326         keypadComposite.addMouseListener(this);
       
   327         keypadComposite.addMouseMoveListener(this);
       
   328 
   359     }
   329     }
   360 
   330 
   361 
   331 
   362     /**
   332     /**
   363              * creates the game keypad - the game action keys A, B, C and D.
   333              * creates the game keypad - the game action keys A, B, C and D.
   368         gameKeysComposite = new Composite(canvas.getShell(), SWT.NONE);
   338         gameKeysComposite = new Composite(canvas.getShell(), SWT.NONE);
   369         gameKeysComposite.setLayout(new FormLayout());
   339         gameKeysComposite.setLayout(new FormLayout());
   370 
   340 
   371         //Add the game buttons
   341         //Add the game buttons
   372         gameA = new Button(gameKeysComposite, SWT.PUSH);
   342         gameA = new Button(gameKeysComposite, SWT.PUSH);
   373         gameA.addMouseListener(mouseListener);
   343         gameA.addMouseListener(this);
   374 
   344 
   375         gameB = new Button(gameKeysComposite, SWT.PUSH);
   345         gameB = new Button(gameKeysComposite, SWT.PUSH);
   376         gameB.addMouseListener(mouseListener);
   346         gameB.addMouseListener(this);
   377 
   347 
   378         gameC = new Button(gameKeysComposite, SWT.PUSH);
   348         gameC = new Button(gameKeysComposite, SWT.PUSH);
   379         gameC.addMouseListener(mouseListener);
   349         gameC.addMouseListener(this);
   380 
   350 
   381         gameD = new Button(gameKeysComposite, SWT.PUSH);
   351         gameD = new Button(gameKeysComposite, SWT.PUSH);
   382         gameD.addMouseListener(mouseListener);
   352         gameD.addMouseListener(this);
   383 
   353 
   384         //set the style sheet
   354         //Set the game key style sheets
   385         String gameKeysStylesheet = "QPushButton" +
   355         cssEngine.applyCSS(gameA,"QPushButton" +
   386                                     "{" +
   356                            "{" +
   387                                     "background-color:blue;" +
   357                            "background-color:white;" +
   388                                     "border-style: outset;" +
   358                            "border-style: outset;" +
   389                                     "border-width: 0px; " +
   359                            "border-width: 1px; " +
   390                                     "border-color: beige;" +
   360                            "border-color: black;" +
   391                                     "font: bold 14px;" +
   361                            "border-radius: 2px;" +
   392                                     "min-height: 2em;" +
   362                            "font: bold 14px;" +
   393                                     "min-width: 5.5em;" +
   363                            "min-height: 2em;" +
   394                                     "}" +
   364                            "min-width: 5.1em;" +
   395                                     "QPushButton:pressed { background-color: rgb(224, 0, 0);" +
   365                            "image: url(" + RESOURCE_PATH + "qtg_mono_game_a.svg);" +
   396                                     " border-style: inset;" +
   366                            "}" +
   397                                     "}";
   367                            "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_game_a.svg);" +
   398 
   368                            " border-style: inset;" +
   399         cssEngine.applyCSS(gameA,gameKeysStylesheet);
   369                            "}");
   400         cssEngine.applyCSS(gameB,gameKeysStylesheet);
   370         cssEngine.applyCSS(gameB,"QPushButton" +
   401         cssEngine.applyCSS(gameC,gameKeysStylesheet);
   371                            "{" +
   402         cssEngine.applyCSS(gameD,gameKeysStylesheet);
   372                            "background-color:white;" +
   403 
   373                            "border-style: outset;" +
   404         gameA.setText("A");
   374                            "border-width: 1px; " +
   405         gameB.setText("B");
   375                            "border-color: black;" +
   406         gameC.setText("C");
   376                            "border-radius: 2px;" +
   407         gameD.setText("D");
   377                            "font: bold 14px;" +
       
   378                            "min-height: 2em;" +
       
   379                            "min-width: 5.1em;" +
       
   380                            "image: url(" + RESOURCE_PATH + "qtg_mono_game_b.svg);" +
       
   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 
   408 
   416 
   409         //layout the game Keys
   417         //layout the game Keys
   410         FormData gameAFormData = new FormData();
   418         FormData gameAFormData = new FormData();
   411         gameAFormData.bottom=new FormAttachment(100,0);
   419         gameAFormData.bottom=new FormAttachment(100,0);
   412         gameAFormData.left=new FormAttachment(0,0);
   420         gameAFormData.left=new FormAttachment(0,0);
   451         {
   459         {
   452 
   460 
   453             if(keypadComposite != null)
   461             if(keypadComposite != null)
   454             {
   462             {
   455 
   463 
       
   464                 Color black = display.getSystemColor(SWT.COLOR_BLACK);
       
   465                 keypadComposite.setBackground(black);
       
   466 
   456                 skright = new Button(keypadComposite, SWT.PUSH);
   467                 skright = new Button(keypadComposite, SWT.PUSH);
   457                 skright.addMouseListener(mouseListener);
   468                 skright.addMouseListener(this);
   458 
   469 
   459                 skleft = new Button(keypadComposite, SWT.PUSH);
   470                 skleft = new Button(keypadComposite, SWT.PUSH);
   460                 skleft.addMouseListener(mouseListener);
   471                 skleft.addMouseListener(this);
   461 
   472 
   462                 //Set the style sheet
   473                 //Set the style sheet
   463                 cssEngine.applyCSS(skright,"QPushButton" +
   474                 cssEngine.applyCSS(skright,"QPushButton" +
   464                                    "{" +
   475                                    "{" +
   465                                    "background-color:blue;" +
   476                                    "background-color:white;" +
   466                                    "border-style: outset;" +
   477                                    "border-style: outset;" +
   467                                    "border-width: 1px; " +
   478                                    "border-width: 1px; " +
   468                                    "border-bottom-left-radius: 22px;"+
   479                                    "border-bottom-left-radius: 22px;"+
   469                                    "border-color: beige;" +
   480                                    "border-color: black;" +
   470                                    "font: bold 14px;" +
   481                                    "font: bold 14px;" +
   471                                    "min-width: 3em;" +
   482                                    "min-width: 3em;" +
   472                                    "min-height: 2em;" +
   483                                    "min-height: 2em;" +
       
   484                                    "image: url(" + RESOURCE_PATH + "qtg_mono_rsk_horizontal.svg);" +
   473                                    "}" +
   485                                    "}" +
   474                                    "QPushButton:pressed { background-color: rgb(224, 0, 0);" +
   486                                    "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_rsk_horizontal.svg);" +
   475                                    " border-style: inset;" +
   487                                    " border-style: inset;" +
   476                                    "}");
   488                                    "}");
   477 
   489 
   478                 cssEngine.applyCSS(skleft,"QPushButton" +
   490                 cssEngine.applyCSS(skleft,"QPushButton" +
   479                                    "{" +
   491                                    "{" +
   480                                    "background-color:blue;" +
   492                                    "background-color:white;" +
   481                                    "border-style: outset;" +
   493                                    "border-style: outset;" +
   482                                    "border-width: 1px; " +
   494                                    "border-width: 1px; " +
   483                                    "border-bottom-right-radius: 22px;"+
   495                                    "border-bottom-right-radius: 22px;"+
   484                                    "border-color: beige;" +
   496                                    "border-color: black;" +
   485                                    "font: bold 14px;" +
   497                                    "font: bold 14px;" +
   486                                    "min-width: 3em;" +
   498                                    "min-width: 3em;" +
   487                                    "min-height: 2em;" +
   499                                    "min-height: 2em;" +
       
   500                                    "image: url(" + RESOURCE_PATH + "qtg_mono_lsk_horizontal.svg);" +
   488                                    "}" +
   501                                    "}" +
   489                                    "QPushButton:pressed { background-color: rgb(224, 0, 0);" +
   502                                    "QPushButton:pressed { image: url(" + RESOURCE_PATH + "qtg_mono_lsk_horizontal.svg);" +
   490                                    " border-style: inset;" +
   503                                    " border-style: inset;" +
   491                                    "}");
   504                                    "}");
   492 
   505 
   493                 skright.setText("RSK");
       
   494                 skleft.setText("LSK");
       
   495 
   506 
   496                 FormData sklFormData = new FormData();
   507                 FormData sklFormData = new FormData();
   497                 sklFormData.top=new FormAttachment(0, 0);
   508                 sklFormData.top=new FormAttachment(0, 0);
   498                 sklFormData.left=new FormAttachment(0, 0);
   509                 sklFormData.left=new FormAttachment(0, 0);
   499                 skleft.setLayoutData(sklFormData);
   510                 skleft.setLayoutData(sklFormData);
   511             //LSK and RSK buttons are not needed, remove the buttons from navigation keypad
   522             //LSK and RSK buttons are not needed, remove the buttons from navigation keypad
   512             if(skright != null)
   523             if(skright != null)
   513                 skright.dispose();
   524                 skright.dispose();
   514             if(skleft != null)
   525             if(skleft != null)
   515                 skleft.dispose();
   526                 skleft.dispose();
   516         }
   527 
   517 
   528             keypadComposite.setBackground(null);
   518     }
   529         }
   519 
   530 
   520     Button getLeftSoftKey()
       
   521     {
       
   522         return skleft;
       
   523     }
       
   524 
       
   525     Button getRightSoftKey()
       
   526     {
       
   527         return skright;
       
   528     }
   531     }
   529 
   532 
   530     /**
   533     /**
   531              * returns the height of the canvas keypad in pixels.
   534              * returns the height of the canvas keypad in pixels.
   532              * @return Height of the canvas keypad in pixels.
   535              * @return Height of the canvas keypad in pixels.
   615         return keyCode;
   618         return keyCode;
   616     }
   619     }
   617 
   620 
   618 
   621 
   619     /**
   622     /**
   620              * Mouse listener.
   623              * Mouse listeners.
   621              */
   624              */
   622     class CanvasKeypadMouseListener implements MouseListener
   625 
   623     {
   626     public void mouseDown(MouseEvent e)
   624 
   627     {
   625         public void mouseDown(MouseEvent e)
   628 
   626         {
   629         //handle the mouse down event
   627 
   630         handleMouseDown(e);
   628             canvas.doKeyPressed(getKeyCode(e.widget));
   631 
   629 
   632     }
       
   633 
       
   634     public void mouseUp(MouseEvent e)
       
   635     {
       
   636 
       
   637         //handle the mouse up event
       
   638         handleMouseUp(e);
       
   639 
       
   640     }
       
   641 
       
   642     public void mouseMove(MouseEvent e)
       
   643     {
       
   644 
       
   645         //handle the mouse move event
       
   646         handleMouseMove(e);
       
   647 
       
   648     }
       
   649 
       
   650     public void mouseDoubleClick(MouseEvent event)
       
   651     {
       
   652         //do nothing
       
   653     }
       
   654 
       
   655 
       
   656 
       
   657 
       
   658 
       
   659     /**
       
   660              * Handles the mouse down event
       
   661              *
       
   662              */
       
   663     private void handleMouseDown(MouseEvent e)
       
   664     {
       
   665 
       
   666         if(e.widget == keypadComposite)
       
   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);
       
   670 
       
   671         }
       
   672         else
       
   673         {
       
   674             //get the keycode of the game or soft keys based on the button that was clicked
   630             keyPressed = getKeyCode(e.widget);
   675             keyPressed = getKeyCode(e.widget);
       
   676         }
       
   677 
       
   678         if(keyPressed != INVALID_CODE)
       
   679         {
       
   680 
       
   681             //notify the canvas
       
   682             canvas.doKeyPressed(keyPressed);
   631 
   683 
   632             // Create and Schedule the long keypress timer
   684             // Create and Schedule the long keypress timer
   633             timerTask = new CanvasKeypadTimerTask();
   685             timerTask = new CanvasKeypadTimerTask();
   634             timer.schedule(timerTask, KEYREPEAT_INTERVAL);
   686             timer.schedule(timerTask, KEYREPEAT_INTERVAL);
   635         }
   687         }
   636 
   688     }
   637         public void mouseUp(MouseEvent e)
   689 
   638         {
   690     /**
   639 
   691              * Handles the mouse up event
   640             if(timerTask != null)
   692              *
   641             {
   693              */
   642                 //cancel the long keypress timer if it is running
   694     private void handleMouseUp(MouseEvent e)
   643                 timerTask.cancel();
   695     {
   644                 timerTask = null;
   696         int keyReleased;
   645             }
   697 
   646             canvas.doKeyReleased(getKeyCode(e.widget));
   698         if(timerTask != null)
   647         }
   699         {
   648 
   700             //cancel the long keypress timer if it is running
   649         public void mouseMove(MouseEvent e)
   701             timerTask.cancel();
   650         {
   702             timerTask = null;
   651 
   703         }
   652             if(timerTask != null)
   704 
   653             {
   705         if(e.widget == keypadComposite)
   654                 timerTask.cancel();
   706         {
   655                 timerTask = null;
   707             //the mouse up was grabbed by the keypad composite, map the key code based on the on rocker images.
   656             }
   708             keyReleased = handleRockerReleased(e);
   657 
   709 
   658             canvas.doKeyPressed(getKeyCode(e.widget));
   710         }
   659 
   711         else
       
   712         {
       
   713             //get the keycode of the game or soft keys based on the button that was clicked
       
   714             keyReleased = getKeyCode(e.widget);
       
   715         }
       
   716 
       
   717         if(keyReleased != INVALID_CODE)
       
   718         {
       
   719 
       
   720             //notify the canvas
       
   721             canvas.doKeyReleased(keyReleased);
       
   722         }
       
   723     }
       
   724 
       
   725 
       
   726     /**
       
   727              * Handles the mouse move event
       
   728              *
       
   729              */
       
   730     private void handleMouseMove(MouseEvent e)
       
   731     {
       
   732 
       
   733         if(timerTask != null)
       
   734         {
       
   735             //cancel the long keypress timer if it is running
       
   736             timerTask.cancel();
       
   737             timerTask = null;
       
   738         }
       
   739 
       
   740         if(e.widget == keypadComposite)
       
   741         {
       
   742             //the mouse move was grabbed by the keypad composite, handle the rocker key swipe
       
   743             handleRockerMoved(e);
       
   744 
       
   745         }
       
   746         else
       
   747         {
       
   748             //get the keycode of the game or soft keys based on the button that was clicked
   660             keyPressed = getKeyCode(e.widget);
   749             keyPressed = getKeyCode(e.widget);
       
   750 
       
   751             //notify the canvas
       
   752             canvas.doKeyPressed(keyPressed);
   661 
   753 
   662             // Create and Schedule Timer
   754             // Create and Schedule Timer
   663             timerTask = new CanvasKeypadTimerTask();
   755             timerTask = new CanvasKeypadTimerTask();
   664             timer.schedule(timerTask, KEYREPEAT_INTERVAL);
   756             timer.schedule(timerTask, KEYREPEAT_INTERVAL);
   665 
   757         }
   666         }
   758 
   667 
   759 
   668         public void mouseDoubleClick(MouseEvent event)
   760     }
   669         {
   761 
   670             //do nothing
   762 
   671         }
   763     /**
   672 
   764              * Handles the rocker key press
   673 
   765              *
   674     }
   766              */
   675 
   767     private int handleRockerPressed(MouseEvent e)
       
   768     {
       
   769 
       
   770         keyPressed = INVALID_CODE;
       
   771 
       
   772         Rectangle image_bounds = center.getBounds();
       
   773 
       
   774         // Find out which rocker button was pressed
       
   775         if(image_bounds.contains(e.x,e.y) == true)
       
   776         {
       
   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;
       
   782                 rockerKeyPressed = center;
       
   783                 center.setImage(center_pressed);
       
   784             }
       
   785             else if(left_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
       
   786             {
       
   787                 //left button was pressed
       
   788                 keyPressed = LEFT;
       
   789                 rockerKeyPressed = left;
       
   790                 left.setImage(left_pressed);
       
   791             }
       
   792             else if(right_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
       
   793             {
       
   794                 //right button was pressed
       
   795                 keyPressed = RIGHT;
       
   796                 rockerKeyPressed = right;
       
   797                 right.setImage(right_pressed);
       
   798             }
       
   799             else if(up_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
       
   800             {
       
   801                 //up button was pressed
       
   802                 keyPressed = UP;
       
   803                 rockerKeyPressed = up;
       
   804                 up.setImage(up_pressed);
       
   805             }
       
   806             else if(down_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT)
       
   807             {
       
   808                 //down button was pressed
       
   809                 keyPressed = DOWN;
       
   810                 rockerKeyPressed = down;
       
   811                 down.setImage(down_pressed);
       
   812             }
       
   813             else
       
   814             {
       
   815                 keyPressed = INVALID_CODE;;
       
   816             }
       
   817 
       
   818         }
       
   819 
       
   820         return keyPressed;
       
   821 
       
   822     }
       
   823 
       
   824 
       
   825     /**
       
   826              * Handles the mouse released on the rocker keys
       
   827              *
       
   828              */
       
   829     private int handleRockerReleased(MouseEvent e)
       
   830     {
       
   831 
       
   832         int keyReleased = INVALID_CODE;
       
   833 
       
   834         Rectangle image_bounds = center.getBounds();
       
   835 
       
   836         // Find out which rocker button was released
       
   837         if(image_bounds.contains(e.x,e.y) == true)
       
   838         {
       
   839             //check each of the button images  to check which osk navkey button was released
       
   840             if((rockerKeyPressed == center) &&
       
   841                     (center_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
       
   842             {
       
   843                 //center button was released
       
   844                 keyReleased = FIRE;
       
   845                 center.setImage(center_normal);
       
   846             }
       
   847             else if((rockerKeyPressed == left) &&
       
   848                     (left_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
       
   849             {
       
   850                 //left button was released
       
   851                 keyReleased = LEFT;
       
   852                 left.setImage(left_normal);
       
   853             }
       
   854             else if((rockerKeyPressed == right) &&
       
   855                     (right_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
       
   856             {
       
   857                 //right button was released
       
   858                 keyReleased = RIGHT;
       
   859                 right.setImage(right_normal);
       
   860             }
       
   861             else if((rockerKeyPressed == up) &&
       
   862                     (up_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
       
   863             {
       
   864                 //up button was released
       
   865                 keyReleased = UP;
       
   866                 up.setImage(up_normal);
       
   867             }
       
   868             else if((rockerKeyPressed == down) &&
       
   869                     (down_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT))
       
   870             {
       
   871                 //down button was released
       
   872                 keyReleased = DOWN;
       
   873                 down.setImage(down_normal);
       
   874             }
       
   875             else
       
   876             {
       
   877                 keyReleased = INVALID_CODE;;
       
   878             }
       
   879 
       
   880         }
       
   881 
       
   882         return keyReleased;
       
   883 
       
   884     }
       
   885 
       
   886 
       
   887     /**
       
   888              * Handles the mouse move on the rocker keys
       
   889              *
       
   890              */
       
   891     private void handleRockerMoved(MouseEvent e)
       
   892     {
       
   893 
       
   894         keyPressed = INVALID_CODE;
       
   895 
       
   896         Rectangle image_bounds = center.getBounds();
       
   897 
       
   898         // Find out which rocker button was traversed to
       
   899         if(image_bounds.contains(e.x,e.y) == true)
       
   900         {
       
   901             //check each of the button images  to check which osk navkey button was traversed to
       
   902             if((center_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
       
   903                     (rockerKeyPressed != center))
       
   904             {
       
   905                 //Mouse moved to center rocker key from another rocker key, release the other(source) rocker key
       
   906                 releaseRockerKey(rockerKeyPressed);
       
   907             }
       
   908             else if((left_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
       
   909                     (rockerKeyPressed != left))
       
   910             {
       
   911                 //Mouse moved to left rocker key from another rocker key, release the other(source) rocker key
       
   912                 // and press the left key
       
   913                 releaseRockerKey(rockerKeyPressed);
       
   914                 keyPressed = LEFT;
       
   915                 left.setImage(left_pressed);
       
   916                 rockerKeyPressed = left;
       
   917                 canvas.doKeyPressed(keyPressed);
       
   918                 pressedKeyData = left_data;
       
   919             }
       
   920             else if((right_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
       
   921                     (rockerKeyPressed != right))
       
   922             {
       
   923                 //Mouse moved to right rocker key from another rocker key, release the other(source) rocker key
       
   924                 // and press the right key
       
   925                 releaseRockerKey(rockerKeyPressed);
       
   926                 keyPressed = RIGHT;
       
   927                 right.setImage(right_pressed);
       
   928                 rockerKeyPressed = right;
       
   929                 canvas.doKeyPressed(keyPressed);
       
   930                 pressedKeyData = right_data;
       
   931             }
       
   932             else if((up_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
       
   933                     (rockerKeyPressed != up))
       
   934             {
       
   935                 //Mouse moved to up rocker key from another rocker key, release the other(source) rocker key
       
   936                 // and press the up key
       
   937                 releaseRockerKey(rockerKeyPressed);
       
   938                 keyPressed = UP;
       
   939                 up.setImage(up_pressed);
       
   940                 canvas.doKeyPressed(keyPressed);
       
   941                 rockerKeyPressed = up;
       
   942                 pressedKeyData = up_data;
       
   943             }
       
   944             else if((down_data.getAlpha(e.x-image_offset,e.y)!=TRANSPARENT) &&
       
   945                     (rockerKeyPressed != down))
       
   946             {
       
   947                 //Mouse moved to down rocker key from another rocker key, release the other(source) rocker key
       
   948                 // and press the down key
       
   949                 releaseRockerKey(rockerKeyPressed);
       
   950                 keyPressed = DOWN;
       
   951                 down.setImage(down_pressed);
       
   952                 rockerKeyPressed = down;
       
   953                 canvas.doKeyPressed(keyPressed);
       
   954                 pressedKeyData = down_data;
       
   955             }
       
   956             else
       
   957             {
       
   958                 if((pressedKeyData != null) &&
       
   959                         (pressedKeyData.getAlpha(e.x-image_offset,e.y) == TRANSPARENT))
       
   960                 {
       
   961                     //Mouse moved to out of all rocker keys from another rocker key, release the other(source) rocker key
       
   962                     releaseRockerKey(rockerKeyPressed);
       
   963                     pressedKeyData = null;
       
   964                     rockerKeyPressed = null;
       
   965 
       
   966                 }
       
   967             }
       
   968 
       
   969         }
       
   970         else
       
   971         {
       
   972             //Mouse moved to out of all rocker keys, release the other(source) rocker key
       
   973             releaseRockerKey(rockerKeyPressed);
       
   974         }
       
   975 
       
   976 
       
   977     }
       
   978 
       
   979 
       
   980 
       
   981     /**
       
   982              * Releases a rocker key
       
   983              *
       
   984              */
       
   985     private void releaseRockerKey(Label rockerKeyPressed)
       
   986     {
       
   987 
       
   988         if(rockerKeyPressed == center)
       
   989         {
       
   990             center.setImage(center_normal);
       
   991             canvas.doKeyReleased(FIRE);
       
   992         }
       
   993         else if(rockerKeyPressed == left)
       
   994         {
       
   995             left.setImage(left_normal);
       
   996             canvas.doKeyReleased(LEFT);
       
   997         }
       
   998         else if(rockerKeyPressed == right)
       
   999         {
       
  1000             right.setImage(right_normal);
       
  1001             canvas.doKeyReleased(RIGHT);
       
  1002         }
       
  1003         else if(rockerKeyPressed == up)
       
  1004         {
       
  1005             up.setImage(up_normal);
       
  1006             canvas.doKeyReleased(UP);
       
  1007         }
       
  1008         else if(rockerKeyPressed == down)
       
  1009         {
       
  1010             down.setImage(down_normal);
       
  1011             canvas.doKeyReleased(DOWN);
       
  1012         }
       
  1013 
       
  1014     }
       
  1015 
       
  1016 
       
  1017 
       
  1018     /**
       
  1019              * Canvas Keypad timer task
       
  1020              *
       
  1021              */
   676     class CanvasKeypadTimerTask extends TimerTask
  1022     class CanvasKeypadTimerTask extends TimerTask
   677     {
  1023     {
   678 
  1024 
   679         public void run()
  1025         public void run()
   680         {
  1026         {