Skip to content

Commit

Permalink
Added tasks for generating samples in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
smilefx committed Jan 9, 2025
1 parent 064a1ee commit c96d621
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package tools.aqua.bgw.application

internal object Config {
val USE_SOCKETS = true
val BGW_VERSION = "0.9-new_backend-73-4fa904c-SNAPSHOT"
val GENERATE_SAMPLES = true
val BGW_VERSION = "0.9-new_backend-81-064a1ee-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ open class CameraPane<T : LayoutView<*>>(
* @param x The x-coordinate to scroll to.
* @param y The y-coordinate to scroll to.
* @param zoom The zoom level to zoom to.
* @param smooth Whether the pan should be smooth or instant.
* @param smooth Whether the pan should be smooth (or instant). Default is true.
*
* @see limitBounds
* @see pan
Expand All @@ -183,7 +183,7 @@ open class CameraPane<T : LayoutView<*>>(
*
* @param x The x-coordinate to scroll to.
* @param y The y-coordinate to scroll to.
* @param smooth Whether the pan should be smooth or instant.
* @param smooth Whether the pan should be smooth (or instant). Default is true.
*
* @see limitBounds
* @see pan
Expand All @@ -207,7 +207,7 @@ open class CameraPane<T : LayoutView<*>>(
* @param xOffset The amount to pan the view horizontally.
* @param yOffset The amount to pan the view vertically.
* @param zoom The zoom level to zoom to.
* @param smooth Whether the pan should be smooth or instant.
* @param smooth Whether the pan should be smooth (or instant). Default is true.
*
* @see limitBounds
* @see pan
Expand All @@ -230,7 +230,7 @@ open class CameraPane<T : LayoutView<*>>(
*
* @param xOffset The amount to pan the view horizontally.
* @param yOffset The amount to pan the view vertically.
* @param smooth Whether the pan should be smooth or instant.
* @param smooth Whether the pan should be smooth (or instant). Default is true.
*
* @see limitBounds
* @see pan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import tools.aqua.bgw.visual.Visual
* @param alignment Alignment to be used for the [text] Default: [Alignment.CENTER].
* @param isWrapText Defines if [text] should be wrapped. Default: `false`.
* @param visual Visual for this button. Default: [ColorVisual.WHITE]
*
* @sample tools.aqua.bgw.main.examples.ExampleUIScene.button
* @sample tools.aqua.bgw.main.examples.ExampleUIScene.button2
*/
open class Button(
posX: Number = 0,
Expand Down
17 changes: 16 additions & 1 deletion bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/main/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,23 @@

package tools.aqua.bgw.main

import PropData
import jsonMapper
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import tools.aqua.bgw.application.Config
import tools.aqua.bgw.core.Frontend
import tools.aqua.bgw.main.examples.ExampleApplication
import tools.aqua.bgw.main.view.Application
import java.io.File

internal fun main() {
Application.show()
if(!Config.USE_SOCKETS || Config.GENERATE_SAMPLES) {
ExampleApplication.showGameScene(ExampleApplication.exampleUIScene)
val jsonData = Json.encodeToString(ExampleApplication.exampleUIScene.map)
File("build/examples").mkdirs()
File("build/examples/bgwSamples.json").writeText(jsonData)
} else {
Application.show()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2025 The BoardGameWork Authors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package tools.aqua.bgw.main.examples

import tools.aqua.bgw.core.AspectRatio
import tools.aqua.bgw.core.BoardGameApplication

internal object ExampleApplication : BoardGameApplication() {
val exampleUIScene = ExampleUIScene()
}
78 changes: 78 additions & 0 deletions bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/main/examples/UIScene.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2025 The BoardGameWork Authors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* ------------------------------------------ */
/* THIS FILE INCLUDES AUTO-GENERATED CODE, */
/* EXPLICIT FORMATING AND IMPORTS. */
/* */
/* ! DO NOT MODIFY THIS FILE ! */
/* ------------------------------------------ */

package tools.aqua.bgw.main.examples

import tools.aqua.bgw.main.examples.ExampleUIScene as Scene
import PropData
import jsonMapper
import kotlinx.serialization.encodeToString
import tools.aqua.bgw.components.ComponentView
import tools.aqua.bgw.components.uicomponents.*
import tools.aqua.bgw.core.*
import tools.aqua.bgw.visual.ColorVisual
import kotlin.reflect.KProperty0

/** Metadata: [Scene] */
internal class ExampleUIScene : BoardGameScene() {
val map = mutableMapOf<String, String>()

val button = Button(
posX = 100,
posY = 50,
width = 200,
height = 100,
text = "I am a Button.",
visual = ColorVisual.LIGHT_GRAY
)

val button2 = Button(
posX = 0,
posY = 0,
width = 200,
height = 200,
text = "Also a Button.",
visual = ColorVisual.RED
)

init {
setComponentAndSerialize(::button)
setComponentAndSerialize(::button2)
}

internal fun <T : ComponentView> setComponentAndSerialize(prop: KProperty0<T>) {
val comp = prop.get()

clearComponents()
addComponents(comp)

val fullyQualifiedName = "${this::class.qualifiedName}.${prop.name}"

val appData = SceneMapper.map(menuScene = null, gameScene = this)
val json = jsonMapper.encodeToString(PropData(appData))
map[fullyQualifiedName] = json

clearComponents()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal object Application : BoardGameApplication(aspectRatio = AspectRatio.of(
// showGameScene(animation)
// showGameScene(grid)
// showGameScene(dragDropScene)
showMenuScene(uiScene)
// showMenuScene(uiScene)
// showGameScene(visualScene)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ internal class HexGridGameScene : BoardGameScene() {
}

private val singleHex =
HexagonView(posX = 1100, posY = 0, visual = ColorVisual.BLUE, size = 50).apply {
HexagonView(posX = 1100, posY = 0, visual = ColorVisual.BLUE.copy().apply {
style.borderRadius = BorderRadius(4)
}, size = 50).apply {
isDraggable = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnLockMismatchReport
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import tools.aqua.defaultFormat
import java.net.URLClassLoader
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KProperty

plugins {
kotlin("multiplatform")
Expand All @@ -44,6 +48,7 @@ plugins {
}

val useSockets: String? = project.findProperty("useSockets")?.toString()
var generateSamples = project.findProperty("generateSamples")?.toString()?.toBoolean() ?: false
val propertyFile = "Config.kt"
val wrappersVersion = "-pre.831"

Expand All @@ -59,6 +64,9 @@ fun buildPropertyFile() {
parentFile.mkdirs()
writeText(generateProperties("application"))
}

println("useSockets: $useSockets")
println("generateSamples: $generateSamples")
}

fun generateProperties(suffix: String = "") =
Expand All @@ -67,6 +75,7 @@ fun generateProperties(suffix: String = "") =
internal object Config {
val USE_SOCKETS = ${useSockets ?: "true"}
val GENERATE_SAMPLES = $generateSamples
val BGW_VERSION = "${rootProject.version}"
}
""".trimIndent()
Expand Down Expand Up @@ -233,4 +242,34 @@ rootProject.plugins.withType(org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlu
YarnLockMismatchReport.NONE
rootProject.the<YarnRootExtension>().reportNewYarnLock = false
rootProject.the<YarnRootExtension>().yarnLockAutoReplace = true
}

tasks.register<JavaExec>("runWithSamples") {
group = "application"
description = "Runs the application with generateSamples=true"
classpath = sourceSets["main"].runtimeClasspath
mainClass.set("tools.aqua.bgw.main.MainKt")
args = listOf("-PgenerateSamples=true")
}

tasks.register("setupGenerateSamples") {
doFirst {
generateSamples = true
buildPropertyFile()
}
}

tasks.named("dokkaHtmlPartial").configure {
shouldRunAfter("setupGenerateSamples")
}

tasks.register("generateDocsAndSamples") {
dependsOn("setupGenerateSamples", "dokkaHtmlPartial", "run")
doLast {
println("Successfully executed dokkaHtmlPartial and run.")
}
}

tasks.named("run").configure {
shouldRunAfter("dokkaHtmlPartial") // Ensure no overlap or conflicts.
}

0 comments on commit c96d621

Please sign in to comment.