Allow draging files to mod pane

This commit is contained in:
huangyuhui 2017-08-20 09:25:37 +08:00
parent bc53e944af
commit cbf2a4e7a8
3 changed files with 26 additions and 5 deletions

View File

@ -20,6 +20,8 @@ package org.jackhuang.hmcl.ui
import com.jfoenix.effects.JFXDepthManager
import javafx.fxml.FXML
import javafx.scene.control.ScrollPane
import javafx.scene.input.TransferMode
import javafx.scene.layout.StackPane
import javafx.scene.layout.VBox
import javafx.stage.FileChooser
import org.jackhuang.hmcl.i18n
@ -29,12 +31,29 @@ import org.jackhuang.hmcl.task.task
class ModController {
@FXML lateinit var scrollPane: ScrollPane
@FXML lateinit var rootPane: VBox
@FXML lateinit var rootPane: StackPane
@FXML lateinit var contentPane: VBox
private lateinit var modManager: ModManager
private lateinit var versionId: String
fun initialize() {
scrollPane.smoothScrolling()
rootPane.setOnDragOver { event ->
if (event.gestureSource != rootPane && event.dragboard.hasFiles())
event.acceptTransferModes(*TransferMode.COPY_OR_MOVE)
event.consume()
}
rootPane.setOnDragDropped { event ->
val mods = event.dragboard.files
?.filter { it.extension in listOf("jar", "zip", "litemod") }
if (mods != null && mods.isNotEmpty()) {
mods.forEach { modManager.addMod(versionId, it) }
loadMods(modManager, versionId)
event.isDropCompleted = true
}
event.consume()
}
}
fun loadMods(modManager: ModManager, versionId: String) {
@ -43,9 +62,9 @@ class ModController {
task {
modManager.refreshMods(versionId)
}.subscribe(Scheduler.JAVAFX) {
rootPane.children.clear()
contentPane.children.clear()
for (modInfo in modManager.getMods(versionId)) {
rootPane.children += ModItem(modInfo) {
contentPane.children += ModItem(modInfo) {
modManager.removeMods(versionId, modInfo)
loadMods(modManager, versionId)
}.apply {

View File

@ -6,14 +6,15 @@
<?import com.jfoenix.controls.JFXButton?>
<StackPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:id="rootPane"
fx:controller="org.jackhuang.hmcl.ui.ModController">
<ScrollPane fx:id="scrollPane" fitToWidth="true" fitToHeight="true">
<VBox fx:id="rootPane" spacing="10" style="-fx-padding: 20;">
<VBox fx:id="contentPane" spacing="10" style="-fx-padding: 20;">
</VBox>
</ScrollPane>
<VBox style="-fx-padding: 15;" spacing="15" pickOnBounds="false" alignment="BOTTOM_RIGHT">
<JFXButton prefWidth="40" prefHeight="40" buttonType="RAISED" fx:id="btnAdd" onMouseClicked="#onAdd"
<JFXButton prefWidth="40" prefHeight="40" buttonType="RAISED" onMouseClicked="#onAdd"
style="-fx-background-color:#5264AE;-fx-background-radius: 50px;">
<graphic>
<fx:include source="/assets/svg/plus.fxml" />

View File

@ -30,6 +30,7 @@ class ModManager(private val repository: GameRepository) {
private val modCache = SimpleMultimap<String, ModInfo>(::HashMap, ::TreeSet)
fun refreshMods(id: String): Collection<ModInfo> {
modCache.removeAll(id)
val modsDirectory = repository.getRunDirectory(id).resolve("mods")
val puter = { modFile: File -> ignoreException { modCache.put(id, ModInfo.fromFile(modFile)) } }
modsDirectory.listFiles()?.forEach { modFile ->