From e917807adccc337e338c7a62a185d97e0cb52cd7 Mon Sep 17 00:00:00 2001 From: xtex Date: Sat, 29 Jul 2023 19:52:23 +0800 Subject: [PATCH] feat: add music projection stub --- common/src/main/kotlin/quaedam/Quaedam.kt | 2 + .../projection/music/MusicProjection.kt | 75 +++++++++++++++++++ .../quaedam/shell/ProjectionEffectShell.kt | 41 +++++++++- .../quaedam/blockstates/music_projection.json | 7 ++ .../resources/assets/quaedam/lang/en_us.json | 7 +- .../resources/assets/quaedam/lang/zh_cn.json | 7 +- .../models/block/music_projection.json | 6 ++ .../quaedam/models/item/music_projection.json | 3 + 8 files changed, 144 insertions(+), 4 deletions(-) create mode 100644 common/src/main/kotlin/quaedam/projection/music/MusicProjection.kt create mode 100644 common/src/main/resources/assets/quaedam/blockstates/music_projection.json create mode 100644 common/src/main/resources/assets/quaedam/models/block/music_projection.json create mode 100644 common/src/main/resources/assets/quaedam/models/item/music_projection.json diff --git a/common/src/main/kotlin/quaedam/Quaedam.kt b/common/src/main/kotlin/quaedam/Quaedam.kt index 29444d8..aa5e118 100644 --- a/common/src/main/kotlin/quaedam/Quaedam.kt +++ b/common/src/main/kotlin/quaedam/Quaedam.kt @@ -18,6 +18,7 @@ import quaedam.projection.SimpleProjectionUpdate import quaedam.projection.misc.NoiseProjection import quaedam.projection.misc.SkylightProjection import quaedam.projection.misc.SoundProjection +import quaedam.projection.music.MusicProjection import quaedam.projection.swarm.SwarmProjection import quaedam.projector.Projector import quaedam.shell.ProjectionShell @@ -53,6 +54,7 @@ object Quaedam { SwarmProjection SoundProjection NoiseProjection + MusicProjection ProjectionCommand SimpleProjectionUpdate ProjectionShell diff --git a/common/src/main/kotlin/quaedam/projection/music/MusicProjection.kt b/common/src/main/kotlin/quaedam/projection/music/MusicProjection.kt new file mode 100644 index 0000000..7cce014 --- /dev/null +++ b/common/src/main/kotlin/quaedam/projection/music/MusicProjection.kt @@ -0,0 +1,75 @@ +package quaedam.projection.music + +import net.minecraft.nbt.CompoundTag +import net.minecraft.world.item.BlockItem +import net.minecraft.world.item.Item +import quaedam.Quaedam +import quaedam.projection.EntityProjectionBlock +import quaedam.projection.ProjectionEffect +import quaedam.projection.ProjectionEffectType +import quaedam.projection.SimpleProjectionEntity +import quaedam.projection.misc.SoundProjection +import quaedam.shell.ProjectionEffectShell +import quaedam.shell.buildProjectionEffectShell +import kotlin.math.min + +object MusicProjection { + + const val ID = "music_projection" + const val SHORT_ID = "projection" + + val block = Quaedam.blocks.register(ID) { MusicProjectionBlock }!! + + val item = Quaedam.items.register(ID) { + BlockItem( + MusicProjectionBlock, Item.Properties() + .`arch$tab`(Quaedam.creativeModeTab) + ) + }!! + + val effect = Quaedam.projectionEffects.register(SHORT_ID) { + ProjectionEffectType { MusicProjectionEffect() } + }!! + + val blockEntity = Quaedam.blockEntities.register(ID) { + SimpleProjectionEntity.createBlockEntityType(block) { MusicProjectionEffect() } + }!! + +} + +object MusicProjectionBlock : EntityProjectionBlock(createProperties().lightLevel { 3 }) { + + override val blockEntity = MusicProjection.blockEntity + +} + +data class MusicProjectionEffect(var volumeFactor: Float = 1.0f, var multiTracks: Boolean = true) : ProjectionEffect(), + ProjectionEffectShell.Provider { + + companion object { + const val TAG_VOLUME_FACTOR = "VolumeFactor" + const val TAG_MULTI_TRACKS = "MultiTracks" + } + + override val type + get() = MusicProjection.effect.get()!! + + override fun toNbt(tag: CompoundTag) { + tag.putFloat(TAG_VOLUME_FACTOR, volumeFactor) + tag.putBoolean(TAG_MULTI_TRACKS, multiTracks) + } + + override fun fromNbt(tag: CompoundTag, trusted: Boolean) { + volumeFactor = tag.getFloat(TAG_VOLUME_FACTOR) + multiTracks = tag.getBoolean(TAG_MULTI_TRACKS) + if (!trusted) { + volumeFactor = min(volumeFactor, 5.0f) + } + } + + override fun createShell() = buildProjectionEffectShell(this) { + floatSlider("quaedam.shell.music.volume_factor", ::volumeFactor, 0.0f..1.0f, 0.1f) + boolean("quaedam.shell.music.multi_tracks", ::multiTracks) + } + +} diff --git a/common/src/main/kotlin/quaedam/shell/ProjectionEffectShell.kt b/common/src/main/kotlin/quaedam/shell/ProjectionEffectShell.kt index 8896759..a35733d 100644 --- a/common/src/main/kotlin/quaedam/shell/ProjectionEffectShell.kt +++ b/common/src/main/kotlin/quaedam/shell/ProjectionEffectShell.kt @@ -28,9 +28,9 @@ class ProjectionEffectShell(val effect: ProjectionEffect) { fun text(key: String, value: Component) = row(key) { StringWidget(value, it.font) } - fun doubleSlider(key: String, property: KMutableProperty0, range: ClosedRange, step: Double) { + fun doubleSlider(key: String, property: KMutableProperty0, range: ClosedRange, rawStep: Double) { val len = range.endInclusive - range.start - val step = step / len + val step = rawStep / len row(key) { object : AbstractSliderButton( 0, 0, width, height, @@ -53,6 +53,35 @@ class ProjectionEffectShell(val effect: ProjectionEffect) { } } + fun floatSlider(key: String, property: KMutableProperty0, range: ClosedRange, rawStep: Float) { + val len = range.endInclusive - range.start + val step = rawStep / len + row(key) { + object : AbstractSliderButton( + 0, + 0, + width, + height, + Component.literal(String.format("%.2f", property.get())), + (property.get() - range.start) / len.toDouble() + ) { + override fun updateMessage() { + message = Component.literal(String.format("%.2f", property.get())) + } + + override fun applyValue() { + val diff = value % step + if (diff < step * 0.5) { + value -= diff + } else { + value += (step - diff) + } + property.set(range.start + (value.toFloat() * len)) + } + } + } + } + fun intSlider(key: String, property: KMutableProperty0, range: IntProgression) { val len = range.last - range.first val step = range.step.toDouble() / len @@ -86,6 +115,14 @@ class ProjectionEffectShell(val effect: ProjectionEffect) { .create(0, 0, width, height, Component.translatable(key)) } + fun boolean(key: String, property: KMutableProperty0) = + row(key) { + CycleButton.builder { Component.translatable("$key.$it") } + .displayOnlyValue() + .withValues(listOf(true, false)) + .create(0, 0, width, height, Component.translatable(key)) + } + } inline fun buildProjectionEffectShell(effect: ProjectionEffect, builder: ProjectionEffectShell.() -> Unit) = diff --git a/common/src/main/resources/assets/quaedam/blockstates/music_projection.json b/common/src/main/resources/assets/quaedam/blockstates/music_projection.json new file mode 100644 index 0000000..55987c2 --- /dev/null +++ b/common/src/main/resources/assets/quaedam/blockstates/music_projection.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "quaedam:block/music_projection" + } + } +} diff --git a/common/src/main/resources/assets/quaedam/lang/en_us.json b/common/src/main/resources/assets/quaedam/lang/en_us.json index 50dbd0b..249f2bf 100644 --- a/common/src/main/resources/assets/quaedam/lang/en_us.json +++ b/common/src/main/resources/assets/quaedam/lang/en_us.json @@ -5,6 +5,7 @@ "block.quaedam.swarm_projection": "Swarm Projection", "block.quaedam.sound_projection": "Sound Projection", "block.quaedam.noise_projection": "Noise Projection", + "block.quaedam.music_projection": "Music Projection", "block.quaedam.causality_anchor": "Causality Anchor", "block.quaedam.reality_stabler": "Reality Stabler", "entity.quaedam.projected_person": "Virtual Person", @@ -19,5 +20,9 @@ "quaedam.shell.noise.rate": "Rate", "quaedam.shell.noise.amount": "Amount", "quaedam.shell.swarm.max_count": "Max Count", - "quaedam.shell.sound.rate": "Rate" + "quaedam.shell.sound.rate": "Rate", + "quaedam.shell.music.volume_factor": "Volume Factor", + "quaedam.shell.music.multi_tracks": "Multi Tracks", + "quaedam.shell.music.multi_tracks.true": "Enabled", + "quaedam.shell.music.multi_tracks.false": "Disabled" } \ No newline at end of file diff --git a/common/src/main/resources/assets/quaedam/lang/zh_cn.json b/common/src/main/resources/assets/quaedam/lang/zh_cn.json index 595ee3d..0e1cb76 100644 --- a/common/src/main/resources/assets/quaedam/lang/zh_cn.json +++ b/common/src/main/resources/assets/quaedam/lang/zh_cn.json @@ -5,6 +5,7 @@ "block.quaedam.swarm_projection": "人群投影", "block.quaedam.sound_projection": "声音投影", "block.quaedam.noise_projection": "噪音投影", + "block.quaedam.music_projection": "音乐投影", "block.quaedam.causality_anchor": "因果锚", "block.quaedam.reality_stabler": "现实稳定器", "entity.quaedam.projected_person": "虚拟个体", @@ -19,5 +20,9 @@ "quaedam.shell.noise.rate": "速率", "quaedam.shell.noise.amount": "数量", "quaedam.shell.swarm.max_count": "最大数量", - "quaedam.shell.sound.rate": "速率" + "quaedam.shell.sound.rate": "速率", + "quaedam.shell.music.volume_factor": "响度因子", + "quaedam.shell.music.multi_tracks": "多轨音乐", + "quaedam.shell.music.multi_tracks.true": "开启", + "quaedam.shell.music.multi_tracks.false": "关闭" } diff --git a/common/src/main/resources/assets/quaedam/models/block/music_projection.json b/common/src/main/resources/assets/quaedam/models/block/music_projection.json new file mode 100644 index 0000000..c263593 --- /dev/null +++ b/common/src/main/resources/assets/quaedam/models/block/music_projection.json @@ -0,0 +1,6 @@ +{ + "parent": "quaedam:block/projection", + "textures": { + "ext": "quaedam:block/music_projection" + } +} diff --git a/common/src/main/resources/assets/quaedam/models/item/music_projection.json b/common/src/main/resources/assets/quaedam/models/item/music_projection.json new file mode 100644 index 0000000..fe61cd4 --- /dev/null +++ b/common/src/main/resources/assets/quaedam/models/item/music_projection.json @@ -0,0 +1,3 @@ +{ + "parent": "quaedam:block/music_projection" +} \ No newline at end of file