aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/config/guiconfig/MParameterEdit.java
blob: b4b2a8e81558ce6a42babc391fb7a75bae90e578 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
 * Copyright (C) 2021  cyoung06
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package kr.syeyoung.dungeonsguide.config.guiconfig;

import com.google.common.base.Predicates;
import kr.syeyoung.dungeonsguide.config.types.AColor;
import kr.syeyoung.dungeonsguide.features.AbstractFeature;
import kr.syeyoung.dungeonsguide.features.FeatureParameter;
import kr.syeyoung.dungeonsguide.gui.MPanel;
import kr.syeyoung.dungeonsguide.gui.elements.*;
import kr.syeyoung.dungeonsguide.utils.RenderUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;

import java.awt.*;
import java.util.function.Predicate;

public class MParameterEdit extends MPanel {
    private AbstractFeature abstractFeature;
    private FeatureParameter featureParameter;
    private RootConfigPanel rootConfigPanel;
    private MPanel valueEditHolder;
    private MPanel valueEdit;

    private Predicate<FeatureParameter> isDisabled ;

    public MParameterEdit(AbstractFeature abstractFeature, FeatureParameter parameter, RootConfigPanel rootConfigPanel) {
        this(abstractFeature, parameter, rootConfigPanel, (a) -> false);
    }

    public MParameterEdit(AbstractFeature abstractFeature, FeatureParameter parameter, RootConfigPanel rootConfigPanel, Predicate<FeatureParameter> isDisabled ) {
        this.abstractFeature = abstractFeature;
        this.featureParameter = parameter;
        this.rootConfigPanel = rootConfigPanel;
        this.isDisabled = isDisabled;

        if (parameter.getValue_type().equals("string")) {
            valueEdit = new MTextField() {
                @Override
                public void edit(String str) {
                    parameter.setValue(str);
                }
            };
            ((MTextField)valueEdit).setText((String) parameter.getValue());
        } else if (parameter.getValue_type().equals("integer")) {
            valueEdit = new MIntegerSelectionButton((Integer) parameter.getValue());
            ((MIntegerSelectionButton)valueEdit).setOnUpdate(() -> {
                parameter.setValue(((MIntegerSelectionButton) valueEdit).getData());
            });
        } else if (parameter.getValue_type().equals("float")) {
            valueEdit = new MFloatSelectionButton((Float) parameter.getValue());
            ((MFloatSelectionButton)valueEdit).setOnUpdate(() -> {
                parameter.setValue(((MFloatSelectionButton) valueEdit).getData());
            });
        } else if (parameter.getValue_type().equals("acolor")) {
            valueEdit = new MEditableAColor();
            ((MEditableAColor)valueEdit).setColor((AColor) parameter.getValue());
            ((MEditableAColor)valueEdit).setEnableEdit(true);
            ((MEditableAColor)valueEdit).setOnUpdate(() -> {
                parameter.setValue(((MEditableAColor) valueEdit).getColor());
            });
        } else if (parameter.getValue_type().equals("color")) {
            valueEdit = new MEditableAColor();
            ((MEditableAColor)valueEdit).setColor(new AColor(((Color) parameter.getValue()).getRGB(), true));
            ((MEditableAColor)valueEdit).setEnableEdit(true);
            ((MEditableAColor)valueEdit).setOnUpdate(() -> {
                parameter.setValue(((MEditableAColor) valueEdit).getColor());
            });
        } else if (parameter.getValue_type().equals("boolean")) {
            valueEdit = new MToggleButton();
            ((MToggleButton)valueEdit).setEnabled((Boolean) parameter.getValue());
            ((MToggleButton)valueEdit).setOnToggle(() -> {
                parameter.setValue(((MToggleButton) valueEdit).isEnabled());
            });
        } else {
            valueEdit = new MLabel();
            ((MLabel)valueEdit).setText("????");
        }


        valueEditHolder = new MPanel() {
            @Override
            public void setBounds(Rectangle bounds) {
                super.setBounds(bounds);
                Dimension dimension = valueEdit.getPreferredSize();
                if (dimension.width <= 0) dimension.width = bounds.width/2;
                if (dimension.height <= 0) dimension.height = bounds.height/2;
                valueEdit.setBounds(new Rectangle((bounds.width - dimension.width)/2,(bounds.height - dimension.height)/2,dimension.width, dimension.height));
            }
        };
        add(valueEditHolder);
        valueEditHolder.add(valueEdit);
    }
    public MParameterEdit(AbstractFeature abstractFeature, FeatureParameter parameter, RootConfigPanel rootConfigPanel, MPanel valueEdit) {
        this.abstractFeature = abstractFeature;
        this.featureParameter = parameter;
        this.rootConfigPanel = rootConfigPanel;


        valueEditHolder = new MPanel() {
            @Override
            public void setBounds(Rectangle bounds) {
                super.setBounds(bounds);
                Dimension dimension = valueEdit.getPreferredSize();
                if (dimension.width <= 0) dimension.width = bounds.width/2;
                if (dimension.height <= 0) dimension.height = bounds.height/2;
                valueEdit.setBounds(new Rectangle((bounds.width - dimension.width)/2,(bounds.height - dimension.height)/2,dimension.width, dimension.height));
            }
        };
        add(valueEditHolder);
        valueEditHolder.add(valueEdit);
    }

