<commit_before>package de.lessvoid.nifty.effects;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import de.lessvoid.nifty.EndNotify;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.render.NiftyRenderEngine;
import de.lessvoid.nifty.render.RenderStates;
import de.lessvoid.nifty.spi.time.TimeProvider;

/**
 * manage all effects of an element.
 * @author void
 */
public class EffectManager {
  private Notify notify;
  private Map<EffectEventId, EffectProcessor> effectProcessor = new Hashtable<EffectEventId, EffectProcessor>();
  private List<EffectProcessor> effectProcessorList;
  private Falloff hoverFalloff;
  private NiftyRenderDeviceProxy renderDeviceProxy = new NiftyRenderDeviceProxy();
  private String alternateKey;
  private boolean isEmpty = true;
  private RenderPhase renderPhasePre = new RenderPhasePre();
  private RenderPhase renderPhasePost = new RenderPhasePost();
  private RenderPhase renderPhaseOverlay = new RenderPhaseOverlay();
  private RenderStates savedRenderStates = new RenderStates();

  /**
   * create a new effectManager with the given listener.
   */
  public EffectManager(final Notify notify) {
    this.notify = notify;
    this.alternateKey = null;

    effectProcessor.put(EffectEventId.onStartScreen, new EffectProcessor(new NotifyAdapter(EffectEventId.onStartScreen, notify), false));
    effectProcessor.put(EffectEventId.onEndScreen, new EffectProcessor(new NotifyAdapter(EffectEventId.onEndScreen, notify), true));
    effectProcessor.put(EffectEventId.onFocus, new EffectProcessor(new NotifyAdapter(EffectEventId.onFocus, notify), true));
    effectProcessor.put(EffectEventId.onGetFocus, new EffectProcessor(new NotifyAdapter(EffectEventId.onGetFocus, notify), false));
    effectProcessor.put(EffectEventId.onLostFocus, new EffectProcessor(new NotifyAdapter(EffectEventId.onLostFocus, notify), false));
    effectProcessor.put(EffectEventId.onClick, new EffectProcessor(new NotifyAdapter(EffectEventId.onClick, notify), false));
    effectProcessor.put(EffectEventId.onHover, new EffectProcessor(new NotifyAdapter(EffectEventId.onHover, notify), true));
    effectProcessor.put(EffectEventId.onStartHover, new EffectProcessor(new NotifyAdapter(EffectEventId.onStartHover, notify), false));
    effectProcessor.put(EffectEventId.onEndHover, new EffectProcessor(new NotifyAdapter(EffectEventId.onEndHover, notify), false));
    effectProcessor.put(EffectEventId.onActive, new EffectProcessor(new NotifyAdapter(EffectEventId.onActive, notify), true));
    effectProcessor.put(EffectEventId.onCustom, new EffectProcessor(new NotifyAdapter(EffectEventId.onCustom, notify), false));
    effectProcessor.put(EffectEventId.onShow, new EffectProcessor(new NotifyAdapter(EffectEventId.onShow, notify), false));
    effectProcessor.put(EffectEventId.onHide, new EffectProcessor(new NotifyAdapter(EffectEventId.onHide, notify), true));
    effectProcessor.put(EffectEventId.onEnabled, new EffectProcessor(new NotifyAdapter(EffectEventId.onEnabled, notify), true));
    effectProcessor.put(EffectEventId.onDisabled, new EffectProcessor(new NotifyAdapter(EffectEventId.onDisabled, notify), true));

    // we'll need to iterate over all effectProcessors later and so we keep them here in an ArrayList
    effectProcessorList = new ArrayList<EffectProcessor>(effectProcessor.values());
  }

  /**
   * register an effect.
   * @param id the id
   * @param e the effect
   */
  public void registerEffect(final EffectEventId id, final Effect e) {
    effectProcessor.get(id).registerEffect(e);
    isEmpty = false;
  }

  /**
   * start all effects with the given id for the given element.
   * @param id the effect id to start
   * @param w the element
   * @param time TimeProvider
   * @param listener the {@link EndNotify} to use.
   */
  public void startEffect(
      final EffectEventId id,
      final Element w,
      final TimeProvider time,
      final EndNotify listener) {
    effectProcessor.get(id).activate(listener, alternateKey, null);
  }

  public void startEffect(
      final EffectEventId id,
      final Element w,
      final TimeProvider time,
      final EndNotify listener,
      final String customKey) {
    effectProcessor.get(id).activate(listener, alternateKey, customKey);
  }

  public void stopEffect(final EffectEventId effectId) {
    effectProcessor.get(effectId).setActive(false);
  }

