3333import java .util .ArrayList ;
3434import java .util .List ;
3535import java .util .Locale ;
36+ import java .util .function .BiConsumer ;
3637import net .minecraft .ChatFormatting ;
3738import net .minecraft .client .gui .Font ;
3839import net .minecraft .client .gui .GuiGraphics ;
3940import net .minecraft .client .gui .screens .inventory .tooltip .ClientTooltipComponent ;
4041import net .minecraft .client .renderer .MultiBufferSource ;
4142import net .minecraft .network .chat .Component ;
4243import net .minecraft .world .item .ItemStack ;
44+ import org .jetbrains .annotations .Nullable ;
4345import org .joml .Matrix4f ;
4446
45- // TODO SWEDZ: there has to be a better way to align all the text and stuff. maybe some kind of builder?
4647public final class ClientStructureMemberBlockTooltip implements ClientTooltipComponent {
4748 private static final int TEXT_ROW_HEIGHT = 9 ;
48- private static final int TEXT_BEFORE_IMAGE_ROW_HEIGHT = TEXT_ROW_HEIGHT + 4 ;
49+ private static final int TEXT_BEFORE_IMAGE_EXTRA_ROW_HEIGHT = 4 ;
4950 private static final int IMAGE_ROW_HEIGHT = 20 ;
5051
5152 private final StructureMultiblockMemberBlockItem .TooltipData data ;
5253
5354 private final ItemStack preview ;
5455 private final List <ItemStack > members ;
5556
57+ private final List <Object > lines = new ArrayList <>();
58+
5659 public ClientStructureMemberBlockTooltip (StructureMultiblockMemberBlockItem .TooltipData data ) {
5760 this .data = data ;
5861
@@ -64,42 +67,64 @@ public ClientStructureMemberBlockTooltip(StructureMultiblockMemberBlockItem.Tool
6467 members .add (stateTest .blockState ().getBlock ().asItem ().getDefaultInstance ());
6568 }
6669 }
67- }
68-
69- @ Override
70- public int getHeight () {
71- int height = 0 ;
72-
73- height += TEXT_ROW_HEIGHT ;
7470
75- if (renderName ()) {
76- height += TEXT_ROW_HEIGHT ;
77- }
71+ // TODO SWEDZ: use translations
72+ lines .add (Component .literal ("Mode: " ).append (data .mode ().text ()));
7873
79- if (renderCasing () ) {
80- height += TEXT_ROW_HEIGHT ;
74+ if (data . mode () == StructureMemberMode . VARIABLE ) {
75+ lines . add ( Component . literal ( "Name: %s" . formatted ( data . name ()))) ;
8176 }
8277
83- if (renderPreview ()) {
84- height += TEXT_BEFORE_IMAGE_ROW_HEIGHT ;
85- height += IMAGE_ROW_HEIGHT ;
78+ if (data .mode () == StructureMemberMode .HATCH ) {
79+ lines .add (Component .literal ("Casing: %s" .formatted (data .casing ().key .getPath ())));
8680 }
8781
88- if (renderMembers ()) {
89- height += TEXT_BEFORE_IMAGE_ROW_HEIGHT ;
90- height += IMAGE_ROW_HEIGHT ;
82+ if (data .mode () == StructureMemberMode .SIMPLE || data .mode () == StructureMemberMode .HATCH ) {
83+ lines .add (Component .literal ("Preview" ).withStyle (ChatFormatting .UNDERLINE ));
84+ lines .add (List .of (preview ));
85+ lines .add (Component .literal ("Members" ).withStyle (ChatFormatting .UNDERLINE ));
86+ lines .add (members );
9187 }
9288
93- if (renderHatch () ) {
94- height += TEXT_ROW_HEIGHT ;
89+ if (data . mode () == StructureMemberMode . HATCH && data . hatchFlags (). flags != 0 ) {
90+ lines . add ( Component . literal ( "Hatches" ). withStyle ( ChatFormatting . UNDERLINE )) ;
9591 for (HatchType hatchType : HatchType .values ()) {
9692 if (data .hatchFlags ().allows (hatchType )) {
97- height += TEXT_ROW_HEIGHT ;
93+ lines .add (Component .literal ("- %s" .formatted (hatchType .name ().toLowerCase (Locale .ROOT ))));
94+ }
95+ }
96+ }
97+ }
98+
99+ private int iterateLines (int startY , @ Nullable BiConsumer <Integer , Component > actionText ,
100+ @ Nullable BiConsumer <Integer , List <ItemStack >> actionStacks ) {
101+ int y = startY ;
102+ for (int i = 0 ; i < lines .size (); i ++) {
103+ Object line = lines .get (i );
104+ if (i > 0 ) {
105+ Object last = lines .get (i - 1 );
106+ if (line instanceof List ) {
107+ y += TEXT_BEFORE_IMAGE_EXTRA_ROW_HEIGHT ;
108+ }
109+ }
110+ if (line instanceof Component text ) {
111+ if (actionText != null ) {
112+ actionText .accept (y , text );
98113 }
114+ y += TEXT_ROW_HEIGHT ;
115+ } else if (line instanceof List list ) {
116+ if (actionStacks != null ) {
117+ actionStacks .accept (y , list );
118+ }
119+ y += IMAGE_ROW_HEIGHT ;
99120 }
100121 }
122+ return y ;
123+ }
101124
102- return height ;
125+ @ Override
126+ public int getHeight () {
127+ return iterateLines (0 , null , null );
103128 }
104129
105130 @ Override
@@ -129,102 +154,14 @@ private void renderRowText(Component text, Font font, int x, int y, Matrix4f mat
129154 font .drawInBatch (text , x , y , -1 , true , matrix , buffer , Font .DisplayMode .NORMAL , 0 , 0xF000F0 );
130155 }
131156
132- private boolean renderName () {
133- return data .mode () == StructureMemberMode .VARIABLE ;
134- }
135-
136- private boolean renderPreview () {
137- return data .mode () == StructureMemberMode .SIMPLE || data .mode () == StructureMemberMode .HATCH ;
138- }
139-
140- private boolean renderMembers () {
141- return data .mode () == StructureMemberMode .SIMPLE || data .mode () == StructureMemberMode .HATCH ;
142- }
143-
144- private boolean renderCasing () {
145- return data .mode () == StructureMemberMode .HATCH ;
146- }
147-
148- private boolean renderHatch () {
149- return data .mode () == StructureMemberMode .HATCH && data .hatchFlags ().flags != 0 ;
150- }
151-
152157 @ Override
153158 public void renderImage (Font font , int x , int y , GuiGraphics graphics ) {
154- int lineY = y ;
155-
156- lineY += TEXT_ROW_HEIGHT ;
157-
158- if (renderName ()) {
159- lineY += TEXT_ROW_HEIGHT ;
160- }
161-
162- if (renderCasing ()) {
163- lineY += TEXT_ROW_HEIGHT ;
164- }
165-
166- if (renderPreview ()) {
167- lineY += TEXT_BEFORE_IMAGE_ROW_HEIGHT ;
168- renderRowImage (List .of (preview ), font , x , lineY , graphics );
169- lineY += IMAGE_ROW_HEIGHT ;
170- }
171-
172- if (renderMembers ()) {
173- lineY += TEXT_BEFORE_IMAGE_ROW_HEIGHT ;
174- renderRowImage (members , font , x , lineY , graphics );
175- lineY += IMAGE_ROW_HEIGHT ;
176- }
177-
178- if (renderHatch ()) {
179- lineY += TEXT_ROW_HEIGHT ;
180- for (HatchType hatchType : HatchType .values ()) {
181- if (data .hatchFlags ().allows (hatchType )) {
182- lineY += TEXT_ROW_HEIGHT ;
183- }
184- }
185- }
159+ iterateLines (y , null , (lineY , stacks ) -> renderRowImage (stacks , font , x , lineY , graphics ));
186160 }
187161
188162 @ Override
189163 public void renderText (Font font , int x , int y , Matrix4f matrix , MultiBufferSource .BufferSource buffer ) {
190- int lineY = y ;
191-
192- // TODO SWEDZ: use translations
193- renderRowText (Component .literal ("Mode: " ).append (data .mode ().text ()), font , x , lineY , matrix , buffer );
194- lineY += TEXT_ROW_HEIGHT ;
195-
196- if (renderName ()) {
197- renderRowText (Component .literal ("Name: %s" .formatted (data .name ())), font , x , lineY , matrix , buffer );
198- lineY += TEXT_ROW_HEIGHT ;
199- }
200-
201- if (renderCasing ()) {
202- renderRowText (Component .literal ("Casing: %s" .formatted (data .casing ().key .getPath ())), font , x , lineY , matrix , buffer );
203- lineY += TEXT_ROW_HEIGHT ;
204- }
205-
206- if (renderPreview ()) {
207- renderRowText (Component .literal ("Preview" ).withStyle (ChatFormatting .UNDERLINE ), font , x , lineY , matrix , buffer );
208- lineY += TEXT_BEFORE_IMAGE_ROW_HEIGHT ;
209- lineY += IMAGE_ROW_HEIGHT ;
210- }
211-
212- if (renderMembers ()) {
213- renderRowText (Component .literal ("Members" ).withStyle (ChatFormatting .UNDERLINE ), font , x , lineY , matrix , buffer );
214- lineY += TEXT_BEFORE_IMAGE_ROW_HEIGHT ;
215- renderRowImageText (members .size (), font , x , lineY , matrix , buffer );
216- lineY += IMAGE_ROW_HEIGHT ;
217- }
218-
219- if (renderHatch ()) {
220- renderRowText (Component .literal ("Hatches" ).withStyle (ChatFormatting .UNDERLINE ), font , x , lineY , matrix , buffer );
221- lineY += TEXT_ROW_HEIGHT ;
222- for (HatchType hatchType : HatchType .values ()) {
223- if (data .hatchFlags ().allows (hatchType )) {
224- renderRowText (Component .literal ("- %s" .formatted (hatchType .name ().toLowerCase (Locale .ROOT ))), font , x , lineY , matrix , buffer );
225- lineY += TEXT_ROW_HEIGHT ;
226- }
227- }
228- }
164+ iterateLines (y , (lineY , text ) -> renderRowText (text , font , x , lineY , matrix , buffer ),
165+ (lineY , stacks ) -> renderRowImageText (stacks .size (), font , x , lineY , matrix , buffer ));
229166 }
230167}
0 commit comments