    @Override
    public void render(int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks, Rectangle scissor) {
        Gui.drawRect(0,0,getBounds().width, getBounds().height, RenderUtils.blendAlpha(0x141414, 0.12f));
        Gui.drawRect(2*bounds.width / 3,1,getBounds().width -1, getBounds().height-1, RenderUtils.blendAlpha(0x141414, 0.15f));
        Gui.drawRect(4, 15,2*bounds.width / 3-5, 16, RenderUtils.blendAlpha(0x141414, 0.3f));


        FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
        fr.drawString(featureParameter.getName(), 5,5, 0xFFFFFFFF);
        fr.drawSplitString(featureParameter.getDescription(), 5,18, 2*bounds.width /3-10, 0xFFAAAAAA);

    }

    @Override
    public void render0(double scale, Point parentPoint, Rectangle parentClip, int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks) {
        super.render0(scale, parentPoint, parentClip, absMousex, absMousey, relMousex0, relMousey0, partialTicks);
        if (isDisabled.test(featureParameter)) {
            Gui.drawRect(0,0, getBounds().width, getBounds().height, 0x55000000);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
        int descriptionHeight = fr.listFormattedStringToWidth(featureParameter.getDescription(), Math.max(50, 2*bounds.width/3-10)).size() * fr.FONT_HEIGHT;
        return new Dimension(100, Math.max(30, descriptionHeight + 23));
    }

    @Override
    public void setBounds(Rectangle bounds) {
        super.setBounds(bounds);
        valueEditHolder.setBounds(new Rectangle(2*bounds.width / 3, 0, bounds.width / 3, bounds.height));
    }

    @Override
    public void keyPressed0(char typedChar, int keyCode) {
        if (isDisabled.test(featureParameter)) return;
        super.keyPressed0(typedChar, keyCode);
    }

    @Override
    public void keyHeld0(char typedChar, int keyCode, long heldMS) {
        if (isDisabled.test(featureParameter)) return;
        super.keyHeld0(typedChar, keyCode, heldMS);
    }

    @Override
    public void keyReleased0(char typedChar, int keyCode, long heldMS) {
        if (isDisabled.test(featureParameter)) return;
        super.keyReleased0(typedChar, keyCode, heldMS);
    }

    @Override
    public boolean mouseClicked0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int mouseButton) {
        if (isDisabled.test(featureParameter)) return false;
        return super.mouseClicked0(absMouseX, absMouseY, relMouseX0, relMouseY0, mouseButton);
    }

    @Override
    public void mouseReleased0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int state) {
        if (isDisabled.test(featureParameter)) return ;
        super.mouseReleased0(absMouseX, absMouseY, relMouseX0, relMouseY0, state);
    }

    @Override
    public void mouseClickMove0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int clickedMouseButton, long timeSinceLastClick) {
        if (isDisabled.test(featureParameter)) return ;
        super.mouseClickMove0(absMouseX, absMouseY, relMouseX0, relMouseY0, clickedMouseButton, timeSinceLastClick);
    }

    @Override
    public void mouseScrolled0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int scrollAmount) {
        if (isDisabled.test(featureParameter)) return ;
        super.mouseScrolled0(absMouseX, absMouseY, relMouseX0, relMouseY0, scrollAmount);
    }

    @Override
    public void mouseMoved0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0) {
        if (isDisabled.test(featureParameter)) return ;
        super.mouseMoved0(absMouseX, absMouseY, relMouseX0, relMouseY0);
    }
}