aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/features/text/PanelTextParameterConfig.java
blob: adc4ca6d171904a9b5dbb19b0965b42c4872d769 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package kr.syeyoung.dungeonsguide.features.text;

import kr.syeyoung.dungeonsguide.config.guiconfig.FeatureEditPane;
import kr.syeyoung.dungeonsguide.config.guiconfig.GuiConfig;
import kr.syeyoung.dungeonsguide.config.guiconfig.MParameter;
import kr.syeyoung.dungeonsguide.config.types.AColor;
import kr.syeyoung.dungeonsguide.features.AbstractFeature;
import kr.syeyoung.dungeonsguide.features.FeatureParameter;
import kr.syeyoung.dungeonsguide.features.text.StyledText;
import kr.syeyoung.dungeonsguide.features.text.TextHUDFeature;
import kr.syeyoung.dungeonsguide.features.text.TextStyle;
import kr.syeyoung.dungeonsguide.gui.MPanel;
import kr.syeyoung.dungeonsguide.gui.elements.MButton;
import kr.syeyoung.dungeonsguide.gui.elements.MColor;
import kr.syeyoung.dungeonsguide.gui.elements.MEditableAColor;
import kr.syeyoung.dungeonsguide.gui.elements.MToggleButton;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.w3c.dom.css.Rect;

import java.awt.*;
import java.io.IOException;
import java.util.*;
import java.util.List;

public class PanelTextParameterConfig extends MPanel {

    private StyledTextProvider feature;

    private MEditableAColor currentColor;
    private MEditableAColor backgroundColor;
    private MToggleButton shadow;

    @Override
    public void onBoundsUpdate() {
    }

    @Override
    public void resize(int parentWidth, int parentHeight) {
        this.setBounds(new Rectangle(5,5,parentWidth-10, 120));
    }

    private GuiConfig config;
    public PanelTextParameterConfig(final GuiConfig config, final StyledTextProvider feature) {
        this.config = config;
        this.feature = feature;
        setBackgroundColor(new Color(38, 38, 38, 255));

        currentColor = new MEditableAColor();
        currentColor.setColor(new AColor(0xff555555, true));
        currentColor.setEnableEdit(false);
        currentColor.setSize(new Dimension(15, 10));
        currentColor.setBounds(new Rectangle(275 , 14, 15, 10));
        currentColor.setOnUpdate(new Runnable() {
            @Override
            public void run() {
                for (String se:selected)
                    feature.getStylesMap().get(se).setColor(currentColor.getColor());
            }
        });
        add(currentColor);
        backgroundColor = new MEditableAColor();
        backgroundColor.setColor(new AColor(0xff555555, true));
        backgroundColor.setEnableEdit(false);
        backgroundColor.setSize(new Dimension(15, 10));
        backgroundColor.setBounds(new Rectangle(415 , 14, 15, 10));
        backgroundColor.setOnUpdate(new Runnable() {
            @Override
            public void run() {
                for (String se:selected)
                    feature.getStylesMap().get(se).setBackground(backgroundColor.getColor());
            }
        });
        add(backgroundColor);
        shadow = new MToggleButton();
        shadow.setSize(new Dimension(20, 10));
        shadow.setBounds(new Rectangle(275 , 30, 20, 10));
        shadow.setOnToggle(new Runnable() {
            @Override
            public void run() {
                for (String se:selected)
                    feature.getStylesMap().get(se).setShadow(shadow.isEnabled());
            }
        });
        add(shadow);


    }

    private Set<String> selected = new HashSet<String>();

