Skip to content

Commit 85bcf15

Browse files
authored
HUD: Show time using primary and secondary colors (#463)
* Allow for custom separators in the TextComponent * HUD: Show time using primary and secondary colors Before this patch the time was always shown as white. Now we show the time/date's numeric characters in the primary color and the non numeric characters in the secondary color :)
1 parent c408288 commit 85bcf15

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

src/main/kotlin/com/lambda/client/gui/hudgui/AbstractLabelHud.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ abstract class AbstractLabelHud(
1818
alwaysListening: Boolean,
1919
enabledByDefault: Boolean,
2020
config: AbstractConfig<out Nameable>,
21+
separator: String = " ",
2122
) : AbstractHudElement(name, alias, category, description, alwaysListening, enabledByDefault, config) {
2223

2324
override val hudWidth: Float get() = displayText.getWidth() + 2.0f
2425
override val hudHeight: Float get() = displayText.getHeight(2)
2526

26-
protected val displayText = TextComponent()
27+
protected val displayText = TextComponent(separator)
2728

2829
init {
2930
safeAsyncListener<TickEvent.ClientTickEvent> {

src/main/kotlin/com/lambda/client/gui/hudgui/LabelHud.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal abstract class LabelHud(
1010
category: Category,
1111
description: String,
1212
alwaysListening: Boolean = false,
13-
enabledByDefault: Boolean = false
14-
) : AbstractLabelHud(name, alias, category, description, alwaysListening, enabledByDefault, GuiConfig),
13+
enabledByDefault: Boolean = false,
14+
separator: String = " ",
15+
) : AbstractLabelHud(name, alias, category, description, alwaysListening, enabledByDefault, GuiConfig, separator),
1516
SettingRegister<Component> by GuiConfig

src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/Time.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import com.lambda.client.util.TimeUtils
77
internal object Time : LabelHud(
88
name = "Time",
99
category = Category.MISC,
10-
description = "System date and time"
10+
description = "System date and time",
11+
separator = "",
1112
) {
1213

1314
private val showDate = setting("Show Date", true)
@@ -17,8 +18,15 @@ internal object Time : LabelHud(
1718
private val timeUnit = setting("Time Unit", TimeUtils.TimeUnit.H12, { showTime.value })
1819

1920
override fun SafeClientEvent.updateText() {
20-
if (showDate.value) displayText.addLine(TimeUtils.getDate(dateFormat.value))
21-
if (showTime.value) displayText.addLine(TimeUtils.getTime(timeFormat.value, timeUnit.value))
21+
if (showDate.value) {
22+
val date = TimeUtils.getDate(dateFormat.value)
23+
date.forEach { displayText.add(it.toString(), if (it.isDigit()) primaryColor else secondaryColor) }
24+
displayText.addLine("")
25+
}
26+
if (showTime.value) {
27+
val time = TimeUtils.getTime(timeFormat.value, timeUnit.value)
28+
time.forEach { displayText.add(it.toString(), if (it.isDigit()) primaryColor else secondaryColor) }
29+
}
2230
}
2331

2432
}

src/main/kotlin/com/lambda/client/util/graphics/font/TextComponent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ class TextComponent(private val separator: String = " ") {
147147
val color = textElement.color.clone()
148148
color.a = (color.a * alpha).toInt()
149149
FontRenderAdapter.drawString(textElement.text, drawShadow = drawShadow, color = color, customFont = customFont, scale = textElement.scale)
150-
val adjustedSeparator = if (separator == " " && customFont) " " else " "
150+
val adjustedSeparator = " ".repeat(if (customFont && separator != "") max(separator.length * 1, 1) else separator.length)
151151
glTranslatef(FontRenderAdapter.getStringWidth(textElement.text + adjustedSeparator, customFont = customFont), 0f, 0f)
152152
}
153153

154154
glPopMatrix()
155155
}
156156

157157
fun getWidth(customFont: Boolean = FontRenderAdapter.useCustomFont): Float {
158-
val adjustedSeparator = if (separator == " " && customFont) " " else " "
158+
val adjustedSeparator = " ".repeat(if (customFont && separator != "") max(separator.length * 1, 1) else separator.length)
159159
val string = textElementList.joinToString(separator = adjustedSeparator)
160160
return FontRenderAdapter.getStringWidth(string, customFont = customFont)
161161
}

0 commit comments

Comments
 (0)