/*
 * Copyright 2017-2018 Nikita Shakarun
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package javax.microedition.lcdui.game;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class GameCanvas extends Canvas {

	public static final int UP_PRESSED = 1 << Canvas.UP;
	public static final int DOWN_PRESSED = 1 << Canvas.DOWN;
	public static final int LEFT_PRESSED = 1 << Canvas.LEFT;
	public static final int RIGHT_PRESSED = 1 << Canvas.RIGHT;
	public static final int FIRE_PRESSED = 1 << Canvas.FIRE;
	public static final int GAME_A_PRESSED = 1 << Canvas.GAME_A;
	public static final int GAME_B_PRESSED = 1 << Canvas.GAME_B;
	public static final int GAME_C_PRESSED = 1 << Canvas.GAME_C;
	public static final int GAME_D_PRESSED = 1 << Canvas.GAME_D;

	private Image image;
	private int key;
	private int repeatedKey;
	private boolean suppressCommands;

	public GameCanvas(boolean suppressCommands) {
		super();
		this.suppressCommands = suppressCommands;
		image = Image.createImage(width, maxHeight);
	}

	@Override
	public void paint(Graphics g) {
		g.drawImage(image, 0, 0, Graphics.LEFT | Graphics.TOP);
	}

	private int convertGameKeyCode(int keyCode) {
		switch (keyCode) {
			case KEY_LEFT:
			case KEY_NUM4:
				return LEFT_PRESSED;
			case KEY_UP:
			case KEY_NUM2:
				return UP_PRESSED;
			case KEY_RIGHT:
			case KEY_NUM6:
				return RIGHT_PRESSED;
			case KEY_DOWN:
			case KEY_NUM8:
				return DOWN_PRESSED;
			case KEY_FIRE:
			case KEY_NUM5:
				return FIRE_PRESSED;
			case KEY_NUM7:
				return GAME_A_PRESSED;
			case KEY_NUM9:
				return GAME_B_PRESSED;
			case KEY_STAR:
				return GAME_C_PRESSED;
			case KEY_POUND:
				return GAME_D_PRESSED;
			default:
				return 0;
		}
	}

	@Override
	public void postKeyPressed(int keyCode) {
		key |= convertGameKeyCode(keyCode);
		if (!(suppressCommands && isGameAction(keyCode))) {
			super.postKeyPressed(keyCode);
		}
	}

	@Override
	public void postKeyReleased(int keyCode) {
		repeatedKey &= ~convertGameKeyCode(keyCode);
		if (!(suppressCommands && isGameAction(keyCode))) {
			super.postKeyReleased(keyCode);
		}
	}

	@Override
	public void postKeyRepeated(int keyCode) {
		repeatedKey |= convertGameKeyCode(keyCode);
		if (!(suppressCommands && isGameAction(keyCode))) {
			super.postKeyRepeated(keyCode);
		}
	}

	private boolean isGameAction(int keyCode) {
		try {
			return getGameAction(keyCode) != 0;
		} catch (IllegalArgumentException iae) {
			return false;
		}
	}

	public int getKeyStates() {
		int temp = key;
		temp |= repeatedKey;
		key = repeatedKey;
		return temp;
	}

	public Graphics getGraphics() {
		return image.getGraphics();
	}

	public void flushGraphics() {
		flushGraphics(0, 0, width, height);
	}

	public void flushGraphics(int x, int y, int width, int height) {
		flushBuffer(image);
	}
}

@@ -1383,34 +1383,8 @@
 ey;%0A
-%09private int repeatedKey;%0A
 %09pri
@@ -2320,13 +2320,17 @@
 %7B%0A%09%09
-key %7C
+int code 
 = co
@@ -2348,32 +2348,47 @@
 yCode(keyCode);%0A
+%09%09key %7C= code;%0A
 %09%09if (!(suppress
@@ -2399,37 +2399,25 @@
 ands && 
-isGameAction(keyCode)
+code != 0
 )) %7B%0A%09%09%09
@@ -2516,24 +2516,19 @@
 %7B%0A%09%09
-repeatedKey &
+int code 
 = 
-~
 conv
@@ -2544,32 +2544,48 @@
 yCode(keyCode);%0A
+%09%09key &= ~code;%0A
 %09%09if (!(suppress
@@ -2596,37 +2596,25 @@
 ands && 
-isGameAction(keyCode)
+code != 0
 )) %7B%0A%09%09%09
@@ -2714,21 +2714,17 @@
 %7B%0A%09%09
-repeatedKey %7C
+int code 
 = co
@@ -2750,16 +2750,31 @@
 yCode);%0A
+%09%09key %7C= code;%0A
 %09%09if (!(
@@ -2797,29 +2797,17 @@
  && 
-isGameAction(keyCode)
+code != 0
 )) %7B
@@ -2856,256 +2856,34 @@
 %0A%0A%09p
-rivate boolean isGameAction(int keyCode) %7B%0A%09%09try %7B%0A%09%09%09return getGameAction(keyCode) != 0;%0A%09%09%7D catch (IllegalArgumentException iae) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%7D%0A%0A%09public int getKeyStates() %7B%0A%09%09int temp = key;%0A%09%09temp %7C= repeatedKey;%0A%09%09key = repeatedKey;
+ublic int getKeyStates() %7B
 %0A%09%09r
@@ -2892,12 +2892,11 @@
 urn 
-temp
+key
 ;%0A%09%7D

Change key states logic in GameCanvas