<commit_before>package jkanvas.painter;

import static jkanvas.util.ArrayUtil.*;

import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingUtilities;

import jkanvas.Camera;
import jkanvas.KanvasContext;
import jkanvas.KanvasPainter;
import jkanvas.util.PaintUtil;

/**
 * A render pass painter renders render passes.
 * 
 * @author Joschi <<EMAIL>>
 */
public class RenderpassPainter implements KanvasPainter {

  /** The HUD render passes. */
  private final List<HUDRenderpass> front;

  /** The normal render passes. */
  private final List<Renderpass> back;

  /** Creates an empty render pass painter. */
  public RenderpassPainter() {
    front = new ArrayList<>();
    back = new ArrayList<>();
  }

  @Override
  public boolean isAllowingPan(final Point2D p, final MouseEvent e) {
    return SwingUtilities.isLeftMouseButton(e);
  }

  /**
   * Adds a new render pass on top.
   * 
   * @param r The render pass.
   */
  public void addPass(final Renderpass r) {
    back.add(r);
  }

  /**
   * Removes a render pass.
   * 
   * @param r The render pass.
   */
  public void removePass(final Renderpass r) {
    back.remove(r);
  }

  /**
   * Adds a new HUD render pass on top.
   * 
   * @param r The HUD render pass.
   */
  public void addHUDPass(final HUDRenderpass r) {
    front.add(r);
  }

  /**
   * Removes a HUD render pass.
   * 
   * @param r The HUD render pass.
   */
  public void removeHUDPass(final HUDRenderpass r) {
    front.remove(r);
  }

  @Override
  public final void draw(final Graphics2D g, final KanvasContext ctx) {
    g.setColor(java.awt.Color.GRAY);
    draw(back, g, ctx);
  }

  /**
   * Draws all render passes given by the list. This method obeys
   * {@link jkanvas.Canvas#DEBUG_BBOX} by using the current color to draw the
   * bounding boxes.
   * 
   * @param passes The render passes to draw.
   * @param gfx The graphics context.
   * @param ctx The canvas context.
   * @see #draw(Graphics2D, KanvasContext)
   */
  public static final void draw(
      final List<Renderpass> passes, final Graphics2D gfx, final KanvasContext ctx) {
    final Rectangle2D view = ctx.getVisibleCanvas();
    final Rectangle2D bbox = new Rectangle2D.Double();
    for(final Renderpass r : passes) {
      if(!r.isVisible()) {
        continue;
      }
      getPassBoundingBox(bbox, r);
      if(!view.intersects(bbox)) {
        continue;
      }
      final Graphics2D g = (Graphics2D) gfx.create();
      g.clip(bbox);
      final double dx = r.getOffsetX();
      final double dy = r.getOffsetY();
      g.translate(dx, dy);
      final KanvasContext c = getContextFor(r, ctx);
      r.draw(g, c);
      g.dispose();
    }
    if(jkanvas.Canvas.DEBUG_BBOX) {
      final Graphics2D g = (Graphics2D) gfx.create();
      PaintUtil.setAlpha(g, 0.3);
      for(final Renderpass r : passes) {
        if(!r.isVisible()) {
          continue;
        }
        getPassBoundingBox(bbox, r);
        if(!view.intersects(bbox)) {
          continue;
        }
        g.fill(bbox);
      }
      g.dispose();
    }
  }

  @Override
  public final boolean click(final Camera cam, final Point2D p, final MouseEvent e) {
    return click(back, cam, p, e);
  }

  /**
   * Clicks on render passes.
   * 
   * @param passes The list of render passes.
   * @param cam The camera on which the interaction happened.
   * @param p The clicked point in canvas coordinates.
   * @param e The mouse event.
   * @return Whether the click was consumed.
   * @see #click(Camera, Point2D, MouseEvent)
   */
  public static final boolean click(
      final List<Renderpass> passes, final Camera cam, final Point2D p, final MouseEvent e) {
    final Rectangle2D bbox = new Rectangle2D.Double();
    for(final Renderpass r : reverseList(passes)) {
      if(!r.isVisible()) {
        continue;
      }
      r.getBoundingBox(bbox);
      final Point2D pos = getPositionFromCanvas(r, p);
      if(!bbox.contains(pos)) {
        continue;
      }
      if(r.click(cam, pos, e)) return true;
    }
    return false;
  }

  @Override
  public boolean doubleClick(final Camera cam, final Point2D p, final MouseEvent e) {
    if(doubleClick(back, cam, p, e)) return true;
    if(!Renderpass.USE_DOUBLE_CLICK_DEFAULT) return false;
    return doubleClickDefault(cam, e);
  }

  protected boolean doubleClickDefault(final Camera cam, final MouseEvent e) {
    final Rectangle2D box = new Rectangle2D.Double();
    getBoundingBox(box, back);
    return Renderpass.defaultDoubleClick(box, cam, e);
  }

