Libgdx游戏引擎之Dialog组件

来源:互联网 发布:org.apache.cxf.jaxws 编辑:程序博客网 时间:2024/06/04 22:44

1、运行界面



2、示例代码

import com.badlogic.gdx.Gdx;import com.badlogic.gdx.graphics.Color;import com.badlogic.gdx.scenes.scene2d.Group;import com.badlogic.gdx.scenes.scene2d.InputEvent;import com.badlogic.gdx.scenes.scene2d.ui.Dialog;import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;import com.badlogic.gdx.scenes.scene2d.ui.Label;import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;import com.swallowgames.supermario.game.Assets;import com.swallowgames.supermario.utils.Utils;/** * <b>类名称:</b>ExitDialog * <b>类描述:</b>退出游戏对话框 * <b>创建人:</b>wanglong * <b>修改人:</b>wanglong * <b>修改时间:</b>2014-12-14 上午3:54:18 * <b>修改备注:</b>类初始化 * @version 1.0.0<br/> */public class ExitDialog extends Dialog{public ExitDialog(String title, WindowStyle windowStyle) {super(title, windowStyle);TextureRegionDrawable textureRegionDrawable = (TextureRegionDrawable) windowStyle.background;float regionWidth = textureRegionDrawable.getRegion().getRegionWidth();float regionHeight = textureRegionDrawable.getRegion().getRegionHeight();setColor(1, 1, 1, 0);setSize(regionWidth, regionHeight);setOrigin(getWidth()/2, getHeight()/2);setPosition(Utils.xAxisCenter(getWidth()), Utils.yAxisCenter(getHeight()));//标题FreeTypeFontActor titleActor = new FreeTypeFontActor(title, 24);titleActor.setColor(Color.YELLOW);titleActor.setPosition((getWidth() - titleActor.getWidth())/2, getHeight() - 4);addActor(titleActor);getContentTable().debug();LabelStyle labelStyle = new LabelStyle(Assets.instance.bitmapTrueFont.getBitmapFont(32), Color.YELLOW);getContentTable().add(new Label("确定退出游戏吗?", labelStyle));getButtonTable().debug();//取消按钮Group cancel = new Group();final ImageButton cancelButton = new ImageButton(new TextureRegionDrawable(Assets.instance.assetUI.greenButton));cancel.addActor(cancelButton);labelStyle = new LabelStyle(Assets.instance.bitmapTrueFont.getBitmapFont(24), Color.BLACK);Label cancelLabel = new Label("取 消", labelStyle);cancelLabel.setPosition((cancelButton.getWidth() - cancelLabel.getWidth())/2, 2.5f + (cancelButton.getHeight() - cancelLabel.getHeight())/2);cancel.addActor(cancelLabel);cancel.setSize(cancelButton.getWidth(), cancelButton.getHeight());cancel.addListener(new ClickListener(){@Overridepublic void clicked(InputEvent event, float x, float y) {super.clicked(event, x, y);ExitDialog.this.hide();}});getButtonTable().add(cancel).padRight(40);//确定按钮Group confirm = new Group();final ImageButton confirmButton = new ImageButton(new TextureRegionDrawable(Assets.instance.assetUI.redButton));confirm.addActor(confirmButton);labelStyle = new LabelStyle(Assets.instance.bitmapTrueFont.getBitmapFont(24), Color.WHITE);Label confirmLabel = new Label("确 定", labelStyle);confirmLabel.setPosition((confirmButton.getWidth() - confirmLabel.getWidth())/2, 2.5f + (confirmButton.getHeight() - confirmLabel.getHeight())/2);confirm.addActor(confirmLabel);confirm.setSize(confirmButton.getWidth(), confirmButton.getHeight());confirm.addListener(new ClickListener(){@Overridepublic void clicked(InputEvent event, float x, float y) {super.clicked(event, x, y);Gdx.app.exit();}});getButtonTable().add(confirm).padLeft(40);//内容表格单元getCells().get(0).padTop(40);//按钮表格单元getCells().get(1).padBottom(40);}}

3、使用代码

显示

WindowStyle windowStyle = new WindowStyle(new BitmapFont(), Color.YELLOW, new TextureRegionDrawable(Assets.instance.assetUI.popdialog));ExitDialog exitWindow = new ExitDialog("系统提示", windowStyle);
<pre code_snippet_id="551993" snippet_file_name="blog_20141214_3_5353603" name="code" class="java">exitWindow.show(stage);

移除

exitWindow.hide();

4、代码说明



这个区域是getButtonTable组件



这个区域是getContentTable组件

Dialog集成Window本质是一个Table组件,通过以下代码可以调整ButtonTable和ContentTable的间距

//内容表格单元
getCells().get(0).padTop(40);
//按钮表格单元
getCells().get(1).padBottom(40);


其中titleActor是我自定义的一个标题,方便调整位置。

FreeTypeFontActor titleActor = new FreeTypeFontActor(title, 24);
titleActor.setColor(Color.YELLOW);
titleActor.setPosition((getWidth() - titleActor.getWidth())/2, getHeight() - 4);
addActor(titleActor);


Dialog显示和隐藏方法说明

show()

public Dialog show (Stage stage) {clearActions();removeCaptureListener(ignoreTouchDown);previousKeyboardFocus = null;Actor actor = stage.getKeyboardFocus();if (actor != null && !actor.isDescendantOf(this)) previousKeyboardFocus = actor;previousScrollFocus = null;actor = stage.getScrollFocus();if (actor != null && !actor.isDescendantOf(this)) previousScrollFocus = actor;pack();setPosition(Math.round((stage.getWidth() - getWidth()) / 2), Math.round((stage.getHeight() - getHeight()) / 2));stage.addActor(this);stage.setKeyboardFocus(this);stage.setScrollFocus(this);if (fadeDuration > 0) {getColor().a = 0;addAction(Actions.fadeIn(fadeDuration, Interpolation.fade));}return this;}
该方法会把Satge上其他Actor的触摸事件,同时添加一个显示的动画

addAction(Actions.fadeIn(fadeDuration, Interpolation.fade));

避免我们自己去实现动画效果


hide()

/** Hides the dialog. Called automatically when a button is clicked. The default implementation fades out the dialog over * {@link #fadeDuration} seconds and then removes it from the stage. */public void hide () {Stage stage = getStage();if (stage != null) {if (previousKeyboardFocus != null && previousKeyboardFocus.getStage() == null) previousKeyboardFocus = null;Actor actor = stage.getKeyboardFocus();if (actor == null || actor.isDescendantOf(this)) stage.setKeyboardFocus(previousKeyboardFocus);if (previousScrollFocus != null && previousScrollFocus.getStage() == null) previousScrollFocus = null;actor = stage.getScrollFocus();if (actor == null || actor.isDescendantOf(this)) stage.setScrollFocus(previousScrollFocus);}if (fadeDuration > 0) {addCaptureListener(ignoreTouchDown);addAction(sequence(fadeOut(fadeDuration, Interpolation.fade), Actions.removeListener(ignoreTouchDown, true),Actions.removeActor()));} elseremove();}
隐藏方法会恢复Stage上其他的触摸事件,同样也实现了一个隐藏的效果

addCaptureListener(ignoreTouchDown);
addAction(sequence(fadeOut(fadeDuration, Interpolation.fade), Actions.removeListener(ignoreTouchDown, true),
Actions.removeActor()));

该方法会将Dialog从Stage上移除


通过设置Dialog的fadeDuration值可以调整显示和消除的时间,默认是0.4s

0 0