172 lines
		
	
	
	
		
			5.2 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			172 lines
		
	
	
	
		
			5.2 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package net.minecraft.client.gui.layouts;
 | |
| 
 | |
| import com.mojang.math.Divisor;
 | |
| import java.util.ArrayList;
 | |
| import java.util.Iterator;
 | |
| import java.util.List;
 | |
| import java.util.function.Consumer;
 | |
| import net.fabricmc.api.EnvType;
 | |
| import net.fabricmc.api.Environment;
 | |
| import net.minecraft.Util;
 | |
| 
 | |
| @Environment(EnvType.CLIENT)
 | |
| public class EqualSpacingLayout extends AbstractLayout {
 | |
| 	private final EqualSpacingLayout.Orientation orientation;
 | |
| 	private final List<EqualSpacingLayout.ChildContainer> children = new ArrayList();
 | |
| 	private final LayoutSettings defaultChildLayoutSettings = LayoutSettings.defaults();
 | |
| 
 | |
| 	public EqualSpacingLayout(int width, int height, EqualSpacingLayout.Orientation orientation) {
 | |
| 		this(0, 0, width, height, orientation);
 | |
| 	}
 | |
| 
 | |
| 	public EqualSpacingLayout(int x, int y, int width, int height, EqualSpacingLayout.Orientation orientation) {
 | |
| 		super(x, y, width, height);
 | |
| 		this.orientation = orientation;
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void arrangeElements() {
 | |
| 		super.arrangeElements();
 | |
| 		if (!this.children.isEmpty()) {
 | |
| 			int i = 0;
 | |
| 			int j = this.orientation.getSecondaryLength(this);
 | |
| 
 | |
| 			for (EqualSpacingLayout.ChildContainer childContainer : this.children) {
 | |
| 				i += this.orientation.getPrimaryLength(childContainer);
 | |
| 				j = Math.max(j, this.orientation.getSecondaryLength(childContainer));
 | |
| 			}
 | |
| 
 | |
| 			int k = this.orientation.getPrimaryLength(this) - i;
 | |
| 			int l = this.orientation.getPrimaryPosition(this);
 | |
| 			Iterator<EqualSpacingLayout.ChildContainer> iterator = this.children.iterator();
 | |
| 			EqualSpacingLayout.ChildContainer childContainer2 = (EqualSpacingLayout.ChildContainer)iterator.next();
 | |
| 			this.orientation.setPrimaryPosition(childContainer2, l);
 | |
| 			l += this.orientation.getPrimaryLength(childContainer2);
 | |
| 			if (this.children.size() >= 2) {
 | |
| 				Divisor divisor = new Divisor(k, this.children.size() - 1);
 | |
| 
 | |
| 				while (divisor.hasNext()) {
 | |
| 					l += divisor.nextInt();
 | |
| 					EqualSpacingLayout.ChildContainer childContainer3 = (EqualSpacingLayout.ChildContainer)iterator.next();
 | |
| 					this.orientation.setPrimaryPosition(childContainer3, l);
 | |
| 					l += this.orientation.getPrimaryLength(childContainer3);
 | |
| 				}
 | |
| 			}
 | |
| 
 | |
| 			int m = this.orientation.getSecondaryPosition(this);
 | |
| 
 | |
| 			for (EqualSpacingLayout.ChildContainer childContainer4 : this.children) {
 | |
| 				this.orientation.setSecondaryPosition(childContainer4, m, j);
 | |
| 			}
 | |
| 
 | |
| 			switch (this.orientation) {
 | |
| 				case HORIZONTAL:
 | |
| 					this.height = j;
 | |
| 					break;
 | |
| 				case VERTICAL:
 | |
| 					this.width = j;
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void visitChildren(Consumer<LayoutElement> visitor) {
 | |
| 		this.children.forEach(childContainer -> visitor.accept(childContainer.child));
 | |
| 	}
 | |
| 
 | |
| 	public LayoutSettings newChildLayoutSettings() {
 | |
| 		return this.defaultChildLayoutSettings.copy();
 | |
| 	}
 | |
| 
 | |
| 	public LayoutSettings defaultChildLayoutSetting() {
 | |
| 		return this.defaultChildLayoutSettings;
 | |
| 	}
 | |
| 
 | |
| 	public <T extends LayoutElement> T addChild(T child) {
 | |
| 		return this.addChild(child, this.newChildLayoutSettings());
 | |
| 	}
 | |
| 
 | |
| 	public <T extends LayoutElement> T addChild(T child, LayoutSettings layoutSettings) {
 | |
| 		this.children.add(new EqualSpacingLayout.ChildContainer(child, layoutSettings));
 | |
| 		return child;
 | |
| 	}
 | |
| 
 | |
| 	public <T extends LayoutElement> T addChild(T child, Consumer<LayoutSettings> layoutSettingsCreator) {
 | |
| 		return this.addChild(child, Util.make(this.newChildLayoutSettings(), layoutSettingsCreator));
 | |
| 	}
 | |
| 
 | |
| 	@Environment(EnvType.CLIENT)
 | |
| 	static class ChildContainer extends AbstractLayout.AbstractChildWrapper {
 | |
| 		protected ChildContainer(LayoutElement layoutElement, LayoutSettings layoutSettings) {
 | |
| 			super(layoutElement, layoutSettings);
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	@Environment(EnvType.CLIENT)
 | |
| 	public static enum Orientation {
 | |
| 		HORIZONTAL,
 | |
| 		VERTICAL;
 | |
| 
 | |
| 		int getPrimaryLength(LayoutElement element) {
 | |
| 			return switch (this) {
 | |
| 				case HORIZONTAL -> element.getWidth();
 | |
| 				case VERTICAL -> element.getHeight();
 | |
| 			};
 | |
| 		}
 | |
| 
 | |
| 		int getPrimaryLength(EqualSpacingLayout.ChildContainer container) {
 | |
| 			return switch (this) {
 | |
| 				case HORIZONTAL -> container.getWidth();
 | |
| 				case VERTICAL -> container.getHeight();
 | |
| 			};
 | |
| 		}
 | |
| 
 | |
| 		int getSecondaryLength(LayoutElement element) {
 | |
| 			return switch (this) {
 | |
| 				case HORIZONTAL -> element.getHeight();
 | |
| 				case VERTICAL -> element.getWidth();
 | |
| 			};
 | |
| 		}
 | |
| 
 | |
| 		int getSecondaryLength(EqualSpacingLayout.ChildContainer container) {
 | |
| 			return switch (this) {
 | |
| 				case HORIZONTAL -> container.getHeight();
 | |
| 				case VERTICAL -> container.getWidth();
 | |
| 			};
 | |
| 		}
 | |
| 
 | |
| 		void setPrimaryPosition(EqualSpacingLayout.ChildContainer container, int position) {
 | |
| 			switch (this) {
 | |
| 				case HORIZONTAL:
 | |
| 					container.setX(position, container.getWidth());
 | |
| 					break;
 | |
| 				case VERTICAL:
 | |
| 					container.setY(position, container.getHeight());
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		void setSecondaryPosition(EqualSpacingLayout.ChildContainer container, int position, int length) {
 | |
| 			switch (this) {
 | |
| 				case HORIZONTAL:
 | |
| 					container.setY(position, length);
 | |
| 					break;
 | |
| 				case VERTICAL:
 | |
| 					container.setX(position, length);
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		int getPrimaryPosition(LayoutElement element) {
 | |
| 			return switch (this) {
 | |
| 				case HORIZONTAL -> element.getX();
 | |
| 				case VERTICAL -> element.getY();
 | |
| 			};
 | |
| 		}
 | |
| 
 | |
| 		int getSecondaryPosition(LayoutElement element) {
 | |
| 			return switch (this) {
 | |
| 				case HORIZONTAL -> element.getY();
 | |
| 				case VERTICAL -> element.getX();
 | |
| 			};
 | |
| 		}
 | |
| 	}
 | |
| }
 |