Skip to content

Commit

Permalink
PaletteSidebarView: Fix bug allowing players to update existing color…
Browse files Browse the repository at this point in the history
…s to other existing colors
  • Loading branch information
aaronjyoder committed Jun 26, 2022
1 parent 425fc91 commit 3185f1c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ fun PaletteSidebarView(model: PaletteViewModel, modifier: Modifier) {
maxIndex = model.colorSet.size - 1,
onMoveSelection = onMoveSelection,
canUpdate = selected && model.isNewColorNameValid() && model.isNewColorHexStringValid()
&& (model.newColorName != model.activeColorName() || model.newColorHexString != model.activeColorHexString()),
&& (model.newColorName != model.activeColorName() || model.newColorHexString != model.activeColorHexString())
&& !model.colorSetContainsHexColor(),
canAdd = !selected && !model.colorSetContainsNewColor()
&& model.isNewColorNameValid() && model.isNewColorHexStringValid(),
canDelete = selected,
Expand Down Expand Up @@ -159,7 +160,7 @@ private fun ColorEditorView(
label = "Color (Hex Code)",
singleLine = true,
onValueChange = {
model.newColorHexString = it
model.newColorHexString = it.uppercase()
}
)

Expand Down Expand Up @@ -270,7 +271,12 @@ private fun SelectableColorListView(
) {
val listState = rememberLazyListState()

Box(modifier = modifier.background(color = ViewColor.UI_BACKGROUND_DARK_ON_DARK, shape = RoundedCornerShape(4.dp))) {
Box(
modifier = modifier.background(
color = ViewColor.UI_BACKGROUND_DARK_ON_DARK,
shape = RoundedCornerShape(4.dp)
)
) {
LazyColumn(Modifier.fillMaxSize().padding(top = 8.dp, bottom = 8.dp, start = 12.dp, end = 16.dp), listState) {
items(list.size) { i ->
val rkpColor = list[i]
Expand All @@ -293,7 +299,13 @@ private fun SelectableColorListView(
}

@Composable
private fun SelectableColorListItem(text: String = "[ITEM]", backgroundColor: Color, index: Int, selected: Boolean, onClick: (Int) -> Unit) {
private fun SelectableColorListItem(
text: String = "[ITEM]",
backgroundColor: Color,
index: Int,
selected: Boolean,
onClick: (Int) -> Unit
) {
val borderStroke = if (selected) {
BorderStroke(4.dp, ColorUtil.getTextLightOrDark(backgroundColor))
} else {
Expand All @@ -302,7 +314,10 @@ private fun SelectableColorListItem(text: String = "[ITEM]", backgroundColor: Co
Box( // TODO: Border color leaks out the corners
modifier = Modifier.height(48.dp)
.fillMaxWidth()
.background(color = backgroundColor, shape = if (selected) RoundedCornerShape(6.dp) else RoundedCornerShape(4.dp)) // Hacky workaround for color leaking from corners
.background(
color = backgroundColor,
shape = if (selected) RoundedCornerShape(6.dp) else RoundedCornerShape(4.dp)
) // Hacky workaround for color leaking from corners
.border(border = borderStroke, shape = RoundedCornerShape(4.dp))
.clip(RoundedCornerShape(4.dp))
.selectable(selected = selected, onClick = { onClick.invoke(index) }),
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/riskrieg/editor/viewmodel/PaletteViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,19 @@ class PaletteViewModel(private val window: ComposeWindow, var mousePosition: Poi
}
}

fun colorSetContainsHexColor(): Boolean {
for (gameColor in colorSet) {
if (newColorHexString.trim() == String.format("#%02X%02X%02X", gameColor.r, gameColor.g, gameColor.b)) {
return true
}
}
return false
}

fun colorSetContainsNewColor(): Boolean {
for (gameColor in colorSet) {
if (newColorName.trim() == gameColor.name || newColorHexString.trim() == String.format("#%02X%02X%02X", gameColor.r, gameColor.g, gameColor.b)) {
println(gameColor.name)
return true
}
}
Expand Down

0 comments on commit 3185f1c

Please sign in to comment.