22
33import com .mojang .blaze3d .platform .GlStateManager ;
44import io .github .axolotlclient .config .Color ;
5+ import io .github .axolotlclient .config .ConfigManager ;
56import io .github .axolotlclient .config .options .ColorOption ;
7+ import io .github .axolotlclient .config .options .IntegerOption ;
68import io .github .axolotlclient .modules .hud .util .DrawUtil ;
79import io .github .axolotlclient .modules .hud .util .Rectangle ;
810import net .minecraft .client .MinecraftClient ;
911import net .minecraft .client .gui .DrawableHelper ;
1012import net .minecraft .client .gui .widget .ButtonWidget ;
13+ import net .minecraft .client .resource .language .I18n ;
1114import net .minecraft .client .util .Window ;
1215import net .minecraft .util .Identifier ;
16+ import org .jetbrains .annotations .NotNull ;
1317import org .lwjgl .opengl .GL11 ;
1418
1519import java .nio .ByteBuffer ;
20+ import java .nio .ByteOrder ;
1621
1722public class ColorSelectionWidget extends ButtonWidget {
1823 private final ColorOption option ;
1924
2025 protected Rectangle pickerImage ;
21- //private final Rectangle rect;
26+ protected Rectangle currentRect ;
27+
28+ protected IntegerOption alpha = new IntegerOption ("alpha" , 0 , 0 , 255 );
29+ protected OptionSliderWidget alphaSlider ;
30+
2231
2332 public ColorSelectionWidget (ColorOption option ) {
2433 super (0 , 100 , 50 , "" );
2534 this .option =option ;
35+ init ();
36+ }
37+
38+ public void init (){
2639 Window window = new Window (MinecraftClient .getInstance ());
2740 width =window .getWidth ()-200 ;
2841 height =window .getHeight ()-100 ;
2942
3043 pickerImage = new Rectangle (120 , 70 , width /2 , height /2 );
31-
32- //rect = new Rectangle(100, 50, width-200, height-100);
44+ currentRect = new Rectangle (pickerImage .x + pickerImage .width + 20 , pickerImage .y + 10 , 60 , 20 );
45+
46+ alpha .set (option .get ().getAlpha ());
47+ alphaSlider = new OptionSliderWidget (0 , pickerImage .x , pickerImage .y + pickerImage .height + 20 , pickerImage .width , 20 , alpha ){
48+ @ Override
49+ protected @ NotNull String getMessage () {
50+ return alpha .getTranslatedName ()+": " + super .getMessage ();
51+ }
52+ };
3353 }
3454
3555 @ Override
@@ -38,29 +58,52 @@ public void render(MinecraftClient client, int mouseX, int mouseY) {
3858 DrawUtil .fillRect (new Rectangle (100 , 50 , width , height ), Color .DARK_GRAY .withAlpha (127 ));
3959 DrawUtil .outlineRect (new Rectangle (100 , 50 , width , height ), Color .BLACK );
4060
41- DrawUtil .outlineRect (pickerImage , Color .DARK_GRAY .withAlpha (127 ));
61+ drawCenteredString (MinecraftClient .getInstance ().textRenderer , I18n .translate ("pickColor" ), new Window (MinecraftClient .getInstance ()).getWidth ()/2 , 54 , -1 );
62+
63+ DrawUtil .drawString (MinecraftClient .getInstance ().textRenderer , I18n .translate ("currentColor" ) + ":" ,currentRect .x , currentRect .y - 10 , -1 , true );
64+
65+ DrawUtil .fillRect (currentRect , option .get ());
66+ DrawUtil .outlineRect (currentRect , Color .DARK_GRAY .withAlpha (127 ));
4267
4368 GlStateManager .color3f (1 , 1 , 1 );
4469 MinecraftClient .getInstance ().getTextureManager ().bindTexture (new Identifier ("axolotlclient" , "textures/gui/colorwheel.png" ));
4570 DrawableHelper .drawTexture (pickerImage .x , pickerImage .y , 0 , 0 , pickerImage .width , pickerImage .height , pickerImage .width , pickerImage .height );
71+ DrawUtil .outlineRect (pickerImage , Color .DARK_GRAY );
4672
47- //super.render(client, mouseX, mouseY);
73+ alphaSlider .render (client , mouseX , mouseY );
74+
75+ option .set (new Color (option .get ().getRed (), option .get ().getGreen (), option .get ().getBlue (), alpha .get ()));
4876 }
4977
5078 public void onClick (int mouseX , int mouseY ){
51- if (pickerImage .isMouseOver (mouseX , mouseY )) {
52- ByteBuffer pixels = ByteBuffer .allocateDirect (16 );
53- //IntBuffer pixels = IntBuffer.allocate(4);
54- //IntBuffer color = buf.asIntBuffer();
5579
56- MinecraftClient .getInstance ().getFramebuffer ().bind (true );
57- GL11 .glReadPixels (mouseX , mouseY , 1 , 1 , GL11 .GL_RGBA , GL11 .GL_UNSIGNED_BYTE , pixels );
80+ if (pickerImage .isMouseOver (mouseX , mouseY )){
81+ final ByteBuffer pixelBuffer = ByteBuffer .allocateDirect (4 );
82+ pixelBuffer .order (ByteOrder .nativeOrder ());
83+
84+ // Helped in the complete confusion:
85+ // https://github.com/MrCrayfish/MrCrayfishDeviceMod/blob/2a06b20ad8873855885285f3cee6a682e161e24c/src/main/java/com/mrcrayfish/device/util/GLHelper.java#L71
86+
87+ Window window = new Window (MinecraftClient .getInstance ());
88+ int scale = window .getScaleFactor ();
89+ GL11 .glReadPixels (mouseX * scale ,
90+ MinecraftClient .getInstance ().height - mouseY * scale - scale ,
91+ 1 , 1 , GL11 .GL_RGBA , GL11 .GL_UNSIGNED_BYTE , pixelBuffer );
5892
59- String color = (pixels .get (0 ) & 0xFF ) +"" + (pixels .get (1 ) & 0xFF )+ (pixels .get (2 ) & 0xFF )+ (pixels .get (3 ) & 0xFF );
60- System .out .println (color );
93+ final int r = pixelBuffer .get (0 ) & 0xff ;
94+ final int g = pixelBuffer .get (1 ) & 0xff ;
95+ final int b = pixelBuffer .get (2 ) & 0xff ;
96+ final Color index = new Color (r , g , b , alpha .get ());
6197
62- option .set (new Color (pixels .get (0 ) & 0xFF , pixels .get (1 ) & 0xFF , pixels .get (2 ) & 0xFF , pixels .get (3 ) & 0xFF ));
98+ option .set (index );
99+ } else if (alphaSlider .isMouseOver (MinecraftClient .getInstance (), mouseX , mouseY )) {
100+ option .set (new Color (option .get ().getRed (), option .get ().getGreen (), option .get ().getBlue (), alpha .get ()));
63101 }
102+
64103 }
65104
105+ @ Override
106+ public void mouseReleased (int mouseX , int mouseY ) {
107+ alphaSlider .mouseReleased (mouseX , mouseY );
108+ }
66109}
0 commit comments