From 02d506f843172940d175919841f46201caf48731 Mon Sep 17 00:00:00 2001 From: Gabriel Machado <97042217+GabrielBRDeveloper@users.noreply.github.com> Date: Fri, 29 Dec 2023 13:01:58 -0400 Subject: [PATCH] Pandroid: Drawer (#356) * Drawer initial commit * Translate drawer, and remove redundant code * bonk --------- Co-authored-by: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> --- src/jni_driver.cpp | 2 + .../com/panda3ds/pandroid/AlberDriver.java | 2 + .../panda3ds/pandroid/app/GameActivity.java | 14 ++- .../pandroid/app/game/AlberInputListener.java | 9 +- .../pandroid/app/game/DrawerFragment.java | 111 ++++++++++++++++++ .../app/src/main/res/drawable/ic_exit.xml | 5 + .../app/src/main/res/drawable/ic_shortcut.xml | 5 + .../main/res/layout/drawer_game_container.xml | 24 ++++ .../main/res/layout/fragment_game_drawer.xml | 92 +++++++++++++++ .../app/src/main/res/layout/game_activity.xml | 2 + .../src/main/res/menu/game_drawer_actions.xml | 11 ++ .../src/main/res/values-pt-rBR/strings.xml | 3 + .../app/src/main/res/values/strings.xml | 3 + .../app/src/main/res/values/styleable.xml | 9 ++ 14 files changed, 286 insertions(+), 6 deletions(-) create mode 100644 src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/game/DrawerFragment.java create mode 100644 src/pandroid/app/src/main/res/drawable/ic_exit.xml create mode 100644 src/pandroid/app/src/main/res/drawable/ic_shortcut.xml create mode 100644 src/pandroid/app/src/main/res/layout/drawer_game_container.xml create mode 100644 src/pandroid/app/src/main/res/layout/fragment_game_drawer.xml create mode 100644 src/pandroid/app/src/main/res/menu/game_drawer_actions.xml diff --git a/src/jni_driver.cpp b/src/jni_driver.cpp index cbc5a16e4..0bdaa3459 100644 --- a/src/jni_driver.cpp +++ b/src/jni_driver.cpp @@ -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(); diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/AlberDriver.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/AlberDriver.java index 33438bb04..46ab2577d 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/AlberDriver.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/AlberDriver.java @@ -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(); diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/GameActivity.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/GameActivity.java index 03593b44c..ade3e2ac9 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/GameActivity.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/GameActivity.java @@ -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; @@ -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) { @@ -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 @@ -67,7 +77,9 @@ protected void onResume() { @Override protected void onPause() { super.onPause(); + InputHandler.reset(); + drawerFragment.open(); } @Override diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/game/AlberInputListener.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/game/AlberInputListener.java index 6daca9a61..ec15a9bb6 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/game/AlberInputListener.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/game/AlberInputListener.java @@ -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 { - 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); @@ -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; } diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/game/DrawerFragment.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/game/DrawerFragment.java new file mode 100644 index 000000000..d18fac2b8 --- /dev/null +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/game/DrawerFragment.java @@ -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(); + } +} diff --git a/src/pandroid/app/src/main/res/drawable/ic_exit.xml b/src/pandroid/app/src/main/res/drawable/ic_exit.xml new file mode 100644 index 000000000..a17ca78bb --- /dev/null +++ b/src/pandroid/app/src/main/res/drawable/ic_exit.xml @@ -0,0 +1,5 @@ + + + diff --git a/src/pandroid/app/src/main/res/drawable/ic_shortcut.xml b/src/pandroid/app/src/main/res/drawable/ic_shortcut.xml new file mode 100644 index 000000000..13be1ace4 --- /dev/null +++ b/src/pandroid/app/src/main/res/drawable/ic_shortcut.xml @@ -0,0 +1,5 @@ + + + diff --git a/src/pandroid/app/src/main/res/layout/drawer_game_container.xml b/src/pandroid/app/src/main/res/layout/drawer_game_container.xml new file mode 100644 index 000000000..4b094e75b --- /dev/null +++ b/src/pandroid/app/src/main/res/layout/drawer_game_container.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/pandroid/app/src/main/res/layout/fragment_game_drawer.xml b/src/pandroid/app/src/main/res/layout/fragment_game_drawer.xml new file mode 100644 index 000000000..2da6b9c8e --- /dev/null +++ b/src/pandroid/app/src/main/res/layout/fragment_game_drawer.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/pandroid/app/src/main/res/layout/game_activity.xml b/src/pandroid/app/src/main/res/layout/game_activity.xml index 9536a847f..6cd91f07d 100644 --- a/src/pandroid/app/src/main/res/layout/game_activity.xml +++ b/src/pandroid/app/src/main/res/layout/game_activity.xml @@ -53,4 +53,6 @@ + + \ No newline at end of file diff --git a/src/pandroid/app/src/main/res/menu/game_drawer_actions.xml b/src/pandroid/app/src/main/res/menu/game_drawer_actions.xml new file mode 100644 index 000000000..9fd3264a1 --- /dev/null +++ b/src/pandroid/app/src/main/res/menu/game_drawer_actions.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/src/pandroid/app/src/main/res/values-pt-rBR/strings.xml b/src/pandroid/app/src/main/res/values-pt-rBR/strings.xml index e57feab59..d3966e18e 100644 --- a/src/pandroid/app/src/main/res/values-pt-rBR/strings.xml +++ b/src/pandroid/app/src/main/res/values-pt-rBR/strings.xml @@ -24,4 +24,7 @@ Claro Escuro Preto + Ações + Sair + Continuar \ No newline at end of file diff --git a/src/pandroid/app/src/main/res/values/strings.xml b/src/pandroid/app/src/main/res/values/strings.xml index 973e7acb4..13db91522 100644 --- a/src/pandroid/app/src/main/res/values/strings.xml +++ b/src/pandroid/app/src/main/res/values/strings.xml @@ -25,4 +25,7 @@ Light Dark Black + Actions + Exit + Resume \ No newline at end of file diff --git a/src/pandroid/app/src/main/res/values/styleable.xml b/src/pandroid/app/src/main/res/values/styleable.xml index c4bce805a..cc26e7d95 100644 --- a/src/pandroid/app/src/main/res/values/styleable.xml +++ b/src/pandroid/app/src/main/res/values/styleable.xml @@ -23,6 +23,15 @@ ?colorSecondary + +