-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from NeptuneMC/stylesheets
Basic Stylesheets
- Loading branch information
Showing
12 changed files
with
210 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/com/neptuneclient/voidui/framework/State.kt
This file contains 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,27 @@ | ||
package com.neptuneclient.voidui.framework | ||
|
||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* A property delegation which rebuilds the widget tree once its value changes. | ||
* | ||
* @param initialValue The initial value of the state property. | ||
* @param onStateChange The action which is run when the value changes. See [Widget.state] for more info. | ||
*/ | ||
class State<T>(initialValue: T, private val onStateChange: (T) -> Unit) : ReadWriteProperty<Any?, T> { | ||
|
||
/** | ||
* Holds the actual value of the property. | ||
*/ | ||
var value: T = initialValue | ||
private set | ||
|
||
override fun getValue(thisRef: Any?, property: KProperty<*>) = value | ||
|
||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | ||
this.value = value | ||
onStateChange(value) | ||
} | ||
|
||
} |
This file contains 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
This file contains 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,6 @@ | ||
package com.neptuneclient.voidui.theme | ||
|
||
/** | ||
* The pure reason for this interface is, so that all stylesheet classes have the same superclass. | ||
*/ | ||
interface StyleSheet |
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/com/neptuneclient/voidui/theme/StyledWidget.kt
This file contains 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,45 @@ | ||
package com.neptuneclient.voidui.theme | ||
|
||
import com.neptuneclient.voidui.event.events.MouseMovementEvent | ||
import com.neptuneclient.voidui.framework.Screen | ||
import com.neptuneclient.voidui.framework.Widget | ||
|
||
abstract class StyledWidget<T : StyleSheet> : Widget() { | ||
|
||
private lateinit var styles: Styles<T> | ||
|
||
private var stylesheetState: T? by state(null) | ||
|
||
protected val stylesheet: T | ||
get() = stylesheetState!! | ||
|
||
override fun init(screen: Screen, parent: Widget) { | ||
try { | ||
this.voidUI = screen.voidUI | ||
this.screen = screen | ||
styles = voidUI.theme.getStyles(this::class) | ||
checkStylesheetUpdate() | ||
|
||
root = build() | ||
root!!.init(screen, this) | ||
} catch (t: Throwable) { | ||
t.printStackTrace() | ||
} | ||
|
||
registerEventAction(MouseMovementEvent::class) { | ||
//println("${hovered()} && ${stylesheetState == styles.hovered}") | ||
checkStylesheetUpdate() | ||
} | ||
} | ||
|
||
private fun checkStylesheetUpdate() { | ||
if (stylesheetState != styles.hovered && hovered()) { | ||
//println("hovered") | ||
stylesheetState = styles.hovered | ||
} else if (!hovered() && stylesheetState != styles.regular) { | ||
//println("regular") | ||
stylesheetState = styles.regular | ||
} | ||
} | ||
|
||
} |
This file contains 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,16 @@ | ||
package com.neptuneclient.voidui.theme | ||
|
||
/** | ||
* A data class which holds multiple stylesheets in different states. | ||
* | ||
* @param regular The regular style of the widget. | ||
* @param hovered The style when the widget is hovered. | ||
* @param active The style when the widget is clicked. | ||
* | ||
* TODO add transitions | ||
*/ | ||
data class Styles<T : StyleSheet>( | ||
val regular: T, | ||
val hovered: T = regular, | ||
val active: T = hovered | ||
) |
This file contains 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,8 +1,40 @@ | ||
package com.neptuneclient.voidui.theme | ||
|
||
import com.neptuneclient.voidui.theme.stylesheets.PanelStyleSheet | ||
import com.neptuneclient.voidui.ui.Panel | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Holds values which define the general style of the whole library. | ||
*/ | ||
abstract class Theme( | ||
val defaultStyles: DefaultStyles | ||
) | ||
val defaultStyles: DefaultStyles, | ||
|
||
panelStyles: Styles<PanelStyleSheet> | ||
) { | ||
|
||
/** | ||
* A map which holds every styled widget type and its adjacent styles. | ||
*/ | ||
private val styles = mapOf( | ||
panelStyles to Panel::class | ||
) | ||
|
||
/** | ||
* Returns the styles of the given styled widget. | ||
* | ||
* @param widget The widget class. | ||
* | ||
* @return The styles of the given element. | ||
* | ||
* @throws IllegalArgumentException If the given widget class has no style sheet in the registry map. | ||
*/ | ||
fun <T : StyleSheet> getStyles(widget: KClass<out StyledWidget<T>>): Styles<T> { | ||
for ((k, v) in styles) { | ||
if (v != widget) continue | ||
return k as Styles<T> | ||
} | ||
throw IllegalArgumentException("No style sheet found for type: ${widget.simpleName}") | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/neptuneclient/voidui/theme/stylesheets/PanelStyleSheet.kt
This file contains 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,12 @@ | ||
package com.neptuneclient.voidui.theme.stylesheets | ||
|
||
import com.neptuneclient.voidui.objects.Border | ||
import com.neptuneclient.voidui.objects.CornerRadius | ||
import com.neptuneclient.voidui.theme.StyleSheet | ||
import java.awt.Color | ||
|
||
data class PanelStyleSheet( | ||
val color: Color, | ||
val border: Border = Border(0f, Color(0)), | ||
val cornerRadius: CornerRadius = CornerRadius.zero, | ||
) : StyleSheet |
This file contains 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,33 @@ | ||
package com.neptuneclient.voidui.ui | ||
|
||
import com.neptuneclient.voidui.framework.Widget | ||
import com.neptuneclient.voidui.objects.EdgeInsets | ||
import com.neptuneclient.voidui.theme.StyledWidget | ||
import com.neptuneclient.voidui.theme.stylesheets.PanelStyleSheet | ||
import com.neptuneclient.voidui.widgets.Container | ||
|
||
/** | ||
* TODO | ||
* | ||
* @param child The child widget inside the panel. | ||
* @param margin The margin around the panel. | ||
* @param padding The padding inside the panel. | ||
*/ | ||
class Panel( | ||
private val child: Widget, | ||
private val margin: EdgeInsets = EdgeInsets.zero, | ||
private val padding: EdgeInsets = EdgeInsets.zero | ||
) : StyledWidget<PanelStyleSheet>() { | ||
|
||
override fun build(): Widget { | ||
return Container( | ||
color = stylesheet.color, | ||
border = stylesheet.border, | ||
cornerRadius = stylesheet.cornerRadius, | ||
margin = margin, | ||
padding = padding, | ||
child = child | ||
) | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
19 changes: 15 additions & 4 deletions
19
src/test/kotlin/com/neptuneclient/voidui/tests/TestTheme.kt
This file contains 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