Skip to content

Commit

Permalink
Give shaders a name
Browse files Browse the repository at this point in the history
Long pressing a shader in the list will open a dialog
to edit a shader's name.
  • Loading branch information
markusfisch committed Jun 25, 2017
1 parent 136b61e commit 4795e9f
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ protected void setFragmentForIntent(Fragment fragment, Intent intent) {
setFragment(getSupportFragmentManager(), fragment);
}

// Lint is missing the commit() but this method should return an
// uncommitted transaction so it can be extended
@SuppressLint("CommitTransaction")
private static FragmentTransaction getReplaceFragmentTransaction(
FragmentManager fm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import de.markusfisch.android.shadereditor.database.DataSource;
import de.markusfisch.android.shadereditor.fragment.EditorFragment;
import de.markusfisch.android.shadereditor.opengl.ShaderRenderer;
import de.markusfisch.android.shadereditor.view.SoftKeyboard;
import de.markusfisch.android.shadereditor.view.SystemBarMetrics;
import de.markusfisch.android.shadereditor.widget.TouchThruDrawerLayout;
import de.markusfisch.android.shadereditor.widget.ShaderEditor;
import de.markusfisch.android.shadereditor.widget.ShaderView;
import de.markusfisch.android.shadereditor.R;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
Expand All @@ -31,6 +33,7 @@
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
Expand Down Expand Up @@ -465,6 +468,20 @@ public void onItemClick(
closeDrawer();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(
AdapterView<?> parent,
View view,
int position,
long id) {
if (shaderAdapter != null) {
editShaderName(id, shaderAdapter.getName(position));
return true;
}
return false;
}
});
}

private void initShaderView() {
Expand Down Expand Up @@ -801,6 +818,42 @@ private void showSettings() {
PreferencesActivity.class));
}

@SuppressLint("InflateParams")
private void editShaderName(final long id, String name) {
View view = getLayoutInflater().inflate(
R.layout.dialog_rename_shader,
// a dialog does not have a parent view group
// so InflateParams must be suppressed
null);
final EditText nameView = (EditText) view.findViewById(R.id.name);
if (name != null) {
nameView.setText(name);
}

new AlertDialog.Builder(this)
.setMessage(R.string.rename_shader)
.setView(view)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
ShaderEditorApplication
.dataSource
.updateShaderName(
id,
nameView.getText().toString());
getShadersAsync();
SoftKeyboard.hide(
MainActivity.this,
nameView);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
}

