Skip to content

Commit

Permalink
Add ava and fix move window
Browse files Browse the repository at this point in the history
  • Loading branch information
LionZXY committed May 11, 2019
1 parent 0139ccf commit 1b85506
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation 'de.codecentric.centerdevice:javafxsvg:1.3.0'
implementation 'com.github.LionZXY:oslib:master-SNAPSHOT'
implementation "org.rauschig:jarchivelib:1.0.0"
implementation "net.minidev:json-smart:1.1.1"
//implementation 'org.bitbucket.shemnon.javafxplugin:gradle-javafx-plugin:8.1.1'


Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/ru/lionzxy/tplauncher/utils/EmptyMonitoring.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.lionzxy.tplauncher.utils

import sk.tomsik68.mclauncher.api.ui.IProgressMonitor

class EmptyMonitoring : IProgressMonitor {
override fun setMax(len: Int) {

}

override fun setProgress(progress: Int) {
}

override fun setStatus(status: String?) {
}

override fun incrementProgress(amount: Int) {
}
}
89 changes: 89 additions & 0 deletions src/main/kotlin/ru/lionzxy/tplauncher/view/common/Avatar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package ru.lionzxy.tplauncher.view.common

import javafx.event.EventTarget
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.scene.layout.StackPane
import javafx.scene.shape.Circle
import javafx.scene.shape.Rectangle
import net.minidev.json.JSONObject
import net.minidev.json.parser.JSONParser
import ru.lionzxy.tplauncher.utils.*
import sk.tomsik68.mclauncher.util.FileUtils
import sk.tomsik68.mclauncher.util.HttpUtils
import tornadofx.attachTo
import tornadofx.circle
import tornadofx.singleAssign
import java.io.File
import java.io.FileInputStream


fun EventTarget.avatarimage(op: Avatar.() -> Unit = {}): Avatar {
return Avatar().attachTo(this, op)
}

class Avatar : StackPane() {
private var circle: Circle by singleAssign()
private var imageView: ImageView by singleAssign()

init {
circle = circle {
radius = 42.0
fill = Constants.backgroundCircleColor
}
imageView = svgview("check-solid") {
fitWidth = 42.0
fitHeight = 42.0
}
}

fun show() {
isVisible = true
isManaged = true
val nickname = ConfigHelper.config.profile?.login
if (nickname != null) {
runAsync {
val cacheImage = getFromCache()
if (cacheImage != null) {
applyImage(cacheImage)
}
applyImage(download(nickname))
}
}
}

private fun getFromCache(): Image? {
val target = File(ConfigHelper.getDefaultDirectory(), "avatar.png")
if (target.exists()) {
return Image(FileInputStream(target))
}
return null
}

private fun download(nickname: String): Image {
val target = File(ConfigHelper.getDefaultDirectory(), "avatar.png")
val json = HttpUtils.httpGet("https://games.glitchless.ru/api/minecraft/users/profiles/$nickname/avatar/")
val jsonObject = (JSONParser(JSONParser.MODE_PERMISSIVE).parse(json) as JSONObject)["data"] as JSONObject
val avatarUrl = jsonObject.get("avatar_url") as String
FileUtils.downloadFileWithProgress(
avatarUrl,
target,
EmptyMonitoring()
)
return Image(FileInputStream(target))
}

private fun applyImage(image: Image) {
runOnUi {
imageView.image = image
//TODO Сделать нормально
val widthAva = 84.0
imageView.fitWidth = widthAva
imageView.fitHeight = widthAva
imageView.clip = Rectangle(widthAva, widthAva).apply {
arcHeight = widthAva
arcWidth = widthAva
}
}
}
}
21 changes: 10 additions & 11 deletions src/main/kotlin/ru/lionzxy/tplauncher/view/main/MainWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import javafx.scene.control.ProgressBar
import javafx.scene.control.TextField
import javafx.scene.layout.HBox
import javafx.scene.layout.Priority
import ru.lionzxy.tplauncher.utils.*
import ru.lionzxy.tplauncher.utils.Constants.DEFAULT_MARGIN
import ru.lionzxy.tplauncher.utils.recursiveDisable
import ru.lionzxy.tplauncher.utils.recursiveEnable
import ru.lionzxy.tplauncher.utils.runOnUi
import ru.lionzxy.tplauncher.utils.svgview
import ru.lionzxy.tplauncher.view.common.Avatar
import ru.lionzxy.tplauncher.view.common.GlobalStylesheet.Companion.activated
import ru.lionzxy.tplauncher.view.common.GlobalStylesheet.Companion.progressbox
import ru.lionzxy.tplauncher.view.common.GlobalStylesheet.Companion.successLogin
import ru.lionzxy.tplauncher.view.common.GlobalStylesheet.Companion.titleStyle
import ru.lionzxy.tplauncher.view.common.avatarimage
import ru.lionzxy.tplauncher.view.main.listener.CloseListener
import ru.lionzxy.tplauncher.view.main.listener.MoveWindowHandler
import ru.lionzxy.tplauncher.view.main.listener.OpenSettingsListener
Expand All @@ -30,6 +35,7 @@ class MainWindow : View(), IImplementState {

//View for state
private var loginCompleteArea: HBox by singleAssign()
private var avatar: Avatar by singleAssign()
private var loginField: Field by singleAssign()
private var passwordField: Field by singleAssign()
private var titleLabel: Label by singleAssign()
Expand Down Expand Up @@ -81,16 +87,7 @@ class MainWindow : View(), IImplementState {
gridpaneConstraints {
rowSpan = 2
}
stackpane {
circle {
radius = 42.0
fill = Constants.backgroundCircleColor
}
svgview("check-solid") {
fitWidth = 42.0
fitHeight = 42.0
}
}
avatar = avatarimage()
vbox {
hboxConstraints {
marginLeft = DEFAULT_MARGIN
Expand Down Expand Up @@ -228,8 +225,10 @@ class MainWindow : View(), IImplementState {
}

if (state.successLoginVisible) {
avatar.show()
loginCompleteArea.show()
} else {
avatar.hide()
loginCompleteArea.hide()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class MoveWindowHandler(private val view: View) : EventHandler<MouseEvent> {
}

if (event.eventType == MouseEvent.MOUSE_DRAGGED) {
view.modalStage?.x = event.screenX - xOffset;
view.modalStage?.y = event.screenY - yOffset;
val stage = view.modalStage ?: view.primaryStage
stage.x = event.screenX - xOffset;
stage.y = event.screenY - yOffset;
}
}

Expand Down

0 comments on commit 1b85506

Please sign in to comment.