Esempio n. 1
0
  /**
   * 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);
  }
Esempio n. 2
0
 @Override
 public void onDraw(Canvas canvas) {
   if (fillTexture != null) {
     int size = TeraMath.floorToInt(canvas.size().x * getValue());
     int barWidth = fillTexture.getWidth();
     int offset = 0;
     if (time != null && animate) {
       offset = TeraMath.floorToInt(time.getRealTimeInMs() / 10) % barWidth;
     }
     int drawnWidth = 0;
     // Draw Offset
     if (offset != 0) {
       int drawWidth = Math.min(size, offset);
       canvas.drawTextureRaw(
           fillTexture,
           Rect2i.createFromMinAndSize(0, 0, drawWidth, canvas.size().y),
           ScaleMode.STRETCH,
           barWidth - offset,
           0,
           drawWidth,
           canvas.size().y);
       drawnWidth += drawWidth;
     }
     // Draw Remainder
     while (drawnWidth < size) {
       int drawWidth = Math.min(size - drawnWidth, barWidth);
       canvas.drawTextureRaw(
           fillTexture,
           Rect2i.createFromMinAndSize(drawnWidth, 0, drawWidth, canvas.size().y),
           ScaleMode.STRETCH,
           0,
           0,
           drawWidth,
           canvas.size().y);
       drawnWidth += drawWidth;
     }
   }
 }