Skip to content

Commit

Permalink
links open now
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed Apr 13, 2024
1 parent b8cc34f commit ec723d4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.neptuneclient.voidui.rendering

import com.neptuneclient.voidui.utils.Font
import com.neptuneclient.voidui.utils.Image
import com.neptuneclient.voidui.widgets.objects.EdgeInsets
import com.neptuneclient.voidui.widgets.objects.Offset
import com.neptuneclient.voidui.widgets.objects.Size
import java.awt.Color
Expand Down
5 changes: 0 additions & 5 deletions src/main/kotlin/com/neptuneclient/voidui/utils/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,3 @@ fun getBufferData(path: Path, bufferSize: UInt = 1024u): ByteBuffer {
buffer.flip()
return buffer.slice()
}

/**
* Converts the string to a file path.
*/
fun String.toPath() = Path.of(this)
2 changes: 2 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Element.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ sealed class Element<S : StyleSheet>(width: LengthUnit? = null, height: LengthUn
}
eventHandler.register(MouseReleasedEvent::class) {
active = false
if (hovered())
widgetClicked()
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.neptuneclient.voidui.widgets

import com.neptuneclient.voidui.VoidUI
import com.neptuneclient.voidui.event.EventHandler
import com.neptuneclient.voidui.event.MouseClickedEvent
import com.neptuneclient.voidui.event.MouseReleasedEvent
import com.neptuneclient.voidui.units.FontSizeUnit
import com.neptuneclient.voidui.units.PercentUnit
import com.neptuneclient.voidui.units.PixelsUnit
Expand All @@ -21,7 +23,8 @@ abstract class Widget(protected val width: LengthUnit? = null, protected val hei
/**
* A shortcut to [VoidUI.eventHandler].
*/
protected lateinit var eventHandler: EventHandler
protected val eventHandler: EventHandler
get() = screen.void.eventHandler

/**
* The widget offset from screen's origin position.
Expand Down Expand Up @@ -54,9 +57,18 @@ abstract class Widget(protected val width: LengthUnit? = null, protected val hei
internal open fun init(screen: Screen, parent: Widget?) {
this.screen = screen
this.parent = parent
this.eventHandler = screen.void.eventHandler

root.init(screen, this)

eventHandler.register(MouseClickedEvent::class) {
if (hovered())
active = true
}
eventHandler.register(MouseReleasedEvent::class) {
active = false
if (hovered())
widgetClicked()
}
}

/**
Expand Down Expand Up @@ -90,6 +102,11 @@ abstract class Widget(protected val width: LengthUnit? = null, protected val hei
*/
abstract fun build(): Widget

/**
* Called if the mouse was clicked on the widget and then released on it again.
*/
open fun widgetClicked() {}

/**
* Lets you declare a property which will trigger the widget to be rebuilt once it's value changes.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/elements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.neptuneclient.voidui.utils.Image
import com.neptuneclient.voidui.widgets.objects.BoxConstraints
import com.neptuneclient.voidui.widgets.objects.Offset
import com.neptuneclient.voidui.widgets.objects.Size
import java.awt.Desktop
import java.net.URI
import java.nio.file.Path

Expand Down Expand Up @@ -122,6 +123,10 @@ class Link(private val address: URI, private val label: String? = null) : Elemen
renderer.text(offset.x, offset.y, label ?: address.toString(), font, styleSheet.color)
}

override fun widgetClicked() {
Desktop.getDesktop().browse(address)
}

}

class Image(private val path: Path, width: LengthUnit, height: LengthUnit) : Element<ImageStyleSheet>(width, height) {
Expand Down

0 comments on commit ec723d4

Please sign in to comment.