Skip to content

Commit

Permalink
refactor and tidy, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nextdayy committed Nov 6, 2024
1 parent 755dacf commit 676d93a
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 308 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,30 @@ fun interface Visualizer {
val options: Array<String> = prop.getMetadata("options") ?: emptyArray()
if (prop.type.isEnum) {
require(options.isEmpty()) { "Dropdowns should not have options when used with enums (offender=${prop.id})" }
val index = prop.type.enumConstants.indexOf(prop.get())
val constants = prop.type.enumConstants
val index = constants.indexOf(prop.get())
return Dropdown(
optPadding = 24f,
initial = index,
entries = prop.type.enumConstants.mapToArray {
entries = constants.mapToArray {
it as Enum<*>
null to (it::class.java.fields[0].get(it) as? String ?: it.name)
},
)
).onChange { i: Int ->
prop.setAs(constants[i])
false
}
} else {
require(prop.type == Int::class.java) { "Dropdowns can only be used with enums or integers (offender=${prop.id}, type=${prop.type})" }
require(options.size >= 2) { "Dropdowns must have at least two options (offender=${prop.id})" }
return Dropdown(
optPadding = 24f,
initial = prop.getAs(),
entries = options.mapToArray { null to it },
)
).onChange { i: Int ->
prop.setAs(i)
false
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public final EventHandler<E> register() {

@ApiStatus.Internal
public final boolean onError() {
return !(errors++ > ERROR_THRESHOLD);
return errors++ > ERROR_THRESHOLD;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ import org.polyfrost.oneconfig.internal.ui.pages.ModsPage
import org.polyfrost.oneconfig.internal.ui.pages.ThemesPage
import org.polyfrost.polyui.animate.Animations
import org.polyfrost.polyui.color.rgba
import org.polyfrost.polyui.component.*
import org.polyfrost.polyui.component.Component
import org.polyfrost.polyui.component.Drawable
import org.polyfrost.polyui.component.extensions.*
import org.polyfrost.polyui.component.impl.*
import org.polyfrost.polyui.data.Cursor
import org.polyfrost.polyui.data.PolyImage
import org.polyfrost.polyui.event.Event
import org.polyfrost.polyui.operations.Move
import org.polyfrost.polyui.operations.Recolor
import org.polyfrost.polyui.data.Cursor
import org.polyfrost.polyui.data.PolyImage
import org.polyfrost.polyui.unit.Align
import org.polyfrost.polyui.unit.Vec2
import org.polyfrost.polyui.unit.seconds
Expand All @@ -59,149 +60,154 @@ object OneConfigUI {
private val searchNoneFound = Text("oneconfig.search.nonefound", fontSize = 16f)
private val search = Group(searchNoneFound, visibleSize = Vec2(1130f, 635f))

lateinit var ui: Drawable
private lateinit var ui: Drawable
private var window: Any? = null


@JvmOverloads
fun open(initialScreen: Component = ModsPage(ConfigManager.active().trees())) {
val builder = OCPolyUIBuilder.create().blurs().atResolution(1920f, 1080f)
.backgroundColor(rgba(21, 21, 21)).size(1400f, 700f) as OCPolyUIBuilder
builder.translatorDelegate("assets/oneconfig")
builder.onClose { _ ->
for (t in ConfigManager.active().trees()) {
ConfigManager.active().save(t)
if (window == null) {
val builder = OCPolyUIBuilder.create().blurs().atResolution(1920f, 1080f)
.backgroundColor(rgba(21, 21, 21)).size(1400f, 700f) as OCPolyUIBuilder
builder.translatorDelegate("assets/oneconfig")
builder.onClose { _ ->
for (t in ConfigManager.active().trees()) {
ConfigManager.active().save(t)
}
}
}
builder.makeAndOpen(
Group(
Block(
size = Vec2(225f, 32f),
).ignoreLayout().afterParentInit {
val modsBtn = parent[3]
Move(this, modsBtn.x, modsBtn.y, false).add()
},
Image("assets/oneconfig/brand/oneconfig.svg".image()).named("Logo"),
Text("oneconfig.sidebar.title.options", fontSize = 11f).setPalette { text.secondary }.padded(0f, 24f, 0f, 0f),
SidebarButton(
"assets/oneconfig/ico/settings.svg".image(),
"oneconfig.mods",
).onClick { openPage(ModsPage(ConfigManager.active().trees()), "oneconfig.mods") },
SidebarButton(
"assets/oneconfig/ico/profiles.svg".image(),
"oneconfig.profiles",
).disable().addHoverInfo(Text("this feature is experimental and is coming soon!")),
SidebarButton("assets/oneconfig/ico/keyboard.svg".image(), "oneconfig.keybinds").disable(),
Text("oneconfig.sidebar.title.personal", fontSize = 11f).setPalette { text.secondary }.padded(0f, 12f, 0f, 0f),
SidebarButton("assets/oneconfig/ico/paintbrush.svg".image(), "oneconfig.themes", label("oneconfig.soon")).onClick {
openPage(ThemesPage(), "oneconfig.themes")
}.disable(),
SidebarButton("assets/oneconfig/ico/cog.svg".image(), "oneconfig.preferences"),
Text("oneconfig.sidebar.title.extra", fontSize = 11f).setPalette { text.secondary }.padded(0f, 12f, 0f, 0f),
SidebarButton("assets/oneconfig/ico/refresh.svg".image(), "oneconfig.changelog"),
SidebarButton(
"assets/oneconfig/ico/text.svg".image(), "oneconfig.feedback"
).onClick { openPage(FeedbackPage(), "oneconfig.feedback") },
SidebarButton0(
"assets/oneconfig/ico/hud.svg".image(),
"oneconfig.edithud",
label("oneconfig.beta")
).onClick {
Platform.screen().display(HudManager.getWithEditor())
}.padded(0f, 200f, 0f, 0f),
size = Vec2(273f, 700f),
alignment = Align(mode = Align.Mode.Vertical, pad = Vec2(6f, 8f)),
).named("Sidebar"),
Group(
val (polyUI, win) = builder.makeAndOpenWithRef(
Group(
Block(
size = Vec2(225f, 32f),
).ignoreLayout().afterParentInit {
val modsBtn = parent[3]
Move(this, modsBtn.x, modsBtn.y, false).add()
},
Image("assets/oneconfig/brand/oneconfig.svg".image()).named("Logo"),
Text("oneconfig.sidebar.title.options", fontSize = 11f).setPalette { text.secondary }.padded(0f, 24f, 0f, 0f),
SidebarButton(
"assets/oneconfig/ico/settings.svg".image(),
"oneconfig.mods",
).onClick { openPage(ModsPage(ConfigManager.active().trees()), "oneconfig.mods") },
SidebarButton(
"assets/oneconfig/ico/profiles.svg".image(),
"oneconfig.profiles",
).disable().addHoverInfo(Text("this feature is experimental and is coming soon!")),
SidebarButton("assets/oneconfig/ico/keyboard.svg".image(), "oneconfig.keybinds").disable(),
Text("oneconfig.sidebar.title.personal", fontSize = 11f).setPalette { text.secondary }.padded(0f, 12f, 0f, 0f),
SidebarButton("assets/oneconfig/ico/paintbrush.svg".image(), "oneconfig.themes", label("oneconfig.soon")).onClick {
openPage(ThemesPage(), "oneconfig.themes")
}.disable(),
SidebarButton("assets/oneconfig/ico/cog.svg".image(), "oneconfig.preferences"),
Text("oneconfig.sidebar.title.extra", fontSize = 11f).setPalette { text.secondary }.padded(0f, 12f, 0f, 0f),
SidebarButton("assets/oneconfig/ico/refresh.svg".image(), "oneconfig.changelog"),
SidebarButton(
"assets/oneconfig/ico/text.svg".image(), "oneconfig.feedback"
).onClick { openPage(FeedbackPage(), "oneconfig.feedback") },
SidebarButton0(
"assets/oneconfig/ico/hud.svg".image(),
"oneconfig.edithud",
label("oneconfig.beta")
).onClick {
Platform.screen().display(HudManager.getWithEditor())
}.padded(0f, 200f, 0f, 0f),
size = Vec2(273f, 700f),
alignment = Align(mode = Align.Mode.Vertical, pad = Vec2(6f, 8f)),
).named("Sidebar"),
Group(
Group(
Image("assets/oneconfig/ico/left-arrow.svg".image()).named("Back").disable(),
Image("assets/oneconfig/ico/right-arrow.svg".image()).named("Forward").disable(),
Text(
"oneconfig.mods",
fontSize = 24f,
).setFont { medium }.named("Current"),
alignment = Align(pad = Vec2(16f, 8f)),
).named("Controls"),
Group(
Group(
Image("assets/oneconfig/ico/cloud.svg".image()),
Image(
"assets/oneconfig/ico/bell.svg".image(),
),
Image(playerHead).radius(6f).named("ProfileImage").withBoarder(
rgba(255, 255, 255, 0.14f),
width = 1f,
).addHoverInfo(Text(Platform.player().playerName.ifEmpty { "Steve" })),
Image("assets/oneconfig/ico/left-arrow.svg".image()).named("Back").disable(),
Image("assets/oneconfig/ico/right-arrow.svg".image()).named("Forward").disable(),
Text(
"oneconfig.mods",
fontSize = 24f,
).setFont { medium }.named("Current"),
alignment = Align(pad = Vec2(16f, 8f)),
).named("Controls"),
Group(
Group(
Image("assets/oneconfig/ico/cloud.svg".image()),
Image(
"assets/oneconfig/ico/bell.svg".image(),
),
Image(playerHead).radius(6f).named("ProfileImage").withBoarder(
rgba(255, 255, 255, 0.14f),
width = 1f,
).addHoverInfo(Text(Platform.player().playerName.ifEmpty { "Steve" })),
alignment = Align(pad = Vec2(16f, 8f)),
),
Block(
Image("assets/oneconfig/ico/search.svg".image()),
TextInput(
placeholder = "oneconfig.search.placeholder",
visibleSize = Vec2(210f, 12f),
).onChange { text: String ->
if (text.length > 2) {
search.children?.clear()
search.children?.addAll(ConfigVisualizer.INSTANCE.getMatching(text))
if (search.children?.size == 0) search.children?.add(searchNoneFound)
if (ui[1][1] !== search) openPage(search, "oneconfig.search")
search.recalculate()
} else if (ui[1][1] === search) {
openPage(ModsPage(ConfigManager.active().trees()), "oneconfig.mods")
}
false
},
size = Vec2(256f, 32f),
alignment = Align(pad = Vec2(10f, 8f)),
).named("SearchField"),
Image(
"assets/oneconfig/ico/close.svg".image(),
).named("Close").onClick {
}.withStates().setDestructivePalette(),
alignment = Align(pad = Vec2(24f, 8f)),
),
Block(
Image("assets/oneconfig/ico/search.svg".image()),
TextInput(
placeholder = "oneconfig.search.placeholder",
visibleSize = Vec2(210f, 12f),
).onChange { text: String ->
if (text.length > 2) {
search.children?.clear()
search.children?.addAll(ConfigVisualizer.INSTANCE.getMatching(text))
if (search.children?.size == 0) search.children?.add(searchNoneFound)
if (ui[1][1] !== search) openPage(search, "oneconfig.search")
search.recalculate()
} else if (ui[1][1] === search) {
openPage(ModsPage(ConfigManager.active().trees()), "oneconfig.mods")
}
false
},
size = Vec2(256f, 32f),
alignment = Align(pad = Vec2(10f, 8f)),
).named("SearchField"),
Image(
"assets/oneconfig/ico/close.svg".image(),
).named("Close").onClick {
}.withStates().setDestructivePalette(),
alignment = Align(pad = Vec2(24f, 8f)),
),
size = Vec2(1130f, 64f),
alignment = Align(main = Align.Main.SpaceBetween),
).named("Header"),
initialScreen,
size = Vec2(1127f, 700f),
alignment = Align(cross = Align.Cross.Start, pad = Vec2.ZERO),
),
).also {
ui = it.master
searchNoneFound.setup(it)
size = Vec2(1130f, 64f),
alignment = Align(main = Align.Main.SpaceBetween),
).named("Header"),
initialScreen,
size = Vec2(1127f, 700f),
alignment = Align(cross = Align.Cross.Start, pad = Vec2.ZERO),
),
)
ui = polyUI.master
window = win
searchNoneFound.setup(polyUI)
(ui as Block).radius(8f)
} else {
Platform.screen().display(window)
if (ui[1][1] != initialScreen) {
openPage(initialScreen, "oneconfig.mods")
}
}
}

fun openPage(page: Drawable, name: String) {
fun openPage(page: Component, name: String) {
val title = ui[1][0][0][2] as Text
val translated = ui.polyUI.translator.translate(name)
title.text = translated.string
ui[1][1] = page
}

fun label(text: String): Drawable {
return Block(
Text(text).setFont { bold },
alignment = Align(main = Align.Main.Center),
size = Vec2(54f, 18f),
).setPalette { brand.fg }
}
fun label(text: String) = Block(
Text(text).setFont { bold },
alignment = Align(main = Align.Main.Center),
size = Vec2(54f, 18f),
).setPalette { brand.fg }


private val sidebarBtnAlign = Align(pad = Vec2(16f, 6f))

fun SidebarButton(image: PolyImage, text: String, extra: Drawable? = null): Group {
return SidebarButton0(image, text, extra).onClick { _ ->
fun SidebarButton(image: PolyImage, text: String, extra: Drawable? = null) =
SidebarButton0(image, text, extra).onClick { _ ->
val it = parent[0]
Move(it, this.x, this.y, false, Animations.Default.create(0.15.seconds)).add()
false
}
}

fun SidebarButton0(image: PolyImage, text: String, extra: Drawable? = null): Group {
return Group(
fun SidebarButton0(image: PolyImage, text: String, extra: Drawable? = null) =
Group(
Image(image),
Text(text, fontSize = 14f),
extra,
Expand All @@ -227,5 +233,4 @@ object OneConfigUI {
false
}
}
}
}
Loading

0 comments on commit 676d93a

Please sign in to comment.