LabelWidget
All checks were successful
/ Auto-Build-App (push) Successful in 2m20s

This commit is contained in:
pietru 2024-09-30 23:46:37 +02:00
parent d1779eb9a8
commit 92a846b7b5
2 changed files with 56 additions and 2 deletions

View file

@ -137,8 +137,7 @@ public class CustomScreen extends BaseItemScreen {
.height(data.getFloat("height",10F));
});
components.put("label",(table,screen,data)->{
Label lbl = new Label(data.getString("text",""),skin);
lbl.setFontScale(data.getFloat("font_scale",1F));
LabelWidget lbl = new LabelWidget(data.getString("text",""));
table.add(lbl);
});
components.put("change_ui_text_button",(table,screen,data)->{

View file

@ -0,0 +1,55 @@
package net.pietru.omni_power.ui;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
import com.badlogic.gdx.utils.viewport.Viewport;
import finalforeach.cosmicreach.ui.FontRenderer;
public class LabelWidget extends Widget {
private static final Vector2 tmpVec = new Vector2();
Vector2 textDim;
String text;
public LabelWidget(String text) {
this.textDim = new Vector2();
this.text = text;
this.setWidth(text.length()*4);
}
public void setText(String text) {
this.text = text;
this.setWidth(text.length()*4);
}
public String getText() {
return text;
}
public void draw(Batch batch, float parentAlpha){
super.draw(batch,parentAlpha);
batch.setColor(Color.WHITE);
drawLabelTextWithDropShadow(batch,0,0,Color.GRAY);
}
public void drawLabelTextWithDropShadow(Batch batch, float xOff, float yOff, Color dropShadowColor) {
Color batchColor = batch.getColor();
float oldR = batchColor.r;
float oldG = batchColor.g;
float oldB = batchColor.b;
float oldA = batchColor.a;
batch.setColor(dropShadowColor);
this.drawLabelText(batch, 2.0F, -2.0F);
batch.setColor(oldR, oldG, oldB, oldA);
this.drawLabelText(batch, 0.0F, 0.0F);
}
public void drawLabelText(Batch batch, float xOff, float yOff) {
Viewport viewport = this.getStage().getViewport();
FontRenderer.getTextDimensions(viewport, this.text, this.textDim);
float x = this.getX()- this.textDim.x;
float y = this.getY();
FontRenderer.drawText(batch, viewport, this.text, x + xOff, y + yOff, true);
}
}