it is done
This commit is contained in:
103
core/src/mindustry/ui/Bar.java
Normal file
103
core/src/mindustry/ui/Bar.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package mindustry.ui;
|
||||
|
||||
import arc.*;
|
||||
import arc.func.*;
|
||||
import arc.graphics.*;
|
||||
import arc.graphics.g2d.*;
|
||||
import arc.math.*;
|
||||
import arc.math.geom.*;
|
||||
import arc.scene.*;
|
||||
import arc.scene.style.*;
|
||||
import arc.util.pooling.*;
|
||||
import mindustry.gen.*;
|
||||
|
||||
public class Bar extends Element{
|
||||
private static Rectangle scissor = new Rectangle();
|
||||
|
||||
private Floatp fraction;
|
||||
private String name = "";
|
||||
private float value, lastValue, blink;
|
||||
private Color blinkColor = new Color();
|
||||
|
||||
public Bar(String name, Color color, Floatp fraction){
|
||||
this.fraction = fraction;
|
||||
this.name = Core.bundle.get(name);
|
||||
this.blinkColor.set(color);
|
||||
lastValue = value = fraction.get();
|
||||
setColor(color);
|
||||
}
|
||||
|
||||
public Bar(Prov<String> name, Prov<Color> color, Floatp fraction){
|
||||
this.fraction = fraction;
|
||||
lastValue = value = Mathf.clamp(fraction.get());
|
||||
update(() -> {
|
||||
this.name = name.get();
|
||||
this.blinkColor.set(color.get());
|
||||
setColor(color.get());
|
||||
});
|
||||
}
|
||||
|
||||
public Bar(){
|
||||
|
||||
}
|
||||
|
||||
public void reset(float value){
|
||||
this.value = lastValue = blink = value;
|
||||
}
|
||||
|
||||
public void set(Prov<String> name, Floatp fraction, Color color){
|
||||
this.fraction = fraction;
|
||||
this.lastValue = fraction.get();
|
||||
this.blinkColor.set(color);
|
||||
setColor(color);
|
||||
update(() -> this.name = name.get());
|
||||
}
|
||||
|
||||
public Bar blink(Color color){
|
||||
blinkColor.set(color);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(){
|
||||
if(fraction == null) return;
|
||||
|
||||
float computed = Mathf.clamp(fraction.get());
|
||||
if(!Mathf.equal(lastValue, computed)){
|
||||
blink = 1f;
|
||||
lastValue = computed;
|
||||
}
|
||||
|
||||
blink = Mathf.lerpDelta(blink, 0f, 0.2f);
|
||||
value = Mathf.lerpDelta(value, computed, 0.15f);
|
||||
|
||||
Drawable bar = Tex.bar;
|
||||
|
||||
Draw.colorl(0.1f);
|
||||
bar.draw(x, y, width, height);
|
||||
Draw.color(color, blinkColor, blink);
|
||||
|
||||
Drawable top = Tex.barTop;
|
||||
float topWidth = width * value;
|
||||
|
||||
if(topWidth > Core.atlas.find("bar-top").getWidth()){
|
||||
top.draw(x, y, topWidth, height);
|
||||
}else{
|
||||
if(ScissorStack.pushScissors(scissor.set(x, y, topWidth, height))){
|
||||
top.draw(x, y, Core.atlas.find("bar-top").getWidth(), height);
|
||||
ScissorStack.popScissors();
|
||||
}
|
||||
}
|
||||
|
||||
Draw.color();
|
||||
|
||||
BitmapFont font = Fonts.outline;
|
||||
GlyphLayout lay = Pools.obtain(GlyphLayout.class, GlyphLayout::new);
|
||||
lay.setText(font, name);
|
||||
|
||||
font.setColor(Color.white);
|
||||
font.draw(name, x + width / 2f - lay.width / 2f, y + height / 2f + lay.height / 2f + 1);
|
||||
|
||||
Pools.free(lay);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user