Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua Patcher and code editor: initial commit #2

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/jni_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ AlberFunction(void, LoadRom)(JNIEnv* env, jobject obj, jstring path) {
env->ReleaseStringUTFChars(path, pathStr);
}

AlberFunction(void, LoadLuaScript)(JNIEnv* env, jobject obj, jstring script) {
const char* scriptStr = env->GetStringUTFChars(script, nullptr);
emulator->getLua().loadString(scriptStr);
env->ReleaseStringUTFChars(script, scriptStr);
}

AlberFunction(void, TouchScreenDown)(JNIEnv* env, jobject obj, jint x, jint y) { hidService->setTouchScreenPress((u16)x, (u16)y); }
AlberFunction(void, TouchScreenUp)(JNIEnv* env, jobject obj) { hidService->releaseTouchScreen(); }
AlberFunction(void, KeyUp)(JNIEnv* env, jobject obj, jint keyCode) { hidService->releaseKey((u32)keyCode); }
Expand Down
4 changes: 4 additions & 0 deletions src/pandroid/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
android:name=".app.GameActivity"
android:configChanges="screenSize|screenLayout|orientation|density|uiMode">
</activity>
<activity
android:name=".app.editor.CodeEditorActivity"
android:configChanges="screenSize|screenLayout|orientation|density|uiMode">
</activity>
<activity android:name=".app.PreferenceActivity"
android:launchMode="standard"
android:configChanges="screenSize|screenLayout|orientation|density"/>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AlberDriver {
public static native void TouchScreenDown(int x, int y);
public static native void Pause();
public static native void Resume();

public static native void LoadLuaScript(String script);
public static native byte[] GetSmdh();

static { System.loadLibrary("Alber"); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import androidx.fragment.app.FragmentManager;
import com.google.android.material.navigation.NavigationBarView;
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.app.editor.CodeEditorActivity;
import com.panda3ds.pandroid.app.main.GamesFragment;
import com.panda3ds.pandroid.app.main.SearchFragment;
import com.panda3ds.pandroid.app.main.SettingsFragment;

import java.io.File;


public class MainActivity extends BaseActivity implements NavigationBarView.OnItemSelectedListener {
private static final int PICK_ROM = 2;
Expand All @@ -28,13 +31,6 @@ public class MainActivity extends BaseActivity implements NavigationBarView.OnIt
private final SearchFragment searchFragment = new SearchFragment();
private final SettingsFragment settingsFragment = new SettingsFragment();

private void openFile() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, PICK_ROM);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.panda3ds.pandroid.app.base;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.panda3ds.pandroid.R;

public class BottomDialogFragment extends DialogFragment {
@Override
public int getTheme() {
return R.style.AlertDialog;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().setGravity(Gravity.CENTER | Gravity.BOTTOM);
dialog.getWindow().getAttributes().y = Math.round(getContext().getResources().getDisplayMetrics().density * 15);
return dialog;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package com.panda3ds.pandroid.app.editor;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.app.BaseActivity;
import com.panda3ds.pandroid.app.base.BottomAlertDialog;
import com.panda3ds.pandroid.lang.Task;
import com.panda3ds.pandroid.utils.FileUtils;
import com.panda3ds.pandroid.view.code.CodeEditor;
import com.panda3ds.pandroid.view.code.syntax.CodeSyntax;

import java.io.Serializable;

public class CodeEditorActivity extends BaseActivity {
private String path;
private String fileName;
private CodeEditor editor;
private AppCompatTextView title;
private View saveButton;
private boolean changed = false;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_code_editor);
Arguments args = (Arguments) getIntent().getSerializableExtra("args");

editor = findViewById(R.id.editor);

path = args.path;
fileName = args.fileName;
title = findViewById(R.id.title);
title.setText(fileName);

saveButton = findViewById(R.id.save);

saveButton.setVisibility(View.GONE);
saveButton.setOnClickListener(v -> save());

new Task(() -> {
String content = FileUtils.readTextFile(path + "/" + fileName);
editor.post(() -> {
editor.setText(content);
editor.setSyntax(CodeSyntax.obtainByFileName(fileName));
editor.setOnContentChangedListener(this::onDocumentContentChanged);
});
}).start();

switch (args.type) {
case LUA_SCRIPT_EDITOR:
setupLuaPatchEditor();
break;
case READ_ONLY_EDITOR:
setupReadOnlyEditor();
break;
}
}

private void setupReadOnlyEditor() {
editor.setEnabled(false);
editor.setFocusable(false);
}

private void setupLuaPatchEditor() {
findViewById(R.id.lua_toolbar).setVisibility(View.VISIBLE);
findViewById(R.id.lua_play).setOnClickListener(v -> {
if (changed) {
save();
}
setResult(Activity.RESULT_OK, new Intent(Result.ACTION_PLAY.name()));
finish();
});
}

@SuppressLint("SetTextI18n")
private void onDocumentContentChanged() {
title.setText("*" + fileName);
changed = true;
saveButton.setVisibility(View.VISIBLE);
}

public void save() {
title.setText(fileName);
saveButton.setVisibility(View.GONE);
changed = false;
new Task(() -> FileUtils.writeTextFile(path, fileName, String.valueOf(editor.getText()))).runSync();
}

@Override
public void onBackPressed() {
if (changed) {
new BottomAlertDialog(this)
.setNeutralButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss())
.setPositiveButton(R.string.save_and_exit, (dialog, which) -> {
save();
finish();
})
.setNegativeButton(R.string.exit_without_save, (dialog, which) -> finish())
.setTitle(String.format(getString(R.string.exit_without_save_title_ff), fileName)).show();
} else {
super.onBackPressed();
}
}

public static final class Arguments implements Serializable {
private final String path;
private final String fileName;
private final EditorType type;

public Arguments(String path, String fileName, EditorType type) {
this.path = path;
this.fileName = fileName;
this.type = type;
}
}

public enum Result {
ACTION_PLAY,
NULL
}

public enum EditorType {
LUA_SCRIPT_EDITOR,
READ_ONLY_EDITOR,
TEXT_EDITOR
}

public static final class Contract extends ActivityResultContract<Arguments, Result> {
@NonNull
@Override
public Intent createIntent(@NonNull Context context, Arguments args) {
return new Intent(context, CodeEditorActivity.class).putExtra("args", args);
}

@Override
public Result parseResult(int i, @Nullable Intent intent) {
return i == RESULT_OK && intent != null ? Result.valueOf(intent.getAction()) : Result.NULL;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
((AppCompatTextView)view.findViewById(R.id.game_publisher)).setText(game.getPublisher());

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

@Override
Expand Down Expand Up @@ -100,6 +101,8 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
close();
} else if (id == R.id.exit) {
requireActivity().onBackPressed();
} else if (id == R.id.lua_script){
new LuaDialogFragment().show(getParentFragmentManager(),null);
}

return false;
Expand Down
Loading
Loading