2015-07-30 20:14:55 +02:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
2015-08-01 21:36:40 +02:00
|
|
|
import android.graphics.RectF;
|
2015-07-30 20:14:55 +02:00
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.DisplayMetrics;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
2015-08-01 18:21:10 +02:00
|
|
|
import java.util.Vector;
|
2015-07-30 20:14:55 +02:00
|
|
|
|
|
|
|
public class Keyboard2View extends View
|
|
|
|
implements View.OnTouchListener
|
|
|
|
{
|
|
|
|
private static final float KEY_PER_ROW = 10;
|
|
|
|
|
2015-07-31 20:48:19 +02:00
|
|
|
private Keyboard2 _ime;
|
|
|
|
private KeyboardData _keyboard;
|
2015-07-30 20:14:55 +02:00
|
|
|
|
2015-08-01 18:21:10 +02:00
|
|
|
private Vector<KeyDown> _downKeys;
|
2015-08-01 16:33:30 +02:00
|
|
|
|
2015-08-01 01:36:41 +02:00
|
|
|
private float _verticalMargin;
|
|
|
|
private float _horizontalMargin;
|
2015-07-30 20:14:55 +02:00
|
|
|
private float _keyWidth;
|
|
|
|
private float _keyHeight;
|
|
|
|
private float _keyPadding;
|
2015-08-01 01:36:41 +02:00
|
|
|
private float _keyBgPadding;
|
2015-08-01 21:36:40 +02:00
|
|
|
private float _keyRound;
|
2015-07-30 20:14:55 +02:00
|
|
|
|
|
|
|
private Paint _keyBgPaint;
|
|
|
|
private Paint _keyDownBgPaint;
|
|
|
|
private Paint _keyLabelPaint;
|
|
|
|
private Paint _keySubLabelPaint;
|
|
|
|
|
|
|
|
public Keyboard2View(Context context, AttributeSet attrs)
|
|
|
|
{
|
|
|
|
super(context, attrs);
|
|
|
|
DisplayMetrics dm = context.getResources().getDisplayMetrics();
|
2015-08-01 18:21:10 +02:00
|
|
|
_downKeys = new Vector<KeyDown>();
|
2015-08-01 01:36:41 +02:00
|
|
|
_verticalMargin = getResources().getDimension(R.dimen.vertical_margin);
|
|
|
|
_horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin);
|
2015-07-31 23:52:47 +02:00
|
|
|
_keyHeight = getResources().getDimension(R.dimen.key_height);
|
|
|
|
_keyPadding = getResources().getDimension(R.dimen.key_padding);
|
2015-08-01 01:36:41 +02:00
|
|
|
_keyBgPadding = getResources().getDimension(R.dimen.key_bg_padding);
|
2015-08-01 21:36:40 +02:00
|
|
|
_keyRound = getResources().getDimension(R.dimen.key_round);
|
2015-08-01 01:36:41 +02:00
|
|
|
_keyWidth = (dm.widthPixels - (_horizontalMargin * 2)) / KEY_PER_ROW;
|
2015-07-30 20:14:55 +02:00
|
|
|
_keyBgPaint = new Paint();
|
|
|
|
_keyBgPaint.setColor(getResources().getColor(R.color.key_bg));
|
|
|
|
_keyDownBgPaint = new Paint();
|
|
|
|
_keyDownBgPaint.setColor(getResources().getColor(R.color.key_down_bg));
|
2015-07-31 23:17:07 +02:00
|
|
|
_keyLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
2015-07-30 20:14:55 +02:00
|
|
|
_keyLabelPaint.setColor(getResources().getColor(R.color.key_label));
|
2015-07-31 23:52:47 +02:00
|
|
|
_keyLabelPaint.setTextSize(getResources().getDimension(R.dimen.label_text_size));
|
2015-07-30 20:14:55 +02:00
|
|
|
_keyLabelPaint.setTextAlign(Paint.Align.CENTER);
|
2015-07-31 23:17:07 +02:00
|
|
|
_keySubLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
_keySubLabelPaint.setColor(getResources().getColor(R.color.key_sub_label));
|
2015-07-31 23:52:47 +02:00
|
|
|
_keySubLabelPaint.setTextSize(getResources().getDimension(R.dimen.sublabel_text_size));
|
2015-07-30 20:14:55 +02:00
|
|
|
_keySubLabelPaint.setTextAlign(Paint.Align.CENTER);
|
|
|
|
setOnTouchListener(this);
|
|
|
|
}
|
|
|
|
|
2015-07-31 20:48:19 +02:00
|
|
|
public void setKeyboard(Keyboard2 ime, KeyboardData keyboardData)
|
2015-07-30 20:14:55 +02:00
|
|
|
{
|
2015-07-31 20:48:19 +02:00
|
|
|
_ime = ime;
|
|
|
|
_keyboard = keyboardData;
|
2015-07-30 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event)
|
|
|
|
{
|
|
|
|
float x;
|
|
|
|
float y;
|
2015-07-30 22:30:29 +02:00
|
|
|
float keyW;
|
2015-07-31 20:48:19 +02:00
|
|
|
int p;
|
2015-07-30 20:14:55 +02:00
|
|
|
|
2015-07-31 01:14:35 +02:00
|
|
|
switch (event.getActionMasked())
|
|
|
|
{
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
case MotionEvent.ACTION_POINTER_UP:
|
|
|
|
onTouchUp(event.getPointerId(event.getActionIndex()));
|
|
|
|
break ;
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
case MotionEvent.ACTION_POINTER_DOWN:
|
2015-07-31 20:48:19 +02:00
|
|
|
p = event.getActionIndex();
|
2015-07-31 01:14:35 +02:00
|
|
|
onTouchDown(event.getX(p), event.getY(p), event.getPointerId(p));
|
|
|
|
break ;
|
2015-07-31 20:48:19 +02:00
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
for (p = 0; p < event.getPointerCount(); p++)
|
|
|
|
onTouchMove(event.getX(p), event.getY(p), event.getPointerId(p));
|
|
|
|
break ;
|
2015-07-31 01:14:35 +02:00
|
|
|
default:
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2015-08-01 18:21:10 +02:00
|
|
|
private KeyDown getKeyDown(int pointerId)
|
2015-07-31 20:48:19 +02:00
|
|
|
{
|
2015-08-01 18:21:10 +02:00
|
|
|
for (KeyDown k : _downKeys)
|
2015-07-31 20:48:19 +02:00
|
|
|
{
|
2015-08-01 18:21:10 +02:00
|
|
|
if (k.pointerId == pointerId)
|
|
|
|
return (k);
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|
2015-08-01 18:21:10 +02:00
|
|
|
return (null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isKeyDown(KeyboardData.Key key)
|
|
|
|
{
|
|
|
|
for (KeyDown k : _downKeys)
|
|
|
|
{
|
|
|
|
if (k.key == key)
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onTouchMove(float moveX, float moveY, int pointerId)
|
|
|
|
{
|
|
|
|
KeyDown k = getKeyDown(pointerId);
|
|
|
|
|
|
|
|
if (k != null)
|
2015-08-01 21:36:40 +02:00
|
|
|
{
|
2015-08-01 18:21:10 +02:00
|
|
|
k.updateDown(moveX, moveY);
|
2015-08-01 21:36:40 +02:00
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onTouchDown(float touchX, float touchY, int pointerId)
|
2015-07-31 01:14:35 +02:00
|
|
|
{
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float keyW;
|
|
|
|
|
2015-08-01 01:36:41 +02:00
|
|
|
y = _verticalMargin - _keyHeight;
|
2015-07-31 20:48:19 +02:00
|
|
|
for (KeyboardData.Row row : _keyboard.getRows())
|
2015-07-30 20:14:55 +02:00
|
|
|
{
|
2015-08-01 01:36:41 +02:00
|
|
|
y += _keyHeight;
|
2015-07-31 20:48:19 +02:00
|
|
|
if (touchY < y || touchY >= (y + _keyHeight))
|
2015-07-31 01:14:35 +02:00
|
|
|
continue ;
|
2015-08-01 01:36:41 +02:00
|
|
|
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2 + _horizontalMargin;
|
2015-08-01 18:21:10 +02:00
|
|
|
for (KeyboardData.Key key : row)
|
2015-07-30 20:14:55 +02:00
|
|
|
{
|
2015-08-01 18:21:10 +02:00
|
|
|
keyW = _keyWidth * key.width;
|
|
|
|
if (touchX >= x && touchX < (x + keyW))
|
2015-07-30 22:30:29 +02:00
|
|
|
{
|
2015-08-01 18:21:10 +02:00
|
|
|
_downKeys.add(new KeyDown(pointerId, key, touchX, touchY));
|
2015-07-31 20:48:19 +02:00
|
|
|
invalidate();
|
|
|
|
return ;
|
2015-07-30 22:30:29 +02:00
|
|
|
}
|
2015-08-01 01:36:41 +02:00
|
|
|
x += keyW;
|
2015-07-30 20:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 01:14:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onTouchUp(int pointerId)
|
|
|
|
{
|
2015-08-01 18:21:10 +02:00
|
|
|
KeyDown k = getKeyDown(pointerId);
|
2015-08-01 16:33:30 +02:00
|
|
|
|
2015-08-01 18:21:10 +02:00
|
|
|
if (k != null)
|
2015-08-01 16:33:30 +02:00
|
|
|
{
|
2015-08-01 18:21:10 +02:00
|
|
|
if (k.value != null)
|
2015-08-01 21:36:40 +02:00
|
|
|
_ime.handleKeyUp(k.value);
|
2015-08-01 18:21:10 +02:00
|
|
|
_downKeys.remove(k);
|
|
|
|
invalidate();
|
|
|
|
return ;
|
2015-07-31 01:14:35 +02:00
|
|
|
}
|
2015-07-30 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMeasure(int wSpec, int hSpec)
|
|
|
|
{
|
|
|
|
int height;
|
|
|
|
|
2015-07-31 20:48:19 +02:00
|
|
|
if (_keyboard.getRows() == null)
|
2015-07-30 20:14:55 +02:00
|
|
|
height = 0;
|
|
|
|
else
|
2015-08-01 01:36:41 +02:00
|
|
|
height = (int)(_keyHeight * ((float)_keyboard.getRows().size())
|
|
|
|
+ (_verticalMargin * 2));
|
2015-07-30 20:14:55 +02:00
|
|
|
setMeasuredDimension(MeasureSpec.getSize(wSpec), height);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDraw(Canvas canvas)
|
|
|
|
{
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
|
2015-08-01 01:36:41 +02:00
|
|
|
y = _verticalMargin;
|
2015-07-31 20:48:19 +02:00
|
|
|
for (KeyboardData.Row row : _keyboard.getRows())
|
2015-07-30 20:14:55 +02:00
|
|
|
{
|
2015-08-01 01:36:41 +02:00
|
|
|
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2 + _horizontalMargin;
|
2015-07-31 20:48:19 +02:00
|
|
|
for (KeyboardData.Key k : row)
|
2015-07-30 20:14:55 +02:00
|
|
|
{
|
|
|
|
float keyW = _keyWidth * k.width;
|
2015-08-01 18:21:10 +02:00
|
|
|
if (isKeyDown(k))
|
2015-08-01 01:36:41 +02:00
|
|
|
canvas.drawRect(x + _keyBgPadding, y + _keyBgPadding,
|
|
|
|
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding, _keyDownBgPaint);
|
2015-07-30 22:30:29 +02:00
|
|
|
else
|
2015-08-01 21:36:40 +02:00
|
|
|
canvas.drawRoundRect(new RectF(x + _keyBgPadding, y + _keyBgPadding,
|
|
|
|
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding), _keyRound, _keyRound, _keyBgPaint);
|
2015-07-30 20:14:55 +02:00
|
|
|
if (k.key0 != null)
|
2015-08-01 16:33:30 +02:00
|
|
|
canvas.drawText(k.key0.getSymbol(), keyW / 2 + x,
|
2015-08-01 01:36:41 +02:00
|
|
|
(_keyHeight + _keyLabelPaint.getTextSize()) / 2 + y, _keyLabelPaint);
|
2015-07-31 23:17:07 +02:00
|
|
|
float textOffsetY = _keySubLabelPaint.getTextSize() / 2;
|
2015-08-01 01:36:41 +02:00
|
|
|
float subPadding = _keyPadding + _keyBgPadding;
|
2015-07-30 20:14:55 +02:00
|
|
|
if (k.key1 != null)
|
2015-08-01 16:33:30 +02:00
|
|
|
canvas.drawText(k.key1.getSymbol(), x + subPadding,
|
2015-08-01 01:36:41 +02:00
|
|
|
y + subPadding + textOffsetY, _keySubLabelPaint);
|
2015-07-30 20:14:55 +02:00
|
|
|
if (k.key2 != null)
|
2015-08-01 16:33:30 +02:00
|
|
|
canvas.drawText(k.key2.getSymbol(), x + keyW - subPadding,
|
2015-08-01 01:36:41 +02:00
|
|
|
y + subPadding + textOffsetY, _keySubLabelPaint);
|
2015-07-31 23:52:47 +02:00
|
|
|
textOffsetY /= 2; // lol
|
2015-07-30 20:14:55 +02:00
|
|
|
if (k.key3 != null)
|
2015-08-01 16:33:30 +02:00
|
|
|
canvas.drawText(k.key3.getSymbol(), x + subPadding,
|
2015-08-01 01:36:41 +02:00
|
|
|
y + _keyHeight - subPadding + textOffsetY, _keySubLabelPaint);
|
2015-07-30 20:14:55 +02:00
|
|
|
if (k.key4 != null)
|
2015-08-01 16:33:30 +02:00
|
|
|
canvas.drawText(k.key4.getSymbol(), x + keyW - subPadding,
|
2015-08-01 01:36:41 +02:00
|
|
|
y + _keyHeight - subPadding + textOffsetY, _keySubLabelPaint);
|
|
|
|
x += keyW;
|
2015-07-30 20:14:55 +02:00
|
|
|
}
|
2015-08-01 01:36:41 +02:00
|
|
|
y += _keyHeight;
|
2015-07-30 20:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-01 18:21:10 +02:00
|
|
|
|
|
|
|
private class KeyDown
|
|
|
|
{
|
|
|
|
private static final float SUB_VALUE_DIST = 6f;
|
|
|
|
|
|
|
|
public int pointerId;
|
|
|
|
public KeyValue value;
|
|
|
|
public KeyboardData.Key key;
|
|
|
|
public float downX;
|
|
|
|
public float downY;
|
|
|
|
|
|
|
|
public KeyDown(int pointerId, KeyboardData.Key key, float x, float y)
|
|
|
|
{
|
|
|
|
this.pointerId = pointerId;
|
2015-08-01 21:36:40 +02:00
|
|
|
value = key.key0;
|
2015-08-01 18:21:10 +02:00
|
|
|
this.key = key;
|
2015-08-01 21:36:40 +02:00
|
|
|
downX = x;
|
|
|
|
downY = y;
|
2015-08-01 18:21:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 21:36:40 +02:00
|
|
|
public boolean updateDown(float x, float y)
|
2015-08-01 18:21:10 +02:00
|
|
|
{
|
2015-08-01 21:36:40 +02:00
|
|
|
KeyValue newValue = getDownValue(x - downX, y - downY);
|
|
|
|
|
|
|
|
if (newValue != null && newValue != value)
|
|
|
|
{
|
|
|
|
value = newValue;
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
return (false);
|
2015-08-01 18:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private KeyValue getDownValue(float x, float y)
|
|
|
|
{
|
|
|
|
if ((Math.abs(x) + Math.abs(y)) < SUB_VALUE_DIST)
|
|
|
|
return (key.key0);
|
|
|
|
if (x < 0)
|
|
|
|
{
|
|
|
|
if (y < 0)
|
|
|
|
return (key.key1);
|
|
|
|
return (key.key3);
|
|
|
|
}
|
|
|
|
else if (y < 0)
|
|
|
|
return (key.key2);
|
|
|
|
return (key.key4);
|
|
|
|
}
|
|
|
|
}
|
2015-07-30 20:14:55 +02:00
|
|
|
}
|