Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Android.Content;
- using Android.Widget;
- using Android.Util;
- using Android.Graphics;
- using System.IO;
- using Newtonsoft.Json;
- using Android.Views;
- namespace Apps.Droid
- {
- public class KeyboardView : LinearLayout
- {
- public static string JSON_PATH = "keyboard/",
- SMALL_PATH = JSON_PATH + "small/",
- LARGE_PATH = JSON_PATH + "large/",
- QWERTY_FILENAME = "qwerty.json",
- MATH_FILENAME = "math.json";
- public static Color BG;
- public int keyWidth { get; set; }
- public int keyHeight { get; set; }
- public int keyMargin { get; set; }
- public bool swapped { get; set; }
- public bool shifted { get; set; }
- public bool superShifted { get; set; }
- public bool hasSuperShifted { get; set; }
- public string subject { get; set; }
- protected Keyboard keyboard { get; set; }
- protected List<List<KeyView>> keyLayout { get; set; }
- protected bool isPortrait { get; set; }
- public KeyboardView(Context context, IAttributeSet attrs)
- : base(context, attrs)
- {
- Init();
- }
- public KeyboardView(Context context, IAttributeSet attrs, int defStyle)
- : base(context, attrs, defStyle)
- {
- Init();
- }
- public KeyboardView(Context context, IAttributeSet attrs, int defStyle, int defStyleRes)
- : base(context, attrs, defStyle, defStyleRes)
- {
- Init();
- }
- protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
- {
- base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
- RelativeLayout parent = (RelativeLayout)Parent;
- int newWidth = parent.Width,
- newHeight = 0;
- // account for landscape mode
- if (parent.Width > parent.Height)
- {
- isPortrait = false;
- newHeight = parent.Height / 2;
- }
- else
- {
- isPortrait = true;
- newHeight = (int)(parent.Height * .4f);
- }
- ((MainActivity)Context).Debug("[" + newWidth + ", " + newHeight + "]");
- SetMeasuredDimension(newWidth, newHeight);
- }
- public void Init()
- {
- // make sure the keyboard will draw
- SetWillNotDraw(false);
- // define colors from XML
- KeyboardView.BG = Color.ParseColor(Context.GetString(Resource.Color.MWBlue));
- KeyView.BG_NORMAL = Color.White;
- KeyView.BG_LIGHT = Color.ParseColor(Context.GetString(Resource.Color.MWLightBlue));
- KeyView.BG_DARK = Color.ParseColor(Context.GetString(Resource.Color.MWDarkBlue));
- KeyView.FG_PRIMARYDARK = Color.White;
- KeyView.FG_PRIMARYLIGHT = Color.Black;
- KeyView.FG_SECONDARY = Color.ParseColor(Context.GetString(Resource.Color.MWGray));
- subject = "algebra";
- // default to qwerty
- LoadKeyboard(KeyboardType.QWERTY, false);
- }
- public void LoadKeyboard(KeyboardType type, bool invalidate = true)
- {
- // determine file path
- string path = "";
- string subjectPath = "";
- switch (type)
- {
- case KeyboardType.MATH_LARGE:
- path = LARGE_PATH + MATH_FILENAME;
- subjectPath = LARGE_PATH + subject + ".json";
- break;
- case KeyboardType.MATH_SMALL:
- path = SMALL_PATH + MATH_FILENAME;
- subjectPath = SMALL_PATH + subject + ".json";
- break;
- default:
- case KeyboardType.QWERTY:
- path = JSON_PATH + QWERTY_FILENAME;
- break;
- }
- // create a new keyboard object
- string json = "",
- subjectJson = "";
- using (StreamReader sr = new StreamReader(Context.Assets.Open(path)))
- {
- json = sr.ReadToEnd();
- }
- if (!string.IsNullOrEmpty(subjectPath))
- {
- using (StreamReader sr = new StreamReader(Context.Assets.Open(subjectPath)))
- {
- subjectJson = sr.ReadToEnd();
- }
- }
- // tell the draw to rebuild
- keyLayout = null;
- // default the states
- swapped = false;
- shifted = false;
- hasSuperShifted = false;
- keyboard = Keyboard.Create(type, json, subjectJson);
- if (invalidate)
- PostInvalidate();
- }
- public void Build()
- {
- // only build once
- if (keyLayout != null)
- return;
- // clear out children
- RemoveAllViews();
- // define sizes of stuff
- if (isPortrait)
- {
- keyMargin = (int)(MeasuredWidth * .01f);
- }
- else
- {
- keyMargin = (int)(MeasuredHeight * .01f);
- }
- keyWidth = (MeasuredWidth - (keyMargin * 2)) / keyboard.MaxCols;
- keyHeight = (MeasuredHeight - (keyMargin * 2)) / keyboard.Rows.Count;
- // set general padding around keyboardview
- SetPadding(keyMargin, keyMargin, keyMargin, keyMargin);
- // build KeyLayout from the keyboard object
- keyLayout = new List<List<KeyView>>();
- int idx = 0;
- foreach (List<Key> row in keyboard.Rows)
- {
- keyLayout.Add(new List<KeyView>());
- // create and add new KeyboardRowView
- KeyboardRowView krv = new KeyboardRowView(Context, this, idx);
- AddView(krv);
- // figure out if we need a margin offset for this row
- int extraMargin = 0;
- int numCols = CountRowCols(row);
- if (numCols < keyboard.MaxCols)
- {
- // measure full width of the button container and the total row margin
- int rowWidth = (int)(numCols * keyWidth);
- int rowMargin = MeasuredWidth - (keyMargin * 2) - rowWidth;
- // add the offset
- extraMargin = rowMargin / 2;
- }
- // build keys and add them to keyLayout and KeyboardRowView
- int idx2 = 0;
- foreach (Key key in row)
- {
- int leftMargin = idx2 == 0 ? extraMargin : 0;
- KeyView kv = new KeyView(Context, this, key, leftMargin);
- keyLayout[idx].Add(kv);
- krv.AddView(kv);
- idx2++;
- }
- idx++;
- }
- }
- protected override void OnDraw(Canvas canvas)
- {
- Build();
- base.OnDraw(canvas);
- // background
- Paint bg = new Paint(PaintFlags.AntiAlias);
- bg.Color = BG; // light blue
- canvas.DrawRect(0, 0, MeasuredWidth, Height, bg);
- //InvalidateKeys();
- }
- // invalidate all the KeyView children manually since Android can't seem to follow its own rules
- protected void InvalidateKeys()
- {
- for (int idx = 0; idx < ChildCount; idx++)
- {
- KeyboardRowView krv = (KeyboardRowView)GetChildAt(idx);
- for (int idx2 = 0; idx2 < krv.ChildCount; idx2++)
- {
- KeyView kv = (KeyView)krv.GetChildAt(idx2);
- kv.Invalidate();
- }
- }
- }
- // invalidate all KeyView's children of this view so they redraw
- //protected void DrawChildren(Canvas canvas)
- //{
- // for (int idx = 0; idx < ChildCount; idx++)
- // {
- // KeyboardRowView krv = (KeyboardRowView)GetChildAt(idx);
- // for (int idx2 = 0; idx2 < krv.ChildCount; idx2++)
- // {
- // KeyView kv = (KeyView)krv.GetChildAt(idx2);
- // kv.Draw(canvas);
- // }
- // }
- //}
- protected int CountRowCols(List<Key> row)
- {
- int cols = 0;
- foreach (Key k in row)
- {
- int span = string.IsNullOrEmpty(k.Span) ? 1 : Convert.ToInt32(k.Span);
- cols += span;
- }
- return cols;
- }
- public void Swap()
- {
- swapped = swapped ? false : true;
- PostInvalidate();
- ((MainActivity)Context).Debug("[SWAP]");
- }
- public void Shift()
- {
- shifted = shifted ? false : true;
- superShifted = false;
- PostInvalidate();
- ((MainActivity)Context).Debug("[SHIFT]");
- }
- public void SuperShift()
- {
- superShifted = superShifted ? false : true;
- shifted = superShifted;
- hasSuperShifted = true;
- PostInvalidate();
- ((MainActivity)Context).Debug("[SUPER SHIFT]");
- }
- public void Backspace()
- {
- MainActivity activity = (MainActivity)Context;
- activity.RunOnUiThread(() =>
- {
- activity.wvEditor.LoadUrl("javascript:(function() { Mathway.UI.Editor.keypress('backspace', '', -1); })()");
- // haptic feedback
- PerformHapticFeedback(FeedbackConstants.KeyboardTap);
- });
- }
- // a non-special key was pressed and should be sent to the app
- public void InsertKey(KeyState ks)
- {
- MainActivity activity = (MainActivity)Context;
- activity.RunOnUiThread(() =>
- {
- string jsCall = shifted ? "keydown" : "keypress";
- activity.wvEditor.LoadUrl("javascript:(function() { Mathway.UI.Editor." + jsCall + "('" + ks.Id + "', '" + ks.Character + "', '" + ks.Ascii + "');})()");
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement