libgdx学习记录8——对话框Dialog

Dialog在游戏中也很常用,尤其在设置、退出、商店、暂停等画面。Dialog的使用也可以通过skin实现,也可以自定义。

下面是一个简单的实例:

  1 package com.fxb.newtest;
  2 
  3 import com.badlogic.gdx.ApplicationListener;
  4 import com.badlogic.gdx.Gdx;
  5 import com.badlogic.gdx.Input.Keys;
  6 import com.badlogic.gdx.graphics.Color;
  7 import com.badlogic.gdx.graphics.GL10;
  8 import com.badlogic.gdx.graphics.Pixmap;
  9 import com.badlogic.gdx.graphics.Pixmap.Format;
 10 import com.badlogic.gdx.graphics.Texture;
 11 import com.badlogic.gdx.graphics.g2d.BitmapFont;
 12 import com.badlogic.gdx.scenes.scene2d.Actor;
 13 import com.badlogic.gdx.scenes.scene2d.InputEvent;
 14 import com.badlogic.gdx.scenes.scene2d.InputListener;
 15 import com.badlogic.gdx.scenes.scene2d.Stage;
 16 import com.badlogic.gdx.scenes.scene2d.ui.Button;
 17 import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
 18 import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
 19 import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
 20 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
 21 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
 22 import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
 23 import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
 24 import com.badlogic.gdx.scenes.scene2d.utils.Align;
 25 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
 26 import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
 27 
 28 public class Lib007_Dialog implements ApplicationListener{
 29 
 30     Stage stage;
 31     Dialog dialog;
 32     Drawable draw1;
 33     Drawable draw2;
 34     Pixmap pixmap;
 35     Texture texture;
 36     Skin skin;
 37     BitmapFont font;
 38     Dialog dialog2;
 39     
 40     @Override
 41     public void create() {
 42         // TODO Auto-generated method stub
 43     
 44         stage = new Stage();
 45         skin = new Skin();
 46         pixmap = new Pixmap( 1, 1, Format.RGBA8888 );
 47         pixmap.setColor( Color.GRAY );
 48         pixmap.fill();
 49         //draw1 = new TextureRegionDrawable(  );
 50         //texture = new Texture( pixmap );
 51         skin.add( "gray", new Texture(pixmap) );
 52         
 53         pixmap.setColor( Color.LIGHT_GRAY );
 54         pixmap.fill();
 55         skin.add( "light_gray", new Texture(pixmap) );
 56         
 57         font = new BitmapFont();
 58         WindowStyle windowStyle = new WindowStyle( font, Color.GREEN, skin.getDrawable( "light_gray" ) );
 59         dialog = new Dialog( "DialogTest", windowStyle );
 60         dialog.setTitleAlignment( Align.center | Align.top );
 61         dialog.setBounds( 10, 10, 100, 50 );
 62         
 63         LabelStyle labelStyle = new LabelStyle( font, Color.RED );
 64         ButtonStyle buttonStyle = new ButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null );
 65         TextButtonStyle textbuttonStyle = new TextButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null, font );
 66         skin.add( "default", buttonStyle );
 67         skin.add( "default", textbuttonStyle );
 68         skin.add( "default", windowStyle );
 69         skin.add( "default", labelStyle );
 70         
 71         Button button1 = new Button( skin );
 72         button1.setBounds( 10, 10, 30, 20 );
 73         TextButton textbutton1 = new TextButton( "Click", skin );
 74         textbutton1.setBounds( 90-30, 10, 30, 20 );
 75         dialog.addActor( button1 );
 76         dialog.addActor( textbutton1 );
 77                 
 78         stage.addActor( dialog );
 79         //dialog2.row().fill().expand();
 80             
 81         dialog2 = new Dialog( "Dialog2", skin, "default" ){
 82             @Override
 83             protected void result(Object object) {
 84                 // TODO Auto-generated method stub
 85                 //System.out.println( "Chosen" + object );
 86                 if( object.toString().equals( "true" ) ){
 87                     System.out.println( "Yes" );
 88                     Gdx.app.exit();
 89                 }else{
 90                     System.out.println( "No" );
 91                 }            
 92             }        
 93         };            
 94         dialog2.text("
Do you want to exit?").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
 95         dialog2.setTitleAlignment( Align.top );
 96         dialog2.pack();
 97         //dialog2.show( stage );
 98             
 99         //TextButton textbutton2 = new TextButton( "Click", skin );
100         textbutton1.addListener( new ChangeListener()
101         {
102             @Override
103             public void changed(ChangeEvent event, Actor actor) {
104                 // TODO Auto-generated method stub
105                 dialog2.show( stage );
106             }
107         });
108                     
109         //stage.addActor( textbutton2 );
110         Gdx.input.setInputProcessor( stage );
111     }
112 
113     @Override
114     public void resize(int width, int height) {
115         // TODO Auto-generated method stub
116         
117     }
118 
119     @Override
120     public void render() {
121         // TODO Auto-generated method stub
122         Gdx.gl.glClearColor( 1, 1, 1, 1 );
123         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
124         
125         stage.act();
126         stage.draw();
127         
128     }
129 
130     @Override
131     public void pause() {
132         // TODO Auto-generated method stub    
133     }
134 
135     @Override
136     public void resume() {
137         // TODO Auto-generated method stub        
138     }
139 
140     @Override
141     public void dispose() {
142         // TODO Auto-generated method stub
143         skin.dispose();
144         stage.dispose();
145         texture.dispose();
146     }
147 
148 }

运行效果:

原文地址:https://www.cnblogs.com/MiniHouse/p/3740732.html