aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/roomedit/valueedit/ValueEditOffsetPointSet.java
blob: 0cdaab0dc694ff1ee8c7278f93254468615740c2 (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
240
241
242
243
244
245
246
247
248
249
250
/*
 *     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.roomedit.valueedit;

import kr.syeyoung.dungeonsguide.dungeon.data.OffsetPoint;
import kr.syeyoung.dungeonsguide.dungeon.data.OffsetPointSet;
import kr.syeyoung.dungeonsguide.roomedit.EditingContext;
import kr.syeyoung.dungeonsguide.gui.MPanel;
import kr.syeyoung.dungeonsguide.roomedit.Parameter;
import kr.syeyoung.dungeonsguide.gui.elements.MButton;
import kr.syeyoung.dungeonsguide.gui.elements.MValue;
import kr.syeyoung.dungeonsguide.roomedit.gui.GuiDungeonAddSet;
import kr.syeyoung.dungeonsguide.utils.RenderUtils;
import lombok.Getter;
import net.minecraft.client.Minecraft;

import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ValueEditOffsetPointSet extends MPanel implements ValueEdit<OffsetPointSet> {
    private Parameter parameter;

    // scroll pane
    // just create
    // add set

    private final MPanel scroll;
    @Getter
    private final List<MPanel> MParameters = new ArrayList<MPanel>();

    private final MButton add;
    private final MButton addSet;

    public void delete(OffsetPoint offsetPoint) {
        ((OffsetPointSet)parameter.getNewData()).getOffsetPointList().remove(offsetPoint);
        Iterator<MPanel> iterator = MParameters.iterator();
        while (iterator.hasNext()) {
            MValue panel = (MValue) iterator.next();
            if (panel.getData() == offsetPoint) {
                iterator.remove();
                break;
            }
        }

    }

    public ValueEditOffsetPointSet(final Parameter parameter2) {
        this.parameter = parameter2;
        {
            scroll = new MPanel() {
                private int offsetY = 0;

                @Override
                public List<MPanel> getChildComponents() {
                    return MParameters;
                }

                @Override
                public void render(int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks, Rectangle scissor) {
                    int heights = 0;
                    for (MPanel panel:getChildComponents()) {
                        panel.setPosition(new Point(0, -offsetY + heights));
                        heights += panel.getBounds().height;
                    }
                }

                @Override
                public boolean mouseClicked0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int mouseButton) {
                    if (!getBounds().contains(relMouseX0, relMouseY0)) {
                        return false;
                    }

                    int relMousex = relMouseX0 - getBounds().x;
                    int relMousey = relMouseY0 - getBounds().y;

                    boolean noClip = true;
                    boolean focusedOverall = false;
                    for (MPanel childComponent  : getChildComponents()) {
                        if (childComponent.mouseClicked0(absMouseX, absMouseY, relMousex, relMousey, mouseButton)) {
                            noClip = false;
                            focusedOverall = true;
                        }
                    }

                    if (getBounds().contains(relMouseX0, relMouseY0) && noClip) {
                        isFocused = true;
                        focusedOverall = true;
                    } else {
                        isFocused = false;
                    }

                    mouseClicked(absMouseX, absMouseY, relMousex, relMousey, mouseButton);
                    return focusedOverall;
                }

                @Override
                public void onBoundsUpdate() {
                    for (MPanel panel :getChildComponents()){
                        panel.setSize(new Dimension(getBounds().width, 20));
                    }
                }
                @Override
                public void resize(int parentWidth, int parentHeight) {
                    this.setBounds(new Rectangle(5,5,parentWidth-10,parentHeight-10));
                }

                @Override
                public void mouseScrolled(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int scrollAmount) {
                    if (new Rectangle(new Point(0,0), getSize()).contains(relMouseX0, relMouseY0)) {
                        if (scrollAmount >0) offsetY += 20;
                        else if (scrollAmount < 0) offsetY -= 20;
                        if (offsetY <0) offsetY = 0;
                    }
                }
            };
            scroll.setBounds(new Rectangle(0,0,getBounds().width, getBounds().height-20));
            add(scroll);
        }

        {
            add = new MButton() {
                @Override
                public void resize(int parentWidth, int parentHeight) {
                    setBounds(new Rectangle(0,parentHeight - 20, parentWidth / 2, 20));
                }
            };
            add.setText("Add");
            add.setBackgroundColor(Color.green);
            add.setOnActionPerformed(new Runnable() {
                @Override
                public void run() {
                    OffsetPoint offsetPoint = new OffsetPoint(EditingContext.getEditingContext().getRoom(), Minecraft.getMinecraft().thePlayer.getPosition());
                    MValue mValue;
                    MParameters.add(mValue = new MValue(offsetPoint, buildAddonsFor(offsetPoint)));
                    ((OffsetPointSet)parameter.getNewData()).getOffsetPointList().add(offsetPoint);
                    mValue.setSize(new Dimension(getBounds().width, 20));
                }
            });

            addSet = new MButton(){
                @Override
                public void resize(int parentWidth, int parentHeight) {
                    setBounds(new Rectangle(parentWidth / 2,parentHeight - 20, parentWidth / 2, 20));
                }
            };
            addSet.setText("Add Set");
            addSet.setBackgroundColor(Color.cyan);
            addSet.setOnActionPerformed(new Runnable() {
                @Override
                public void run() {
                    EditingContext.getEditingContext().openGui(new GuiDungeonAddSet(ValueEditOffsetPointSet.this));
                }
            });
            add(add);
            add(addSet);
        }
        for (OffsetPoint offsetPoint : ((OffsetPointSet)parameter.getNewData()).getOffsetPointList()) {
            MParameters.add(new MValue(offsetPoint, buildAddonsFor(offsetPoint)));
        }
    }

    public List<MPanel> buildAddonsFor(final OffsetPoint offsetPoint) {
        ArrayList<MPanel> panels = new ArrayList<MPanel>();
        MButton mButton = new MButton();
        mButton.setText("Delete");
        mButton.setForeground(Color.white);
        mButton.setBackgroundColor(Color.red);
        mButton.setOnActionPerformed(new Runnable() {
            @Override
            public void run() {
                delete(offsetPoint);
            }
        });
        panels.add(mButton);
        return panels;
    }

    @Override
    public void onBoundsUpdate() {
        scroll.setBounds(new Rectangle(0,0,getBounds().width, getBounds().height-20));
        add.setBounds(new Rectangle(0,getBounds().height-20,getBounds().width / 2, 20));
        addSet.setBounds(new Rectangle(getBounds().width / 2,getBounds().height-20,getBounds().width / 2, 20));
    }

    @Override
    public void setParameter(Parameter parameter) {
        this.parameter = parameter;
    }

    @Override
    public void renderWorld(float partialTicks) {
        for (OffsetPoint offsetPoint :((OffsetPointSet)parameter.getNewData()).getOffsetPointList()) {
            RenderUtils.highlightBlock(offsetPoint.getBlockPos(EditingContext.getEditingContext().getRoom()), new Color(0,255,255,50), partialTicks);
        }
    }

    @Override
    public void resize(int parentWidth, int parentHeight) {
        this.setBounds(new Rectangle(0,0,parentWidth, parentHeight));
    }

    public void addAll(List<OffsetPoint> blockPoses) {
        ((OffsetPointSet)parameter.getNewData()).getOffsetPointList().addAll(blockPoses);
        for (OffsetPoint blockPose : blockPoses) {
            MParameters.add(new MValue(blockPose, buildAddonsFor(blockPose)));
        }
    }

    public static class Generator implements ValueEditCreator<ValueEditOffsetPointSet> {

        @Override
        public ValueEditOffsetPointSet createValueEdit(Parameter parameter) {
            return new ValueEditOffsetPointSet(parameter);
        }

        @Override
        public Object createDefaultValue(Parameter parameter) {
            return new OffsetPointSet();
        }

        @Override
        public Object cloneObj(Object object) {
            try {
                return ((OffsetPointSet)object).clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            assert false;
            return null;
        }
    }
}