44import com .cleanroommc .modularui .api .widget .IWidget ;
55import com .cleanroommc .modularui .widget .ParentWidget ;
66import com .cleanroommc .modularui .widgets .slot .ItemSlot ;
7+ import com .cleanroommc .modularui .widgets .slot .SlotGroup ;
78
89import it .unimi .dsi .fastutil .chars .Char2IntMap ;
910import it .unimi .dsi .fastutil .chars .Char2IntOpenHashMap ;
1314import java .util .ArrayList ;
1415import java .util .Collections ;
1516import java .util .List ;
17+ import java .util .function .Consumer ;
1618import java .util .function .IntFunction ;
1719
1820public class SlotGroupWidget extends ParentWidget <SlotGroupWidget > {
@@ -57,15 +59,66 @@ public static SlotGroupWidget playerInventory(SlotConsumer slotConsumer) {
5759 return slotGroupWidget ;
5860 }
5961
60- public interface SlotConsumer {
62+ private String slotGroupName ;
63+ private SlotGroup slotGroup ;
64+ private boolean sortButtonsAdded = false ;
65+ private Consumer <SortButtons > sortButtonsEditor ;
6166
62- ItemSlot apply (int index , ItemSlot widgetSlot );
67+ @ Override
68+ public void onInit () {
69+ super .onInit ();
70+ if (!this .sortButtonsAdded ) {
71+ SortButtons sb = new SortButtons ();
72+ if (this .sortButtonsEditor == null ) placeSortButtonsTopRightHorizontal ();
73+ if (getName () != null ) {
74+ sb .name (getName () + "_sorter_buttons" );
75+ }
76+ child (sb );
77+ }
78+ }
79+
80+ @ Override
81+ public void afterInit () {
82+ super .afterInit ();
83+ if (this .slotGroup != null ) {
84+ for (IWidget widget : getChildren ()) {
85+ if (widget instanceof ItemSlot itemSlot ) {
86+ itemSlot .getSlot ().slotGroup (this .slotGroup );
87+ }
88+ }
89+ } else if (this .slotGroupName != null ) {
90+ for (IWidget widget : getChildren ()) {
91+ if (widget instanceof ItemSlot itemSlot ) {
92+ itemSlot .getSlot ().slotGroup (this .slotGroupName );
93+ }
94+ }
95+ }
6396 }
6497
65- private String slotsKeyName ;
98+ @ Override
99+ protected void onChildAdd (IWidget child ) {
100+ super .onChildAdd (child );
101+ if (child instanceof SortButtons sortButtons ) {
102+ this .sortButtonsAdded = true ;
103+ if (sortButtons .getSlotGroup () == null && sortButtons .getSlotGroupName () == null ) {
104+ if (this .slotGroup != null ) {
105+ sortButtons .slotGroup (this .slotGroup );
106+ } else if (this .slotGroupName != null ) {
107+ sortButtons .slotGroup (this .slotGroupName );
108+ }
109+ }
110+ if (this .sortButtonsEditor != null ) {
111+ this .sortButtonsEditor .accept (sortButtons );
112+ }
113+ }
114+ }
115+
116+ public SlotGroupWidget disableSortButtons () {
117+ this .sortButtonsAdded = true ;
118+ return this ;
119+ }
66120
67121 public void setSlotsSynced (String name ) {
68- this .slotsKeyName = name ;
69122 int i = 0 ;
70123 for (IWidget widget : getChildren ()) {
71124 if (widget instanceof ISynced <?> synced ) {
@@ -75,15 +128,59 @@ public void setSlotsSynced(String name) {
75128 }
76129 }
77130
131+ public SlotGroupWidget editSortButtons (Consumer <SortButtons > sortButtonsEditor ) {
132+ this .sortButtonsEditor = sortButtonsEditor ;
133+ return this ;
134+ }
135+
136+ public SlotGroupWidget placeSortButtonsTopRightVertical () {
137+ return placeSortButtonsTopRightVertical (this .sortButtonsEditor );
138+ }
139+
140+ public SlotGroupWidget placeSortButtonsTopRightHorizontal () {
141+ return placeSortButtonsTopRightHorizontal (this .sortButtonsEditor );
142+ }
143+
144+ public SlotGroupWidget placeSortButtonsTopRightVertical (Consumer <SortButtons > additionalEdits ) {
145+ return editSortButtons (sb -> {
146+ sb .vertical ().leftRelOffset (1f , 1 ).top (0 );
147+ if (additionalEdits != null ) additionalEdits .accept (sb );
148+ });
149+ }
150+
151+ public SlotGroupWidget placeSortButtonsTopRightHorizontal (Consumer <SortButtons > additionalEdits ) {
152+ return editSortButtons (sb -> {
153+ sb .horizontal ().bottomRelOffset (1f , 1 ).right (0 );
154+ if (additionalEdits != null ) additionalEdits .accept (sb );
155+ });
156+ }
157+
158+ public SlotGroupWidget slotGroup (String slotGroupName ) {
159+ this .slotGroupName = slotGroupName ;
160+ return this ;
161+ }
162+
163+ public SlotGroupWidget slotGroup (SlotGroup slotGroup ) {
164+ this .slotGroup = slotGroup ;
165+ return this ;
166+ }
167+
78168 public static Builder builder () {
79169 return new Builder ();
80170 }
81171
172+ public interface SlotConsumer {
173+
174+ ItemSlot apply (int index , ItemSlot widgetSlot );
175+ }
176+
82177 public static class Builder {
83178
84179 private String syncKey ;
85180 private final List <String > matrix = new ArrayList <>();
86181 private final Char2ObjectMap <Object > keys = new Char2ObjectOpenHashMap <>();
182+ private String slotGroupName ;
183+ private SlotGroup slotGroup ;
87184
88185 private Builder () {
89186 this .keys .put (' ' , null );
@@ -115,8 +212,20 @@ public Builder key(char c, IntFunction<IWidget> widget) {
115212 return this ;
116213 }
117214
215+ public Builder slotGroup (String slotGroupName ) {
216+ this .slotGroupName = slotGroupName ;
217+ return this ;
218+ }
219+
220+ public Builder slotGroup (SlotGroup slotGroup ) {
221+ this .slotGroup = slotGroup ;
222+ return this ;
223+ }
224+
118225 public SlotGroupWidget build () {
119- SlotGroupWidget slotGroupWidget = new SlotGroupWidget ();
226+ SlotGroupWidget slotGroupWidget = new SlotGroupWidget ()
227+ .slotGroup (this .slotGroupName )
228+ .slotGroup (this .slotGroup );
120229 Char2IntMap charCount = new Char2IntOpenHashMap ();
121230 int x = 0 , y = 0 , maxWidth = 0 ;
122231 int syncId = 0 ;
@@ -129,6 +238,10 @@ public SlotGroupWidget build() {
129238 IWidget widget ;
130239 if (o instanceof IWidget iWidget ) {
131240 widget = iWidget ;
241+ if (count > 0 ) {
242+ throw new IllegalArgumentException ("A widget can only exist once in the widget tree, but the char '" + c +
243+ "' exists more than once in this slot group widget and it has a static widget supplied." );
244+ }
132245 } else if (o instanceof IntFunction <?> function ) {
133246 widget = (IWidget ) function .apply (count );
134247 } else {
0 commit comments