Skip to content

Commit 2b0f6a8

Browse files
committed
update editor
1 parent 6ab63f7 commit 2b0f6a8

32 files changed

+1202
-344
lines changed

src/main/java/com/teammoeg/chorda/client/cui/Button.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.minecraft.client.gui.GuiGraphics;
99
import net.minecraft.client.gui.components.AbstractWidget;
1010
import net.minecraft.network.chat.Component;
11+
import net.minecraft.network.chat.ComponentContents;
1112

1213
public abstract class Button extends UIWidget {
1314
protected Component title;
@@ -31,14 +32,22 @@ public Component getTitle() {
3132

3233
public Button setTitle(Component s) {
3334
title = s;
35+
fitSize();
3436
return this;
3537
}
3638

39+
public boolean hasIcon() {
40+
return icon!=CIcons.nop();
41+
}
3742
public Button setIcon(CIcon i) {
3843
icon = i;
44+
fitSize();
3945
return this;
4046
}
41-
47+
protected void fitSize() {
48+
setWidth(parent.getFont().width(title)+((title.getContents()==ComponentContents.EMPTY&&hasIcon())?0:8) + (hasIcon() ? 20 : 0));
49+
setHeight(hasIcon() ?20:16);
50+
}
4251
private int getTextureY() {
4352
int i = 1;
4453
if (!this.isEnabled()) {

src/main/java/com/teammoeg/chorda/client/cui/Layer.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ public void render(GuiGraphics graphics, int x, int y, int w, int h) {
198198
//graphics.pose().pushPose();
199199
//graphics.pose().translate(0, 0, 1);
200200
for(UIWidget elm:elements) {
201-
drawElement(graphics, elm, contentX, contentY, w, h);
201+
if(elm.isVisible())
202+
drawElement(graphics, elm, contentX, contentY, w, h);
202203
}
203204

204205
if(scissorEnabled)
@@ -218,8 +219,21 @@ public void drawBackground(GuiGraphics graphics, int x, int y, int w, int h) {
218219
}
219220

220221
public void drawElement(GuiGraphics graphics, UIWidget element, int x, int y, int w, int h) {
222+
int childX=element.getX()+x;
223+
int childY=element.getY()+y;
224+
int childW=element.getWidth();
225+
int childH=element.getHeight();
221226

222-
element.render(graphics, element.getX()+x, element.getY()+y, element.getWidth(), element.getHeight());
227+
if(scissorEnabled) {
228+
if(childX+childW<x&&childY+childH<y) {
229+
return;
230+
}else if(childX>x+w&&childY>y+h) {
231+
return;
232+
}
233+
234+
235+
}
236+
element.render(graphics, childX, childY, childW, childH);
223237
}
224238

225239
@Override

src/main/java/com/teammoeg/chorda/client/cui/ScrollBar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public boolean onMousePressed(MouseButton button) {
5858
int scrollBarSize=getScrollBarSize();
5959
if(isVertical) {
6060
int barPos=(int) (lerpValue(height - scrollBarSize-2)+1);
61-
System.out.println(getMouseY()+"/"+barPos+"("+height);
61+
//System.out.println(getMouseY()+"/"+barPos+"("+height);
6262
if(getMouseY()<barPos) {
6363
this.setValue(this.getValue()-getPage());
6464
}else if(getMouseY()>barPos+scrollBarSize) {

src/main/java/com/teammoeg/chorda/client/cui/TextButton.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,14 @@
1717
public abstract class TextButton extends Button {
1818
public TextButton(UIWidget panel, Component txt, CIcon icon) {
1919
super(panel, txt, icon);
20-
setWidth(panel.getFont().width(txt)+((txt.getContents()==ComponentContents.EMPTY&&hasIcon())?0:8) + (hasIcon() ? 20 : 0));
21-
setHeight(20);
22-
}
23-
24-
@Override
25-
public TextButton setTitle(Component txt) {
26-
super.setTitle(txt);
27-
setWidth(getFont().width(getTitle()) + (hasIcon() ? 28 : 8));
28-
return this;
20+
fitSize();
2921
}
3022

3123
public boolean renderTitleInCenter() {
3224
return false;
3325
}
3426

3527

36-
public boolean hasIcon() {
37-
return icon!=CIcons.nop();
38-
}
3928

4029
@Override
4130
public void getTooltip(Consumer<Component> list) {

src/main/java/com/teammoeg/chorda/client/icon/IconEditor.java

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.teammoeg.frostedheart.content.research.gui.editor.BaseEditDialog;
2222
import com.teammoeg.frostedheart.content.research.gui.editor.EditListDialog;
2323
import com.teammoeg.frostedheart.content.research.gui.editor.EditPrompt;
24-
import com.teammoeg.frostedheart.content.research.gui.editor.EditUtils;
24+
import com.teammoeg.frostedheart.content.research.gui.editor.ResearchEditUtils;
2525
import com.teammoeg.frostedheart.content.research.gui.editor.Editor;
2626
import com.teammoeg.frostedheart.content.research.gui.editor.EditorSelector;
2727
import com.teammoeg.frostedheart.content.research.gui.editor.IngredientEditor;
@@ -41,41 +41,23 @@ public abstract class IconEditor<T extends CIcon> extends BaseEditDialog {
4141
public static final Editor<TextureIcon> TEXTURE_EDITOR = (p, l, v, c) -> EditPrompt.TEXT_EDITOR.open(p, l, v == null ? "" : (v.rl == null ? "" : v.rl.toString()),
4242
e -> c.accept(new TextureIcon(new ResourceLocation(e))));
4343
public static final Editor<IngredientIcon> INGREDIENT_EDITOR = (p, l, v, c) -> IngredientEditor.EDITOR_INGREDIENT_EXTERN.open(p, l, v == null ? null : v.igd,
44-
e -> c.accept(new IngredientIcon(e))); public static final Editor<CIcon> EDITOR = (p, l, v, c) -> {
45-
if (v == null || v instanceof NopIcon) {
46-
new EditorSelector<>(p, l, c).addEditor("Empty", IconEditor.NOP_EDITOR)
47-
.addEditor("ItemStack", IconEditor.ITEM_EDITOR).addEditor("Texture", IconEditor.TEXTURE_EDITOR)
48-
.addEditor("Texture with UV", IconEditor.UV_EDITOR).addEditor("Text", IconEditor.TEXT_EDITOR)
49-
.addEditor("Ingredient", IconEditor.INGREDIENT_EDITOR)
50-
.addEditor("IngredientWithSize", IconEditor.INGREDIENT_SIZE_EDITOR)
51-
.addEditor("Internal", IconEditor.INTERNAL_EDITOR)
52-
.addEditor("Combined", IconEditor.COMBINED_EDITOR)
53-
.addEditor("Animated", IconEditor.ANIMATED_EDITOR).open();
54-
} else
55-
new EditorSelector<>(p, l, (o, t) -> true, v, c).addEditor("Edit", IconEditor.CHANGE_EDITOR)
56-
.addEditor("New", IconEditor.NOP_CHANGE_EDITOR).open();
57-
};
44+
e -> c.accept(new IngredientIcon(e)));
45+
public static final Editor<CIcon> EDITOR =
46+
new EditorSelector.EditorSelectorBuilder<CIcon>(v->v==null||v==CIcons.nop())
47+
.addEditorWhenEmpty("Empty", IconEditor.NOP_EDITOR)
48+
.addEditor("ItemStack", IconEditor.ITEM_EDITOR,v->v.getClass()==ItemIcon.class)
49+
.addEditor("Texture", IconEditor.TEXTURE_EDITOR,v->v.getClass()==TextureIcon.class)
50+
.addEditor("Texture with UV", IconEditor.UV_EDITOR,v->v.getClass()==TextureIcon.class)
51+
.addEditor("Text", IconEditor.TEXT_EDITOR,v->v.getClass()==TextIcon.class)
52+
.addEditor("Ingredient", IconEditor.INGREDIENT_EDITOR,v->v.getClass()==IngredientIcon.class)
53+
.addEditorWhenEmpty("IngredientWithSize", IconEditor.INGREDIENT_SIZE_EDITOR)
54+
.addEditor("Internal", IconEditor.INTERNAL_EDITOR,v->v instanceof FHDelegateIcon)
55+
.addEditor("Combined", IconEditor.COMBINED_EDITOR,v->v.getClass()== CombinedIcon.class)
56+
.addEditor("Animated", IconEditor.ANIMATED_EDITOR,v->v.getClass()== AnimatedIcon.class)
57+
.build();
58+
59+
;
5860
public static final Editor<CIcon> INGREDIENT_SIZE_EDITOR = (p, l, v, c) -> IngredientEditor.EDITOR.open(p, l, null, e -> c.accept(CIcons.getIcon(e.getFirst(),e.getSecond())));
59-
public static final Editor<CIcon> CHANGE_EDITOR = (p, l, v, c) -> {
60-
if (v instanceof ItemIcon) {
61-
IconEditor.ITEM_EDITOR.open(p, l, (ItemIcon) v, c::accept);
62-
} else if (v instanceof CombinedIcon) {
63-
IconEditor.COMBINED_EDITOR.open(p, l, (CombinedIcon) v, c::accept);
64-
} else if (v instanceof IngredientIcon) {
65-
IconEditor.INGREDIENT_EDITOR.open(p, l, (IngredientIcon) v, c::accept);
66-
} else if (v instanceof AnimatedIcon) {
67-
IconEditor.ANIMATED_EDITOR.open(p, l, (AnimatedIcon) v, c::accept);
68-
} else if (v instanceof TextureIcon) {
69-
IconEditor.TEXTURE_EDITOR.open(p, l, (TextureIcon) v, c::accept);
70-
} else if (v instanceof TextureUVIcon) {
71-
IconEditor.UV_EDITOR.open(p, l, (TextureUVIcon) v, e -> c.accept(v));
72-
} else if (v instanceof TextIcon) {
73-
IconEditor.TEXT_EDITOR.open(p, l, (TextIcon) v, c::accept);
74-
} else if (v instanceof FHDelegateIcon) {
75-
IconEditor.INTERNAL_EDITOR.open(p, l, (FHDelegateIcon) v, c::accept);
76-
} else
77-
IconEditor.NOP_CHANGE_EDITOR.open(p, l, v, c);
78-
};
7961
public static final Editor<TextIcon> TEXT_EDITOR = (p, l, v, c) -> EditPrompt.TEXT_EDITOR.open(p, l, v == null ? null : v.text.getString(), e -> c.accept(new TextIcon(StringTextComponentParser.parse(e))));
8062
public static final Editor<CIcon> NOP_EDITOR = (p, l, v, c) -> {
8163
c.accept(NopIcon.INSTANCE);
@@ -109,7 +91,7 @@ public Combined(UIWidget panel, Component label, CombinedIcon v, Consumer<Combin
10991

11092
@Override
11193
public void addUIElements() {
112-
add(EditUtils.getTitle(this, label));
94+
add(ResearchEditUtils.getTitle(this, label));
11395
add(new OpenEditorButton<>(this, Components.str("Edit base icon"), EDITOR, v.large, e -> v.large = e));
11496
add(new OpenEditorButton<>(this, Components.str("Edit corner icon"), EDITOR, v.small, e -> v.small = e));
11597
}
@@ -149,7 +131,7 @@ public UV(UIWidget panel, Component label, TextureUVIcon v, Consumer<TextureUVIc
149131

150132
@Override
151133
public void addUIElements() {
152-
add(EditUtils.getTitle(this, label));
134+
add(ResearchEditUtils.getTitle(this, label));
153135
add(rl);
154136
add(x);
155137
add(y);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.teammoeg.chorda.config;
2+
3+
import java.io.File;
4+
5+
import com.mojang.serialization.Codec;
6+
7+
import net.minecraftforge.fml.loading.FMLPaths;
8+
/**
9+
* Represent a subfolder with handful of configuration files
10+
* */
11+
public record ConfigFileType<T>(Codec<T> codec,File folder) {
12+
public ConfigFileType(Codec<T> codec,String subfolder) {
13+
this(codec,new File(FMLPaths.CONFIGDIR.get().toFile(),subfolder));
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return folder.getName() ;
19+
}
20+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.teammoeg.chorda.util;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.LinkedHashMap;
6+
import java.util.Map;
7+
import java.util.Map.Entry;
8+
import java.util.function.Consumer;
9+
10+
import javax.annotation.Nullable;
11+
12+
import org.apache.commons.lang3.mutable.MutableObject;
13+
14+
import com.google.gson.Gson;
15+
import com.google.gson.GsonBuilder;
16+
import com.google.gson.JsonElement;
17+
import com.google.gson.JsonParser;
18+
import com.mojang.serialization.JsonOps;
19+
import com.teammoeg.chorda.config.ConfigFileType;
20+
import com.teammoeg.chorda.io.CodecUtil;
21+
import com.teammoeg.chorda.io.FileUtil;
22+
import com.teammoeg.frostedheart.FHMain;
23+
24+
public class ConfigFileUtil {
25+
private static Gson gs = new GsonBuilder().setPrettyPrinting().create();
26+
27+
public ConfigFileUtil() {
28+
}
29+
30+
@Nullable
31+
public static <T> T load(ConfigFileType<T> c, String name) {
32+
File f = new File(c.folder(), name + ".json");
33+
if (f.exists())
34+
try {
35+
JsonElement je = JsonParser.parseString(FileUtil.readString(f));
36+
return c.codec().parse(JsonOps.INSTANCE, je).resultOrPartial(FHMain.LOGGER::error).orElse(null);
37+
} catch (IOException e) {
38+
FHMain.LOGGER.error("Cannot load data " + c + ":" + f.getName() + ": " + e.getMessage());
39+
}
40+
return null;
41+
}
42+
43+
public static <T> Map<String, T> loadAll(ConfigFileType<T> c) {
44+
FHMain.LOGGER.info("loading " + c + " data from files...");
45+
Map<String, T> list = new LinkedHashMap<>();
46+
MutableObject<String> sk = new MutableObject<>();
47+
Consumer<T> addToList = t -> list.put(sk.getValue(), t);
48+
if (c.folder().exists())
49+
for (File f : c.folder().listFiles((dir, name) -> name.endsWith(".json"))) {
50+
try {
51+
JsonElement je = JsonParser.parseString(FileUtil.readString(f));
52+
String id = f.getName();
53+
id = id.substring(0, id.length() - 5);
54+
sk.setValue(id);
55+
c.codec().parse(JsonOps.INSTANCE, je).resultOrPartial(FHMain.LOGGER::error).ifPresent(addToList);
56+
} catch (Exception e) {
57+
e.printStackTrace();
58+
FHMain.LOGGER.warn("Cannot load data " + f.getName() + ": " + e.getMessage());
59+
}
60+
61+
}
62+
return list;
63+
}
64+
65+
public static <T> void load(ConfigFileType<T> c, String name, T data) {
66+
c.folder().mkdirs();
67+
68+
File out = new File(c.folder(), name + ".json");
69+
try {
70+
FileUtil.transfer(gs.toJson(CodecUtil.encodeOrThrow(c.codec().encodeStart(JsonOps.INSTANCE, data))),
71+
out);
72+
} catch (IOException e) {
73+
74+
throw new RuntimeException("Cannot save data " + c + ":" + name + ": " + e.getMessage());
75+
}
76+
}
77+
78+
public static <T> void saveAll(ConfigFileType<T> c, Map<String, T> data) {
79+
c.folder().mkdirs();
80+
81+
for (Entry<String, T> r : data.entrySet()) {
82+
File out = new File(c.folder(), r.getKey() + ".json");
83+
try {
84+
FileUtil.transfer(
85+
gs.toJson(CodecUtil.encodeOrThrow(c.codec().encodeStart(JsonOps.INSTANCE, r.getValue()))), out);
86+
} catch (IOException e) {
87+
throw new RuntimeException("Cannot save data " + c + ":" + r.getKey() + ": " + e.getMessage());
88+
}
89+
90+
}
91+
}
92+
}

src/main/java/com/teammoeg/frostedheart/content/research/FHResearch.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import net.minecraft.world.level.storage.LevelResource;
5151
import net.minecraftforge.api.distmarker.Dist;
5252
import net.minecraftforge.common.MinecraftForge;
53+
import net.minecraftforge.common.util.Lazy;
5354
import net.minecraftforge.fml.DistExecutor;
5455
import net.minecraftforge.fml.loading.FMLPaths;
5556
import net.minecraftforge.network.PacketDistributor.PacketTarget;
@@ -77,7 +78,7 @@ public class FHResearch {
7778
/**
7879
* Cache for all Researches.
7980
*/
80-
private static OptionalLazy<List<Research>> allResearches = OptionalLazy.of(() -> researches.all());
81+
private static Lazy<List<Research>> allResearches = Lazy.of(() -> researches.all());
8182

8283
public static void clearAll() {
8384
//clues.clear();
@@ -87,8 +88,7 @@ public static void clearAll() {
8788

8889
// clear cache when modification applied
8990
public static void clearCache() {
90-
if (allResearches.isResolved())
91-
allResearches = OptionalLazy.of(() -> researches.all());
91+
allResearches = Lazy.of(() -> researches.all());
9292
}
9393

9494
/**
@@ -114,7 +114,7 @@ public static void finishReload() {
114114
}
115115

116116
public static List<Research> getAllResearch() {
117-
return allResearches.resolve().get();
117+
return allResearches.get();
118118
}
119119

120120
/*public static Clue getClue(int id) {
@@ -388,8 +388,8 @@ public static void register(Research t) {
388388
// called after reload
389389
public static void reindex() {
390390
try {
391-
allResearches.orElse(Collections.emptyList()).forEach(Research::doReindex);
392-
allResearches.orElse(Collections.emptyList()).forEach(Research::doIndex);
391+
allResearches.get().forEach(Research::doReindex);
392+
allResearches.get().forEach(Research::doIndex);
393393
} catch (Throwable t) {
394394
t.printStackTrace();
395395
}

src/main/java/com/teammoeg/frostedheart/content/research/ResearchUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,5 @@ public static void refreshResearchGui() {
3434
bs.refreshWidgets();
3535
}
3636
}
37-
ClientUtils.mc().getLanguageManager();
3837
}
3938
}

src/main/java/com/teammoeg/frostedheart/content/research/data/ClientResearchData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.teammoeg.frostedheart.content.research.research.Research;
2323

2424
public class ClientResearchData {
25-
public static Research last;
25+
public static String last;
2626

2727

2828
}

0 commit comments

Comments
 (0)