Question: AbstractElement 2 D: public void updateAlignment ( ) { if ( this . parent ! = null ) { final float parentScale = this.parent.getStyle (

AbstractElement2D:
public void updateAlignment(){
if (this.parent != null){
final float parentScale = this.parent.getStyle(Style.SCALE);
this.anchorX =(this.getStyle(Style.H_ALIGNMENT).anchor(parent.getStyle(Style.WIDTH)* parentScale)+ parent.getStyle(Style.X)* parentScale);
this.anchorY =(this.getStyle(Style.V_ALIGNMENT).anchor(parent.getStyle(Style.HEIGHT)* parentScale)+ parent.getStyle(Style.Y)* parentScale);
this.offsetX = this.getStyle(Style.H_ALIGNMENT).offset(this.getStyle(Style.WIDTH), this.getStyle(Style.X));
this.offsetY = this.getStyle(Style.V_ALIGNMENT).offset(this.getStyle(Style.HEIGHT), this.getStyle(Style.Y));
}
this.getChildren().values().forEach(IElement::updateAlignment);
}
private void preRender(final Render2DEventPre event){
// Apply transitions and animations before rendering
this.transitions.values().forEach(t -> t.apply(this));
this.animations.values().forEach(t -> t.apply(this));
final MatrixStack matrices = event.getDrawContext().getMatrices();
matrices.push();
// Reverse MC scaling
// final float inverseScale =(float)(1F / MinecraftClient.getInstance().getWindow().getScaleFactor());
// matrices.scale(inverseScale, inverseScale, 1);
// Apply parent's anchor position
matrices.translate(anchorX, anchorY, 0.0f);
// Apply scaling style for dimensions, but not position
final float scale = this.getStyle(Style.SCALE);
matrices.scale(scale, scale, 1.0f);
// Apply unscaled offset positions (after scaling dimensions)
matrices.translate(offsetX / scale, offsetY / scale, 0.0f);
}
private void postRender(final Render2DEventPre event){
final MatrixStack matrices = event.getDrawContext().getMatrices();
final float scale =(float) MinecraftClient.getInstance().getWindow().getScaleFactor();
matrices.scale(scale, scale, 1);
// Render children without scaling back
final Object[] childrenArr = this.children.values().toArray();
for (final Object child : childrenArr){
if (!((IElement) child).isVisible()){
continue;
}
((IElement) child).doRender(event);
}
matrices.pop();
}
public final void doRender(final Render2DEventPre event){
final MatrixStack matrices = event.getDrawContext().getMatrices();
matrices.push();
final float inverseScale =(float)(1F / MinecraftClient.getInstance().getWindow().getScaleFactor());
matrices.scale(inverseScale, inverseScale, 1);
preRender(event);
render(event);
postRender(event);
matrices.pop();
}
public enum Alignment {
START(
(parentSize)->0,
(elementSize, position)-> position
),
CENTER(
(parentSize)-> parentSize /2.0F,
(elementSize, position)->-(elementSize /2.0F)+ position
),
END(
(parentSize)-> parentSize,
(elementSize, position)->-elementSize - position
);
private final IAlignmentAnchorFunction anchorFunction;
private final IAlignmentOffsetFunction offsetFunction;
Alignment(final IAlignmentAnchorFunction anchorFunction, final IAlignmentOffsetFunction offsetFunction){
this.anchorFunction = anchorFunction;
this.offsetFunction = offsetFunction;
}
public final float anchor(final float parentSize){
return this.anchorFunction.anchor(parentSize);
}
public final float offset(final float elementSize, final float position){
return this.offsetFunction.offset(elementSize, position);
}
public interface IAlignmentAnchorFunction {
float anchor(final float parentSize);
}
public interface IAlignmentOffsetFunction {
float offset(final float elementSize, final float position);
}
}
Some class:
this.watermarkTextElement .setStyle(Style.TEXT, "winstrol"); this.watermarkBackgroundElement .setStyle(Style.X,5F).setStyle(Style.Y,5F).setStyle(Style.WIDTH, 100F).setStyle(Style.HEIGHT, 20F)//.setStyle(Style.SCALE, 2F).setStyle(Style.BACKGROUND_COLOR, new Color(0,0,0,170).getRGB()); this.watermarkBackgroundElement.addChild(watermarkTextElement); As soon as I uncomment the SCALE part, it breaks. the text element does not render properly...it is supposed to render at the top left of the watermarkBackgroundElement, instead it calculates the position / renders it at TWICE needed...i.e. it renders at "position" x 10 y 10(due to .setStyle(Style.X,5F).setStyle(Style.Y,5F) with scale being 2, but in reality the scale should not affect the x and y like that...idk)(THE ISSUE ONLY HAPPENS TO CHILDREN ELEMENTS WHOS PARENTS SCALE IS NOT 1)
AbstractElement 2 D: public void updateAlignment

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!