<commit_before>/*******************************************************************************
 * Copyright 2011 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.scenes.scene2d.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.BitmapFontCache;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ActorEvent;
import com.badlogic.gdx.scenes.scene2d.ActorListener;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;

/** A table that can be dragged and act as a modal window.
 * <p>
 * The preferred size of a window is the preferred size of the children as layed out by the table. After adding children to the
 * window, it can be convenient to call {@link #pack()} to size the window to the size of the children.
 * @author <NAME> */
public class Window extends Table {
	private WindowStyle style;
	private String title;
	private BitmapFontCache titleCache;
	boolean isMovable = true, isModal;
	final Vector2 dragOffset = new Vector2();
	boolean dragging;

	public Window (Skin skin) {
		this("", skin.getStyle(WindowStyle.class));
		setSkin(skin);
	}

	public Window (String title, Skin skin) {
		this(title, skin.getStyle(WindowStyle.class));
		setSkin(skin);
	}

	public Window (String title, WindowStyle style) {
		if (title == null) throw new IllegalArgumentException("title cannot be null.");
		setClip(true);
		this.title = title;
		setStyle(style);
		setWidth(150);
		setHeight(150);

		addCaptureListener(new ActorListener() {
			public boolean touchDown (ActorEvent event, float x, float y, int pointer, int button) {
				if (pointer != 0) return false;
				toFront();
				return false;
			}
		});
		addListener(new ActorListener() {
			public boolean touchDown (ActorEvent event, float x, float y, int pointer, int button) {
				if (pointer != 0) return false;
				dragging = isMovable && getHeight() - y <= getTitleBarHeight() && y < getHeight() && x > 0 && x < getWidth();
				dragOffset.set(x, y);
				return true;
			}

			public void touchDragged (ActorEvent event, float x, float y, int pointer) {
				if (!dragging) return;
				translate(x - dragOffset.x, y - dragOffset.y);
			}
		});
	}

	public void setStyle (WindowStyle style) {
		if (style == null) throw new IllegalArgumentException("style cannot be null.");
		this.style = style;
		setBackground(style.background);
		titleCache = new BitmapFontCache(style.titleFont);
		titleCache.setColor(style.titleFontColor);
		invalidateHierarchy();
	}

	/** Returns the window's style. Modifying the returned style may not have an effect until {@link #setStyle(WindowStyle)} is
	 * called. */
	public WindowStyle getStyle () {
		return style;
	}

	float getTitleBarHeight () {
		return getPadTop().height(this);
	}

	public void layout () {
		super.layout();
		TextBounds bounds = style.titleFont.getMultiLineBounds(title);
		titleCache.setMultiLineText(title, getWidth() / 2 - bounds.width / 2, getHeight() - getTitleBarHeight() / 2 + bounds.height
			/ 2);
	}

	protected void drawBackground (SpriteBatch batch, float parentAlpha) {
		super.drawBackground(batch, parentAlpha);
		// Draw the title without the batch transformed or clipping applied.
		titleCache.setPosition(getX(), getY());
		titleCache.draw(batch, parentAlpha);
	}

	public Actor hit (float x, float y) {
		Actor hit = super.hit(x, y);
		if (hit == null && isModal) return this;
		return hit;
	}

	public void setTitle (String title) {
		this.title = title;
	}

	public String getTitle () {
		return title;
	}

	public void setMovable (boolean isMovable) {
		this.isMovable = isMovable;
	}

	public void setModal (boolean isModal) {
		this.isModal = isModal;
	}

	/** The style for a window, see {@link Window}.
	 * @author <NAME> */
	static public class WindowStyle {
		public Drawable background;
		public BitmapFont titleFont;
		public Color titleFontColor = new Color(1, 1, 1, 1);

		public WindowStyle () {
		}

		public WindowStyle (BitmapFont titleFont, Color titleFontColor, Drawable background) {
			this.background = background;
			this.titleFont = titleFont;
			this.titleFontColor.set(titleFontColor);
		}

		public WindowStyle (WindowStyle style) {
			this.background = style.background;
			this.titleFont = style.titleFont;
			this.titleFontColor = new Color(style.titleFontColor);
		}
	}
}<commit_msg>Fixed issue 909, added isDragging to Window.<commit_after>/*******************************************************************************
 * Copyright 2011 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.scenes.scene2d.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.BitmapFontCache;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ActorEvent;
import com.badlogic.gdx.scenes.scene2d.ActorListener;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;

/** A table that can be dragged and act as a modal window.
 * <p>
 * The preferred size of a window is the preferred size of the children as layed out by the table. After adding children to the
 * window, it can be convenient to call {@link #pack()} to size the window to the size of the children.
 * @author <NAME> */
