Remove fixed keyboard width

Remove the constant of keys per rows. Add the 'shift' attribute to
declare eventual alignment/blanks in layouts.
This commit is contained in:
Jules Aguillon 2021-04-17 23:56:51 +02:00
parent ca07bff133
commit 1421bccc7b
3 changed files with 29 additions and 38 deletions

View File

@ -13,7 +13,7 @@
<key key0="p" key1="insert" key2="0" />
</row>
<row>
<key key0="a" key1="tab" key2="`" />
<key shift="0.5" key0="a" key1="tab" key2="`" />
<key key0="s" key1="accent1" key2="accent2" key4="accent6" />
<key key0="d" key1="accent3" key2="accent4" key3="accent5" />
<key key0="f" />

View File

@ -18,8 +18,6 @@ import java.util.ArrayList;
public class Keyboard2View extends View
implements View.OnTouchListener, Handler.Callback
{
private static final float KEY_PER_ROW = 10;
private static final long VIBRATE_MIN_INTERVAL = 100;
private KeyboardData _keyboard;
@ -187,20 +185,17 @@ public class Keyboard2View extends View
private void onTouchDown(float touchX, float touchY, int pointerId)
{
float x;
float y;
float keyW;
y = _config.marginTop - _config.keyHeight;
float y = _config.marginTop - _config.keyHeight;
for (KeyboardData.Row row : _keyboard.getRows())
{
y += _config.keyHeight;
if (touchY < y || touchY >= (y + _config.keyHeight))
continue ;
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2 + _config.horizontalMargin;
float x = _config.horizontalMargin;
for (KeyboardData.Key key : row.getKeys())
{
keyW = _keyWidth * key.width;
x += key.shift * _keyWidth;
float keyW = _keyWidth * key.width;
if (touchX >= x && touchX < (x + keyW))
{
KeyDown down = getKeyDown(key);
@ -333,21 +328,19 @@ public class Keyboard2View extends View
height = (int)(_config.keyHeight * ((float)_keyboard.getRows().size())
+ _config.marginTop + _config.marginBottom);
setMeasuredDimension(dm.widthPixels, height);
_keyWidth = (getWidth() - (_config.horizontalMargin * 2)) / KEY_PER_ROW;
_keyWidth = (getWidth() - (_config.horizontalMargin * 2)) / _keyboard.getKeysWidth();
}
@Override
protected void onDraw(Canvas canvas)
{
float x;
float y;
y = _config.marginTop;
float y = _config.marginTop;
for (KeyboardData.Row row : _keyboard.getRows())
{
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2f + _config.horizontalMargin;
float x = _config.horizontalMargin;
for (KeyboardData.Key k : row.getKeys())
{
x += k.shift * _keyWidth;
float keyW = _keyWidth * k.width;
KeyDown keyDown = getKeyDown(k);
_tmpRect.set(x + _config.keyBgPadding, y + _config.keyBgPadding,

View File

@ -7,10 +7,14 @@ import java.util.List;
class KeyboardData
{
private final List<Row> _rows;
private final float _keysWidth;
public KeyboardData(ArrayList<Row> rows)
public KeyboardData(List<Row> rows)
{
float kpr = 0.f;
for (Row r : rows) kpr = Math.max(kpr, r.keysWidth());
_rows = rows;
_keysWidth = kpr;
}
public static KeyboardData parse(XmlResourceParser parser)
@ -45,10 +49,9 @@ class KeyboardData
return new KeyboardData(rows);
}
public List<Row> getRows()
{
return (_rows);
}
public List<Row> getRows() { return _rows; }
public float getKeysWidth() { return _keysWidth; }
public KeyboardData removeKeys(MapKeys f)
{
@ -61,12 +64,13 @@ class KeyboardData
public static class Row
{
private final List<Key> _keys;
/* Total width of very keys. Unit is abstract. */
private final float _keysWidth;
public Row(List<Key> keys)
{
float kw = 0.f;
for (Key k : keys) kw += k.width;
for (Key k : keys) kw += k.width + k.shift;
_keys = keys;
_keysWidth = kw;
}
@ -91,10 +95,7 @@ class KeyboardData
public List<Key> getKeys() { return _keys; }
public float getWidth(float keyWidth)
{
return (keyWidth * _keysWidth);
}
public float keysWidth() { return _keysWidth; }
public Row removeKeys(MapKeys f)
{
@ -118,9 +119,12 @@ class KeyboardData
public final KeyValue key3;
public final KeyValue key4;
/* Key width in relative unit. */
public final float width;
/* Extra empty space on the left of the key. */
public final float shift;
public Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w)
public Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w, float s)
{
key0 = k0;
key1 = k1;
@ -128,6 +132,7 @@ class KeyboardData
key3 = k3;
key4 = k4;
width = w;
shift = s;
}
public static Key parse(XmlResourceParser parser) throws Exception
@ -137,23 +142,16 @@ class KeyboardData
KeyValue k2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
KeyValue k3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
KeyValue k4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
float width;
try
{
width = parser.getAttributeFloatValue(null, "width", 1f);
}
catch (Exception e)
{
width = 1f;
}
float width = parser.getAttributeFloatValue(null, "width", 1f);
float shift = parser.getAttributeFloatValue(null, "shift", 0.f);
while (parser.next() != XmlResourceParser.END_TAG)
continue ;
return new Key(k0, k1, k2, k3, k4, width);
return new Key(k0, k1, k2, k3, k4, width, shift);
}
public Key removeKeys(MapKeys f)
{
return new Key(f.map(key0), f.map(key1), f.map(key2), f.map(key3), f.map(key4), width);
return new Key(f.map(key0), f.map(key1), f.map(key2), f.map(key3), f.map(key4), width, shift);
}
}