1+ package com .evandev .fieldguide .client .gui .widget ;
2+
3+ import com .mojang .blaze3d .platform .InputConstants ;
4+ import com .mojang .blaze3d .vertex .Tesselator ;
5+ import io .github .mortuusars .exposure .ExposureClient ;
6+ import io .github .mortuusars .exposure .item .PhotographItem ;
7+ import io .github .mortuusars .exposure .render .PhotographRenderer ;
8+ import net .minecraft .client .Minecraft ;
9+ import net .minecraft .client .gui .GuiGraphics ;
10+ import net .minecraft .client .gui .components .AbstractButton ;
11+ import net .minecraft .client .gui .components .Tooltip ;
12+ import net .minecraft .client .gui .narration .NarrationElementOutput ;
13+ import net .minecraft .client .gui .screens .inventory .tooltip .ClientTooltipPositioner ;
14+ import net .minecraft .client .renderer .LightTexture ;
15+ import net .minecraft .client .renderer .MultiBufferSource ;
16+ import net .minecraft .client .renderer .Rect2i ;
17+ import net .minecraft .network .chat .Component ;
18+ import net .minecraft .resources .ResourceLocation ;
19+ import net .minecraft .world .item .ItemStack ;
20+ import org .jetbrains .annotations .NotNull ;
21+ import org .joml .Vector2i ;
22+
23+ import java .util .function .Supplier ;
24+
25+ public class FieldGuidePhotographWidget extends AbstractButton {
26+ private final Rect2i exposureArea ;
27+ private final Supplier <ItemStack > photographGetter ;
28+ private final Runnable onLeftClick ;
29+ private final Runnable onRightClick ;
30+ private final ResourceLocation backgroundTexture ;
31+
32+ public FieldGuidePhotographWidget (int x , int y , int width , int height , Rect2i exposureArea ,
33+ ResourceLocation backgroundTexture ,
34+ Supplier <ItemStack > photographGetter ,
35+ Runnable onLeftClick , Runnable onRightClick ,
36+ Component tooltipText ) {
37+ super (x , y , width , height , Component .empty ());
38+ this .exposureArea = exposureArea ;
39+ this .backgroundTexture = backgroundTexture ;
40+ this .photographGetter = photographGetter ;
41+ this .onLeftClick = onLeftClick ;
42+ this .onRightClick = onRightClick ;
43+
44+ this .setTooltip (Tooltip .create (tooltipText ));
45+ }
46+
47+ @ Override
48+ public void renderWidget (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTick ) {
49+ guiGraphics .blit (backgroundTexture , this .getX (), this .getY (), 0 , 0 , this .width , this .height , this .width , this .height );
50+
51+ ItemStack photograph = photographGetter .get ();
52+ if (photograph .getItem () instanceof PhotographItem ) {
53+ guiGraphics .pose ().pushPose ();
54+ float scale = exposureArea .getWidth () / (float ) ExposureClient .getExposureRenderer ().getSize ();
55+ guiGraphics .pose ().translate (exposureArea .getX (), exposureArea .getY (), 1 );
56+ guiGraphics .pose ().scale (scale , scale , scale );
57+
58+ MultiBufferSource .BufferSource bufferSource = MultiBufferSource .immediate (Tesselator .getInstance ().getBuilder ());
59+ PhotographRenderer .render (photograph , false , false , guiGraphics .pose (),
60+ bufferSource , LightTexture .FULL_BRIGHT , 255 , 255 , 255 , 255 );
61+ bufferSource .endBatch ();
62+ guiGraphics .pose ().popPose ();
63+ }
64+ }
65+
66+ @ Override
67+ protected @ NotNull ClientTooltipPositioner createTooltipPositioner () {
68+ return (screenWidth , screenHeight , mouseX , mouseY , tooltipWidth , tooltipHeight ) -> {
69+ int x = mouseX + 12 ;
70+ int y = mouseY - 12 ;
71+
72+ if (x + tooltipWidth > screenWidth ) {
73+ x -= 28 + tooltipWidth ;
74+ }
75+ if (y + tooltipHeight + 6 > screenHeight ) {
76+ y = screenHeight - tooltipHeight - 6 ;
77+ }
78+ return new Vector2i (x , y );
79+ };
80+ }
81+
82+ @ Override
83+ public void onPress () {
84+ onLeftClick .run ();
85+ }
86+
87+ @ Override
88+ protected boolean clicked (double mouseX , double mouseY ) {
89+ return this .active && this .visible
90+ && mouseX >= this .getX () && mouseY >= this .getY ()
91+ && mouseX < this .getX () + this .width && mouseY < this .getY () + this .height ;
92+ }
93+
94+ @ Override
95+ public boolean mouseClicked (double mouseX , double mouseY , int button ) {
96+ if (this .clicked (mouseX , mouseY )) {
97+ if (button == InputConstants .MOUSE_BUTTON_LEFT ) {
98+ this .playDownSound (Minecraft .getInstance ().getSoundManager ());
99+ this .onPress ();
100+ return true ;
101+ } else if (button == InputConstants .MOUSE_BUTTON_RIGHT ) {
102+ this .playDownSound (Minecraft .getInstance ().getSoundManager ());
103+ this .onRightClick .run ();
104+ return true ;
105+ }
106+ }
107+ return false ;
108+ }
109+
110+ @ Override
111+ public boolean mouseScrolled (double mouseX , double mouseY , double delta ) {
112+ if (delta > 0 && this .isHovered () && !photographGetter .get ().isEmpty ()) {
113+ this .onPress ();
114+ return true ;
115+ }
116+ return super .mouseScrolled (mouseX , mouseY , delta );
117+ }
118+
119+ @ Override
120+ protected void updateWidgetNarration (@ NotNull NarrationElementOutput output ) {
121+ this .defaultButtonNarrationText (output );
122+ }
123+ }
0 commit comments