Skip to content

Commit

Permalink
TouchScreen input (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBRDeveloper authored Nov 28, 2023
1 parent 9aded6e commit 7c9d17f
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.view.PandaGlSurfaceView;
import com.panda3ds.pandroid.view.PandaLayoutController;
import com.panda3ds.pandroid.view.controller.ControllerLayout;

public class GameActivity extends BaseActivity {
private PandaGlSurfaceView pandaSurface;
Expand All @@ -46,11 +42,21 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.addView(pandaSurface, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

controllerLayout = findViewById(R.id.controller_layout);

controllerLayout.initialize();

((CheckBox)findViewById(R.id.hide_screen_controller))
.setOnCheckedChangeListener((buttonView, isChecked) -> {
controllerLayout.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
findViewById(R.id.overlay_controller)
.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
});

}

@Override
protected void onResume() {
super.onResume();
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

public class Vector2 {
public float x,y;

public Vector2(){
this(0.0f);
}

public Vector2(float value){
this(value,value);
public Vector2(Vector2 value){
this(value.x,value.y);
}

public Vector2(float x, float y){
Expand All @@ -23,4 +18,9 @@ public float distanceTo(Vector2 vec){
public static float distance(float x, float y, float x2, float y2){
return (float) Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
}

public void set(float x, float y) {
this.x = x;
this.y = y;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@
import static android.opengl.GLES32.*;

import android.content.res.Resources;
import android.graphics.Rect;
import android.opengl.GLSurfaceView;
import android.util.Log;

import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.view.renderer.layout.ConsoleLayout;
import com.panda3ds.pandroid.view.renderer.ConsoleRenderer;
import com.panda3ds.pandroid.view.renderer.layout.DefaultScreenLayout;

import java.util.ArrayList;

public class PandaGlRenderer implements GLSurfaceView.Renderer {
public class PandaGlRenderer implements GLSurfaceView.Renderer, ConsoleRenderer {

private final String romPath;
private ConsoleLayout displayLayout;
private int screenWidth, screenHeight;
private int screenTexture;
public int screenFbo;

PandaGlRenderer(String romPath) {
super();
this.romPath = romPath;

screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
setLayout(new DefaultScreenLayout());
}

@Override
Expand All @@ -40,8 +46,6 @@ protected void finalize() throws Throwable {
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
Log.i("pandroid", glGetString(GL_EXTENSIONS));
Log.w("pandroid", glGetString(GL_VERSION));
screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
Expand Down Expand Up @@ -71,40 +75,48 @@ public void onDrawFrame(GL10 unused) {
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, screenFbo);

if (screenWidth > screenHeight) {
int topDisplayWidth = (int)((screenHeight / (float)Constants.N3DS_HEIGHT) * Constants.N3DS_WIDTH);
int topDisplayHeight = screenHeight;

if (topDisplayWidth > screenWidth * 0.7){
topDisplayWidth = (int)(screenWidth * 0.7);
topDisplayHeight = (int)((topDisplayWidth / (float)Constants.N3DS_WIDTH) * Constants.N3DS_HEIGHT);
}

int bottomDisplayHeight = (int)(((screenWidth - topDisplayWidth) / 320) * Constants.N3DS_HEIGHT);
int topDisplayY = screenHeight - topDisplayHeight;
int bottomDisplayY = screenHeight - bottomDisplayHeight;

glBlitFramebuffer(0, Constants.N3DS_HEIGHT,
Constants.N3DS_WIDTH, Constants.N3DS_HEIGHT * 2,
0, topDisplayY,
topDisplayWidth,topDisplayY+topDisplayHeight,
GL_COLOR_BUFFER_BIT, GL_LINEAR);

glBlitFramebuffer(
40, 0,
360, Constants.N3DS_HEIGHT,
topDisplayWidth, bottomDisplayY,
screenWidth,bottomDisplayY+bottomDisplayHeight,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
} else {
int h = (int)((screenWidth / (float)Constants.N3DS_WIDTH) * Constants.N3DS_HEIGHT * 2);
glBlitFramebuffer(0, 0, Constants.N3DS_WIDTH, Constants.N3DS_HEIGHT * 2, 0, screenHeight - h, screenWidth, screenHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
Rect topScreen = displayLayout.getTopDisplayBounds();
Rect bottomScreen = displayLayout.getBottomDisplayBounds();

glBlitFramebuffer(
0, 480,
400, 240,
topScreen.left, screenHeight - topScreen.top,
topScreen.right, screenHeight - topScreen.bottom,
GL_COLOR_BUFFER_BIT, GL_LINEAR);

glBlitFramebuffer(
40, 240,
360, 0,
bottomScreen.left, screenHeight - bottomScreen.top,
bottomScreen.right, screenHeight - bottomScreen.bottom,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
}

public void onSurfaceChanged(GL10 unused, int width, int height) {
screenWidth = width;
screenHeight = height;
glDisable(GL_SCISSOR_TEST);

displayLayout.update(screenWidth, screenHeight);
}

@Override
public void setLayout(ConsoleLayout layout) {
displayLayout = layout;
displayLayout.setTopDisplaySourceSize(400, 240);
displayLayout.setBottomDisplaySourceSize(320, 240);
displayLayout.update(screenWidth, screenHeight);
}

@Override
public ConsoleLayout getLayout() {
return displayLayout;
}

@Override
public String getBackendName() {
return "OpenGL";
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package com.panda3ds.pandroid.view;

import android.content.Context;
import android.graphics.Canvas;
import android.opengl.GLSurfaceView;
import android.util.Log;

public class PandaGlSurfaceView extends GLSurfaceView {
import androidx.annotation.NonNull;

import com.panda3ds.pandroid.math.Vector2;
import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.view.controller.TouchEvent;
import com.panda3ds.pandroid.view.controller.nodes.TouchScreenNodeImpl;
import com.panda3ds.pandroid.view.renderer.ConsoleRenderer;

public class PandaGlSurfaceView extends GLSurfaceView implements TouchScreenNodeImpl {
final PandaGlRenderer renderer;
private int size_width;
private int size_height;

public PandaGlSurfaceView(Context context, String romPath) {
super(context);
Expand All @@ -13,4 +25,26 @@ public PandaGlSurfaceView(Context context, String romPath) {
renderer = new PandaGlRenderer(romPath);
setRenderer(renderer);
}

public ConsoleRenderer getRenderer() {
return renderer;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
size_width = getMeasuredWidth();
size_height = getMeasuredHeight();
}

@NonNull
@Override
public Vector2 getSize() {
return new Vector2(size_width, size_height);
}

@Override
public void onTouch(TouchEvent event) {
onTouchScreenPress(renderer, event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ public void initialize(){
.setJoystickListener((joystick, axisX, axisY) -> {
AlberDriver.SetCirclepadAxis((int)(axisX*0x9C), (int)(axisY*0x9C)*-1);
});

refreshChildren();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.panda3ds.pandroid.math.Vector2;
import com.panda3ds.pandroid.utils.Constants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

public class ControllerLayout extends RelativeLayout {
Expand All @@ -34,8 +38,14 @@ public ControllerLayout(Context context, AttributeSet attrs, int defStyleAttr, i
}

public void refreshChildren(){
ArrayList<ControllerNode> nodes = new ArrayList<>();
populateNodesArray(this, nodes);

//Need Reverse: First view is in back and last view is in front for respect android View hierarchy
Collections.reverse(nodes);

controllerNodes.clear();
populateNodesArray(this, controllerNodes);
controllerNodes.addAll(nodes);
}

private void populateNodesArray(ViewGroup group, ArrayList<ControllerNode> list){
Expand Down Expand Up @@ -89,7 +99,7 @@ private void processTouch(boolean up, float x, float y, int index){

float cx = (pos.x - globalPosition[0]);
float cy = (pos.y - globalPosition[1]);
if( x > cx && x < cx+size.x && y > cy && y < cy+size.y){
if(item.isVisible() && x > cx && x < cx+size.x && y > cy && y < cy+size.y){
node = item;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ default Vector2 getPosition(){
return new Vector2(position[0], position[1]);
}

default boolean isVisible(){
return ((View)this).isShown();
}

@NonNull Vector2 getSize();

void onTouch(TouchEvent event);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.panda3ds.pandroid.view.controller.nodes;

import android.graphics.Rect;
import android.util.Log;
import android.view.View;

import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.view.controller.ControllerNode;
import com.panda3ds.pandroid.view.controller.TouchEvent;
import com.panda3ds.pandroid.view.renderer.ConsoleRenderer;

public interface TouchScreenNodeImpl extends ControllerNode {
default void onTouchScreenPress(ConsoleRenderer renderer, TouchEvent event){

View me = (View) this;
boolean hasDownEvent = me.getTag(R.id.TagEventHasDown) != null && (boolean) me.getTag(R.id.TagEventHasDown);

Rect bounds = renderer.getLayout().getBottomDisplayBounds();;

if (event.getX() >= bounds.left && event.getY() >= bounds.top && event.getX() <= bounds.right && event.getY() <= bounds.bottom){
int x = (int) (event.getX() - bounds.left);
int y = (int) (event.getY() - bounds.top);

x = Math.round((x/(float)bounds.width())*320);
y = Math.round((y/(float)bounds.height())*240);

AlberDriver.TouchScreenDown(x,y);

me.setTag(R.id.TagEventHasDown, true);
}

if (hasDownEvent && event.getAction() == TouchEvent.ACTION_UP){
AlberDriver.TouchScreenUp();
me.setTag(R.id.TagEventHasDown, false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.panda3ds.pandroid.view.renderer;

import com.panda3ds.pandroid.view.renderer.layout.ConsoleLayout;

public interface ConsoleRenderer {
void setLayout(ConsoleLayout layout);
ConsoleLayout getLayout();
String getBackendName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.panda3ds.pandroid.view.renderer.layout;

import android.graphics.Rect;

import com.panda3ds.pandroid.math.Vector2;

public interface ConsoleLayout {
void update(int screenWidth, int screenHeight);

void setBottomDisplaySourceSize(int width, int height);
void setTopDisplaySourceSize(int width, int height);

Rect getBottomDisplayBounds();
Rect getTopDisplayBounds();
}
Loading

0 comments on commit 7c9d17f

Please sign in to comment.