opening TIC-80 sources

This commit is contained in:
BADIM-PC\Vadim
2017-09-26 09:59:34 +03:00
parent b003d8f56f
commit e502b89a1d
325 changed files with 186092 additions and 11 deletions

View File

@@ -0,0 +1,129 @@
package com.nesbox.tic;
import com.samsung.sprc.fileselector.FileSelector;
import com.samsung.sprc.fileselector.FileOperation;
import com.samsung.sprc.fileselector.OnHandleFileListener;
import org.libsdl.app.SDLActivity;
public class TIC extends SDLActivity
{
@Override
protected String[] getLibraries() {
return new String[] {
"SDL2",
"lua",
"z",
"gif",
"main"
};
}
protected final String[] fileSelectorResult = new String[]{""};
public String saveFile(final String name)
{
fileSelectorResult[0] = "";
runOnUiThread(new Runnable()
{
@Override
public void run()
{
new FileSelector(TIC.this, FileOperation.SAVE, mSaveFileListener, name).show();
}
});
synchronized (fileSelectorResult)
{
try
{
fileSelectorResult.wait();
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
return fileSelectorResult[0];
}
OnHandleFileListener mSaveFileListener = new OnHandleFileListener()
{
@Override
public void handleFile(final String filePath)
{
fileSelectorResult[0] = filePath;
synchronized (fileSelectorResult)
{
fileSelectorResult.notify();
}
}
@Override
public void handleCancel()
{
fileSelectorResult[0] = "";
synchronized (fileSelectorResult)
{
fileSelectorResult.notify();
}
}
};
public String loadFile()
{
fileSelectorResult[0] = "";
runOnUiThread(new Runnable()
{
@Override
public void run()
{
new FileSelector(TIC.this, FileOperation.LOAD, mLoadFileListener, "").show();
}
});
synchronized (fileSelectorResult)
{
try
{
fileSelectorResult.wait();
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
return fileSelectorResult[0];
}
OnHandleFileListener mLoadFileListener = new OnHandleFileListener()
{
@Override
public void handleFile(final String filePath)
{
fileSelectorResult[0] = filePath;
synchronized (fileSelectorResult)
{
fileSelectorResult.notify();
}
}
@Override
public void handleCancel()
{
fileSelectorResult[0] = "";
synchronized (fileSelectorResult)
{
fileSelectorResult.notify();
}
}
};
}

View File

@@ -0,0 +1,56 @@
package com.samsung.sprc.fileselector;
/**
* This class contais information about the file name and type
*/
public class FileData implements Comparable<FileData> {
/** Constant that specifies the object is a reference to the parent */
public static final int UP_FOLDER = 0;
/** Constant that specifies the object is a folder */
public static final int DIRECTORY = 1;
/** Constant that specifies the object is a file */
public static final int FILE = 2;
/** The file's name */
final private String mFileName;
/** Defines the type of file. Can be one of UP_FOLDER, DIRECTORY or FILE */
final private int mFileType;
/**
* This class holds information about the file.
*
* @param fileName
* - file name
* @param fileType
* - file type - can be UP_FOLDER, DIRECTORY or FILE
* @throws IllegalArgumentException
* - when illegal type (different than UP_FOLDER, DIRECTORY or
* FILE)
*/
public FileData(final String fileName, final int fileType) {
if (fileType != UP_FOLDER && fileType != DIRECTORY && fileType != FILE) {
throw new IllegalArgumentException("Illegel type of file");
}
this.mFileName = fileName;
this.mFileType = fileType;
}
@Override
public int compareTo(final FileData another) {
if (mFileType != another.mFileType) {
return mFileType - another.mFileType;
}
return mFileName.compareTo(another.mFileName);
}
public String getFileName() {
return mFileName;
}
public int getFileType() {
return mFileType;
}
}

View File

@@ -0,0 +1,67 @@
package com.samsung.sprc.fileselector;
import com.nesbox.tic.R;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/**
* Adapter used to display a files list
*/
public class FileListAdapter extends BaseAdapter {
/** Array of FileData objects that will be used to display a list */
private final ArrayList<FileData> mFileDataArray;
private final Context mContext;
public FileListAdapter(Context context, List<FileData> aFileDataArray) {
mFileDataArray = (ArrayList<FileData>) aFileDataArray;
mContext = context;
}
@Override
public int getCount() {
return mFileDataArray.size();
}
@Override
public Object getItem(int position) {
return mFileDataArray.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FileData tempFileData = mFileDataArray.get(position);
TextViewWithImage tempView = new TextViewWithImage(mContext);
tempView.setText(tempFileData.getFileName());
int imgRes = -1;
switch (tempFileData.getFileType()) {
case FileData.UP_FOLDER: {
imgRes = R.drawable.up_folder;
break;
}
case FileData.DIRECTORY: {
imgRes = R.drawable.folder;
break;
}
case FileData.FILE: {
imgRes = R.drawable.file;
break;
}
}
tempView.setImageResource(imgRes);
return tempView;
}
}

View File

@@ -0,0 +1,6 @@
package com.samsung.sprc.fileselector;
/** Enum used to determine the file operation being performed. */
public enum FileOperation {
SAVE, LOAD
}

View File

@@ -0,0 +1,308 @@
package com.samsung.sprc.fileselector;
import com.nesbox.tic.R;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* Create the file selection dialog. This class will create a custom dialog for
* file selection which can be used to save files.
*/
public class FileSelector {
/** The list of files and folders which you can choose from */
private ListView mFileListView;
/** Button to save/load file */
private Button mSaveLoadButton;
/** Cancel Button - close dialog */
private Button mCancelButton;
/** Button to create a new folder */
private Button mNewFolderButton;
/** Spinner by which to select the file type filtering */
// private Spinner mFilterSpinner;
/**
* Indicates current location in the directory structure displayed in the
* dialog.
*/
private File mCurrentLocation;
/**
* The file selector dialog.
*/
private final Dialog mDialog;
private Context mContext;
/** Save or Load file listener. */
final OnHandleFileListener mOnHandleFileListener;
/**
* Constructor that creates the file selector dialog.
*
* @param context
* The current context.
* @param operation
* LOAD - to load file / SAVE - to save file
* @param onHandleFileListener
* Notified after pressing the save or load button.
*/
public FileSelector(final Context context, final FileOperation operation,
final OnHandleFileListener onHandleFileListener, final String fileName) {
mContext = context;
mOnHandleFileListener = onHandleFileListener;
final File sdCard = Environment.getExternalStorageDirectory();
if (sdCard.canRead()) {
mCurrentLocation = sdCard;
} else {
mCurrentLocation = Environment.getRootDirectory();
}
mDialog = new Dialog(context);
mDialog.setContentView(R.layout.dialog);
mDialog.setTitle(mCurrentLocation.getAbsolutePath());
switch(operation)
{
case SAVE:
((EditText) mDialog.findViewById(R.id.fileName)).setText(fileName);
break;
}
prepareFilesList();
setSaveLoadButton(operation);
setNewFolderButton(operation);
setCancelButton();
}
/**
* This method prepares the mFileListView
*
*/
private void prepareFilesList() {
mFileListView = (ListView) mDialog.findViewById(R.id.fileList);
mFileListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
// Check if "../" item should be added.
if (id == 0) {
final String parentLocation = mCurrentLocation.getParent();
if (parentLocation != null) { // text == "../"
mCurrentLocation = new File(parentLocation);
makeList(mCurrentLocation, "*.*");
} else {
onItemSelect(parent, position);
}
} else {
onItemSelect(parent, position);
}
}
});
makeList(mCurrentLocation, "*.*");
}
/**
* The method that fills the list with a directories contents.
*
* @param location
* Indicates the directory whose contents should be displayed in
* the dialog.
* @param fitlesFilter
* The filter specifies the type of file to be displayed
*/
private void makeList(final File location, final String fitlesFilter) {
final ArrayList<FileData> fileList = new ArrayList<FileData>();
final String parentLocation = location.getParent();
if (parentLocation != null) {
// First item on the list.
fileList.add(new FileData("../", FileData.UP_FOLDER));
}
File listFiles[] = location.listFiles();
if (listFiles != null) {
ArrayList<FileData> fileDataList = new ArrayList<FileData>();
for (int index = 0; index < listFiles.length; index++) {
File tempFile = listFiles[index];
if (FileUtils.accept(tempFile, fitlesFilter)) {
int type = tempFile.isDirectory() ? FileData.DIRECTORY : FileData.FILE;
fileDataList.add(new FileData(listFiles[index].getName(), type));
}
}
fileList.addAll(fileDataList);
Collections.sort(fileList);
}
// Fill the list with the contents of fileList.
if (mFileListView != null) {
FileListAdapter adapter = new FileListAdapter(mContext, fileList);
mFileListView.setAdapter(adapter);
}
}
/**
* Handle the file list item selection.
*
* Change the directory on the list or change the name of the saved file if
* the user selected a file.
*
* @param parent
* First parameter of the onItemClick() method of
* OnItemClickListener. It's a value of text property of the
* item.
* @param position
* Third parameter of the onItemClick() method of
* OnItemClickListener. It's the index on the list of the
* selected item.
*/
private void onItemSelect(final AdapterView<?> parent, final int position) {
final String itemText = ((FileData) parent.getItemAtPosition(position)).getFileName();
final String itemPath = mCurrentLocation.getAbsolutePath() + File.separator + itemText;
final File itemLocation = new File(itemPath);
if (!itemLocation.canRead()) {
Toast.makeText(mContext, "Access denied!!!", Toast.LENGTH_SHORT).show();
} else if (itemLocation.isDirectory()) {
mCurrentLocation = itemLocation;
makeList(mCurrentLocation, "*.*");
} else if (itemLocation.isFile()) {
final EditText fileName = (EditText) mDialog.findViewById(R.id.fileName);
fileName.setText(itemText);
}
}
/**
* Set button name and click handler for Save or Load button.
*
* @param operation
* Performed file operation.
*/
private void setSaveLoadButton(final FileOperation operation) {
mSaveLoadButton = (Button) mDialog.findViewById(R.id.fileSaveLoad);
switch (operation) {
case SAVE:
mSaveLoadButton.setText(R.string.saveButtonText);
break;
case LOAD:
mSaveLoadButton.setText(R.string.loadButtonText);
break;
}
mSaveLoadButton.setOnClickListener(new SaveLoadClickListener(operation, this, mContext));
}
/**
* Set button visibility and click handler for New folder button.
*
* @param operation
* Performed file operation.
*/
private void setNewFolderButton(final FileOperation operation) {
mNewFolderButton = (Button) mDialog.findViewById(R.id.newFolder);
OnClickListener newFolderListener = new OnClickListener() {
@Override
public void onClick(final View v) {
openNewFolderDialog();
}
};
switch (operation) {
case SAVE:
mNewFolderButton.setVisibility(View.VISIBLE);
mNewFolderButton.setOnClickListener(newFolderListener);
break;
case LOAD:
mNewFolderButton.setVisibility(View.GONE);
break;
}
}
/** Opens a dialog for creating a new folder. */
private void openNewFolderDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
alert.setTitle(R.string.newFolderButtonText);
alert.setMessage(R.string.newFolderDialogMessage);
final EditText input = new EditText(mContext);
alert.setView(input);
alert.setPositiveButton(R.string.createButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int whichButton) {
File file = new File(mCurrentLocation.getAbsolutePath() + File.separator + input.getText().toString());
if (file.mkdir()) {
Toast t = Toast.makeText(mContext, R.string.folderCreationOk, Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
} else {
Toast t = Toast.makeText(mContext, R.string.folderCreationError, Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
makeList(mCurrentLocation, "*.*");
}
});
alert.show();
}
/** Set onClick() event handler for the cancel button. */
private void setCancelButton()
{
mDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
mOnHandleFileListener.handleCancel();
}
});
mCancelButton = (Button) mDialog.findViewById(R.id.fileCancel);
mCancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View view) {
mDialog.cancel();
}
});
}
public String getSelectedFileName() {
final EditText fileName = (EditText) mDialog.findViewById(R.id.fileName);
return fileName.getText().toString();
}
public File getCurrentLocation() {
return mCurrentLocation;
}
/** Simple wrapper around the Dialog.show() method. */
public void show() {
mDialog.show();
}
/** Simple wrapper around the Dialog.dissmiss() method. */
public void dismiss() {
mDialog.dismiss();
}
}

