Skip to content

Commit 2e827ef

Browse files
committed
add support for custom styles in layoutStyle
1 parent 90d2860 commit 2e827ef

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ plugins {
2121
}
2222

2323
group = "dev.dediamondpro"
24-
version = "1.2.0"
24+
version = "1.2.1"
2525

2626
repositories {
2727
mavenCentral()

minecraft/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mod Configuration
22
mod_name=MineMark
33
mod_id=minemark
4-
mod_version=1.2.0
4+
mod_version=1.2.1
55
mod_description=Markdown rendering library
66
mod_license=LGPL

src/main/java/dev/dediamondpro/minemark/LayoutStyle.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package dev.dediamondpro.minemark;
1919

2020
import dev.dediamondpro.minemark.style.Style;
21+
import dev.dediamondpro.minemark.utils.StyleType;
2122

2223
import java.awt.*;
24+
import java.util.HashMap;
2325

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

40-
public LayoutStyle(Alignment alignment, float fontSize, Color textColor, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean partOfLink, boolean partOfCodeBlock, boolean preFormatted) {
43+
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) {
4144
this.alignment = alignment;
4245
this.fontSize = fontSize;
4346
this.textColor = textColor;
@@ -48,14 +51,15 @@ public LayoutStyle(Alignment alignment, float fontSize, Color textColor, boolean
4851
this.preFormatted = preFormatted;
4952
this.partOfCodeBlock = partOfCodeBlock;
5053
this.strikethrough = strikethrough;
54+
this.customStyles = customStyles;
5155
}
5256

5357
public LayoutStyle(Style style) {
54-
this(Alignment.LEFT, style.getTextStyle().getDefaultFontSize(), style.getTextStyle().getDefaultTextColor(), false, false, false, false, false, false, false);
58+
this(Alignment.LEFT, style.getTextStyle().getDefaultFontSize(), style.getTextStyle().getDefaultTextColor(), false, false, false, false, false, false, false, new HashMap<>());
5559
}
5660

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

6165
public Alignment getAlignment() {
@@ -138,6 +142,22 @@ public void setPartOfCodeBlock(boolean partOfCodeBlock) {
138142
this.partOfCodeBlock = partOfCodeBlock;
139143
}
140144

145+
public <T> void put(StyleType<T> styleType, T value) {
146+
customStyles.put(styleType, value);
147+
}
148+
149+
public <T> boolean has(StyleType<T> styleType) {
150+
return customStyles.containsKey(styleType);
151+
}
152+
153+
public <T> T get(StyleType<T> styleType) {
154+
return styleType.getStyleClass().cast(customStyles.get(styleType));
155+
}
156+
157+
public <T> T getOrDefault(StyleType<T> styleType, T defaultValue) {
158+
return styleType.getStyleClass().cast(customStyles.getOrDefault(styleType, defaultValue));
159+
}
160+
141161
public enum Alignment {
142162
CENTER, LEFT, RIGHT
143163
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of MineMark
3+
* Copyright (C) 2024 DeDiamondPro
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License Version 3 as published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package dev.dediamondpro.minemark.utils;
19+
20+
public class StyleType<T> {
21+
private final String id;
22+
private final Class<T> styleClass;
23+
24+
public StyleType(String id, Class<T> styleClass) {
25+
this.id = id;
26+
this.styleClass = styleClass;
27+
}
28+
29+
public String getId() {
30+
return id;
31+
}
32+
33+
public Class<T> getStyleClass() {
34+
return styleClass;
35+
}
36+
37+
public static <T> StyleType<T> of(String id, Class<T> styleClass) {
38+
return new StyleType<T>(id, styleClass);
39+
}
40+
}

0 commit comments

Comments
 (0)