  /**
   * prepare rendering.
   * @param renderDevice RenderDevice
   */
  public void begin(final NiftyRenderEngine renderDevice, final Element element) {
    savedRenderStates.addAll();
    for (int i=0; i<effectProcessorList.size(); i++) {
      EffectProcessor processor = effectProcessorList.get(i);
      processor.getRenderStatesToSave(renderDeviceProxy);
      savedRenderStates.removeAll(renderDeviceProxy.getStates());
    }<commit_msg>bugfix: starting an effect now makes sure that the old effect stops<commit_after>package de.lessvoid.nifty.effects;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import de.lessvoid.nifty.EndNotify;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.render.NiftyRenderEngine;
import de.lessvoid.nifty.render.RenderStates;
import de.lessvoid.nifty.spi.time.TimeProvider;

/**
 * manage all effects of an element.
 * @author void
 */
public class EffectManager {
  private Notify notify;
  private Map<EffectEventId, EffectProcessor> effectProcessor = new Hashtable<EffectEventId, EffectProcessor>();
  private List<EffectProcessor> effectProcessorList;
  private Falloff hoverFalloff;
  private NiftyRenderDeviceProxy renderDeviceProxy = new NiftyRenderDeviceProxy();
  private String alternateKey;
  private boolean isEmpty = true;
  private RenderPhase renderPhasePre = new RenderPhasePre();
  private RenderPhase renderPhasePost = new RenderPhasePost();
  private RenderPhase renderPhaseOverlay = new RenderPhaseOverlay();
  private RenderStates savedRenderStates = new RenderStates();

  /**
   * create a new effectManager with the given listener.
   */
  public EffectManager(final Notify notify) {
    this.notify = notify;
    this.alternateKey = null;

    effectProcessor.put(EffectEventId.onStartScreen, new EffectProcessor(new NotifyAdapter(EffectEventId.onStartScreen, notify), false));
    effectProcessor.put(EffectEventId.onEndScreen, new EffectProcessor(new NotifyAdapter(EffectEventId.onEndScreen, notify), true));
    effectProcessor.put(EffectEventId.onFocus, new EffectProcessor(new NotifyAdapter(EffectEventId.onFocus, notify), true));
    effectProcessor.put(EffectEventId.onGetFocus, new EffectProcessor(new NotifyAdapter(EffectEventId.onGetFocus, notify), false));
    effectProcessor.put(EffectEventId.onLostFocus, new EffectProcessor(new NotifyAdapter(EffectEventId.onLostFocus, notify), false));
    effectProcessor.put(EffectEventId.onClick, new EffectProcessor(new NotifyAdapter(EffectEventId.onClick, notify), false));
    effectProcessor.put(EffectEventId.onHover, new EffectProcessor(new NotifyAdapter(EffectEventId.onHover, notify), true));
    effectProcessor.put(EffectEventId.onStartHover, new EffectProcessor(new NotifyAdapter(EffectEventId.onStartHover, notify), false));
    effectProcessor.put(EffectEventId.onEndHover, new EffectProcessor(new NotifyAdapter(EffectEventId.onEndHover, notify), false));
    effectProcessor.put(EffectEventId.onActive, new EffectProcessor(new NotifyAdapter(EffectEventId.onActive, notify), true));
    effectProcessor.put(EffectEventId.onCustom, new EffectProcessor(new NotifyAdapter(EffectEventId.onCustom, notify), false));
    effectProcessor.put(EffectEventId.onShow, new EffectProcessor(new NotifyAdapter(EffectEventId.onShow, notify), false));
    effectProcessor.put(EffectEventId.onHide, new EffectProcessor(new NotifyAdapter(EffectEventId.onHide, notify), true));
    effectProcessor.put(EffectEventId.onEnabled, new EffectProcessor(new NotifyAdapter(EffectEventId.onEnabled, notify), true));
    effectProcessor.put(EffectEventId.onDisabled, new EffectProcessor(new NotifyAdapter(EffectEventId.onDisabled, notify), true));

    // we'll need to iterate over all effectProcessors later and so we keep them here in an ArrayList
    effectProcessorList = new ArrayList<EffectProcessor>(effectProcessor.values());
  }

  /**
   * register an effect.
   * @param id the id
   * @param e the effect
   */
  public void registerEffect(final EffectEventId id, final Effect e) {
    effectProcessor.get(id).registerEffect(e);
    isEmpty = false;
  }

  /**
   * start all effects with the given id for the given element.
   * @param id the effect id to start
   * @param w the element
   * @param time TimeProvider
   * @param listener the {@link EndNotify} to use.
   */
  public void startEffect(
      final EffectEventId id,
      final Element w,
      final TimeProvider time,
      final EndNotify listener) {
    effectProcessor.get(id).activate(listener, alternateKey, null);
  }

  public void startEffect(
      final EffectEventId id,
      final Element w,
      final TimeProvider time,
      final EndNotify listener,
      final String customKey) {
    stopEffect(id);
    effectProcessor.get(id).activate(listener, alternateKey, customKey);
  }

  public void stopEffect(final EffectEventId effectId) {
    effectProcessor.get(effectId).setActive(false);
  }

  /**
   * prepare rendering.
   * @param renderDevice RenderDevice
   */
  public void begin(final NiftyRenderEngine renderDevice, final Element element) {
    savedRenderStates.addAll();
    for (int i=0; i<effectProcessorList.size(); i++) {
      EffectProcessor processor = effectProcessorList.get(i);
      processor.getRenderStatesToSave(renderDeviceProxy);
      savedRenderStates.removeAll(renderDeviceProxy.getStates());
    }
{'fraction_non_alphanumeric': 0.09185124974598659, 'fraction_numerical': 0.000203210729526519, 'mean_word_length': 5.24952380952381, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '419221', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2711, 'n_words': 668}