view game/IDMContainer.java @ 188:4568c8016377

Apply scale to IDMContainers also, not only to child widgets.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 20 Mar 2017 12:17:04 +0200
parents 55ea5821c802
children 189cd8fe2304
line wrap: on
line source

/*
 * Ristipolku IDM widget container
 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
 */
package game;

import java.awt.*;
import java.awt.event.*;
import java.util.*;



public class IDMContainer extends IDMWidget
{
    private ArrayList<IDMWidget> children, queue;
    private int iterated;
    private boolean modified;

    public IDMContainer()
    {
        children = new ArrayList<IDMWidget>();
        iterated = 0;
        modified = false;
    }

    synchronized public void add(IDMWidget widget)
    {
        widget.setParent(this);
        if (iterated > 0)
        {
            queue.add(0, widget);
            modified = true;
        }
        else
            children.add(widget);
    }

    synchronized public void remove(IDMWidget widget)
    {
        widget.setParent(null);
        if (iterated > 0)
        {
            queue.remove(widget);
            modified = true;
        }
        else
            children.remove(widget);
    }

    synchronized private void beginIteration()
    {
        modified = false;
        if (iterated == 0)
        {
            queue = new ArrayList<IDMWidget>();
            for (IDMWidget widget : children)
                queue.add(widget);
        }
        iterated++;
    }

    synchronized private void endIteration()
    {
        if (modified && iterated == 1)
        {
            children = queue;
            queue = null;
        }
        iterated--;
    }

    synchronized public void paint(Graphics2D g)
    {
        // Paint in reverse order
        ListIterator<IDMWidget> iter = children.listIterator(children.size());

        while (iter.hasPrevious())
        {
            final IDMWidget widget = iter.previous();
            widget.paint(g);
        }
    }

    synchronized public boolean mousePressed(MouseEvent e)
    {
        IDMWidget modal = getActiveModalWidget();
        if (modal != null)
            return modal.mousePressed(e);

        try {
            beginIteration();
            for (IDMWidget widget : children)
            {
                if (widget.contains(e.getPoint()))
                {
                    if (widget.mousePressed(e))
                        return true;
                }
            }
        }
        finally { endIteration(); }
        return false;
    }

    synchronized public boolean mouseReleased(MouseEvent e)
    {
        IDMWidget modal = getActiveModalWidget();
        if (modal != null)
            return modal.mouseReleased(e);

        try {
            beginIteration();
            for (IDMWidget widget : children)
            {
                if (widget.mouseReleased(e))
                    return true;
            }
        }
        finally { endIteration(); }
        return false;
    }

    synchronized public boolean mouseExited(MouseEvent e)
    {
        IDMWidget modal = getActiveModalWidget();
        if (modal != null)
            return modal.mouseExited(e);

        try {
            beginIteration();
            for (IDMWidget widget : children)
            {
                if (widget.mouseExited(e))
                    return true;
            }
        }
        finally { endIteration(); }
        return false;
    }

    synchronized public boolean mouseEntered(MouseEvent e)
    {
        IDMWidget modal = getActiveModalWidget();
        if (modal != null)
            return modal.mouseEntered(e);

        try {
            beginIteration();
            for (IDMWidget widget : children)
            {
                if (widget.contains(e.getPoint()))
                {
                    if (widget.mouseEntered(e))
                        return true;
                }
            }
        }
        finally { endIteration(); }
        return false;
    }

    synchronized public boolean keyPressed(KeyEvent e)
    {
        IDMWidget modal = getActiveModalWidget();
        if (modal != null)
            return modal.keyPressed(e);

        try {
            beginIteration();
            for (IDMWidget widget : children)
            {
                 if (widget.keyPressed(e))
                    return true;
            }
        }
        finally { endIteration(); }
        return false;
    }

    synchronized public void setScale(IDMPoint scale)
    {
        super.setScale(scale);
        beginIteration();
        for (IDMWidget widget : children)
        {
            widget.setScale(scale);
        }
        endIteration();
    }

    synchronized public boolean hasObject(Class c)
    {
        for (IDMWidget widget : children)
        {
            if (widget.getClass() == c)
                return true;
        }
        return false;
    }

    synchronized public boolean containsObject(Object o)
    {
        for (IDMWidget widget : children)
        {
            if (widget == o)
                return true;
        }
        return false;
    }

    synchronized public IDMWidget getActiveModalWidget()
    {
        for (IDMWidget widget : children)
        {
            if (widget.isModal())
                return widget;
        }
        return null;
    }
}