Skip to content

Commit

Permalink
Pandroid: Drawer (wheremyfoodat#356)
Browse files Browse the repository at this point in the history
* Drawer initial commit

* Translate drawer, and remove redundant code

* bonk

---------

Co-authored-by: wheremyfoodat <[email protected]>
  • Loading branch information
GabrielBRDeveloper and wheremyfoodat committed Dec 29, 2023
1 parent e536f5d commit 02d506f
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/jni_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ JNIEnv* jniEnv() {
extern "C" {

AlberFunction(void, Setup)(JNIEnv* env, jobject obj) { env->GetJavaVM(&jvm); }
AlberFunction(void, Pause)(JNIEnv* env, jobject obj) { emulator->pause(); }
AlberFunction(void, Resume)(JNIEnv* env, jobject obj) { emulator->resume(); }

AlberFunction(void, Initialize)(JNIEnv* env, jobject obj) {
emulator = std::make_unique<Emulator>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class AlberDriver {
public static native void SetCirclepadAxis(int x, int y);
public static native void TouchScreenUp();
public static native void TouchScreenDown(int x, int y);
public static native void Pause();
public static native void Resume();

public static native byte[] GetSmdh();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.app.game.AlberInputListener;
import com.panda3ds.pandroid.app.game.DrawerFragment;
import com.panda3ds.pandroid.data.config.GlobalConfig;
import com.panda3ds.pandroid.input.InputHandler;
import com.panda3ds.pandroid.input.InputMap;
Expand All @@ -22,7 +23,14 @@
import com.panda3ds.pandroid.view.PandaLayoutController;

public class GameActivity extends BaseActivity {
private final AlberInputListener inputListener = new AlberInputListener(this);
private final DrawerFragment drawerFragment = new DrawerFragment();
private final AlberInputListener inputListener = new AlberInputListener(() -> {
if (drawerFragment.isOpened()) {
drawerFragment.close();
} else {
drawerFragment.open();
}
});

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -52,6 +60,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
GlobalConfig.set(GlobalConfig.KEY_SCREEN_GAMEPAD_VISIBLE, checked);
});
((CheckBox) findViewById(R.id.hide_screen_controller)).setChecked(GlobalConfig.get(GlobalConfig.KEY_SCREEN_GAMEPAD_VISIBLE));

getSupportFragmentManager().beginTransaction().replace(R.id.drawer_fragment, drawerFragment).commitNow();
}

@Override
Expand All @@ -67,7 +77,9 @@ protected void onResume() {
@Override
protected void onPause() {
super.onPause();

InputHandler.reset();
drawerFragment.open();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.panda3ds.pandroid.app.game;

import android.app.Activity;
import android.view.KeyEvent;
import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.input.InputEvent;
import com.panda3ds.pandroid.input.InputMap;
import com.panda3ds.pandroid.input.KeyName;
import com.panda3ds.pandroid.lang.Function;
import com.panda3ds.pandroid.math.Vector2;

import java.util.Objects;


public class AlberInputListener implements Function<InputEvent> {
private final Activity activity;
public AlberInputListener(Activity activity) { this.activity = activity; }
private final Runnable backListener;
public AlberInputListener(Runnable backListener) { this.backListener = backListener; }

private final Vector2 axis = new Vector2(0.0f, 0.0f);

Expand All @@ -22,7 +21,7 @@ public void run(InputEvent event) {
KeyName key = InputMap.relative(event.getName());

if (Objects.equals(event.getName(), "KEYCODE_BACK")) {
activity.onBackPressed();
backListener.run();
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.panda3ds.pandroid.app.game;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import com.google.android.material.navigation.NavigationView;
import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.data.game.GameMetadata;
import com.panda3ds.pandroid.utils.GameUtils;
import com.panda3ds.pandroid.view.gamesgrid.GameIconView;

public class DrawerFragment extends Fragment implements DrawerLayout.DrawerListener, NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawerContainer;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
drawerContainer = requireActivity().findViewById(R.id.drawer_container);
drawerContainer.removeDrawerListener(this);
drawerContainer.addDrawerListener(this);
drawerContainer.setScrimColor(Color.argb(160, 0,0,0));
drawerContainer.setVisibility(View.GONE);

return inflater.inflate(R.layout.fragment_game_drawer, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
drawerContainer.setVisibility(View.GONE);

GameMetadata game = GameUtils.getCurrentGame();

((GameIconView)view.findViewById(R.id.game_icon)).setImageBitmap(game.getIcon());
((AppCompatTextView)view.findViewById(R.id.game_title)).setText(game.getTitle());
((AppCompatTextView)view.findViewById(R.id.game_publisher)).setText(game.getPublisher());

((NavigationView)view.findViewById(R.id.action_navigation)).setNavigationItemSelectedListener(this);
}

@Override
public void onDetach() {
drawerContainer.removeDrawerListener(this);
super.onDetach();
}

private void refreshLayout() {
drawerContainer.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY);
drawerContainer.requestLayout();
drawerContainer.invalidate();
drawerContainer.forceLayout();
}

public void open() {
if (!drawerContainer.isOpen()) {
drawerContainer.setVisibility(View.VISIBLE);
drawerContainer.open();
drawerContainer.postDelayed(this::refreshLayout, 20);
}
}

public void close() {
if (drawerContainer.isOpen()) {
drawerContainer.close();
}
}

@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {}

@Override
public void onDrawerOpened(@NonNull View drawerView) {
AlberDriver.Pause();
}

@Override
public void onDrawerClosed(@NonNull View drawerView) {
drawerContainer.setVisibility(View.GONE);
AlberDriver.Resume();
}

@Override
public void onDrawerStateChanged(int newState) {}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.resume) {
close();
} else if (id == R.id.exit) {
requireActivity().onBackPressed();
}

return false;
}

public boolean isOpened() {
return drawerContainer.isOpen();
}
}
5 changes: 5 additions & 0 deletions src/pandroid/app/src/main/res/drawable/ic_exit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#000000" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M10.09,15.59L11.5,17l5,-5 -5,-5 -1.41,1.41L12.67,11H3v2h9.67l-2.58,2.59zM19,3H5c-1.11,0 -2,0.9 -2,2v4h2V5h14v14H5v-4H3v4c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z"/>
</vector>
5 changes: 5 additions & 0 deletions src/pandroid/app/src/main/res/drawable/ic_shortcut.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#000000" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M21,11l-6,-6v5H8c-2.76,0 -5,2.24 -5,5v4h2v-4c0,-1.65 1.35,-3 3,-3h7v5L21,11z"/>
</vector>
24 changes: 24 additions & 0 deletions src/pandroid/app/src/main/res/layout/drawer_game_container.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">

<FrameLayout
android:id="@+id/drawer_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>
92 changes: 92 additions & 0 deletions src/pandroid/app/src/main/res/layout/fragment_game_drawer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="?colorSurface">


<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:minHeight="190dp"
android:background="?colorSurfaceVariant">

<androidx.cardview.widget.CardView
android:layout_width="90dp"
android:layout_height="90dp"
app:cardCornerRadius="10dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">

<com.panda3ds.pandroid.view.gamesgrid.GameIconView
android:id="@+id/game_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?colorSurface"/>

</androidx.cardview.widget.CardView>

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_marginBottom="20dp">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/game_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="?colorOnSurfaceVariant"
android:paddingBottom="4dp"
android:gravity="center"
android:textStyle="bold"
android:textSize="19sp"/>

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/game_publisher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="?colorOnSurfaceVariant"
android:textSize="14sp"/>

</androidx.appcompat.widget.LinearLayoutCompat>

</androidx.appcompat.widget.LinearLayoutCompat>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/actions"
style="@style/TextAppearanceGameDrawerSubTitle"/>

<com.google.android.material.navigation.NavigationView
android:id="@+id/action_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/game_drawer_actions"
android:background="?colorSurface"/>

</androidx.appcompat.widget.LinearLayoutCompat>

</ScrollView>

</androidx.appcompat.widget.LinearLayoutCompat>
2 changes: 2 additions & 0 deletions src/pandroid/app/src/main/res/layout/game_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@

</FrameLayout>

<include layout="@layout/drawer_game_container"/>

</FrameLayout>
11 changes: 11 additions & 0 deletions src/pandroid/app/src/main/res/menu/game_drawer_actions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/resume"
android:icon="@drawable/ic_shortcut"
android:title="@string/resume"/>
<item
android:id="@+id/exit"
android:icon="@drawable/ic_exit"
android:title="@string/exit"/>
</menu>
3 changes: 3 additions & 0 deletions src/pandroid/app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
<string name="light">Claro</string>
<string name="dark">Escuro</string>
<string name="black">Preto</string>
<string name="actions">Ações</string>
<string name="exit">Sair</string>
<string name="resume">Continuar</string>
</resources>
3 changes: 3 additions & 0 deletions src/pandroid/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
<string name="light">Light</string>
<string name="dark">Dark</string>
<string name="black">Black</string>
<string name="actions">Actions</string>
<string name="exit">Exit</string>
<string name="resume">Resume</string>
</resources>
9 changes: 9 additions & 0 deletions src/pandroid/app/src/main/res/values/styleable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
<item name="android:color">?colorSecondary</item>
</style>

<style name="TextAppearanceGameDrawerSubTitle">
<item name="android:textSize">14sp</item>
<item name="textAllCaps">true</item>
<item name="android:alpha">0.7</item>
<item name="android:paddingStart">24dp</item>
<item name="android:paddingEnd">24dp</item>
<item name="android:textStyle">bold</item>
</style>

<style name="Pandroid.SeekbarPreference" parent="Preference.SeekBarPreference">
<item name="android:textColorSecondary">#C00</item>
</style>
Expand Down

0 comments on commit 02d506f

Please sign in to comment.