View File

@@ -0,0 +1,36 @@
package com.samsung.sprc.fileselector;
import java.io.File;
/**
* A set of tools for file operations
*/
public class FileUtils {
/** Filter which accepts every file */
public static final String FILTER_ALLOW_ALL = "*.*";
/**
* This method checks that the file is accepted by the filter
*
* @param file
* - file that will be checked if there is a specific type
* @param filter
* - criterion - the file type(for example ".jpg")
* @return true - if file meets the criterion - false otherwise.
*/
public static boolean accept(final File file, final String filter) {
if (filter.compareTo(FILTER_ALLOW_ALL) == 0) {
return true;
}
if (file.isDirectory()) {
return true;
}
int lastIndexOfPoint = file.getName().lastIndexOf('.');
if (lastIndexOfPoint == -1) {
return false;
}
String fileType = file.getName().substring(lastIndexOfPoint).toLowerCase();
return fileType.compareTo(filter) == 0;
}
}

View File

@@ -0,0 +1,15 @@
package com.samsung.sprc.fileselector;
public interface OnHandleFileListener {
/**
* This method is called after clicking the Save or Load button on the
* dialog, if the file name was correct. It should be used to save or load a
* file using the filePath path.
*
* @param filePath
* File path set in the dialog when the Save or Load button was
* clicked.
*/
void handleFile(String filePath);
void handleCancel();
}