private void selectShaderAndUpdate(long id) {
selectShader(id);
getShadersAsync();
Expand Down Expand Up @@ -873,14 +926,9 @@ private void setToolbarTitle(long id) {
}

private void setToolbarTitle(Cursor cursor) {
if (cursor == null) {
return;
if (cursor != null && shaderAdapter != null) {
setToolbarTitle(shaderAdapter.getTitle(cursor));
}

String modified = cursor.getString(
cursor.getColumnIndex(DataSource.SHADERS_MODIFIED));

setToolbarTitle(modified);
}

private void setToolbarTitle(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class ShaderAdapter extends CursorAdapter {
private int idIndex;
private int thumbIndex;
private int nameIndex;
private int modifiedIndex;
private int textColorSelected;
private int textColorUnselected;
Expand All @@ -33,6 +34,18 @@ public void setSelectedId(long id) {
selectedShaderId = id;
}

public String getName(int position) {
Cursor cursor = (Cursor) getItem(position);
return cursor != null ? cursor.getString(nameIndex) : null;
}

public String getTitle(Cursor cursor) {
String title = cursor.getString(nameIndex);
return title != null && title.length() > 0 ?
title :
cursor.getString(modifiedIndex);
}

@Override
public View newView(
Context context,
Expand Down Expand Up @@ -82,14 +95,16 @@ protected void setData(ViewHolder holder, Cursor cursor) {
bytes.length));
}

holder.title.setText(cursor.getString(modifiedIndex));
holder.title.setText(getTitle(cursor));
}

private void indexColumns(Cursor cursor) {
idIndex = cursor.getColumnIndex(
DataSource.SHADERS_ID);
thumbIndex = cursor.getColumnIndex(
DataSource.SHADERS_THUMB);
nameIndex = cursor.getColumnIndex(
DataSource.SHADERS_NAME);
modifiedIndex = cursor.getColumnIndex(
DataSource.SHADERS_MODIFIED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DataSource {
public static final String SHADERS_ID = "_id";
public static final String SHADERS_FRAGMENT_SHADER = "shader";
public static final String SHADERS_THUMB = "thumb";
public static final String SHADERS_NAME = "name";
public static final String SHADERS_CREATED = "created";
public static final String SHADERS_MODIFIED = "modified";
public static final String SHADERS_QUALITY = "quality";
Expand Down Expand Up @@ -92,6 +93,7 @@ public Cursor getShaders() {
"SELECT " +
SHADERS_ID + "," +
SHADERS_THUMB + "," +
SHADERS_NAME + "," +
SHADERS_MODIFIED +
" FROM " + SHADERS +
" ORDER BY " + SHADERS_ID,
Expand Down Expand Up @@ -131,6 +133,7 @@ public Cursor getShader(long id) {
"SELECT " +
SHADERS_ID + "," +
SHADERS_FRAGMENT_SHADER + "," +
SHADERS_NAME + "," +
SHADERS_MODIFIED + "," +
SHADERS_QUALITY +
" FROM " + SHADERS +
Expand Down Expand Up @@ -224,13 +227,15 @@ public Bitmap getTextureBitmap(Cursor cursor) {
public static long insertShader(
SQLiteDatabase db,
String shader,
String name,
byte thumbnail[],
float quality) {
String now = currentTime();

ContentValues cv = new ContentValues();
cv.put(SHADERS_FRAGMENT_SHADER, shader);
cv.put(SHADERS_THUMB, thumbnail);
cv.put(SHADERS_NAME, name);
cv.put(SHADERS_CREATED, now);
cv.put(SHADERS_MODIFIED, now);
cv.put(SHADERS_QUALITY, quality);
Expand All @@ -242,7 +247,7 @@ public long insertShader(
String shader,
byte[] thumbnail,
float quality) {
return insertShader(db, shader, thumbnail, quality);
return insertShader(db, shader, null, thumbnail, quality);
}

public long insertNewShader(Context context) {
Expand All @@ -252,6 +257,7 @@ public long insertNewShader(Context context) {
loadRawResource(
context,
R.raw.new_shader),
null,
loadBitmapResource(
context,
R.drawable.thumbnail_new_shader),
Expand Down Expand Up @@ -334,6 +340,16 @@ public void updateShaderQuality(
null);
}

public void updateShaderName(long id, String name) {
ContentValues cv = new ContentValues();
cv.put(SHADERS_NAME, name);
db.update(
SHADERS,
cv,
SHADERS_ID + "=" + id,
null);
}

public void removeShader(long id) {
db.delete(
SHADERS,
Expand Down Expand Up @@ -393,44 +409,44 @@ private static float calculateRatio(int width, int height) {

private void createShadersTable(SQLiteDatabase db, Context context) {
db.execSQL("DROP TABLE IF EXISTS " + SHADERS);
db.execSQL(
"CREATE TABLE " + SHADERS + " (" +
SHADERS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
SHADERS_FRAGMENT_SHADER + " TEXT NOT NULL," +
SHADERS_THUMB + " BLOB," +
SHADERS_CREATED + " DATETIME," +
SHADERS_MODIFIED + " DATETIME," +
SHADERS_QUALITY + " REAL );");
db.execSQL("CREATE TABLE " + SHADERS + " (" +
SHADERS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
SHADERS_FRAGMENT_SHADER + " TEXT NOT NULL," +
SHADERS_THUMB + " BLOB," +
SHADERS_NAME + " TEXT," +
SHADERS_CREATED + " DATETIME," +
SHADERS_MODIFIED + " DATETIME," +
SHADERS_QUALITY + " REAL );");

insertInitalShaders(db, context);
}

private static void addShadersQuality(SQLiteDatabase db) {
db.execSQL(
"ALTER TABLE " + SHADERS +
" ADD COLUMN " + SHADERS_QUALITY + " REAL;");
db.execSQL(
"UPDATE " + SHADERS +
" SET " + SHADERS_QUALITY + " = 1;");
db.execSQL("ALTER TABLE " + SHADERS +
" ADD COLUMN " + SHADERS_QUALITY + " REAL;");
db.execSQL("UPDATE " + SHADERS +
" SET " + SHADERS_QUALITY + " = 1;");
}

private void insertInitalShaders(SQLiteDatabase db, Context context) {
try {
DataSource.insertShader(
insertShader(
db,
loadRawResource(
context,
R.raw.color_hole),
context.getString(R.string.demo_color_hole),
loadBitmapResource(
context,
R.drawable.thumbnail_color_hole),
1f);

DataSource.insertShader(
insertShader(
db,
loadRawResource(
context,
R.raw.gravity),
context.getString(R.string.demo_gravity),
loadBitmapResource(
context,
R.drawable.thumbnail_gravity),
Expand All @@ -443,29 +459,25 @@ private void insertInitalShaders(SQLiteDatabase db, Context context) {

private void createTexturesTable(SQLiteDatabase db, Context context) {
db.execSQL("DROP TABLE IF EXISTS " + TEXTURES);
db.execSQL(
"CREATE TABLE " + TEXTURES + " (" +
TEXTURES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
TEXTURES_NAME + " TEXT NOT NULL UNIQUE," +
TEXTURES_WIDTH + " INTEGER," +
TEXTURES_HEIGHT + " INTEGER," +
TEXTURES_RATIO + " REAL," +
TEXTURES_THUMB + " BLOB," +
TEXTURES_MATRIX + " BLOB );");
db.execSQL("CREATE TABLE " + TEXTURES + " (" +
TEXTURES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
TEXTURES_NAME + " TEXT NOT NULL UNIQUE," +
TEXTURES_WIDTH + " INTEGER," +
TEXTURES_HEIGHT + " INTEGER," +
TEXTURES_RATIO + " REAL," +
TEXTURES_THUMB + " BLOB," +
TEXTURES_MATRIX + " BLOB );");

insertInitalTextures(db, context);
}

private static void addTexturesWidthHeightRatio(SQLiteDatabase db) {
db.execSQL(
"ALTER TABLE " + TEXTURES +
" ADD COLUMN " + TEXTURES_WIDTH + " INTEGER;");
db.execSQL(
"ALTER TABLE " + TEXTURES +
" ADD COLUMN " + TEXTURES_HEIGHT + " INTEGER;");
db.execSQL(
"ALTER TABLE " + TEXTURES +
" ADD COLUMN " + TEXTURES_RATIO + " REAL;");
db.execSQL("ALTER TABLE " + TEXTURES +
" ADD COLUMN " + TEXTURES_WIDTH + " INTEGER;");
db.execSQL("ALTER TABLE " + TEXTURES +
" ADD COLUMN " + TEXTURES_HEIGHT + " INTEGER;");
db.execSQL("ALTER TABLE " + TEXTURES +
" ADD COLUMN " + TEXTURES_RATIO + " REAL;");

Cursor cursor = db.rawQuery(
"SELECT " +
Expand All @@ -490,20 +502,24 @@ private static void addTexturesWidthHeightRatio(SQLiteDatabase db) {
float ratio = calculateRatio(width, height);
bm.recycle();

db.execSQL(
"UPDATE " + TEXTURES +
" SET " +
TEXTURES_WIDTH + " = " + width + ", " +
TEXTURES_HEIGHT + " = " + height + ", " +
TEXTURES_RATIO + " = " + ratio +
" WHERE " + TEXTURES_ID + " = " + cursor.getLong(
cursor.getColumnIndex(TEXTURES_ID)) + ";");
db.execSQL("UPDATE " + TEXTURES +
" SET " +
TEXTURES_WIDTH + " = " + width + ", " +
TEXTURES_HEIGHT + " = " + height + ", " +
TEXTURES_RATIO + " = " + ratio +
" WHERE " + TEXTURES_ID + " = " + cursor.getLong(
cursor.getColumnIndex(TEXTURES_ID)) + ";");

} while (cursor.moveToNext());

cursor.close();
}

private static void addShaderNames(SQLiteDatabase db) {
db.execSQL("ALTER TABLE " + SHADERS +
" ADD COLUMN " + SHADERS_NAME + " TEXT;");
}

private void insertInitalTextures(SQLiteDatabase db, Context context) {
DataSource.insertTexture(
db,
Expand Down Expand Up @@ -549,10 +565,14 @@ public void onUpgrade(
if (oldVersion < 4) {
addTexturesWidthHeightRatio(db);
}

if (oldVersion < 5) {
addShaderNames(db);
}
}

private OpenHelper(Context context) {
super(context, "shaders.db", null, 4);
super(context, "shaders.db", null, 5);
this.context = context;
}
}
Expand Down

0 comments on commit 4795e9f

Please sign in to comment.