summaryrefslogtreecommitdiff
path: root/src/us/minak/IMEView.java
blob: a49ca6915a7467ab2a0d848983d3132ce832747c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 ********************************************************************************
 * Copyright (c) 2012 Samsung Electronics, Inc.
 * All rights reserved.
 *
 * This software is a confidential and proprietary information of Samsung
 * Electronics, Inc. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Samsung Electronics.
 ********************************************************************************
 */

package us.minak;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Queue;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

/**
 * Represents the container for the drawing space and the two side panels.
 */
public class IMEView extends RelativeLayout {
	private StringReciever mOnCharacterEnteredListener;
	private OnBackspacePressedListener mOnBackspacePressedListener;
	private Button mShiftButton;
	private ShiftState mShiftState = ShiftState.OFF;
	private final Queue<Character> mSymbolsQueue = new LinkedList<Character>();
	private float x;
	private float y;
	private boolean ongoingGesture = false;

	public boolean setTouchLocation(float x, float y) {
		if (!ongoingGesture) {
			this.x = x;
			this.y = y;
			return true;
		}
		return false;
	}

	public void setState(boolean state) {
		ongoingGesture = state;
	}

	public boolean getState() {
		return ongoingGesture;
	}

	private enum ShiftState {
		OFF, ON, CAPS_LOCK
	};

	public IMEView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onFinishInflate() {
		IMEGestureOverlayView drawingSpaceView = (IMEGestureOverlayView) findViewById(R.id.drawing_space);
		drawingSpaceView.setOnGestureRecognizedListener(new StringReciever() {
			@Override
			public void putString(String character) {
				enterCharacter(character);
			}
		});

		mShiftButton = (Button) findViewById(R.id.shift_btn);
		mShiftButton.setOnClickListener(mButtonClickListener);
		mShiftButton.setOnLongClickListener(mButtonLongClickListener);

		final Button backspaceButton = (Button) findViewById(R.id.backspace_btn);
		backspaceButton.setOnClickListener(mButtonClickListener);
		backspaceButton.setOnLongClickListener(mButtonLongClickListener);

		final Button spaceButton = (Button) findViewById(R.id.space_btn);
		spaceButton.setOnClickListener(mButtonClickListener);
		spaceButton.setOnLongClickListener(mButtonLongClickListener);

		//dynamic MetaCircle adding stuff here. replace null with Shift or Ctrl or Meta or Alt or Hyper or whatever.
		drawingSpaceView.circles.add(new MetaCircle((float)50.0, (float)50.0, (float)20.0, Color.RED, new MetaExpression(null)));
		drawingSpaceView.circles.add(new MetaCircle((float)70.0, (float)70.0, (float)20.0, Color.RED, new MetaExpression(null)));
		drawingSpaceView.circles.add(new MetaCircle((float)50.0, (float)30.0, (float)20.0, Color.RED, new MetaExpression(null)));
	}

	public void setOnCharacterEnteredListener(StringReciever onCharacterEnteredListener) {
		mOnCharacterEnteredListener = onCharacterEnteredListener;
	}

	public void setOnBackspacePressedListener(OnBackspacePressedListener onBackspacePressedListener) {
		mOnBackspacePressedListener = onBackspacePressedListener;
	}

	public Queue<Character> getSymbolsQueue() {
		return mSymbolsQueue;
	}

	/**
	 * Listener handling pressing all buttons.
	 */
	private final OnClickListener mButtonClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.shift_btn:
				shift();
				break;
			case R.id.backspace_btn:
				mOnBackspacePressedListener.backspacePressed(false);
				break;
			case R.id.space_btn:
				mOnCharacterEnteredListener.putString(" ");
				break;
			default:
				throw new IllegalArgumentException();
			}
		}
	};

	/**
	 * Listener handling long pressing all buttons.
	 */
	private final OnLongClickListener mButtonLongClickListener = new OnLongClickListener() {
		@Override
		public boolean onLongClick(View v) {
			switch (v.getId()) {
			case R.id.shift_btn:
				break;
			case R.id.backspace_btn:
				mOnBackspacePressedListener.backspacePressed(true);
				return true;
			case R.id.space_btn:
				break;
			default:
				throw new IllegalArgumentException();
			}

			return false;
		}
	};

	/*private final OnTouchListener mOnTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			return setTouchLocation(event.getX(), event.getY());
		}	
	};*/

	/**
	 * Changes shift state to the next one (OFF -> ON -> CAPS LOCK).
	 */
	private void shift() {
		switch (mShiftState) {
		case OFF:
			mShiftState = ShiftState.ON;
			//mShiftButton.setBackgroundResource(R.drawable.shift_on);
			break;
		case ON:
			mShiftState = ShiftState.CAPS_LOCK;
			//mShiftButton.setBackgroundResource(R.drawable.shift_caps_lock);
			break;
		case CAPS_LOCK:
			mShiftState = ShiftState.OFF;
			//mShiftButton.setBackgroundResource(R.drawable.shift_off);
			break;
		default:
			throw new IllegalArgumentException();
		}
	}

	/**
	 * Passes the given character to the input service.
	 *
	 * @param character
	 *            The character to enter
	 */
	private void enterCharacter(String character) {
		//for each circle in circles check to see if the touch was in the circle and apply the meta-key
		switch (mShiftState) {
		case OFF:
			mOnCharacterEnteredListener.putString(character);
			break;
		case ON:
			mOnCharacterEnteredListener.putString(character.toUpperCase(Locale.ENGLISH));
			shift();
			break;
		case CAPS_LOCK:
			mOnCharacterEnteredListener.putString(character.toUpperCase(Locale.ENGLISH));
			break;
		default:
			throw new IllegalArgumentException();
		}
	}



}