Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
Merge branch 'mods'
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzz committed Apr 27, 2019
2 parents 026e819 + ef2fa23 commit f7b7a9f
Show file tree
Hide file tree
Showing 18 changed files with 631 additions and 12 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ repositories {
dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.anko:anko-sqlite:$anko_version"

implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
implementation 'com.crashlytics.sdk.android:crashlytics-ndk:2.0.5'
}
Expand Down
6 changes: 0 additions & 6 deletions app/openmw-base.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
no-sound=0
fallback-archive=Morrowind.bsa
fallback-archive=Tribunal.bsa
fallback-archive=Bloodmoon.bsa
fallback=LightAttenuation_UseConstant,1
fallback=LightAttenuation_ConstantValue,0.0
fallback=LightAttenuation_UseLinear,1
Expand Down Expand Up @@ -567,6 +564,3 @@ fallback=Moons_Script_Color,255,20,20
fallback=General_Werewolf_FOV,100
encoding=win1252
data="specify-me!"
content=Morrowind.esm
content=Bloodmoon.esm
content=Tribunal.esm
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="fullUser"
android:theme="@style/MyTheme"
android:exported="true"
android:label="@string/activity_mods"
/>
</application>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/file/ConfigsFileStorageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public class ConfigsFileStorageHelper {
public static final String CONFIGS_FILES_STORAGE_PATH = Environment.getExternalStorageDirectory() + "/Android/data/" + BuildConfig.APPLICATION_ID;
public static final String SETTINGS_CFG = CONFIGS_FILES_STORAGE_PATH + "/config/openmw/settings.cfg";
public static final String OPENMW_CFG = CONFIGS_FILES_STORAGE_PATH + "/config/openmw/openmw.cfg";
public static final String OPENMW_BASE_CFG = CONFIGS_FILES_STORAGE_PATH + "/config/openmw/openmw-base.cfg";

}
62 changes: 62 additions & 0 deletions app/src/main/java/mods/Mod.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package mods

import android.database.sqlite.SQLiteDatabase
import org.jetbrains.anko.db.*

enum class ModType(val v: Int) {
Plugin(1),
Resource(2);

companion object {
private val reverseValues: Map<Int, ModType> = values().associate { it.v to it }
fun valueFrom(i: Int): ModType = reverseValues.getValue(i)
}
}

/**
* Representation of a single mod in the database
* @param type Type of the mod: plugin or resource
* @param filename Filename of the mod, without the path
* @param order Load order, or order in the list
* @param enabled Whether the mod is enabled
*/
class Mod(val type: ModType, val filename: String, var order: Int, var enabled: Boolean) {

/// Set to true when DB update is needed to keep consistency
var dirty: Boolean = false

/**
* Updates the representation of this mod in the database
* @param db Database connection
*/
fun update(db: SQLiteDatabase) {
db.update("mod",
"load_order" to order,
"enabled" to enabled)
.whereArgs("filename = {filename} AND type = {type}",
"filename" to filename,
"type" to type.v).exec()
}

/**
* Inserts this mod into the database
* @param db Database connection
*/
fun insert(db: SQLiteDatabase) {
db.insert("mod",
"type" to type.v,
"filename" to filename,
"load_order" to order,
"enabled" to (if (enabled) 1 else 0))
}
}

class ModRowParser : RowParser<Mod> {
override fun parseRow(columns: Array<Any?>): Mod {
return Mod(
ModType.valueFrom((columns[0] as Long).toInt()),
columns[1] as String,
(columns[2] as Long).toInt(),
(columns[3] as Long) != 0L)
}
}
65 changes: 65 additions & 0 deletions app/src/main/java/mods/ModMoveCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package mods

import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper

/**
* Callback for dragging the mods around to change load order
*/
class ModMoveCallback(private val mAdapter: ModsAdapter) : ItemTouchHelper.Callback() {

override fun isLongPressDragEnabled(): Boolean {
return true
}

override fun isItemViewSwipeEnabled(): Boolean {
return false
}


override fun onSwiped(viewHolder: RecyclerView.ViewHolder, i: Int) {

}

override fun getMovementFlags(recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder): Int {
val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
return ItemTouchHelper.Callback.makeMovementFlags(dragFlags, 0)
}

override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder): Boolean {
mAdapter.onRowMoved(viewHolder.adapterPosition, target.adapterPosition)
return true
}

override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?,
actionState: Int) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder is ModsAdapter.ModViewHolder) {
mAdapter.onRowSelected(viewHolder)
}
}

super.onSelectedChanged(viewHolder, actionState)
}

override fun clearView(recyclerView: RecyclerView?,
viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder)

if (viewHolder is ModsAdapter.ModViewHolder) {
mAdapter.onRowClear(viewHolder)
}
}

/**
* Speed up dragging of a list element
*/
override fun interpolateOutOfBoundsScroll(recyclerView: RecyclerView, viewSize: Int,
viewSizeOutOfBounds: Int, totalSize: Int,
msSinceStartScroll: Long): Int {
val direction = Math.signum(viewSizeOutOfBounds.toFloat()).toInt()
return 20 * direction
}
}
95 changes: 95 additions & 0 deletions app/src/main/java/mods/ModsAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package mods

import android.annotation.SuppressLint
import android.graphics.Color
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import com.libopenmw.openmw.R
import java.util.*

/**
* An adapter to put a ModsCollection into a UI list
*/
class ModsAdapter(private val collection: ModsCollection) : RecyclerView.Adapter<ModsAdapter.ModViewHolder>() {

lateinit var touchHelper: ItemTouchHelper

/**
* A row representation of a mod
*/
inner class ModViewHolder(internal var rowView: View) : RecyclerView.ViewHolder(rowView) {
val mTitle: TextView = rowView.findViewById(R.id.txtTitle)
val mHandle: ImageView = rowView.findViewById(R.id.handle)
val mCheckbox: CheckBox = rowView.findViewById(R.id.modCheckbox)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ModViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.mod_item, parent, false)
return ModViewHolder(itemView)
}

@SuppressLint("ClickableViewAccessibility")
override fun onBindViewHolder(holder: ModViewHolder, position: Int) {
val mod = collection.mods[position]
holder.mTitle.text = mod.filename
holder.mHandle.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
touchHelper.startDrag(holder)
}
false
}
holder.mCheckbox.isChecked = mod.enabled
holder.mCheckbox.setOnClickListener {
mod.enabled = (it as CheckBox).isChecked
mod.dirty = true
collection.update()
}
}

override fun getItemCount(): Int {
return collection.mods.size
}

private fun swapMods(from: Int, to: Int) {
// Swap the orders
val tmp = collection.mods[from].order
collection.mods[from].order = collection.mods[to].order
collection.mods[to].order = tmp

// Swap the mods inside the list
Collections.swap(collection.mods, from, to)

// Mark mods as dirty to update when the user releases currently dragged mod
collection.mods[from].dirty = true
collection.mods[to].dirty = true
}

fun onRowMoved(fromPosition: Int, toPosition: Int) {
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
swapMods(i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
swapMods(i, i - 1)
}
}
notifyItemMoved(fromPosition, toPosition)
}

fun onRowSelected(modViewHolder: ModViewHolder) {
modViewHolder.rowView.setBackgroundColor(Color.LTGRAY)
}

fun onRowClear(modViewHolder: ModViewHolder) {
modViewHolder.rowView.setBackgroundColor(Color.WHITE)
collection.update()
}
}
Loading

0 comments on commit f7b7a9f

Please sign in to comment.