Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce0203 committed Aug 3, 2023
1 parent afdc5aa commit e8b1da4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
29 changes: 21 additions & 8 deletions client/src/commonMain/kotlin/ui/custom/TextEditController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class TextEditController(

private fun setTextNoSnapshot(text: String, out: TextSnapshot = TextSnapshot("", 0..0)): TextSnapshot? {
if (!acceptTextChange(textView.text, text)) return null
println("'$text'")
out.text = textView.text
out.selectionRange = selectionRange
textView.text = text
Expand Down Expand Up @@ -121,14 +122,19 @@ class TextEditController(
val lastStr = text[max(0, index - 1)]
val insertion = khangul.HangulProcessor.composeHangul(lastStr + substr)
if (insertion !== null) {
text = rangedText
val tempText = rangedText
.withoutIndex(max(0, index - 1))
.withInsertion(index, insertion)
cursorIndex += insertion.length - 1
text = tempText.makeStartWithSpace()
if (tempText.startsWith(" ")) {
cursorIndex += insertion.length - 1
} else {
cursorIndex += insertion.length
}
return
}
}
text = rangedText.withInsertion(index, substr)
text = rangedText.withInsertion(index, substr).makeStartWithSpace()
cursorIndex += substr.length
}

Expand Down Expand Up @@ -406,7 +412,7 @@ class TextEditController(
}
if (it.key == Key.X) {
val selection = selectionRange
text = text.withoutRange(selectionRange)
text = text.withoutRange(selectionRange).makeStartWithSpace()
moveToIndex(false, selection.first)
}
}
Expand All @@ -424,18 +430,23 @@ class TextEditController(
Key.BACKSPACE, Key.DELETE -> {
val range = selectionRange
if (range.length > 0) {
text = text.withoutRange(range)
text = text.withoutRange(range).makeStartWithSpace()
cursorIndex = range.first
} else {
if (it.key == Key.BACKSPACE) {
if (cursorIndex > 0) {
val oldCursorIndex = cursorIndex
text = text.withoutIndex(cursorIndex - 1)
cursorIndex = oldCursorIndex - 1 // This [oldCursorIndex] is required since changing text might change the cursorIndex already in some circumstances
var tempText = text.withoutIndex(cursorIndex - 1)
if (tempText.startsWith(" ")) {
text = tempText.makeStartWithSpace()
cursorIndex = oldCursorIndex - 1 // This [oldCursorIndex] is required since changing text might change the cursorIndex already in some circumstances
} else {

}
}
} else {
if (cursorIndex < text.length) {
text = text.withoutIndex(cursorIndex)
text = text.withoutIndex(cursorIndex).makeStartWithSpace()
}
}
}
Expand Down Expand Up @@ -529,3 +540,5 @@ class TextEditController(

fun Text.editText(caretContainer: Container = this): TextEditController =
TextEditController(this, caretContainer)

fun String.makeStartWithSpace() = if (startsWith(" ")) this else " $this"
2 changes: 1 addition & 1 deletion client/src/commonMain/kotlin/ui/custom/uiTextInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import korlibs.math.geom.*

@KorgeExperimental
inline fun Container.customUiTextInput(
initialText: String = "",
initialText: String = " ",
size: Size = Size(128, 24),
block: @ViewDslMarker UITextInput.() -> Unit = {}
): UITextInput = UITextInput(initialText, size)
Expand Down
15 changes: 10 additions & 5 deletions client/src/commonMain/kotlin/ui/waitingRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import korlibs.image.color.Colors
import korlibs.image.text.HorizontalAlign
import korlibs.image.text.TextAlignment
import korlibs.korge.annotations.KorgeExperimental
import korlibs.korge.input.onClick
import korlibs.korge.style.styles
import korlibs.korge.style.textAlignment
import korlibs.korge.ui.uiButton
Expand All @@ -27,23 +28,27 @@ import kotlin.math.abs

@OptIn(KorgeExperimental::class)
suspend fun waitingRoom(room: UUID) {
sceneContainer.uiContainer {
lateinit var waitingRoom: View
waitingRoom = sceneContainer.uiContainer {
val padding = 25f
val sidebarSize = Size(sceneContainer.width / 5f, sceneContainer.height)
val sidebarSize = Size(sceneContainer.width / 3.5f, sceneContainer.height)
styles(styler)
uiText(getRoomName(room))
.alignY(root, 0.075, true)
uiText(getRoomName(room)).position(padding, padding)
val belowElementHeight = sceneContainer.width / 25f
val leaveButton = Size(belowElementHeight*1.75f, belowElementHeight)
val inputBarSize = Size(sceneContainer.width - sidebarSize.width - padding*2 - leaveButton.width - padding*2, belowElementHeight)
customUiButton(size = leaveButton) {
val back = solidRect(size, color = ColorPalette.out).centerOn(this)
customUiText("나가기").centerOn(this)
customUiText("나가기").centerOn(this).onClick {
waitingRoom.removeFromParent()
MainMenuState().mainMenu()
}
positionX(padding)
positionY(sceneContainer.height - padding - size.height)
}
uiContainer {
val input = customUiTextInput(size = inputBarSize.minus(Size(padding/2, 0f))) {
text = " "
styles { textAlignment = TextAlignment.MIDDLE_LEFT }
controller.textView.alignment = TextAlignment.MIDDLE_LEFT
controller.caretContainer.alignY(this, 0.75, false)
Expand Down

0 comments on commit e8b1da4

Please sign in to comment.