Skip to content

Commit

Permalink
resource check boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
ManApart committed Dec 9, 2023
1 parent fd077d5 commit a344666
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 39 deletions.
13 changes: 1 addition & 12 deletions src/jsMain/kotlin/components/CheckBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ import org.w3c.dom.HTMLInputElement
import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty0

fun TagConsumer<HTMLElement>.checkBox(name: String, getValue: () -> Boolean, onChange: (Boolean) -> Unit = {}) {
input(InputType.checkBox, classes = "checkbox") {
id = "${name}-checkbox"
checked = getValue()
onChangeFunction = {
val newVal = el<HTMLInputElement>(this.id).checked
onChange(newVal)
}
}
}

fun TagConsumer<HTMLElement>.checkBox(property: KMutableProperty0<Boolean>, onChange: (Boolean) -> Unit = {}) {
input(InputType.checkBox, classes = "checkbox") {
id = "${property.name}-checkbox"
Expand Down Expand Up @@ -49,7 +38,7 @@ fun TagConsumer<HTMLElement>.checkBox(
onChange: (Boolean) -> Unit = {}
) {
input(InputType.checkBox, classes = "checkbox") {
id = "${property.name}-checkbox"
id = "${property.name}-$index-checkbox"
checked = property.get().contains(index)
onChangeFunction = {
val newVal = el<HTMLInputElement>(this.id).checked
Expand Down
46 changes: 43 additions & 3 deletions src/jsMain/kotlin/components/ResourcePopup.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package components

import ResourceType
import doRouting
import el
import kotlinx.browser.window
import kotlinx.dom.addClass
Expand All @@ -11,21 +12,60 @@ import kotlinx.html.js.onClickFunction
import mouseX
import mouseY
import org.w3c.dom.HTMLElement
import persistMemory
import kotlin.reflect.KProperty0

fun TagConsumer<HTMLElement>.resourceSquares(resources: Set<ResourceType>) {
resources.forEach { resourceSquare(it) }
fun TagConsumer<HTMLElement>.resourceSquares(
resources: Set<ResourceType>,
checkBoxes: KProperty0<MutableSet<Int>>? = null
) {
if (checkBoxes != null) {
resources.forEachIndexed { i, resourceType -> resourceSquare(resourceType, i, checkBoxes) }
} else {
resources.forEach { resourceSquare(it) }
}
}

fun TagConsumer<HTMLElement>.resourceSquare(resource: ResourceType) {
div("resource") {
style = "background-color: #${resource.color}"
div("resource-inner") {
a("https://starfieldwiki.net/wiki/Starfield:${resource.readableName.replace(" ", "_")}", target = "_blank") { +resource.name }
a(
"https://starfieldwiki.net/wiki/Starfield:${resource.readableName.replace(" ", "_")}",
target = "_blank"
) { +resource.name }
}
title = resource.readableName
}
}

private fun TagConsumer<HTMLElement>.resourceSquare(
resource: ResourceType,
i: Int,
checkBoxes: KProperty0<MutableSet<Int>>
) {
div("resource") {
id = "resource-${resource.name}"
val color = if(checkBoxes.get().contains(i)) "#" +resource.color else "var(--menu-gray)"

style = "background-color: $color"
div("resource-hover-check") {
checkBox(i, checkBoxes) {
el("resource-${resource.name}").style.backgroundColor = if(it) "#" +resource.color else "var(--menu-gray)"
persistMemory()
}
}
div("resource-inner") {
a(
"https://starfieldwiki.net/wiki/Starfield:${resource.readableName.replace(" ", "_")}",
target = "_blank"
) { +resource.name }
}
title = resource.readableName
}
}


fun showResourcePicker(resources: Set<ResourceType>, result: (ResourceType) -> Unit) {
val root = el("popup")
root.removeClass("hidden")
Expand Down
2 changes: 1 addition & 1 deletion src/jsMain/kotlin/views/system/PlanetView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fun TagConsumer<HTMLElement>.detailView(system: StarSystem, planet: Planet, link
}
traitsRow(info.scan, traits)
organicResourceRow(organicResources)
inorganicResourceRow(inorganicResources)
inorganicResourceRow(inorganicResources, info.scan::resources)
outPostsRow(info.outPosts)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/jsMain/kotlin/views/system/SystemView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import persistMemory
import replaceElement
import updateUrl
import views.*
import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty0


var currentSystem: StarSystem? = null
Expand Down Expand Up @@ -105,14 +107,14 @@ fun TagConsumer<HTMLElement>.organicResourceRow(resources: Set<String>) {
}
}

fun TagConsumer<HTMLElement>.inorganicResourceRow(resources: Set<ResourceType>) {
fun TagConsumer<HTMLElement>.inorganicResourceRow(resources: Set<ResourceType>, checkBoxes: KProperty0<MutableSet<Int>>? = null) {
tr {
td("resource-td") { +"Inorganic Resources" }
td("resource-value-td") {
if (resources.isEmpty()) {
+"None"
} else {
resourceSquares(resources)
resourceSquares(resources, checkBoxes)
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/jsMain/resources/css/components.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,37 @@ input[type="checkbox"]:checked {
}

.counter {
}

.resource {
position: relative;
float: right;
text-align: center;
width: 50px;
height: 50px;
border: 1px solid gray;
margin: 2px;
font-size: medium;
}

.resource-inner {
margin-top: 25%;
}

.resource-wrapper {
height: 55px;
}
.resource-wrapper .resource {
float: left;
}

.resource-hover-check{
display: none;
position: absolute;
top: 0px;
left: 0px;
}

.resource:hover .resource-hover-check{
display: block;
}
21 changes: 0 additions & 21 deletions src/jsMain/resources/css/system.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,6 @@
direction: rtl;
}

.resource {
float: right;
text-align: center;
width: 50px;
height: 50px;
border: 1px solid gray;
margin: 2px;
font-size: medium;
}

.resource-inner {
margin-top: 25%;
}

.resource-wrapper {
height: 55px;
}
.resource-wrapper .resource {
float: left;
}

#system-map {
white-space: nowrap;
overflow-x: scroll;
Expand Down

0 comments on commit a344666

Please sign in to comment.