aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/config/guiconfig/MFeatureEdit.java
blob: f2ee35bc5e5c6eb90155f7f02131ff3ad72ed334 (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
/*
 * 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 kr.syeyoung.dungeonsguide.features.AbstractFeature;
import kr.syeyoung.dungeonsguide.features.FeatureParameter;
import kr.syeyoung.dungeonsguide.gui.MPanel;
import kr.syeyoung.dungeonsguide.gui.elements.MButton;
import kr.syeyoung.dungeonsguide.gui.elements.MList;
import kr.syeyoung.dungeonsguide.gui.elements.MModalConfirmation;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class MFeatureEdit extends MPanel {
    private MList list;
    private MButton goBack, resetToDefault;
    private RootConfigPanel rootConfigPanel;
    private AbstractFeature abstractFeature;

    private Map<String, MPanel> parameterEdits = new HashMap<>();

    public MFeatureEdit(AbstractFeature abstractFeature, RootConfigPanel rootConfigPanel) {
        this.abstractFeature = abstractFeature;
        this.rootConfigPanel = rootConfigPanel;
        list = new MList();
        list.setGap(5);
        list.setDrawLine(false);
        add(list);

        goBack = new MButton();
        goBack.setText("< Go Back");
        goBack.setOnActionPerformed(rootConfigPanel::goBack);
        add(goBack);
        resetToDefault = new MButton();
        resetToDefault.setText("Reset To Default");
        resetToDefault.setForeground(Color.red);
        resetToDefault.setOnActionPerformed(() -> {
            openResetConfirmation();
        });
        add(resetToDefault);
    }

    public void openResetConfirmation() {
        MModalConfirmation mModal = new MModalConfirmation("Are you sure?",
                "Resetting to default will reset your configuration for the selected feature to default",
                () -> {
            for (FeatureParameter parameter : abstractFeature.getParameters()) {
                parameter.setToDefault();
            }
            abstractFeature.onParameterReset();
            rootConfigPanel.invalidatePage(abstractFeature.getEditRoute(rootConfigPanel));
            }, () -> {});
        mModal.setScale(getScale());
        mModal.getYes().setBorder(0xFFFF0000);
        mModal.getYes().setText("Yes, Reset it");
        mModal.getNo().setText("Cancel");
        mModal.open(MFeatureEdit.this);
    }

    public void addParameterEdit(String name, MPanel paramEdit) {
        parameterEdits.put(name, paramEdit);
        list.add(paramEdit);
    }
    public MPanel removeParameterEdit(String name) {
        MPanel panel = parameterEdits.remove(name);
        list.remove(panel);
        return panel;
    }

    @Override
    public void resize(int parentWidth, int parentHeight) {
        super.resize(parentWidth, parentHeight);
        setBounds(new Rectangle(0,0,parentWidth,parentHeight));
        Dimension prefSize = getPreferredSize();
        int hei = prefSize.height;
        setBounds(new Rectangle(0,0,parentWidth,hei));
    }

    @Override
    public void setBounds(Rectangle bounds) {
        super.setBounds(bounds);
        goBack.setBounds(new Rectangle(5,5,75,15));
        resetToDefault.setBounds(new Rectangle(bounds.width - 105, 5, 100, 15));

        list.setBounds(new Rectangle(5,25,bounds.width - 10, bounds.height - 10));
        list.realignChildren();

    }

    @Override
    public Dimension getPreferredSize() {
        Dimension listPref = list.getPreferredSize();
        return new Dimension(listPref.width + 10, listPref.height + 30);
    }
}