From 41d8ee9f32adfaca01ff7e94bbd49fb978a14e0c Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 3 Dec 2021 13:54:21 +0100 Subject: [PATCH] profiles: rendering --- .../config/config/game/OtherGameConfig.kt | 1 - .../config/profile/GlobalProfileManager.kt | 2 ++ .../config/profile/ProfileCollection.kt | 3 ++ .../change/listener/SimpleChangeListener.kt | 14 ++++++++ .../profiles/account/AccountProfile.kt | 10 ++---- .../profiles/rendering/RenderingProfile.kt | 36 +++++++++++++++++++ .../rendering/RenderingProfileManager.kt | 36 +++++++++++++++++++ .../rendering/RenderingProfileSelectEvent.kt | 7 ++++ .../profiles/rendering/advanced/AdvancedC.kt | 14 ++++++++ .../minosoft/gui/rendering/RenderWindow.kt | 2 +- .../gui/rendering/system/window/BaseWindow.kt | 9 +++-- .../gui/rendering/system/window/GLFWWindow.kt | 5 +-- 12 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfile.kt create mode 100644 src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfileManager.kt create mode 100644 src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfileSelectEvent.kt create mode 100644 src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/advanced/AdvancedC.kt diff --git a/src/main/java/de/bixilon/minosoft/config/config/game/OtherGameConfig.kt b/src/main/java/de/bixilon/minosoft/config/config/game/OtherGameConfig.kt index e220df42c..4916376e9 100644 --- a/src/main/java/de/bixilon/minosoft/config/config/game/OtherGameConfig.kt +++ b/src/main/java/de/bixilon/minosoft/config/config/game/OtherGameConfig.kt @@ -20,5 +20,4 @@ data class OtherGameConfig( @Json(name = "flower_random_offset") var flowerRandomOffset: Boolean = true, @Json(name = "block_outline") var blockOutline: BlockOutline = BlockOutline(), @Json(name = "experimental_fps") var experimentalFPS: Boolean = false, - @Json(name = "super_dumb_advanced_setting_leave_at_1") var swapInterval: Int = 1, ) diff --git a/src/main/java/de/bixilon/minosoft/config/profile/GlobalProfileManager.kt b/src/main/java/de/bixilon/minosoft/config/profile/GlobalProfileManager.kt index f787c1e5e..f7492b22e 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/GlobalProfileManager.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/GlobalProfileManager.kt @@ -7,6 +7,7 @@ import de.bixilon.minosoft.config.profile.profiles.audio.AudioProfileManager import de.bixilon.minosoft.config.profile.profiles.entity.EntityProfileManager import de.bixilon.minosoft.config.profile.profiles.eros.ErosProfileManager import de.bixilon.minosoft.config.profile.profiles.particle.ParticleProfileManager +import de.bixilon.minosoft.config.profile.profiles.rendering.RenderingProfileManager import de.bixilon.minosoft.config.profile.profiles.resources.ResourcesProfileManager import de.bixilon.minosoft.data.registries.ResourceLocation import de.bixilon.minosoft.gui.eros.crash.ErosCrashReport.Companion.crash @@ -30,6 +31,7 @@ object GlobalProfileManager { EntityProfileManager, ResourcesProfileManager, AccountProfileManager, + RenderingProfileManager, ) private val SELECTED_PROFILES_TYPE: MapType = Jackson.MAPPER.typeFactory.constructMapType(HashMap::class.java, ResourceLocation::class.java, String::class.java) val CLASS_MAPPING: Map, ProfileManager<*>> diff --git a/src/main/java/de/bixilon/minosoft/config/profile/ProfileCollection.kt b/src/main/java/de/bixilon/minosoft/config/profile/ProfileCollection.kt index a27dd676e..ac17d6eb3 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/ProfileCollection.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/ProfileCollection.kt @@ -8,6 +8,8 @@ import de.bixilon.minosoft.config.profile.profiles.eros.ErosProfile import de.bixilon.minosoft.config.profile.profiles.eros.ErosProfileManager import de.bixilon.minosoft.config.profile.profiles.particle.ParticleProfile import de.bixilon.minosoft.config.profile.profiles.particle.ParticleProfileManager +import de.bixilon.minosoft.config.profile.profiles.rendering.RenderingProfile +import de.bixilon.minosoft.config.profile.profiles.rendering.RenderingProfileManager import de.bixilon.minosoft.config.profile.profiles.resources.ResourcesProfile import de.bixilon.minosoft.config.profile.profiles.resources.ResourcesProfileManager @@ -17,4 +19,5 @@ data class ProfileCollection( val audio: AudioProfile = AudioProfileManager.selected, val entity: EntityProfile = EntityProfileManager.selected, val resources: ResourcesProfile = ResourcesProfileManager.selected, + val rendering: RenderingProfile = RenderingProfileManager.selected, ) diff --git a/src/main/java/de/bixilon/minosoft/config/profile/change/listener/SimpleChangeListener.kt b/src/main/java/de/bixilon/minosoft/config/profile/change/listener/SimpleChangeListener.kt index 53384a3d2..63062e937 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/change/listener/SimpleChangeListener.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/change/listener/SimpleChangeListener.kt @@ -3,6 +3,7 @@ package de.bixilon.minosoft.config.profile.change.listener import de.bixilon.minosoft.config.profile.change.ProfilesChangeManager import de.bixilon.minosoft.config.profile.profiles.Profile import de.bixilon.minosoft.gui.eros.util.JavaFXUtil +import de.bixilon.minosoft.gui.rendering.Rendering import de.bixilon.minosoft.util.KUtil.unsafeCast import java.lang.reflect.Field import kotlin.reflect.KProperty @@ -41,5 +42,18 @@ class SimpleChangeListener( fun KProperty.listenFX(reference: Any, instant: Boolean = false, profile: Profile? = null, callback: ((T) -> Unit)) { ProfilesChangeManager.register(reference, SimpleChangeListener(this, javaField!!, profile, instant) { JavaFXUtil.runLater { callback(it) } }) } + + @JvmOverloads + fun KProperty.listenRendering(reference: Any, instant: Boolean = false, profile: Profile? = null, callback: ((T) -> Unit)) { + val context = Rendering.currentContext ?: throw IllegalStateException("Can only be registered in a render context!") + ProfilesChangeManager.register(reference, SimpleChangeListener(this, javaField!!, profile, instant) { + val changeContext = Rendering.currentContext + if (changeContext === context) { + callback(it) + } else { + context.queue += { callback(it) } + } + }) + } } } diff --git a/src/main/java/de/bixilon/minosoft/config/profile/profiles/account/AccountProfile.kt b/src/main/java/de/bixilon/minosoft/config/profile/profiles/account/AccountProfile.kt index 50f791735..3837d428d 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/profiles/account/AccountProfile.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/profiles/account/AccountProfile.kt @@ -26,15 +26,9 @@ class AccountProfile( var entries: MutableMap by mapDelegate() private set - @get:JsonProperty("selected") - private var _selected: String? by delegate(null) + @get:JsonProperty("selected") private var _selected: String? by delegate(null) - @get:JsonIgnore - var selected: Account? by backingDelegate(getter = { - entries[_selected] - }, setter = { - _selected = it?.id - }) + @get:JsonIgnore var selected: Account? by backingDelegate(getter = { entries[_selected] }, setter = { _selected = it?.id }) override fun toString(): String { diff --git a/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfile.kt b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfile.kt new file mode 100644 index 000000000..d640ca7b7 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfile.kt @@ -0,0 +1,36 @@ +package de.bixilon.minosoft.config.profile.profiles.rendering + +import de.bixilon.minosoft.config.profile.profiles.Profile +import de.bixilon.minosoft.config.profile.profiles.rendering.RenderingProfileManager.delegate +import de.bixilon.minosoft.config.profile.profiles.rendering.RenderingProfileManager.latestVersion +import de.bixilon.minosoft.config.profile.profiles.rendering.advanced.AdvancedC + +/** + * Profile for general rendering + */ +class RenderingProfile( + description: String? = null, +) : Profile { + override var initializing: Boolean = true + private set + override var saved: Boolean = true + override val version: Int = latestVersion + override val description by delegate(description ?: "") + + /** + * Enabled or disables the whole rendering subsystem + * Does skip the loading of audio. Exits the rendering if disabled + */ + var enabled by delegate(true) + + val advanced = AdvancedC() + + + override fun toString(): String { + return RenderingProfileManager.getName(this) + } + + init { + initializing = false + } +} diff --git a/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfileManager.kt b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfileManager.kt new file mode 100644 index 000000000..f3d116d62 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfileManager.kt @@ -0,0 +1,36 @@ +package de.bixilon.minosoft.config.profile.profiles.rendering + +import com.google.common.collect.HashBiMap +import de.bixilon.minosoft.config.profile.GlobalProfileManager +import de.bixilon.minosoft.config.profile.ProfileManager +import de.bixilon.minosoft.modding.event.master.GlobalEventMaster +import de.bixilon.minosoft.util.KUtil.toResourceLocation +import de.bixilon.minosoft.util.KUtil.unsafeCast +import java.util.concurrent.locks.ReentrantLock + +object RenderingProfileManager : ProfileManager { + override val namespace = "minosoft:rendering".toResourceLocation() + override val latestVersion = 1 + override val saveLock = ReentrantLock() + override val profileClass = RenderingProfile::class.java + + + override var currentLoadingPath: String? = null + override val profiles: HashBiMap = HashBiMap.create() + + override var selected: RenderingProfile = null.unsafeCast() + set(value) { + field = value + GlobalProfileManager.selectProfile(this, value) + GlobalEventMaster.fireEvent(RenderingProfileSelectEvent(value)) + } + + override fun createDefaultProfile(name: String): RenderingProfile { + currentLoadingPath = name + val profile = RenderingProfile("Default rendering profile") + currentLoadingPath = null + profiles[name] = profile + + return profile + } +} diff --git a/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfileSelectEvent.kt b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfileSelectEvent.kt new file mode 100644 index 000000000..57322b3be --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/RenderingProfileSelectEvent.kt @@ -0,0 +1,7 @@ +package de.bixilon.minosoft.config.profile.profiles.rendering + +import de.bixilon.minosoft.modding.event.events.Event + +class RenderingProfileSelectEvent( + val profile: RenderingProfile, +) : Event diff --git a/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/advanced/AdvancedC.kt b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/advanced/AdvancedC.kt new file mode 100644 index 000000000..3ff2d6e82 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/config/profile/profiles/rendering/advanced/AdvancedC.kt @@ -0,0 +1,14 @@ +package de.bixilon.minosoft.config.profile.profiles.rendering.advanced + +import de.bixilon.minosoft.config.profile.profiles.rendering.RenderingProfileManager.delegate + +class AdvancedC { + + /** + * Sets the window swap interval (vsync) + * 0 means vsync disabled + * Every value above 0 means 1/x * + * Must not be negative + */ + var swapInterval by delegate(1) { check(it >= 0) { "Swap interval must not be negative!" } } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index 06e7f2147..6439e7f41 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -149,7 +149,7 @@ class RenderWindow( Log.log(LogMessageType.RENDERING_LOADING) { "Creating window..." } val stopwatch = Stopwatch() - window.init() + window.init(connection.profiles.rendering) window.setDefaultIcon(connection.assetsManager) inputHandler.camera.init(this) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/window/BaseWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/window/BaseWindow.kt index 73b6b8bd8..022593bf4 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/window/BaseWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/window/BaseWindow.kt @@ -13,8 +13,9 @@ package de.bixilon.minosoft.gui.rendering.system.window -import de.bixilon.minosoft.Minosoft import de.bixilon.minosoft.config.StaticConfiguration +import de.bixilon.minosoft.config.profile.change.listener.SimpleChangeListener.Companion.listenRendering +import de.bixilon.minosoft.config.profile.profiles.rendering.RenderingProfile import de.bixilon.minosoft.data.assets.AssetsManager import de.bixilon.minosoft.util.KUtil.toResourceLocation import de.matthiasmann.twl.utils.PNGDecoder @@ -44,9 +45,9 @@ interface BaseWindow { val time: Double - fun init() { + fun init(profile: RenderingProfile) { resizable = true - swapInterval = Minosoft.config.config.game.other.swapInterval + profile.advanced::swapInterval.listenRendering(this, true, profile) { swapInterval = it } if (!StaticConfiguration.DEBUG_MODE) { cursorMode = CursorModes.DISABLED @@ -54,6 +55,8 @@ interface BaseWindow { size = DEFAULT_WINDOW_SIZE minSize = DEFAULT_MINIMUM_WINDOW_SIZE maxSize = DEFAULT_MAXIMUM_WINDOW_SIZE + + } fun destroy() diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/window/GLFWWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/window/GLFWWindow.kt index 0a15aa9aa..5788779ec 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/window/GLFWWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/window/GLFWWindow.kt @@ -15,6 +15,7 @@ package de.bixilon.minosoft.gui.rendering.system.window import de.bixilon.minosoft.Minosoft import de.bixilon.minosoft.config.key.KeyCodes +import de.bixilon.minosoft.config.profile.profiles.rendering.RenderingProfile import de.bixilon.minosoft.gui.rendering.modding.events.ResizeWindowEvent import de.bixilon.minosoft.gui.rendering.modding.events.WindowCloseEvent import de.bixilon.minosoft.gui.rendering.modding.events.WindowFocusChangeEvent @@ -127,7 +128,7 @@ class GLFWWindow( field = value } - override fun init() { + override fun init(profile: RenderingProfile) { GLFWErrorCallback.createPrint(System.err).set() check(glfwInit()) { "Unable to initialize GLFW" } @@ -148,7 +149,7 @@ class GLFWWindow( glfwMakeContextCurrent(window) - super.init() + super.init(profile) val primaryMonitor = glfwGetPrimaryMonitor() if (primaryMonitor != MemoryUtil.NULL) {