-
Notifications
You must be signed in to change notification settings - Fork 144
Implement notifications HUD #488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
urFate
wants to merge
15
commits into
lambda-client:master
Choose a base branch
from
urFate:notifications-hud
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bf3762f
Implement notifications HUD
urFate 729deb6
Respect color scheme and text width
urFate 61af962
Notifications: Modes, scaling, and various additions
rfresh2 a9b5b8e
Code refactor
urFate 087242b
Make text scale more consistent
urFate e540b3d
Implement "Fade In" and "Fade Out" effects
urFate 264b14b
Reformat code
urFate ba169cf
Add customizable timeout depending on types
urFate b37f5ef
Fix border direction
urFate ae28bf9
Add "Bottom Left" render direction
urFate 5780b9e
add moving animation
kdh8219 79ca2b1
Implement notifications functionality to some amount of modules
urFate ea63fc7
Fix y moving animation
kdh8219 2aa958a
Fix Y moving animation
kdh8219 0a25bf1
Merge remote-tracking branch 'github/notifications-hud' into notifica…
kdh8219 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/main/kotlin/com/lambda/client/gui/hudgui/elements/client/Notifications.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.lambda.client.gui.hudgui.elements.client | ||
|
||
import com.lambda.client.gui.hudgui.HudElement | ||
import com.lambda.client.util.color.ColorHolder | ||
import com.lambda.client.util.graphics.RenderUtils2D | ||
import com.lambda.client.util.graphics.VertexHelper | ||
import com.lambda.client.util.graphics.font.FontRenderAdapter | ||
import com.lambda.client.util.math.Vec2d | ||
import com.lambda.client.util.threads.runSafe | ||
import com.lambda.client.util.threads.safeAsyncListener | ||
import net.minecraft.client.renderer.GlStateManager | ||
import net.minecraftforge.fml.common.gameevent.TickEvent | ||
|
||
internal object Notifications : HudElement( | ||
name = "Notifications", | ||
category = Category.CLIENT, | ||
description = "Shows notifications" | ||
) { | ||
|
||
private var cacheWidth = 90.0 | ||
private var cacheHeight = 23.0 | ||
|
||
override val hudWidth: Float | ||
get() = cacheWidth.toFloat() | ||
override val hudHeight: Float | ||
get() = cacheHeight.toFloat() | ||
|
||
private val notifications = mutableListOf<Notification>() | ||
|
||
init { | ||
safeAsyncListener<TickEvent.ClientTickEvent> { event -> | ||
if (event.phase != TickEvent.Phase.END) return@safeAsyncListener | ||
|
||
val removalList = notifications.filter { notification -> | ||
notification.startTime + notification.duration < System.currentTimeMillis() | ||
} | ||
notifications.removeAll(removalList) | ||
|
||
cacheHeight = if (notifications.isEmpty()) 23.0 else notifications.size * 26.0 | ||
} | ||
} | ||
|
||
override fun renderHud(vertexHelper: VertexHelper) { | ||
super.renderHud(vertexHelper) | ||
|
||
runSafe { | ||
notifications.forEachIndexed { index, notification -> | ||
GlStateManager.pushMatrix() | ||
|
||
GlStateManager.translate(0.0, index * 28.0, 0.0) | ||
drawNotification(vertexHelper, notification) | ||
|
||
GlStateManager.popMatrix() | ||
} | ||
} | ||
} | ||
|
||
private fun drawNotification(vertexHelper: VertexHelper, notification: Notification) { | ||
|
||
val color = when (notification.type) { | ||
NotificationType.INFO -> ColorHolder(3, 169, 244) | ||
NotificationType.WARNING -> ColorHolder(255, 255, 0) | ||
NotificationType.ERROR -> ColorHolder(255, 0, 0) | ||
} | ||
|
||
val startTime = notification.startTime | ||
val duration = notification.duration | ||
|
||
val currentTime = System.currentTimeMillis() | ||
val elapsedTime = currentTime - startTime | ||
val timeout = (88 * elapsedTime) / duration | ||
val timeoutBarWidth = if (timeout > 88) 88 else if (timeout < 0) 0 else timeout | ||
|
||
// Draw background | ||
RenderUtils2D.drawRectFilled(vertexHelper, Vec2d.ZERO, Vec2d(90.0, 23.0), ColorHolder(0, 0, 0, 127)) | ||
|
||
// Draw timeout bar | ||
RenderUtils2D.drawRectFilled(vertexHelper, Vec2d(timeoutBarWidth.toDouble(), 23.0), Vec2d(88.0, 22.0), color) | ||
|
||
// Draw right border | ||
RenderUtils2D.drawRectFilled(vertexHelper, Vec2d(90.0, 0.0), Vec2d(88.0, 23.0), color) | ||
|
||
// Draw text | ||
FontRenderAdapter.drawString(notification.text, 10.0f, 9.0f, true, ColorHolder(), 0.6f, true) | ||
} | ||
|
||
fun addNotification(notification: Notification) { | ||
notifications.add(notification) | ||
} | ||
} | ||
|
||
data class Notification( | ||
val text: String, | ||
val type: NotificationType, | ||
val duration: Int, | ||
val startTime: Long = System.currentTimeMillis(), | ||
) | ||
|
||
enum class NotificationType { | ||
INFO, | ||
WARNING, | ||
ERROR | ||
} |
15 changes: 11 additions & 4 deletions
15
src/main/kotlin/com/lambda/client/manager/managers/NotificationManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,30 @@ | ||
package com.lambda.client.manager.managers | ||
|
||
import com.lambda.client.gui.hudgui.elements.client.Notification | ||
import com.lambda.client.gui.hudgui.elements.client.NotificationType | ||
import com.lambda.client.gui.hudgui.elements.client.Notifications | ||
import com.lambda.client.manager.Manager | ||
import com.lambda.client.util.text.MessageSendHelper | ||
import com.lambda.client.util.threads.safeListener | ||
import net.minecraftforge.fml.common.gameevent.TickEvent | ||
import java.util.* | ||
|
||
object NotificationManager : Manager { | ||
private val pendingNotifications: Queue<String> = LinkedList() | ||
private val pendingNotifications: Queue<Notification> = LinkedList() | ||
|
||
init { | ||
safeListener<TickEvent.ClientTickEvent> { | ||
while (pendingNotifications.isNotEmpty()) { | ||
MessageSendHelper.sendErrorMessage(pendingNotifications.poll()) | ||
val notification = pendingNotifications.poll() | ||
Notifications.addNotification(notification) | ||
} | ||
} | ||
} | ||
|
||
fun registerNotification(message: String) { | ||
pendingNotifications.add(message) | ||
pendingNotifications.add(Notification(message, NotificationType.INFO, 3000)) | ||
} | ||
|
||
fun registerNotification(message: String, type: NotificationType) { | ||
pendingNotifications.add(Notification(message, type, 3000)) | ||
urFate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.