aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/gui/modularui/widget/CoverDataFollowerNumericWidget.java
blob: 316109ed6483447f28b05be755c17720dbc9530f (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
package gregtech.common.gui.modularui.widget;

import java.util.function.Consumer;
import java.util.function.Function;

import com.gtnewhorizons.modularui.api.math.Alignment;
import com.gtnewhorizons.modularui.api.math.Color;
import com.gtnewhorizons.modularui.common.widget.textfield.NumericWidget;

import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.gui.modularui.IDataFollowerWidget;
import gregtech.api.util.ISerializableObject;

public class CoverDataFollowerNumericWidget<T extends ISerializableObject> extends NumericWidget
    implements IDataFollowerWidget<T, Double> {

    private Function<T, Double> dataToStateGetter;

    public CoverDataFollowerNumericWidget() {
        super();
        setGetter(() -> 0); // fake getter; used only for init
        setSynced(false, false);
        setTextColor(Color.WHITE.dark(1));
        setTextAlignment(Alignment.CenterLeft);
        setBackground(GTUITextures.BACKGROUND_TEXT_FIELD.withOffset(-1, -1, 2, 2));
    }

    @Override
    public void onPostInit() {
        // Widget#onPostInit is called earlier than IDataFollowerWidget#onPostInit,
        // so we make sure cursor is set after text is set
        super.onPostInit();

        // On first call #handler does not contain text.
        // On second call, it contains correct text to update #lastText,
        // but #shouldGetFocus call is skipped by Cursor#updateFocused,
        // so we need to manually call this.
        if (focusOnGuiOpen) {
            shouldGetFocus();
        }
    }

    @Override
    public CoverDataFollowerNumericWidget<T> setDataToStateGetter(Function<T, Double> dataToStateGetter) {
        this.dataToStateGetter = dataToStateGetter;
        return this;
    }

    @Override
    public CoverDataFollowerNumericWidget<T> setStateSetter(Consumer<Double> setter) {
        super.setSetter(setter::accept);
        return this;
    }

    @Override
    public void updateState(T data) {
        setValue(dataToStateGetter.apply(data));
    }

}