@Override
public void onDraw(Canvas canvas) {
canvas.setPart(ICON_PART);
if (icon != null && getMaxValue() > 0) {
Vector2i iconSize = getIconSize(canvas);
float ratio = maxIcons * getValue() / getMaxValue();
int fullIcons = TeraMath.floorToInt(ratio);
boolean halfIcon = false;
if (ratio - fullIcons >= 0.5f) {
fullIcons++;
} else if (ratio - fullIcons > 0) {
halfIcon = true;
}
Vector2i offset = new Vector2i();
for (int i = 0; i < maxIcons; ++i) {
Rect2i iconRegion = Rect2i.createFromMinAndSize(offset, iconSize);
canvas.drawBackground(iconRegion);
if (ratio - i >= 0.5f) {
canvas.drawTexture(icon, iconRegion);
} else if (ratio - i > 0f) {
switch (halfIconMode) {
case SHRINK:
Vector2i halfSize = new Vector2i(iconSize);
halfSize.x /= 2;
halfSize.y /= 2;
canvas.drawTexture(icon,
Rect2i.createFromMinAndSize(new Vector2i(offset.x + halfSize.x / 2, offset.y + halfSize.y / 2), halfSize));
break;
case SPLIT:
canvas.drawTextureRaw(icon,
Rect2i.createFromMinAndSize(offset, new Vector2i(iconSize.x / 2, iconSize.y)),
ScaleMode.STRETCH, 0f, 0f, (float) (iconSize.x / 2) / iconSize.x, 1.0f);
break;
default:
canvas.drawTexture(icon, iconRegion);
break;