-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/ru/lionzxy/tplauncher/utils/EmptyMonitoring.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
89
src/main/kotlin/ru/lionzxy/tplauncher/view/common/Avatar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters