forked from ThinkingStudios/ObsidianUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpruceCheckboxWidget.java
More file actions
131 lines (113 loc) · 4.49 KB
/
Copy pathSpruceCheckboxWidget.java
File metadata and controls
131 lines (113 loc) · 4.49 KB
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
/*
* Copyright © 2020~2024 LambdAurora <email@lambdaurora.dev>
* Copyright © 2024 ThinkingStudio
*
* This file is part of ObsidianUI.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/
package org.thinkingstudio.obsidianui.widget;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.RenderPipelines;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Language;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.MathHelper;
import org.thinkingstudio.obsidianui.Position;
import org.thinkingstudio.obsidianui.util.ColorUtil;
/**
* Represents a checkbox widget.
*
* @author LambdAurora
* @version 5.0.0
* @since 1.0.0
*/
public class SpruceCheckboxWidget extends AbstractSpruceBooleanButtonWidget {
private static final Identifier TEXTURE = Identifier.of("obsidianui", "textures/gui/checkbox.png");
private boolean showCross = false;
private boolean colored = false;
public SpruceCheckboxWidget(Position position, int width, int height, Text message, boolean value) {
super(position, width, height, message, value);
}
public SpruceCheckboxWidget(Position position, int width, int height, Text message, boolean value, boolean showMessage) {
super(position, width, height, message, value, showMessage);
}
public SpruceCheckboxWidget(Position position, int width, int height, Text message, PressAction action, boolean value) {
super(position, width, height, message, action, value);
}
public SpruceCheckboxWidget(Position position, int width, int height, Text message, PressAction action, boolean value, boolean showMessage) {
super(position, width, height, message, action, value, showMessage);
}
/**
* Returns whether this checkbox shows a cross for the {@code false} value.
*
* @return {@code true} if this checkbox can show a cross, else {@code false}
*/
public boolean showCross() {
return this.showCross;
}
/**
* Sets whether this checkbox shows a cross for the {@code false} value.
*
* @param showCross {@code true} if this checkbox can show a cross, else {@code false}
*/
public void setShowCross(boolean showCross) {
this.showCross = showCross;
}
/**
* Returns whether this checkbox is colored or not.
*
* @return {@code true} if colored, else {@code false}
*/
public boolean isColored() {
return this.colored;
}
/**
* Sets whether this checkbox is colored or not.
*
* @param colored {@code true} if colored, else {@code false}
*/
public void setColored(boolean colored) {
this.colored = colored;
}
/* Rendering */
@Override
protected void renderButton(DrawContext drawContext, int mouseX, int mouseY, float delta) {
if (this.getValue()) {
int color = ColorUtil.WHITE;
if (this.colored)
color = ColorHelper.fromFloats(this.alpha, 0.f, 1.f, 0.f);
drawContext.drawTexture(RenderPipelines.GUI_TEXTURED, TEXTURE, this.getX(), this.getY(), 0.f, 40.f, this.getHeight(), this.getHeight(), 64, 64, color);
} else if (this.showCross) {
int color = ColorUtil.WHITE;
if (this.colored)
color = ColorHelper.fromFloats(this.alpha, 1.f, 0.f, 0.f);
drawContext.drawTexture(RenderPipelines.GUI_TEXTURED, TEXTURE, this.getX(), this.getY(), 0.f, 20.f, this.getHeight(), this.getHeight(), 64, 64, color);
}
if (this.showMessage) {
OrderedText message = Language.getInstance().reorder(this.client.textRenderer.trimToWidth(this.getMessage(), this.getWidth() - this.getHeight() - 4));
drawContext.drawTextWithShadow(MinecraftClient.getInstance().textRenderer, message, this.getX() + this.getHeight() + 4, this.getY() + (this.getHeight() - 8) / 2,
14737632 | MathHelper.ceil(this.alpha * 255.0F) << 24);
}
}
@Override
protected void renderBackground(DrawContext drawContext, int mouseX, int mouseY, float delta) {
int color = ColorHelper.fromFloats(this.alpha, 1.f, 1.f, 1.f);
drawContext.drawTexture(RenderPipelines.GUI_TEXTURED, TEXTURE, this.getX(), this.getY(), this.isFocusedOrHovered() ? 20.f : 0.f, 0.f, this.getHeight(), this.getHeight(), 64, 64, color);
}
/* Narration */
@Override
protected Text getNarrationFocusedUsageMessage() {
return Text.translatable("narration.checkbox.usage.focused");
}
@Override
protected Text getNarrationHoveredUsageMessage() {
return Text.translatable("narration.checkbox.usage.hovered");
}
}