    @Override
    public void render(int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks, Rectangle scissor) {

        GlStateManager.pushMatrix();

        int width = 200, height = 100;
        Gui.drawRect(0,0,getBounds().width, getBounds().height, 0xFF444444);
        Gui.drawRect(4,4,width+6, height+6, 0xFF222222);
        Gui.drawRect(5,5,width+5, height+5, 0xFF555555);
        Rectangle clip = new Rectangle(scissor.x + 5, scissor.y + 5, width, height);
        clip(new ScaledResolution(Minecraft.getMinecraft()), clip.x, clip.y, clip.width, clip.height);

        GlStateManager.pushMatrix();
        GlStateManager.translate(offsetX + 5, offsetY + 5, 0);
        GlStateManager.scale(scale, scale, 0);


        List<StyledText> texts = feature.getDummyText();
        Map<String, TextStyle> styles = feature.getStylesMap();
        List<StyledTextRenderer.StyleTextAssociated> calc = StyledTextRenderer.drawTextWithStylesAssociated(texts, 0,0, styles);
        boolean bool =clip.contains(absMousex, absMousey);
        for (StyledTextRenderer.StyleTextAssociated calc3: calc) {
            if (selected.contains(calc3.getStyledText().getGroup())) {
                Gui.drawRect(calc3.getRectangle().x, calc3.getRectangle().y, calc3.getRectangle().x + calc3.getRectangle().width, calc3.getRectangle().y + calc3.getRectangle().height, 0x4244A800);
            } else if (bool && calc3.getRectangle().contains((relMousex0-5 -offsetX) * scale , (relMousey0 - 5 - offsetY) * scale)) {
                for (StyledTextRenderer.StyleTextAssociated calc2 : calc) {
                    if (calc2.getStyledText().getGroup().equals(calc3.getStyledText().getGroup()))
                        Gui.drawRect(calc2.getRectangle().x, calc2.getRectangle().y, calc2.getRectangle().x + calc2.getRectangle().width, calc2.getRectangle().y + calc2.getRectangle().height, 0x55777777);
                }
            }
        }
        clip(new ScaledResolution(Minecraft.getMinecraft()), scissor.x, scissor.y, scissor.width, scissor.height);

        GlStateManager.popMatrix();

        GlStateManager.translate(5, height + 7, 0);
        GlStateManager.scale(0.5,0.5,0);
        FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
        fr.drawString("Press Shift to multi-select", 0, 0, 0xFFBFBFBF);
        GlStateManager.popMatrix();

        GlStateManager.pushMatrix();
        GlStateManager.translate(width + 15, 5, 0);
        GlStateManager.pushMatrix();
        GlStateManager.scale(0.5,0.5,0);
        fr.drawString("Selected Groups: "+selected, 0, 0, 0xFFBFBFBF);
        GlStateManager.popMatrix();
        fr.drawString("Text Color: ", 0, 10, 0xFFFFFFFF);
        fr.drawString("Background Color: ", 100, 10, 0xFFFFFFFF);
        fr.drawString("Shadow: ", 0, 26, 0xFFFFFFFF);

        GlStateManager.popMatrix();
    }

    private int offsetX = 0;
    private int offsetY = 0;
    private float scale = 1;

    private int lastX;
    private int lastY;
    private boolean dragStart = false;
    @Override
    public void mouseClicked(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int mouseButton) {
        List<StyledText> texts = feature.getDummyText();
        Map<String, TextStyle> styles = feature.getStylesMap();
        boolean existed = selected.isEmpty();
        boolean found = false;
        List<StyledTextRenderer.StyleTextAssociated> calc = StyledTextRenderer.calculate(texts, 0,0, styles);
        for (StyledTextRenderer.StyleTextAssociated calc3: calc) {
            if (calc3.getRectangle().contains((relMouseX-5 -offsetX) * scale , (relMouseY - 5 - offsetY) * scale)) {
                if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
                    if (!selected.contains(calc3.getStyledText().getGroup()))
                        selected.add(calc3.getStyledText().getGroup());
                    else
                        selected.remove(calc3.getStyledText().getGroup());
                } else {
                    selected.clear();
                    selected.add(calc3.getStyledText().getGroup());
                }
                found = true;
            }
        }

        if (!found && !(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) && relMouseX >= 5 && relMouseX <= 205 && relMouseY >= 5 && relMouseY <= 105) {
            selected.clear();
            dragStart = true;
            lastX = absMouseX;
            lastY = absMouseY;
        }
        currentColor.setEnableEdit(selected.size() != 0);
        backgroundColor.setEnableEdit(selected.size() != 0);
        if (existed != selected.isEmpty()) {
            if (selected.size() != 0) {
                currentColor.setColor(styles.get(selected.iterator().next()).getColor());
                backgroundColor.setColor(styles.get(selected.iterator().next()).getBackground());
                shadow.setEnabled(styles.get(selected.iterator().next()).isShadow());
            } else {
                currentColor.setColor(new AColor(0xff555555, true));
                backgroundColor.setColor(new AColor(0xff555555, true));
                shadow.setEnabled(false);
            }
        }

        if (selected.size() == 1) {
            currentColor.setColor(styles.get(selected.iterator().next()).getColor());
            backgroundColor.setColor(styles.get(selected.iterator().next()).getBackground());
            shadow.setEnabled(styles.get(selected.iterator().next()).isShadow());
        }
    }

    @Override
    public void mouseReleased(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int state) {
        dragStart = false;
    }

    @Override
    public void mouseClickMove(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int clickedMouseButton, long timeSinceLastClick) {
        if (dragStart) {
            offsetX += absMouseX - lastX;
            offsetY += absMouseY - lastY;
            lastX = absMouseX;
            lastY = absMouseY;

            if (offsetX < 0) offsetX = 0;
            if (offsetY < 0) offsetY =0;
        }
    }

    @Override
    public void mouseScrolled(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int scrollAmount) {
        if ( relMouseX0 >= 5 && relMouseX0 <= 205 && relMouseY0 >= 5 && relMouseY0 <= 105) {
            if (scrollAmount > 0) {
                scale += 0.1;
            } else if (scrollAmount < 0) {
                scale -= 0.1;
            }
            if (scale < 0.1) scale = 0.1f;
            if (scale > 5) scale = 5.0f;
        }
    }
}