Skip to content

Commit

Permalink
completely redo how styles are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Jan 5, 2024
1 parent 3420d80 commit ce052d7
Show file tree
Hide file tree
Showing 41 changed files with 623 additions and 542 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "dev.dediamondpro"
version = "1.0-SNAPSHOT76"
version = "1.0-SNAPSHOT77"

repositories {
mavenCentral()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.dediamondpro.minemark.elementa
import dev.dediamondpro.minemark.MineMarkCore
import dev.dediamondpro.minemark.MineMarkCoreBuilder
import dev.dediamondpro.minemark.elementa.elements.*
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
import dev.dediamondpro.minemark.elements.Elements
import dev.dediamondpro.minemark.utils.MouseButton
import gg.essential.elementa.UIComponent
Expand All @@ -12,11 +13,11 @@ import gg.essential.universal.UMatrixStack

class MineMarkComponent(
markdown: String,
layoutConfig: LayoutConfigImpl = LayoutConfigImpl(),
core: MineMarkCore<LayoutConfigImpl, RenderData> = defaultCore
style: MarkdownStyle = MarkdownStyle(),
core: MineMarkCore<MarkdownStyle, RenderData> = defaultCore
) : UIComponent() {

private val parsedMarkdown = core.parse(layoutConfig, markdown).apply {
private val parsedMarkdown = core.parse(style, markdown).apply {
addLayoutCallback(this@MineMarkComponent::layoutCallback)
}

Expand Down Expand Up @@ -70,11 +71,11 @@ class MineMarkComponent(

companion object {
private val defaultCore = MineMarkCore
.builder<LayoutConfigImpl, RenderData>()
.builder<MarkdownStyle, RenderData>()
.addElementaExtensions()
.build()

fun MineMarkCoreBuilder<LayoutConfigImpl, RenderData>.addElementaExtensions(): MineMarkCoreBuilder<LayoutConfigImpl, RenderData> {
fun MineMarkCoreBuilder<MarkdownStyle, RenderData>.addElementaExtensions(): MineMarkCoreBuilder<MarkdownStyle, RenderData> {
return this.addElement(Elements.TEXT, ::MarkdownTextComponent)
.addElement(Elements.HEADING, ::MarkdownHeadingComponent)
.addElement(Elements.IMAGE, ::MarkdownImageComponent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.LayoutData
import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.LayoutStyle
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
import dev.dediamondpro.minemark.elements.ChildMovingElement
import dev.dediamondpro.minemark.elements.Element
import gg.essential.elementa.components.UIBlock
import org.xml.sax.Attributes
import java.awt.Color

class MarkdownBlockquoteComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
style: MarkdownStyle,
layoutStyle: LayoutStyle,
parent: Element<MarkdownStyle, RenderData>?,
qName: String, attributes: Attributes?
) : ChildMovingElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
) : ChildMovingElement<MarkdownStyle, RenderData>(style, layoutStyle, parent, qName, attributes) {
override fun drawMarker(x: Float, y: Float, height: Float, renderData: RenderData) {
UIBlock.drawBlockSized(
renderData.matrixStack,
Expand All @@ -28,6 +30,7 @@ class MarkdownBlockquoteComponent(
}

override fun getPadding(layoutData: LayoutData?): Float {
return layoutConfig.spacingConfig.blockQuotePadding
// TODO: give blockquote own styling
return style.getParagraphStyle().padding
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.LayoutData
import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.LayoutStyle
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
import dev.dediamondpro.minemark.elements.Element
import dev.dediamondpro.minemark.elements.impl.formatting.HeadingElement
import gg.essential.elementa.components.UIBlock
import org.xml.sax.Attributes
import java.awt.Color

class MarkdownHeadingComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
style: MarkdownStyle,
layoutStyle: LayoutStyle,
parent: Element<MarkdownStyle, RenderData>?,
qName: String, attributes: Attributes?
) : HeadingElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
) : HeadingElement<MarkdownStyle, RenderData>(style, layoutStyle, parent, qName, attributes) {
private var position: LayoutData.MarkDownLine? = null

override fun generateLayout(layoutData: LayoutData) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.LayoutData
import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.LayoutStyle
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
import dev.dediamondpro.minemark.elements.BasicElement
import dev.dediamondpro.minemark.elements.Element
import gg.essential.elementa.components.UIBlock
import org.xml.sax.Attributes
import java.awt.Color

class MarkdownHorizontalLineComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
style: MarkdownStyle,
layoutStyle: LayoutStyle,
parent: Element<MarkdownStyle, RenderData>?,
qName: String, attributes: Attributes?
) : BasicElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
) : BasicElement<MarkdownStyle, RenderData>(style, layoutStyle, parent, qName, attributes) {
override fun drawElement(x: Float, y: Float, renderData: RenderData) {
UIBlock.drawBlockSized(
renderData.matrixStack,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.LayoutStyle
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
import dev.dediamondpro.minemark.elementa.util.EmptyImage
import dev.dediamondpro.minemark.elements.Element
import dev.dediamondpro.minemark.elements.impl.ImageElement
Expand All @@ -12,10 +13,11 @@ import java.awt.image.BufferedImage
import java.util.concurrent.CompletableFuture

class MarkdownImageComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
style: MarkdownStyle,
layoutStyle: LayoutStyle,
parent: Element<MarkdownStyle, RenderData>?,
qName: String, attributes: Attributes?
) : ImageElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
) : ImageElement<MarkdownStyle, RenderData>(style, layoutStyle, parent, qName, attributes) {
private var uiImage: UIImage? = null

override fun drawImage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.LayoutData
import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.LayoutStyle
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
import dev.dediamondpro.minemark.elements.Element
import dev.dediamondpro.minemark.elements.impl.list.ListElement
import dev.dediamondpro.minemark.elements.impl.list.ListHolderElement
import org.xml.sax.Attributes

class MarkdownListElementComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
style: MarkdownStyle,
layoutStyle: LayoutStyle,
parent: Element<MarkdownStyle, RenderData>?,
qName: String, attributes: Attributes?
) : ListElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
private val fontProvider = layoutConfig.fontProvider
) : ListElement<MarkdownStyle, RenderData>(style, layoutStyle, parent, qName, attributes) {
private val fontProvider = style.textStyle.font
private var markerStr: String = when (listType) {
ListHolderElement.ListType.ORDERED -> "${elementIndex + 1}. "
ListHolderElement.ListType.UNORDERED -> ""
else -> ""
}

override fun drawMarker(x: Float, y: Float, renderData: RenderData) {
val scale = layoutConfig.fontSize
val scale = layoutStyle.fontSize
renderData.matrixStack.push()
renderData.matrixStack.scale(scale, scale, 1f)
fontProvider.drawString(
renderData.matrixStack,
markerStr,
layoutConfig.textColor,
layoutStyle.textColor,
x / scale, y / scale,
1f, 1f
)
renderData.matrixStack.pop()
}

override fun getMarkerWidth(): Float {
return fontProvider.getStringWidth(markerStr, 1f) * layoutConfig.fontSize
return fontProvider.getStringWidth(markerStr, 1f) * layoutStyle.fontSize
}

override fun getMarkerHeight(layoutData: LayoutData?): Float {
return fontProvider.getStringHeight(markerStr, 1f) * layoutConfig.fontSize
return fontProvider.getStringHeight(markerStr, 1f) * layoutStyle.fontSize
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.LayoutData
import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.LayoutStyle
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
import dev.dediamondpro.minemark.elements.Element
import dev.dediamondpro.minemark.elements.impl.TextElement
import gg.essential.universal.UResolution
import org.xml.sax.Attributes
import kotlin.math.round

class MarkdownTextComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
style: MarkdownStyle,
layoutStyle: LayoutStyle,
parent: Element<MarkdownStyle, RenderData>?,
qName: String, attributes: Attributes?
) : TextElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
private val font = layoutConfig.fontProvider
private var scale = layoutConfig.fontSize
) : TextElement<MarkdownStyle, RenderData>(style, layoutStyle, parent, qName, attributes) {
private val font = style.textStyle.font
private var scale = layoutStyle.fontSize
private var prefix = buildString {
if (layoutConfig.isBold) append("§l")
if (layoutConfig.isItalic) append("§o")
if (layoutConfig.isUnderlined) append("§n")
if (layoutConfig.isStrikethrough) append("§m")
if (layoutStyle.isBold) append("§l")
if (layoutStyle.isItalic) append("§o")
if (layoutStyle.isUnderlined) append("§n")
if (layoutStyle.isStrikethrough) append("§m")
}

override fun generateLayout(layoutData: LayoutData?) {
val mcScale = UResolution.scaleFactor.toFloat()
scale = round(layoutConfig.fontSize * mcScale) / mcScale
scale = round(layoutStyle.fontSize * mcScale) / mcScale
super.generateLayout(layoutData)
}

override fun drawText(text: String, x: Float, bottomY: Float, hovered: Boolean, renderData: RenderData) {
prefix = buildString {
if (layoutConfig.isBold) append("§l")
if (layoutConfig.isItalic) append("§o")
if (layoutConfig.isStrikethrough) append("§m")
if (layoutConfig.isUnderlined || layoutConfig.isPartOfLink && hovered) append("§n")
if (layoutStyle.isBold) append("§l")
if (layoutStyle.isItalic) append("§o")
if (layoutStyle.isStrikethrough) append("§m")
if (layoutStyle.isUnderlined || layoutStyle.isPartOfLink && hovered) append("§n")
}

val y = bottomY - getTextHeight(text)
Expand All @@ -43,7 +45,7 @@ class MarkdownTextComponent(
font.drawString(
renderData.matrixStack,
prefix + text,
layoutConfig.textColor,
layoutStyle.textColor,
x / scale, y / scale,
1f, 1f
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.dediamondpro.minemark.elementa.style

import dev.dediamondpro.minemark.elementa.util.ElementaBrowserProvider
import dev.dediamondpro.minemark.providers.DefaultImageProvider
import dev.dediamondpro.minemark.style.*
import gg.essential.elementa.font.DefaultFonts
import gg.essential.elementa.font.FontProvider
import java.awt.Color

data class MarkdownStyle @JvmOverloads constructor(
private val textStyle: MarkdownTextStyle = MarkdownTextStyle(
1f, Color.WHITE, 2f, DefaultFonts.VANILLA_FONT_RENDERER
),
private val paragraphStyle: ParagraphStyleConfig = ParagraphStyleConfig(6f),
private val linkStyle: LinkStyleConfig = LinkStyleConfig(Color(65, 105, 225), true, ElementaBrowserProvider),
private val headerStyle: HeaderStyleConfig = HeaderStyleConfig(
HeaderLevelStyleConfig(2f, 12f, true, Color(80, 80, 80), 2f, 5f),
HeaderLevelStyleConfig(1.66f, 10f, true, Color(80, 80, 80), 2f, 5f),
HeaderLevelStyleConfig(1.33f, 8f),
HeaderLevelStyleConfig(1f, 6f),
HeaderLevelStyleConfig(0.7f, 4f),
HeaderLevelStyleConfig(0.7f, 4f)
),
private val imageStyle: ImageStyleConfig = ImageStyleConfig(DefaultImageProvider.INSTANCE),
private val listStyle: ListStyleConfig = ListStyleConfig(16f, 6f)
) : Style {
override fun getTextStyle(): MarkdownTextStyle = textStyle
override fun getParagraphStyle(): ParagraphStyleConfig = paragraphStyle
override fun getLinkStyle(): LinkStyleConfig = linkStyle
override fun getHeaderStyle(): HeaderStyleConfig = headerStyle
override fun getImageStyle(): ImageStyleConfig = imageStyle
override fun getListStyle(): ListStyleConfig = listStyle
}

class MarkdownTextStyle(
defaultFontSize: Float,
defaultTextColor: Color?,
padding: Float,
val font: FontProvider
) : TextStyleConfig(defaultFontSize, defaultTextColor, padding)
Loading

0 comments on commit ce052d7

Please sign in to comment.