Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce0203 committed Aug 5, 2023
1 parent eb0a604 commit 11f1658
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 57 deletions.
2 changes: 0 additions & 2 deletions client/src/commonMain/kotlin/start.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ suspend fun start() {
single(named("profile")) { profile }

}))
client()
startWebSocket()
Korge(
windowSize = Size(960, 540),
title = "Skeep",
Expand Down
4 changes: 2 additions & 2 deletions client/src/commonMain/kotlin/ui/custom/TextEditController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TextEditController(
val textView: Text,
val caretContainer: Container = textView,
val eventHandler: View = textView,
val hint: View? = null,
val hint: UIText? = null,
val bg: RenderableView? = null,
) : Closeable, UIFocusable, ISoftKeyboardConfig by SoftKeyboardConfig() {
init {
Expand Down Expand Up @@ -438,6 +438,7 @@ class TextEditController(
if (range.length > 0) {
text = text.withoutRange(range).makeStartWithSpace()
cursorIndex = range.first
if (range.first == 0) cursorIndex += 1
} else {
if (it.key == Key.BACKSPACE) {
if (cursorIndex > 0) {
Expand All @@ -447,7 +448,6 @@ class TextEditController(
text = tempText.makeStartWithSpace()
cursorIndex = oldCursorIndex - 1 // This [oldCursorIndex] is required since changing text might change the cursorIndex already in some circumstances
} else {

}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion client/src/commonMain/kotlin/ui/custom/uiText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UIText(
protected var bpressing by uiObservable(false) { updateState() }

private val background = solidRect(size, Colors.TRANSPARENT)
val textView = text(text)
val textView = text(text, alignment = styles.textAlignment)
var bgcolor: RGBA = Colors.TRANSPARENT

var text: String by textView::text
Expand Down
4 changes: 3 additions & 1 deletion client/src/commonMain/kotlin/ui/custom/uiTextInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ui.custom

import korlibs.event.*
import korlibs.image.font.*
import korlibs.image.text.HorizontalAlign
import korlibs.image.text.TextAlignment
import korlibs.io.async.*
import korlibs.korge.annotations.*
import korlibs.korge.style.*
Expand Down Expand Up @@ -40,7 +42,7 @@ class UITextInput(hint: String, initialText: String = "", size: Size = Size(128,
//private val container = fixedSizeContainer(width - 4.0, height - 4.0).position(2.0, 3.0)
private val textView = customUiText(initialText, this.size)
//private val textView = container.text(initialText, 16.0, color = Colors.BLACK, font = DefaultTtfFont)
val controller = TextEditController(textView.textView, uiContainer(textView.size) { }, this, bg = bg,
val controller = TextEditController(textView.textView, uiContainer(textView.size), this, bg = bg,
hint = textView.uiText(" $hint", size = size) {
centerYOn(textView).alignX(textView, 0.1, true)
alpha = 0.5f
Expand Down
78 changes: 44 additions & 34 deletions client/src/commonMain/kotlin/ui/loginMenu.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ui

import khangul.HangulProcessor
import korlibs.event.Key
import korlibs.image.color.Colors
import korlibs.image.font.Font
import korlibs.image.text.TextAlignment
import korlibs.korge.annotations.KorgeExperimental
import korlibs.korge.input.mouse
import korlibs.korge.input.onMove
import korlibs.korge.input.*
import korlibs.korge.style.styles
import korlibs.korge.style.textColor
import korlibs.korge.style.textFont
Expand All @@ -19,17 +21,26 @@ import korlibs.korge.view.align.centerXOn
import korlibs.korge.view.position
import korlibs.korge.view.solidRect
import korlibs.math.geom.Size
import korlibs.math.log
import network.client
import network.username
import org.koin.core.qualifier.named
import org.koin.mp.KoinPlatform
import scene.styler
import sceneContainer
import ui.custom.UITextInput
import ui.custom.customUiButton
import ui.custom.customUiText
import util.ColorPalette
import util.launchNow
import websocket.startWebSocket

@KorgeExperimental
fun loginMenu(container: Container) {
container.uiContainer() {
suspend fun loginMenu(container: Container) {
lateinit var inputText: UITextInput
lateinit var warningText: UIText
lateinit var loginMenu: Container
loginMenu = container.uiContainer {
styles(styler)
val padding = 15.75f
uiVerticalStack(adjustSize = false, padding = padding) {
Expand All @@ -41,18 +52,10 @@ fun loginMenu(container: Container) {
val blockSize = Size(sceneContainer.width / 3.6f, sceneContainer.width / 23f)
uiContainer(size = blockSize) {
styles.textSize = styles.textSize * 0.95f
materialInput("닉네임", padding, this, border = Colors.TRANSPARENT, bg = ColorPalette.base)
.apply {
input.mouse {
// onMove { materialLayer.borderColor = ColorPalette.hover }
onMoveOutside { materialLayer.borderColor = ColorPalette.base }
}
}
}.centerXOn(this)
uiContainer(size = blockSize) {
styles.textSize = styles.textSize * 0.95f
materialInput("비밀번호", padding, this, border = Colors.TRANSPARENT, bg = ColorPalette.base)
.apply {
materialInput("닉네임", padding, this,
border = Colors.TRANSPARENT, bg = ColorPalette.base
).apply {
inputText = input
input.mouse {
// onMove { materialLayer.borderColor = ColorPalette.hover }
onMoveOutside { materialLayer.borderColor = ColorPalette.base }
Expand All @@ -73,30 +76,37 @@ fun loginMenu(container: Container) {
onMoveOutside { borderColor = ColorPalette.out }
}
}
uiText("게스트 >").centerOn(this)
}
customUiButton(size = horizontalSize) {
uiMaterialLayer(size) {
shadowColor = Colors.TRANSPARENT
bgColor = ColorPalette.out
borderColor = ColorPalette.out
borderSize = padding / 4
this@customUiButton.mouse {
onMove { borderColor = ColorPalette.hover }
onMoveOutside { borderColor = ColorPalette.out }
uiText("입장 >").centerOn(this)
suspend fun join() {
val txt = inputText.text.trim()
if (txt.isEmpty()) {
warningText.text = "닉네임을 입력해주세요"
warningText.styles.textColor = Colors.PALEVIOLETRED
return
} else if (txt.length !in 3..16) {
warningText.text = "닉네임은 3글자 이상 16글자 이하여야 합니다"
warningText.styles.textColor = Colors.PALEVIOLETRED
return
}
warningText.text = "로그인 중..."
warningText.styles.textColor = ColorPalette.out
username = inputText.text.trim()
client()
loginMenu.removeFromParent()
launchNow { MainMenuState().mainMenu() }
}
uiText("로그인 >").centerOn(this)
onClick { join() }
onMouseDragCloseable { launchNow { join() } }
keys { down(Key.ENTER) { join() } }

}
}.centerXOn(this)
uiSpacing(size = Size(0f, space.size.height / 16f))
val guestModeText = uiText("계정 없이 플레이하실 건가요?") {
// uiSpacing(size = Size(0f, space.size.height / 16f))
val guestModeText = uiText("") {
styles.textSize = styles.textSize * 0.75f
mouse {
onMove { styles.textColor = ColorPalette.text }
onMoveOutside { styles.textColor = ColorPalette.out }
}
styles.textColor = ColorPalette.out
}.centerXOn(this)
warningText = guestModeText
val guestModeTextContainer = uiContainer(guestModeText.size.times(3)) {
}.centerXOn(this)
guestModeText.removeFromParent()
Expand Down
5 changes: 2 additions & 3 deletions client/src/commonMain/kotlin/ui/waitingRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,11 @@ data class MaterialInput(
)
fun materialInput(hint: String, padding: Float, container: Container,
border: RGBA = ColorPalette.out,
bg: RGBA = Colors.TRANSPARENT
bg: RGBA = Colors.TRANSPARENT,
): MaterialInput {
val input = container.customUiTextInput(hint, size = container.size) {
text = " "
styles { textAlignment = TextAlignment.MIDDLE_LEFT }
controller.textView.alignment = TextAlignment.MIDDLE_LEFT
container.styles.textAlignment = TextAlignment.MIDDLE_LEFT
controller.caretContainer.alignY(this, 0.75, false)
positionX(padding / 2)
}.zIndex(2)
Expand Down
25 changes: 12 additions & 13 deletions client/src/commonMain/kotlin/websocket/websocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ suspend fun websocketClient(): WebSocketClient {
}
return _websocketClient!!
}
suspend fun newWebsocketClient() = WebSocketClient(currentUrl.httpToWs())
suspend fun newWebsocketClient() =
WebSocketClient(currentUrl.httpToWs())
.also { it.startWebSocket() }

@OptIn(InternalAPI::class)
suspend inline fun <reified T> sendToServer(packet: Enum<*>, t: T) {
Expand All @@ -27,18 +29,15 @@ suspend inline fun <reified T> sendToServer(packet: Enum<*>, t: T) {
}


suspend fun startWebSocket(): Job {
return launchNow {
val websocketClient = websocketClient()
websocketClient.send(Json.encodeToString(sessionUUID))
websocketClient.onStringMessage {
val packetFrame = Json.decodeFromString<PacketFrame>(it)
val serverPacket = ServerPacket.values()[packetFrame.type]
val packetController = serverPacket(serverPacket)
launchNow {
val data = decode(packetFrame.data, packetController.typeInfo)!!
packetController.invoke(data)
}
suspend fun WebSocketClient.startWebSocket() {
send(Json.encodeToString(sessionUUID))
onStringMessage {
val packetFrame = Json.decodeFromString<PacketFrame>(it)
val serverPacket = ServerPacket.values()[packetFrame.type]
val packetController = serverPacket(serverPacket)
launchNow {
val data = decode(packetFrame.data, packetController.typeInfo)!!
packetController.invoke(data)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion shared/src/commonMain/kotlin/network/Login.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.ktor.client.call.*
import kotlinx.uuid.UUID
import kotlinx.uuid.generateUUID

val username: String = generateUsername()
var username: String = generateUsername()
lateinit var sessionId: String
lateinit var sessionUUID: UUID

Expand Down

0 comments on commit 11f1658

Please sign in to comment.