组件性

组件性

一个用Kotlin构建/为Kotlin而建的声明式GUI创建库。

基础库

Componency

一个简单的、声明式 UI 库,从零开始构建,可在任何环境中使用。

wakatime


Discord Badge

Ko-Fi Badge PayPal Badge GH Sponsors Badge


灵感来源

Componency 大致基于另外 3 个项目:

我借鉴了这 3 个项目的概念,创建了一个简单的、声明式 UI 库,可在任何环境中使用。


免责声明

该库主要为 Kotlin 设计,但也对 Java 提供了一些额外支持。

以下所有示例都将使用 Kotlin,但该库也可在 Java 中使用。


设置

仓库

Groovy (.gradle)
maven {
    name = "Deftu Snapshots"
    url = "https://maven.deftu.dev/snapshots"
}
Kotlin (.gradle.kts)
maven(url = "https://maven.deftu.dev/snapshots") {
    name = "Deftu Snapshots"
}

依赖

Repository badge

Groovy (.gradle)
modImplementation "dev.deftu:componency:<VERSION>"
Kotlin (.gradle.kts)
implementation("dev.deftu:componency:<VERSION>")

用法

引擎

要使用该库,你需要创建你的 Componency 引擎。引擎负责为 Componency 提供其所需的所有数据,并使其能够与你选择的渲染系统进行交互。

package com.example

import dev.deftu.componency.engine.Engine
import dev.deftu.componency.engine.InputEngine
import dev.deftu.componency.engine.RenderEngine
import java.awt.Color

class MyEngine : Engine {

    override val inputEngine: InputEngine by lazy {
        MyInputEngine()
    }

    override val renderEngine: RenderEngine by lazy {
        MyRenderEngine()
    }

}

class MyInputEngine : InputEngine {

    override val mouseX: Float = 0f

    override val mouseY: Float = 0f

}

class MyRenderEngine : RenderEngine {

    override val viewportWidth: Int = 0

    override val viewportHeight: Int = 0

    override val animationFps: Int = 144 // Recommended to be no more than 300

    override fun startFrame() {
        // Start rendering frame (apply any transformations, etc.)
    }

    override fun endFrame() {
        // End rendering frame (apply any transformations, etc.)
    }

    override fun fill(x1: Float, y1: Float, x2: Float, y2: Float, color: Color, radius: Float) {
        // Fill a rectangle with a color and a radius
    }

}

你的 UI

现在你已经设置好了引擎,可以开始创建你的 UI 了。

object Main {
    
    @JvmStatic
    fun main(args: Array<String>) {
        val engine = MyEngine()
        val ui = MyUI(engine)
        
        // Inside your render loop, you can simply call `ui.render()` to render your UI.
        ui.render() // (This is a placeholder for your actual render loop)
    }
    
}

class MyUI(engine: Engine) {
    
    // Common practice is to use a frame as your root component
    private val frame = FrameComponent().configure {
        // In Componency, we define properties inside their own scope when configuring.
        // The majority of the most commonly used properties have extension variables and functions to make them easier to access.
        properties {
            width = 100.percent
            height = 100.percent
        }
    }.makeRoot(engine) // Finally, we make the frame the root component of this UI by giving it the engine.
    
    private val box = RectangleComponent().configure {
        properties {
            x = 25.percent
            y = 25.percent
            width = 50.percent
            height = 50.percent
            color = Color.RED.asProperty
        }
    }.attachTo(frame) // We make the box a child of the frame.
    
    // From here, you can add more components to the frame or the box.

    // Finally, we provide an external means of rendering our frame.
    fun render() {
        frame.handleRender()
    }

}

更多示例

如需更多示例,你可以查看 基础示例 目录。(Java、Kotlin)

或者,如果你想为 LWJGL3 实现,可以查看 LWJGL3 示例 目录。

在 Minecraft 中设置

现代和旧版 Minecraft 版本仍然需要用户使用 设置 部分中定义的仓库。Minecraft 构件的版本号与主库相同。

现代 Minecraft(1.16.5 及以上)

只需在构建脚本中定义依赖即可在现代 Minecraft 版本中使用 Componency Minecraft 引擎。

Groovy (.gradle)
modImplementation "dev.deftu:componency-minecraft-<MINECRAFT VERSION>-<MOD LOADER>:<VERSION>"
Kotlin (.gradle.kts)
implementation("dev.deftu:componency-minecraft-<MINECRAFT VERSION>-<MOD LOADER>:<VERSION>")

旧版 Minecraft(1.12.2 和 1.8.9)

我们需要为旧版 Minecraft 版本进行额外设置,因为我们需要使用依赖管理 tweaker 来确保加载最新版本的 Componency。

它不仅处理库的版本,还强制 Minecraft 使用 LWJGL 3 来加载 NanoVG 和 STB,以便我们的引擎可以使用两者进行渲染。

有关与其他 tweaker 一起使用的附加说明,请参见 类文件本身。

Groovy (.gradle)
dependencies {
    modImplementation "dev.deftu:componency-minecraft-<MINECRAFT VERSION>-<MOD LOADER>:<VERSION>"
    modImplementation "dev.deftu:componency-minecraft-tweaker-<MINECRAFT VERSION>-<MOD LOADER>:<VERSION>"
}

jar {
    manifest {
        attributes(
            "TweakClass": "dev.deftu.componency.minecraft.tweaker.ComponencyTweaker"
        )
    }
}
Kotlin (.gradle.kts)
dependencies {
    implementation("dev.deftu:componency-minecraft-<MINECRAFT VERSION>-<MOD LOADER>:<VERSION>")
    implementation("dev.deftu:componency-minecraft-tweaker-<MINECRAFT VERSION>-<MOD LOADER>:<VERSION>")
}

tasks.jar {
    manifest {
        attributes(
            "TweakClass" to "dev.deftu.componency.minecraft.tweaker.ComponencyTweaker"
        )
    }
}

致谢


即将推出!

  • 调试器

BisectHosting


本项目基于 LGPL-3.0 许可
© 2024 Deftu