forked from wheremyfoodat/Panda3DS
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lua Patcher and code editor: initial commit
- Loading branch information
1 parent
0768342
commit 8603ced
Showing
31 changed files
with
1,405 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/base/BottomDialogFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/editor/CodeEditorActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.