Skip to content

Commit

Permalink
add support for custom styles in layoutStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Jun 30, 2024
1 parent 90d2860 commit 2e827ef
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ plugins {
}

group = "dev.dediamondpro"
version = "1.2.0"
version = "1.2.1"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion minecraft/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mod Configuration
mod_name=MineMark
mod_id=minemark
mod_version=1.2.0
mod_version=1.2.1
mod_description=Markdown rendering library
mod_license=LGPL
26 changes: 23 additions & 3 deletions src/main/java/dev/dediamondpro/minemark/LayoutStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package dev.dediamondpro.minemark;

import dev.dediamondpro.minemark.style.Style;
import dev.dediamondpro.minemark.utils.StyleType;

import java.awt.*;
import java.util.HashMap;

/**
* Class that will be given to an element at parsing time
Expand All @@ -36,8 +38,9 @@ public class LayoutStyle {
private boolean partOfLink;
private boolean partOfCodeBlock;
private boolean preFormatted;
private final HashMap<StyleType<?>, Object> customStyles;

public LayoutStyle(Alignment alignment, float fontSize, Color textColor, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean partOfLink, boolean partOfCodeBlock, boolean preFormatted) {
public LayoutStyle(Alignment alignment, float fontSize, Color textColor, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean partOfLink, boolean partOfCodeBlock, boolean preFormatted, HashMap<StyleType<?>, Object> customStyles) {
this.alignment = alignment;
this.fontSize = fontSize;
this.textColor = textColor;
Expand All @@ -48,14 +51,15 @@ public LayoutStyle(Alignment alignment, float fontSize, Color textColor, boolean
this.preFormatted = preFormatted;
this.partOfCodeBlock = partOfCodeBlock;
this.strikethrough = strikethrough;
this.customStyles = customStyles;
}

public LayoutStyle(Style style) {
this(Alignment.LEFT, style.getTextStyle().getDefaultFontSize(), style.getTextStyle().getDefaultTextColor(), false, false, false, false, false, false, false);
this(Alignment.LEFT, style.getTextStyle().getDefaultFontSize(), style.getTextStyle().getDefaultTextColor(), false, false, false, false, false, false, false, new HashMap<>());
}

public LayoutStyle clone() {
return new LayoutStyle(alignment, fontSize, new Color(textColor.getRGB()), bold, italic, underlined, strikethrough, partOfLink, partOfCodeBlock, preFormatted);
return new LayoutStyle(alignment, fontSize, new Color(textColor.getRGB()), bold, italic, underlined, strikethrough, partOfLink, partOfCodeBlock, preFormatted, (HashMap<StyleType<?>, Object>) customStyles.clone());
}

public Alignment getAlignment() {
Expand Down Expand Up @@ -138,6 +142,22 @@ public void setPartOfCodeBlock(boolean partOfCodeBlock) {
this.partOfCodeBlock = partOfCodeBlock;
}

public <T> void put(StyleType<T> styleType, T value) {
customStyles.put(styleType, value);
}

public <T> boolean has(StyleType<T> styleType) {
return customStyles.containsKey(styleType);
}

public <T> T get(StyleType<T> styleType) {
return styleType.getStyleClass().cast(customStyles.get(styleType));
}

public <T> T getOrDefault(StyleType<T> styleType, T defaultValue) {
return styleType.getStyleClass().cast(customStyles.getOrDefault(styleType, defaultValue));
}

public enum Alignment {
CENTER, LEFT, RIGHT
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/dev/dediamondpro/minemark/utils/StyleType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of MineMark
* Copyright (C) 2024 DeDiamondPro
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License Version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.dediamondpro.minemark.utils;

public class StyleType<T> {
private final String id;
private final Class<T> styleClass;

public StyleType(String id, Class<T> styleClass) {
this.id = id;
this.styleClass = styleClass;
}

public String getId() {
return id;
}

public Class<T> getStyleClass() {
return styleClass;
}

public static <T> StyleType<T> of(String id, Class<T> styleClass) {
return new StyleType<T>(id, styleClass);
}
}

0 comments on commit 2e827ef

Please sign in to comment.