aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/inyourwalls/customlevelcolour/ConfigScreen.java
blob: de06150f8f0dbbd35f5a32005b88bca722edf18b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package net.inyourwalls.customlevelcolour;

import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.layouts.CommonLayouts;
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
import net.minecraft.client.gui.layouts.LinearLayout;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.util.ARGB;

import java.util.regex.Pattern;

public class ConfigScreen extends Screen {
    private static final Component TITLE = Component.translatable("gui.customlevelcolour.title");
    private static final Component OUTLINE_COLOUR_LABEL = Component.translatable("gui.customlevelcolour.outlineColour");
    private static final Component TEXT_COLOUR_LABEL = Component.translatable("gui.customlevelcolour.textColour");
    private static final Component BACK_BUTTON_LABEL = Component.translatable("gui.back");
    private static final Component SAVE_BUTTON_LABEL = Component.translatable("gui.customlevelcolour.saveConfig");
    private final Screen previous;
    private final CustomLevelColourConfig config;
    private int previewOutlineColour;
    private int previewTextColour;

    public ConfigScreen() {
        this(null);
    }

    public ConfigScreen(Screen previous) {
        super(TITLE);
        this.previous = previous;
        this.config = CustomLevelColour.getConfig();
    }

    @Override
    public void init() {
	    this.previewOutlineColour = this.config.outlineColour;
	    this.previewTextColour = this.config.textColour;
	    Button saveButton = Button.builder(SAVE_BUTTON_LABEL, btn -> {
			this.config.textColour = this.previewTextColour;
			this.config.outlineColour = this.previewOutlineColour;
			CustomLevelColour.saveConfig();
	    }).build();

	    Button backButton = Button.builder(BACK_BUTTON_LABEL, btn -> {
		    this.onClose();
	    }).build();

	    EditBox textColourInput = new EditBox(this.font, TEXT_COLOUR_LABEL);
	    textColourInput.setValue(String.format("%x", this.previewTextColour));
	    textColourInput.setMaxLength(6);
	    textColourInput.setResponder(v -> {
			try {
				this.previewTextColour = Integer.parseInt(v, 16);
				textColourInput.setTextColor(ARGB.white(1.0f)); 
				if (!v.matches("[0-9a-fA-F]{6}")) {
					textColourInput.setTextColor(ARGB.opaque(TextColor.YELLOW.getValue()));
				}
			} catch (NumberFormatException ex) {
				textColourInput.setTextColor(ARGB.opaque(TextColor.RED.getValue()));
			}
	    });

	    EditBox outlineColourInput = new EditBox(this.font, OUTLINE_COLOUR_LABEL);
	    outlineColourInput.setValue(String.format("%x", this.previewOutlineColour));
	    outlineColourInput.setMaxLength(6);
	   	outlineColourInput.setResponder(v -> {
			try {
				this.previewOutlineColour = Integer.parseInt(v, 16);
				textColourInput.setTextColor(ARGB.white(1.0f)); 
				if (!v.matches("[0-9a-fA-F]{6}")) {
					outlineColourInput.setTextColor(ARGB.opaque(TextColor.YELLOW.getValue()));
				}
			} catch (NumberFormatException ex) {
				outlineColourInput.setTextColor(ARGB.opaque(TextColor.RED.getValue()));
			}
	   	}); 

	    HeaderAndFooterLayout root = new HeaderAndFooterLayout(this);
	    root.addTitleHeader(TITLE, this.font);

	    LinearLayout contents = LinearLayout.vertical().spacing(4);
	    contents.addChild(CommonLayouts.labeledElement(this.font, textColourInput, TEXT_COLOUR_LABEL));
	    contents.addChild(CommonLayouts.labeledElement(this.font, outlineColourInput, OUTLINE_COLOUR_LABEL));
	    root.addToContents(contents);

	    LinearLayout footer = LinearLayout.horizontal().spacing(4);
	    footer.addChild(backButton);
	    footer.addChild(saveButton);
	    root.addToFooter(footer);

	    root.arrangeElements();
	    root.visitWidgets(elem -> this.addRenderableWidget(elem)); 
    }

    @Override
    public void onClose() {
	    this.minecraft.setScreenAndShow(this.previous);
    }
}