public class Window extends Table {
	private WindowStyle style;
	private String title;
	private BitmapFontCache titleCache;
	boolean isMovable = true, isModal;
	final Vector2 dragOffset = new Vector2();
	boolean dragging;

	public Window (Skin skin) {
		this("", skin.getStyle(WindowStyle.class));
		setSkin(skin);
	}

	public Window (String title, Skin skin) {
		this(title, skin.getStyle(WindowStyle.class));
		setSkin(skin);
	}

	public Window (String title, WindowStyle style) {
		if (title == null) throw new IllegalArgumentException("title cannot be null.");
		setClip(true);
		this.title = title;
		setStyle(style);
		setWidth(150);
		setHeight(150);

		addCaptureListener(new ActorListener() {
			public boolean touchDown (ActorEvent event, float x, float y, int pointer, int button) {
				if (pointer != 0) return false;
				toFront();
				return false;
			}
		});
		addListener(new ActorListener() {
			public boolean touchDown (ActorEvent event, float x, float y, int pointer, int button) {
				if (pointer != 0) return false;
				dragging = isMovable && getHeight() - y <= getTitleBarHeight() && y < getHeight() && x > 0 && x < getWidth();
				dragOffset.set(x, y);
				return true;
			}

			public void touchDragged (ActorEvent event, float x, float y, int pointer) {
				if (!dragging) return;
				translate(x - dragOffset.x, y - dragOffset.y);
			}
		});
	}

	public void setStyle (WindowStyle style) {
		if (style == null) throw new IllegalArgumentException("style cannot be null.");
		this.style = style;
		setBackground(style.background);
		titleCache = new BitmapFontCache(style.titleFont);
		titleCache.setColor(style.titleFontColor);
		invalidateHierarchy();
	}

	/** Returns the window's style. Modifying the returned style may not have an effect until {@link #setStyle(WindowStyle)} is
	 * called. */
	public WindowStyle getStyle () {
		return style;
	}

	float getTitleBarHeight () {
		return getPadTop().height(this);
	}

	public void layout () {
		super.layout();
		TextBounds bounds = style.titleFont.getMultiLineBounds(title);
		titleCache.setMultiLineText(title, getWidth() / 2 - bounds.width / 2, getHeight() - getTitleBarHeight() / 2 + bounds.height
			/ 2);
	}

	protected void drawBackground (SpriteBatch batch, float parentAlpha) {
		super.drawBackground(batch, parentAlpha);
		// Draw the title without the batch transformed or clipping applied.
		titleCache.setPosition(getX(), getY());
		titleCache.draw(batch, parentAlpha);
	}

	public Actor hit (float x, float y) {
		Actor hit = super.hit(x, y);
		if (hit == null && isModal) return this;
		return hit;
	}

	public void setTitle (String title) {
		this.title = title;
	}

	public String getTitle () {
		return title;
	}

	public void setMovable (boolean isMovable) {
		this.isMovable = isMovable;
	}

	public void setModal (boolean isModal) {
		this.isModal = isModal;
	}

	public boolean isDragging () {
		return dragging;
	}

	/** The style for a window, see {@link Window}.
	 * @author <NAME> */
	static public class WindowStyle {
		public Drawable background;
		public BitmapFont titleFont;
		public Color titleFontColor = new Color(1, 1, 1, 1);

		public WindowStyle () {
		}

		public WindowStyle (BitmapFont titleFont, Color titleFontColor, Drawable background) {
			this.background = background;
			this.titleFont = titleFont;
			this.titleFontColor.set(titleFontColor);
		}

		public WindowStyle (WindowStyle style) {
			this.background = style.background;
			this.titleFont = style.titleFont;
			this.titleFontColor = new Color(style.titleFontColor);
		}
	}
}
{'fraction_non_alphanumeric': 0.13415205774819536, 'fraction_numerical': 0.007781006843536139, 'mean_word_length': 4.219178082191781, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6983077', 'n_tokens_mistral': 3518, 'n_tokens_neox': 3165, 'n_words': 1007}