Skip to content

Commit

Permalink
UI recognizes touch
Browse files Browse the repository at this point in the history
  • Loading branch information
david-palm committed Sep 21, 2022
1 parent 98a4ec6 commit 4b469e0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.opengl3renderer.math.Vec4;
import com.example.opengl3renderer.scene.Scene;
import com.example.opengl3renderer.ui.Component;
import com.example.opengl3renderer.ui.UI;
import com.example.opengl3renderer.ui.object2d.card.Card;
import com.example.opengl3renderer.ui.object2d.card.CardMaterial;

Expand All @@ -36,9 +37,8 @@ public AppRenderer(Context context){
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig){
layerStack.add(new Scene(new Vec2(width, height), context));
// Creating UI test
CardMaterial material = new CardMaterial(context, new Vec4(0.8f, 0.8f, 0.8f, 0.2f));
Card card = new Card(material, new Vec2(0.0f, -0.9f), new Vec2(1.0f, 0.2f), 0.0f, 0.5f);;
layerStack.add(new Component(card));
UI ui = new UI(context);
ui.addToLayerStack(layerStack);

GLES32.glEnable(GLES32.GL_DEPTH_TEST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean onTouchEvent(MotionEvent e){

static private Vec2 convertToDeviceCoordinates(Vec2 coordinate, Vec2 resolution){
float x = coordinate.x / resolution.x * 2.0f - 1.0f;
float y = coordinate.y / resolution.y * 2.0f - 1.0f;
float y = coordinate.y / resolution.y * - 2.0f + 1.0f;
return new Vec2(x, y);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public void rotateObject(float dX, float dY){
float rotationSpeed = 150.0f;
float degToRad = (float) (Math.PI/180.0);
float rotX = dX * rotationSpeed * degToRad;
float rotY = dY * rotationSpeed * degToRad;
Log.d("Layer", rotX + ", " + rotY);
float rotY = -dY * rotationSpeed * degToRad;
object.rotate(new Vec4(0.0f, 1.0f, 0.0f, rotX));
object.rotate(new Vec4(1.0f, 0.0f, 0.0f, rotY));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Component(ArrayList<Object2D> objects){
}

public boolean isInside(float x, float y){
for(Object2D object : objects) {
for(Object2D object : objects){
if(object.isInside(x, y)){
return true;
}
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/example/opengl3renderer/ui/UI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package com.example.opengl3renderer.ui;

import android.content.Context;

import com.example.opengl3renderer.layers.Layer;
import com.example.opengl3renderer.math.Vec2;
import com.example.opengl3renderer.math.Vec4;
import com.example.opengl3renderer.ui.object2d.card.Card;
import com.example.opengl3renderer.ui.object2d.card.CardMaterial;

import java.util.ArrayList;
import java.util.List;

public class UI {
public ArrayList<Component> components;

public UI(Context context){
components = new ArrayList<Component>();
CardMaterial material = new CardMaterial(context, new Vec4(0.8f, 0.8f, 0.8f, 0.2f));
Card card = new Card(material, new Vec2(0.0f, -0.8f), new Vec2(1.0f, 0.2f), 0.0f, 0.5f);
Component menu = new Component(card);
components.add(menu);
}

public ArrayList getComponents(){
return components;
}

public void addToLayerStack(List<Layer> layerStack){
for(int i = 0; i < components.size(); i++){
layerStack.add(components.get(i));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public Card(CardMaterial material){

@Override
public boolean isInside(float x, float y){
if(x >= (position.x - scale.x) && y >= (position.y - scale.y)) {
if(x <= (position.x + scale.x) && y <= (position.y + scale.y)) {
if(x >= (position.x - scale.x) && x <= (position.x + scale.x)) {
if(y >= (position.y - scale.y) && y <= (position.y + scale.y)) {
return true;
}
}
Expand Down

0 comments on commit 4b469e0

Please sign in to comment.