  /**
   * Double clicks on render passes.
   * 
   * @param passes The list of render passes.
   * @param cam The camera on which the interaction happened.
   * @param p The double clicked point in canvas coordinates.
   * @param e The mouse event.
   * @return Whether the click was consumed.
   * @see #doubleClick(Camera, Point2D, MouseEvent)
   */
  public static final boolean doubleClick(
      final List<Renderpass> passes, final Camera cam, final Point2D p, final MouseEvent e) {
    final Rectangle2D bbox = new Rectangle2D.Double();
    for(final Renderpass r : reverseList(passes)) {<commit_msg>less overhead<commit_after>package jkanvas.painter;

import static jkanvas.util.ArrayUtil.*;

import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingUtilities;

import jkanvas.Camera;
import jkanvas.KanvasContext;
import jkanvas.KanvasPainter;
import jkanvas.util.PaintUtil;

/**
 * A render pass painter renders render passes.
 * 
 * @author Joschi <<EMAIL>>
 */
public class RenderpassPainter implements KanvasPainter {

  /** The HUD render passes. */
  private final List<HUDRenderpass> front;

  /** The normal render passes. */
  private final List<Renderpass> back;

  /** Creates an empty render pass painter. */
  public RenderpassPainter() {
    front = new ArrayList<>();
    back = new ArrayList<>();
  }

  @Override
  public boolean isAllowingPan(final Point2D p, final MouseEvent e) {
    return SwingUtilities.isLeftMouseButton(e);
  }

  /**
   * Adds a new render pass on top.
   * 
   * @param r The render pass.
   */
  public void addPass(final Renderpass r) {
    back.add(r);
  }

  /**
   * Removes a render pass.
   * 
   * @param r The render pass.
   */
  public void removePass(final Renderpass r) {
    back.remove(r);
  }

  /**
   * Adds a new HUD render pass on top.
   * 
   * @param r The HUD render pass.
   */
  public void addHUDPass(final HUDRenderpass r) {
    front.add(r);
  }

  /**
   * Removes a HUD render pass.
   * 
   * @param r The HUD render pass.
   */
  public void removeHUDPass(final HUDRenderpass r) {
    front.remove(r);
  }

  @Override
  public final void draw(final Graphics2D g, final KanvasContext ctx) {
    g.setColor(java.awt.Color.GRAY);
    draw(back, g, ctx);
  }

  /**
   * Draws all render passes given by the list. This method obeys
   * {@link jkanvas.Canvas#DEBUG_BBOX} by using the current color to draw the
   * bounding boxes.
   * 
   * @param passes The render passes to draw.
   * @param gfx The graphics context.
   * @param ctx The canvas context.
   * @see #draw(Graphics2D, KanvasContext)
   */
  public static final void draw(
      final List<Renderpass> passes, final Graphics2D gfx, final KanvasContext ctx) {
    final Rectangle2D view = ctx.getVisibleCanvas();
    final Rectangle2D bbox = new Rectangle2D.Double();
    for(final Renderpass r : passes) {
      if(!r.isVisible()) {
        continue;
      }
      getPassBoundingBox(bbox, r);
      if(!view.intersects(bbox)) {
        continue;
      }
      final Graphics2D g = (Graphics2D) gfx.create();
      g.clip(bbox);
      final double dx = r.getOffsetX();
      final double dy = r.getOffsetY();
      g.translate(dx, dy);
      final KanvasContext c = getContextFor(r, ctx);
      r.draw(g, c);
      g.dispose();
    }
    if(jkanvas.Canvas.DEBUG_BBOX) {
      final Graphics2D g = (Graphics2D) gfx.create();
      PaintUtil.setAlpha(g, 0.3);
      for(final Renderpass r : passes) {
        if(!r.isVisible()) {
          continue;
        }
        getPassBoundingBox(bbox, r);
        if(!view.intersects(bbox)) {
          continue;
        }
        g.fill(bbox);
      }
      g.dispose();
    }
  }

  @Override
  public final boolean click(final Camera cam, final Point2D p, final MouseEvent e) {
    return click(back, cam, p, e);
  }

  /**
   * Clicks on render passes.
   * 
   * @param passes The list of render passes.
   * @param cam The camera on which the interaction happened.
   * @param p The clicked point in canvas coordinates.
   * @param e The mouse event.
   * @return Whether the click was consumed.
   * @see #click(Camera, Point2D, MouseEvent)
   */
  public static final boolean click(
      final List<Renderpass> passes, final Camera cam, final Point2D p, final MouseEvent e) {
    final Rectangle2D bbox = new Rectangle2D.Double();
    for(final Renderpass r : reverseList(passes)) {
      if(!r.isVisible()) {
        continue;
      }
      r.getBoundingBox(bbox);
      final Point2D pos = getPositionFromCanvas(r, p);
      if(!bbox.contains(pos)) {
        continue;
      }
      if(r.click(cam, pos, e)) return true;
    }
    return false;
  }

  @Override
  public boolean doubleClick(final Camera cam, final Point2D p, final MouseEvent e) {
    if(doubleClick(back, cam, p, e)) return true;
    if(!Renderpass.USE_DOUBLE_CLICK_DEFAULT) return false;
    final Rectangle2D box = new Rectangle2D.Double();
    getBoundingBox(box);
    return Renderpass.defaultDoubleClick(box, cam, e);
  }

  /**
   * Double clicks on render passes.
   * 
   * @param passes The list of render passes.
   * @param cam The camera on which the interaction happened.
   * @param p The double clicked point in canvas coordinates.
   * @param e The mouse event.
   * @return Whether the click was consumed.
   * @see #doubleClick(Camera, Point2D, MouseEvent)
   */
  public static final boolean doubleClick(
      final List<Renderpass> passes, final Camera cam, final Point2D p, final MouseEvent e) {
    final Rectangle2D bbox = new Rectangle2D.Double();
    for(final Renderpass r : reverseList(passes)) {
{'fraction_non_alphanumeric': 0.10780846793462678, 'fraction_numerical': 0.005543343209404568, 'mean_word_length': 2.84, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6883964', 'n_tokens_mistral': 3411, 'n_tokens_neox': 3347, 'n_words': 1003}