View File

@@ -0,0 +1,92 @@
package com.samsung.sprc.fileselector;
import com.nesbox.tic.R;
import java.io.File;
import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
/**
* This Listener handles Save or Load button clicks.
*/
public class SaveLoadClickListener implements OnClickListener {
/** Performed operation. */
private final FileOperation mOperation;
/** FileSelector in which you used SaveLoadClickListener */
private final FileSelector mFileSelector;
private final Context mContext;
/**
* @param operation
* Performed operation.
* @param fileSelector
* The FileSeletor which used this Listener.
* @param context
* context.
*/
public SaveLoadClickListener(final FileOperation operation, final FileSelector fileSelector, final Context context) {
mOperation = operation;
mFileSelector = fileSelector;
mContext = context;
}
@Override
public void onClick(final View view) {
final String text = mFileSelector.getSelectedFileName();
if (checkFileName(text)) {
final String filePath = mFileSelector.getCurrentLocation().getAbsolutePath() + File.separator + text;
final File file = new File(filePath);
int messageText = 0;
// Check file access rights.
switch (mOperation) {
case SAVE:
if ((file.exists()) && (!file.canWrite())) {
messageText = R.string.cannotSaveFileMessage;
}
break;
case LOAD:
if (!file.exists()) {
messageText = R.string.missingFile;
} else if (!file.canRead()) {
messageText = R.string.accessDenied;
}
break;
}
if (messageText != 0) {
// Access denied.
final Toast t = Toast.makeText(mContext, messageText, Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
} else {
// Access granted.
mFileSelector.mOnHandleFileListener.handleFile(filePath);
mFileSelector.dismiss();
}
}
}
/**
* Check if file name is correct, e.g. if it isn't empty.
*
* @return False, if file name is empty true otherwise.
*/
boolean checkFileName(String text) {
if (text.length() == 0) {
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(R.string.information);
builder.setMessage(R.string.fileNameFirstMessage);
builder.setNeutralButton(R.string.okButtonText, null);
builder.show();
return false;
}
return true;
}
}

View File

@@ -0,0 +1,58 @@
package com.samsung.sprc.fileselector;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Class which combines ImageView and TextView in LineralLayout with horizontal
* orientation
*/
public class TextViewWithImage extends LinearLayout {
/**
* Image - in this project will be used to display icon representing the
* file type
*/
private ImageView mImage;
/** Text - in this project will be used to display the file name */
private TextView mText;
public TextViewWithImage(Context context) {
super(context);
setOrientation(HORIZONTAL);
mImage = new ImageView(context);
mText = new TextView(context);
LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
lp.weight = 1;
addView(mImage, lp);
lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 3);
addView(mText, lp);
}
/** Simple wrapper around the TextView.getText() method. */
public CharSequence getText() {
return mText.getText();
}
/**
* Simple wrapper around ImageView.setImageResource() method. but if resId
* is equal -1 this method sets Images visibility as GONE
*/
public void setImageResource(int resId) {
if (resId == -1) {
mImage.setVisibility(View.GONE);
return;
}
mImage.setImageResource(resId);
}
/** Simple wrapper around TextView.setText() method. */
public void setText(String aText) {
mText.setText(aText);
}
}

File diff suppressed because it is too large Load Diff