/******************************************************************************* * Copyright 2014 See AUTHORS file. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.badlogic.gdx.ai.tests.utils.scene2d; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Window; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; /** A {@code CollapsableWindow} can be expanded/collapsed with a double click on the title bar. * * @author Xoppa */ public class CollapsableWindow extends Window { private boolean collapsed; private float collapseHeight = 20f; private float expandHeight; public CollapsableWindow (String title, Skin skin) { super(title, skin); addListener(new ClickListener() { @Override public void clicked (InputEvent event, float x, float y) { if (getTapCount() == 2 && getHeight() - y <= getPadTop() && y < getHeight() && x > 0 && x < getWidth()) toggleCollapsed(); } }); } public void expand () { if (!collapsed) return; setHeight(expandHeight); setY(getY() - expandHeight + collapseHeight); collapsed = false; } public void collapse () { if (collapsed) return; expandHeight = getHeight(); setHeight(collapseHeight); setY(getY() + expandHeight - collapseHeight); collapsed = true; if (getStage() != null) getStage().setScrollFocus(null); } public void toggleCollapsed () { if (collapsed) expand(); else collapse(); } public boolean isCollapsed () { return collapsed; } }