/** * Defines how the fluid container widget is drawn. * * @param canvas The canvas on which the widget is drawn */ @Override public void onDraw(Canvas canvas) { TextureRegion texture = getImage(); if (texture != null) { FluidInventoryComponent fluidInventory = entity.getComponent(FluidInventoryComponent.class); FluidComponent fluid = fluidInventory.fluidSlots.get(slotNo).getComponent(FluidComponent.class); float maxVolume = fluidInventory.maximumVolumes.get(slotNo); float currentVolume = 0f; String fluidType = null; FluidRenderer fluidRenderer = null; if (fluid != null) { currentVolume = fluid.volume; fluidType = fluid.fluidType; float result = fluid.volume / maxVolume; fluidRenderer = fluidRegistry.getFluidRenderer(fluid.fluidType); Vector2i size = canvas.size(); if (minY < maxY) { float yPerc = 1f * (minY + result * (maxY - minY)) / texture.getHeight(); fluidRenderer.renderFluid( canvas, Rect2i.createFromMinAndSize(minX, minY, maxX, Math.round(yPerc * size.y) - minY)); } else { float yPerc = 1f * (minY - result * (minY - maxY)) / texture.getHeight(); int y = Math.round(yPerc * size.y); fluidRenderer.renderFluid(canvas, Rect2i.createFromMinAndSize(minX, y, maxX, minY - y)); } } canvas.drawTexture(texture, canvas.getRegion()); setTooltipDelay(0); String fluidDisplay = fluidType == null ? "Fluid" : fluidRenderer.getFluidName(); setTooltip(String.format(fluidDisplay + ": %.0f/%.0f", currentVolume, maxVolume)); } canvas.addInteractionRegion(listener); }