Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Revision:

sokoban / distorted-sokoban / src / main / java / org / distorted / keyboard / TouchKeyboard.java @ c2d85688

1
package org.distorted.keyboard;
2

    
3
import android.content.Context;
4
import android.content.res.Resources;
5
import android.graphics.Bitmap;
6
import android.graphics.BitmapFactory;
7
import android.graphics.Canvas;
8
import android.graphics.Paint;
9
import android.graphics.Paint.Align;
10
import android.graphics.Paint.Style;
11
import android.graphics.drawable.NinePatchDrawable;
12

    
13
import android.view.KeyEvent;
14
import android.view.MotionEvent;
15
import android.view.View;
16

    
17
import org.distorted.sokoban.R;
18

    
19
//import android.util.Log;
20

    
21
////////////////////////////////////////////////////////////////////////////////
22

    
23
public class TouchKeyboard extends View
24
  {
25
  //private static final String TAG_KEYBOARD = "RubikKeyboard";    
26
       
27
  private static final int  MARGIN = 2;
28
  private static final int WMARGIN = 8;
29
  private static final int BUTTON_NONE = -4;
30
  private static final int BUTTON_OK   = -3;
31
  private static final int BUTTON_BKSP = -2;
32
  private static final int BUTTON_CAPS = -1;
33
 
34
  private int currPressed;
35
 
36
  private TextFieldArea textField;
37
  private Paint mPaint;
38
  private NinePatchDrawable keyBlue, keyRed, keyWhite;
39
  private Bitmap ok, bksp, caps;
40
 
41
  private int bmpSize;
42
 
43
  char[][] lowerCaseL =
44
      { {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'},
45
        {'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r'},
46
        {'s', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0'},
47
        {'1', '2', '3', '4', '5', '6', '7', '8', '9'}
48
      };
49
   
50
  char[][] upperCaseL =
51
      { {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'},
52
        {'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R'},
53
        {'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0'},
54
        {'1', '2', '3', '4', '5', '6', '7', '8', '9'}
55
      };
56
 
57
  char[][] lowerCaseP =
58
      { {'a', 'b', 'c', 'd', 'e', 'f'},
59
        {'g', 'h', 'i', 'j', 'k', 'l'},
60
        {'m', 'n', 'o', 'p', 'q', 'r'},
61
        {'s', 't', 'u', 'v', 'w', 'x'},
62
        {'y', 'z', '0', '1', '2', '3'},
63
        {'4', '5', '6', '7', '8', '9'}
64
      };
65

    
66
  char[][] upperCaseP =
67
      { {'A', 'B', 'C', 'D', 'E', 'F'},
68
        {'G', 'H', 'I', 'J', 'K', 'L'},
69
        {'M', 'N', 'O', 'P', 'Q', 'R'},
70
        {'S', 'T', 'U', 'V', 'W', 'X'},
71
        {'Y', 'Z', '0', '1', '2', '3'},
72
        {'4', '5', '6', '7', '8', '9'}
73
      };
74
 
75
  private int width, height;
76
 
77
  private int textFieldWidth;  
78
  private int textFieldHeight;
79
  private int textTopLeftX;  
80
  private int textTopLeftY;
81
  private int keyWidth, keyHeight;
82
  private int sKeyWidth, sKeyHeight;
83
     
84
  private VirtualKeyboardListener invoker;
85
   
86
  private int charH;
87
   
88
  private int backgroundColor = 0xffffffff;
89
  private int buttonsColor    = 0xff000000;
90
 
91
  private boolean uppercase;
92
  private int minSize=0;
93
 
94
  public interface VirtualKeyboardListener
95
    {    
96
    void okPressed(String string);
97
    }
98
 
99
////////////////////////////////////////////////////////////////////////////////
100

    
101
  public TouchKeyboard(Context context, int scrWid, int scrHei)
102
    {
103
    super(context);
104
    mPaint = new Paint();
105
    mPaint.setAntiAlias(true);
106
   
107
    currPressed = BUTTON_NONE;
108
    width = scrWid;
109
    height= scrHei;
110
   
111
    Resources res = context.getResources();
112

    
113
    keyBlue = (NinePatchDrawable)res.getDrawable(R.drawable.keys_blue);
114
    keyRed  = (NinePatchDrawable)res.getDrawable(R.drawable.keys_red);
115
    keyWhite= (NinePatchDrawable)res.getDrawable(R.drawable.keys_white);
116
   
117
    ok  = BitmapFactory.decodeResource(res, R.drawable.ok  );
118
    bksp= BitmapFactory.decodeResource(res, R.drawable.bksp);
119
    caps= BitmapFactory.decodeResource(res, R.drawable.caps);
120
   
121
    if( width<=height )
122
      {
123
      sKeyWidth = width/3;
124
      sKeyHeight= width/5;
125
      keyWidth  = width/6;
126
      keyHeight = (height-2*sKeyHeight)/6;
127
     
128
      textFieldHeight = height-sKeyHeight-6*keyHeight;
129
      textFieldWidth = width;
130
      textTopLeftX = 0;
131
      textTopLeftY = sKeyHeight;
132
      }
133
    else
134
      {
135
      sKeyWidth = sKeyHeight = keyHeight = height/5;
136
      keyWidth = (width+1)/9;
137
               
138
      textFieldHeight= sKeyHeight;
139
      textFieldWidth = width-3*sKeyWidth;
140
      textTopLeftX = sKeyWidth;
141
      textTopLeftY = 0;
142
      }
143
 
144
    bmpSize = ok.getHeight();
145
   
146
    if( bmpSize> 0.8*sKeyHeight )
147
      {
148
      bmpSize = (int)(0.8*sKeyHeight);
149
      ok   = Bitmap.createScaledBitmap(ok  , bmpSize, bmpSize, true);
150
      bksp = Bitmap.createScaledBitmap(bksp, bmpSize, bmpSize, true);
151
      caps = Bitmap.createScaledBitmap(caps, bmpSize, bmpSize, true);
152
      }
153
   
154
    textField = new TextFieldArea();
155
    charH= (int)(keyHeight*0.7);  
156
    uppercase = true;
157
    }
158

    
159
////////////////////////////////////////////////////////////////////////////////
160

    
161
  public void resetKeyBoard()
162
    {
163
    textField.clearTextField();
164
    invalidate();
165
    }
166

    
167
////////////////////////////////////////////////////////////////////////////////
168

    
169
  protected void onDraw(Canvas c)
170
    {      
171
    mPaint.setColor(backgroundColor);
172
    mPaint.setStyle(Style.FILL);
173
    mPaint.setTextSize(charH);
174
    mPaint.setTextAlign(Align.CENTER);
175
   
176
    c.drawRect(0, 0, width, height, mPaint);
177
   
178
    mPaint.setColor(buttonsColor);
179
   
180
    textField.paint( c, textTopLeftX+1, textTopLeftY+MARGIN/2+1, textTopLeftX+textFieldWidth-1, textTopLeftY+textFieldHeight-MARGIN/2-1);
181
   
182
    if( currPressed==BUTTON_OK )
183
      {
184
      keyRed.setBounds( MARGIN/2, MARGIN/2, sKeyWidth-MARGIN/2, sKeyHeight-MARGIN/2);
185
      keyRed.draw(c);
186
      }
187
    else
188
      {
189
      keyBlue.setBounds( MARGIN/2, MARGIN/2,sKeyWidth-MARGIN/2, sKeyHeight-MARGIN/2);
190
      keyBlue.draw(c); 
191
      }
192
   
193
    if( currPressed==BUTTON_BKSP )
194
      {
195
      keyRed.setBounds( width-2*sKeyWidth+MARGIN/2, MARGIN/2, width-sKeyWidth-MARGIN/2,sKeyHeight-MARGIN/2);
196
      keyRed.draw(c);
197
      }
198
    else
199
      {
200
      keyBlue.setBounds(width-2*sKeyWidth+MARGIN/2, MARGIN/2, width-sKeyWidth-MARGIN/2,sKeyHeight-MARGIN/2);
201
      keyBlue.draw(c); 
202
      }
203
   
204
    if( currPressed==BUTTON_CAPS )
205
      {
206
      keyRed.setBounds( width-sKeyWidth+MARGIN/2, MARGIN/2, width-MARGIN/2,sKeyHeight-MARGIN/2);
207
      keyRed.draw(c);
208
      }
209
    else
210
      {
211
      keyBlue.setBounds(width-sKeyWidth+MARGIN/2, MARGIN/2, width-MARGIN/2,sKeyHeight-MARGIN/2);
212
      keyBlue.draw(c); 
213
      }
214
   
215
    c.drawBitmap(ok  ,                   MARGIN/2+sKeyWidth/2-bmpSize/2, MARGIN/2+sKeyHeight/2-bmpSize/2, mPaint);
216
    c.drawBitmap(bksp, width-2*sKeyWidth+MARGIN/2+sKeyWidth/2-bmpSize/2, MARGIN/2+sKeyHeight/2-bmpSize/2, mPaint);
217
    c.drawBitmap(caps, width-  sKeyWidth+MARGIN/2+sKeyWidth/2-bmpSize/2, MARGIN/2+sKeyHeight/2-bmpSize/2, mPaint);
218
   
219
    int hei = (width<=height ? textFieldHeight+sKeyHeight : textFieldHeight);
220
    int numCol = (width<=height ? lowerCaseP[0].length : lowerCaseL[0].length);
221
    int numRow = (width<=height ? lowerCaseP.length : lowerCaseL.length);
222
   
223
    for(int j=0; j<numRow; j++)
224
      {
225
      for(int i=0; i<numCol; i++)
226
        {  
227
        if( currPressed==j*numCol+i )
228
          {
229
          keyRed.setBounds(  i*width/numCol+ MARGIN/2, hei+ MARGIN/2,
230
                                     (i+1)*width/numCol- MARGIN/2, hei+ MARGIN/2 +keyHeight );
231
          keyRed.draw(c);
232
          keyWhite.setBounds(i*width/numCol+WMARGIN/2, hei+WMARGIN/2-keyHeight,
233
                                 (i+1)*width/numCol-WMARGIN/2, hei-WMARGIN/2 );
234
          keyWhite.draw(c);              
235
         
236
          if( width<=height )
237
            c.drawText( uppercase? upperCaseP[j]:lowerCaseP[j], i,1,(float)(i*width/numCol+keyWidth/2),
238
                        (float)(hei-keyHeight/2+charH*0.4), mPaint);
239
          else
240
                c.drawText( uppercase? upperCaseL[j]:lowerCaseL[j], i,1,(float)(i*width/numCol+keyWidth/2),
241
                        (float)(hei-keyHeight/2+charH*0.4), mPaint);  
242
          }
243
        else
244
          {
245
          keyBlue.setBounds(  i*width/numCol+ MARGIN/2, hei+ MARGIN/2,
246
                                  (i+1)*width/numCol- MARGIN/2, hei+ MARGIN/2 +keyHeight );
247
          keyBlue.draw(c);
248
          }
249
       
250
        if( width<=height )
251
            c.drawText( uppercase? upperCaseP[j]:lowerCaseP[j], i,1,(float)(i*width/numCol+keyWidth/2),
252
                        (float)(hei+keyHeight/2+charH*0.4), mPaint);
253
        else
254
            c.drawText( uppercase? upperCaseL[j]:lowerCaseL[j], i,1,(float)(i*width/numCol+keyWidth/2),
255
                        (float)(hei+keyHeight/2+charH*0.4), mPaint);
256
        }
257
      hei+=keyHeight;
258
      }
259
    }
260

    
261
///////////////////////////////////////////////////////////////////
262

    
263
  public boolean onTouchEvent(MotionEvent event)
264
    {
265
    switch(event.getAction())
266
      {
267
      case MotionEvent.ACTION_DOWN: buttonPressed( (int)event.getX(), (int)event.getY() );
268
                                    break;
269
      case MotionEvent.ACTION_UP  : buttonReleased();
270
                                    break;
271
      case MotionEvent.ACTION_MOVE: buttonPressed( (int)event.getX(), (int)event.getY() );
272
                                    break;
273
      }
274

    
275
    invalidate();
276
    return true;
277
    }
278

    
279
////////////////////////////////////////////////////////////////////////////////
280

    
281
  private void buttonPressed(int x, int y)
282
    {    
283
    if( width<height )
284
        {
285
        if( y>=0 && y<sKeyHeight )
286
              {
287
                   if( x<=  sKeyWidth ) currPressed = BUTTON_OK;
288
              else if( x<=2*sKeyWidth ) currPressed = BUTTON_BKSP;
289
              else                      currPressed = BUTTON_CAPS;
290
              }
291
        else if( y>=sKeyHeight+textFieldHeight && y<=height )
292
              {
293
              currPressed = (int)((y-sKeyHeight-textFieldHeight)/keyHeight)*lowerCaseP[0].length + x/keyWidth;
294
              }
295
        }
296
    else
297
        {
298
        if( y>=0 && y<textFieldHeight )
299
              {
300
                   if( x<=sKeyWidth )                               currPressed = BUTTON_OK;
301
              else if( x>=width-2*sKeyWidth && x<=width-sKeyWidth ) currPressed = BUTTON_BKSP;
302
              else if( x>=width- sKeyWidth && x<=width )            currPressed = BUTTON_CAPS;
303
              else                                                  currPressed = BUTTON_NONE;
304
              }
305
        else if( y>=textFieldHeight && y<=height)
306
              {
307
              currPressed = (int)((y-textFieldHeight)/keyHeight)*lowerCaseL[0].length + x/keyWidth;
308
              }
309
        }
310
    }
311

    
312
////////////////////////////////////////////////////////////////////////////////
313

    
314
  private void buttonReleased()
315
    {
316
         if( currPressed==BUTTON_OK   ) okButtonAction();
317
    else if( currPressed==BUTTON_BKSP ) bkspButtonAction();  
318
    else if( currPressed==BUTTON_CAPS ) capsButtonAction();  
319
    else if( currPressed>=0           ) charAreaAction(currPressed);        
320
               
321
    currPressed = BUTTON_NONE;
322
    }
323

    
324
////////////////////////////////////////////////////////////////////////////////
325
 
326
  @Override public boolean onKeyDown(int keyCode, KeyEvent event)
327
    {
328
    if( currPressed!=BUTTON_NONE ) return false;
329
   /*
330
    switch( keyCode )
331
      {
332
      case Keypad.KEY_ALT        :
333
      case Keypad.KEY_SHIFT_RIGHT:
334
      case Keypad.KEY_SHIFT_LEFT : currPressed= BUTTON_CAPS; invalidate(); break;
335
      case Keypad.KEY_ENTER      : currPressed= BUTTON_OK  ; invalidate(); break;
336
      case Keypad.KEY_DELETE     :
337
      case Keypad.KEY_BACKSPACE  : currPressed= BUTTON_BKSP; invalidate(); break;
338
      }
339
   
340
    if( key>='A' && key<='Z' ) { currPressed = key-'A'          ; invalidate(); }
341
    if( key>='a' && key<='z' ) { currPressed = key-'a'          ; invalidate(); }
342
    if( key>='0' && key<='9' ) { currPressed = 'z'-'a'+1+key-'0'; invalidate(); }
343
   */
344
    return true;
345
    }
346
   
347
////////////////////////////////////////////////////////////////////////////////
348
   
349
  @Override public boolean onKeyUp(int keyCode, KeyEvent event)
350
    {
351
    buttonReleased();
352
    return true;
353
    }
354
   
355
////////////////////////////////////////////////////////////////////////////////
356

    
357
  private void capsButtonAction()
358
    {
359
    uppercase = !uppercase;
360
    }
361

    
362
////////////////////////////////////////////////////////////////////////////////
363

    
364
  private void okButtonAction()
365
    {
366
    if ( invoker != null )
367
      {
368
      String text = textField.getText();
369

    
370
      if( text.length()>= minSize )
371
        invoker.okPressed(text);
372
      }
373
    }
374

    
375
////////////////////////////////////////////////////////////////////////////////
376

    
377
  private void bkspButtonAction()
378
    {
379
    textField.deleteLastChar();
380
    }
381

    
382
////////////////////////////////////////////////////////////////////////////////
383

    
384
  private void charAreaAction(int pressed)
385
    {    
386
    int numCol = (width<height ? lowerCaseP[0].length:lowerCaseL[0].length);
387
    int j = pressed/numCol;
388
    int i = pressed%numCol;
389
             
390
    if( width<height )
391
      textField.insertNewChar( uppercase ? upperCaseP[j][i]:lowerCaseP[j][i]);
392
    else
393
      textField.insertNewChar( uppercase ? upperCaseL[j][i]:lowerCaseL[j][i]);
394
    }
395
 
396
////////////////////////////////////////////////////////////////////////////////
397

    
398
  public void setText(String text)
399
    {
400
    textField.setText( (text==null||text.length()==0)?"":text);
401
    invalidate();
402
    }
403

    
404
////////////////////////////////////////////////////////////////////////////////
405
 
406
  public void setTextFieldBkgColor(int color)
407
    {
408
    textField.setTextFieldBkgColor(color);  
409
    }
410

    
411
////////////////////////////////////////////////////////////////////////////////
412
 
413
  public void setTextFieldBorderColor(int color)
414
    {
415
    textField.setTextFieldBorderColor(color);  
416
    }
417

    
418
////////////////////////////////////////////////////////////////////////////////
419
 
420
  public void setTextFieldFontColor(int color)
421
    {
422
    textField.setTextFieldFontColor(color);  
423
    }
424
 
425
////////////////////////////////////////////////////////////////////////////////
426

    
427
  public int getBackgroundColor()
428
    {
429
    return backgroundColor;
430
    }
431
 
432
////////////////////////////////////////////////////////////////////////////////
433

    
434
  public void setBackgroundColor(int backgroundColor)
435
    {
436
    this.backgroundColor = backgroundColor;
437
    }
438

    
439
////////////////////////////////////////////////////////////////////////////////
440

    
441
  public void setMaxSize(int maxSize)
442
    {
443
    textField.setMaxSize(maxSize);
444
    }
445

    
446
////////////////////////////////////////////////////////////////////////////////
447

    
448
  public void setMinSize(int minSize)
449
    {
450
    this.minSize = minSize;
451
    }
452

    
453
////////////////////////////////////////////////////////////////////////////////
454

    
455
  public void setVirtualKeyboardListener(VirtualKeyboardListener invoker)
456
    {
457
    this.invoker = invoker;
458
    }
459
////////////////////////////////////////////////////////////////////////////////
460
// end of TouchKeyboard  
461
}
(3-3/3)