zoukankan      html  css  js  c++  java
  • libgdx退出对话框

    package com.fxb.newtest;
    
    import com.badlogic.gdx.ApplicationListener;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Input;
    import com.badlogic.gdx.InputAdapter;
    import com.badlogic.gdx.InputMultiplexer;
    import com.badlogic.gdx.Input.Keys;
    import com.badlogic.gdx.graphics.Color;
    import com.badlogic.gdx.graphics.GL10;
    import com.badlogic.gdx.graphics.Pixmap;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.Pixmap.Format;
    import com.badlogic.gdx.graphics.g2d.BitmapFont;
    import com.badlogic.gdx.scenes.scene2d.Actor;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
    import com.badlogic.gdx.scenes.scene2d.ui.Skin;
    import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
    import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
    import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
    import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
    import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
    import com.badlogic.gdx.scenes.scene2d.utils.Align;
    import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
    
    public class Lib005_CancelKey extends InputAdapter implements ApplicationListener{
    
        Stage stage;
        Skin skin;
        Dialog dialog1, dialog2;
    
        @Override
        public void create() {
            // TODO Auto-generated method stub
            
            CreateSkin();
            
            stage = new Stage();
            InputMultiplexer multiplexer = new InputMultiplexer();
            multiplexer.addProcessor( stage );
            multiplexer.addProcessor( this );
            Gdx.input.setInputProcessor( multiplexer );
            Gdx.input.setCatchBackKey( true );
            Gdx.input.setCatchMenuKey( true );
            
            TextButton textbutton1 = new TextButton( "Menu", skin );
            TextButton textbutton2 = new TextButton( "Exit", skin );
            
            textbutton1.addListener( new ChangeListener(){
                @Override
                public void changed(ChangeEvent event, Actor actor) {
                    // TODO Auto-generated method stub
                    dialog1.show(stage);
                }            
            });
            textbutton2.addListener( new ChangeListener(){
                @Override
                public void changed(ChangeEvent event, Actor actor) {
                    // TODO Auto-generated method stub
                    dialog2.show(stage);
                }            
            });    
            textbutton1.setPosition( 100, 100 );
            textbutton2.setPosition( 200, 100 );
            
            stage.addActor( textbutton1 );
            stage.addActor( textbutton2 );
            
        }
    
        private void CreateSkin(){
            skin = new Skin();
            Pixmap pixmap = new Pixmap( 1, 1, Format.RGBA8888 );
            pixmap.setColor( Color.GRAY );
            pixmap.fill();
            //draw1 = new TextureRegionDrawable(  );
            //texture = new Texture( pixmap );
            skin.add( "gray", new Texture(pixmap) );
            
            pixmap.setColor( Color.LIGHT_GRAY );
            pixmap.fill();
            skin.add( "light_gray", new Texture(pixmap) );
            
            BitmapFont font = new BitmapFont();
            WindowStyle windowStyle = new WindowStyle( font, Color.GREEN, skin.getDrawable( "light_gray" ) );
            
            LabelStyle labelStyle = new LabelStyle( font, Color.RED );
            ButtonStyle buttonStyle = new ButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null );
            TextButtonStyle textbuttonStyle = new TextButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null, font );
            skin.add( "default", buttonStyle );
            skin.add( "default", textbuttonStyle );
            skin.add( "default", windowStyle );
            skin.add( "default", labelStyle );
            
            dialog1 = new Dialog( "Menu", skin, "default" ){
                @Override
                protected void result(Object object) {
                    // TODO Auto-generated method stub
                    //System.out.println( "Chosen" + object );
                    if( object.toString().equals( "true" ) ){
                        System.out.println( "Yes" );
                        Gdx.app.exit();
                    }else{
                        System.out.println( "No" );
                    }            
                }};            
            dialog1.text("
    Menu").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
            dialog1.setTitleAlignment( Align.top );
            
            dialog2 = new Dialog( "Exit", skin, "default" ){
                @Override
                protected void result(Object object) {
                    // TODO Auto-generated method stub
                    //System.out.println( "Chosen" + object );
                    if( object.toString().equals( "true" ) ){
                        System.out.println( "Yes" );
                        Gdx.app.exit();
                    }else{
                        System.out.println( "No" );
                    }    
                }};        
            dialog2.text("
    Do you want to exit?").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
            dialog2.setTitleAlignment( Align.top );
            
        }
        
        @Override
        public void resize(int width, int height) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void render() {
            // TODO Auto-generated method stub
            Gdx.gl.glClearColor( 1, 1, 1, 1 );
            Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
            
            stage.act();
            stage.draw();
        }
    
        @Override
        public void pause() {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void resume() {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void dispose() {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public boolean keyDown(int keycode) {
            // TODO Auto-generated method stub
            
            if( keycode == Input.Keys.BACK ){
                Gdx.app.log( "back", "" );
                dialog2.show(stage);
            }
            if( keycode == Input.Keys.MENU ){
                Gdx.app.log( "menu", "" );
                dialog1.show(stage);
            }
            
            return false;
        }
    
    }

    libgdx开发中返回键(cancel)和菜单键(menu)的使用。

    开始时需要设置Gdx.input.enableCancelKey或者Gdx.input.enableMenuKey,然后捕获到相应事件时添加事件响应即可。

  • 相关阅读:
    pandas之DataFrame
    python浅拷贝和深拷贝
    Numpy 机器学习三剑客之Numpy
    django--验证码功能实现
    python基础题
    python武器库
    django-rest-framework
    django--admin组件
    【转载】关于DBUtils中QueryRunner的一些解读
    【转载】java中的反射
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/3728066.html
Copyright © 2011-2022 走看看