2017-07-31 83 views
0

我有一個需要看起來像這樣的項目:如何在libgdx中創建不帶標題欄的窗口

enter image description here

使用的窗口,我得到以下結果

enter image description here

我用下面的代碼創建了上述結果

private Table buildControlsWindowLayer() { 
     Window window = new Window("", skinLibgdx); 
     // + play button 
     playButton = new Button(maputoSkin, "play"); 
     window.add(playButton).pad(10, 0, 10, 0).row(); 
     playButton.addListener(listener); 
     // + options button 
     optionsButton = new Button(maputoSkin, "options"); 
     window.add(optionsButton).pad(10, 0, 10, 0).row(); 
     optionsButton.addListener(listener); 
     // + leaders button 
     leadersButton = new Button(maputoSkin, "leaders"); 
     window.add(leadersButton).pad(10, 0, 10, 0).row(); 
     leadersButton.addListener(listener); 
     if (Constants.DEBUG_FLAG_MENU_SCREEN) window.debug(); 
     window.pack(); 
     window.setPosition(Constants.VIEWPORT_GUI_WIDTH/2 - window.getWidth()/2, 
          Constants.VIEWPORT_GUI_HEIGHT/2 - window.getHeight()); 
     return window; 
    } 

我的問題

一,我怎麼能擺脫標題欄

二,如何設置我自己的背景而不是libgdx默認

三。這種方法適用於第三種情況嗎?

回答

0

我的做法是使用表而不是窗口,從皮膚

Skin skin = SkinManager.getInstance().getSkin(); 
    final Table outerTable = new Table(); 
    outerTable.setFillParent(true); 
    stage.addActor(outerTable); 

    // Layer to hold the buttons and dark background 
    Table buttonsTable = new Table(); 

    playButton = new TextButton("Play", skin, "play"); 
    settingsButton = new TextButton("Setting", skin, "setting"); 
    highScoreButton = new TextButton("Leaders", skin, "leaders"); 

    buttonsTable.add(playButton).width().height().pad(); 
    buttonsTable.row(); 
    buttonsTable.add(settingsButton).width().height().pad(0); 
    buttonsTable.row(); 
    buttonsTable.add(highScoreButton).width().height().pad(); 
    buttonsTable.row(); 
    // + background color 
    buttonsTable.setBackground(controlsBackground); 

    // Adding button table to outer table 
    outerTable.add(buttonsTable).colspan(2).expand(); 

定義其爲ninepatch繪製背景,我離開width()height()pad()空的,因爲它們依賴於你的世界的大小。 還有一件事

controlsBackground = new NinePatchDrawable(new NinePatch(
       SkinManager.getInstance().getSkin().getRegion("menu-background"), 10, 10, 10, 10 
     )); 
1

com.badlogic.gdx.scenes.scene2d.ui.Window帶有一個構造函數:Window(java.lang.String title, Window.WindowStyle style) 並且還有一個setStyle(Window.WindowStyle style)方法。 Window.WindowStyle類允許您爲此窗口定義樣式,包括背景。根據libgdxs DOC(https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Window.WindowStyle.html

至於標題欄的窗口是一個:

「可以被拖動並作爲一個模式窗口的頂部填充被用作窗口的標題高度A表」 ,我沒有找到任何直接擺脫標題欄的方法,但是如果您擴展窗口類,則可以重寫某些功能以實現所需的結果。看看它的來源:https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Window.java

要回答第三個問題,你必須提供更多的信息,你在這種情況下的含義是什麼?

0
private Table buildControlsWindowLayer() { 
     SpriteDrawable bgDrawble=new SpriteDrawable(new Sprite(createTexture(120, 400, Color.BLACK, 1))); 
     WindowStyle windowStyle=new WindowStyle(new BitmapFont(), Color.GREEN, bgDrawble); 
     Window window = new Window("", windowStyle); 
     // window.setWidth(0); 
     // + play button 
     SpriteDrawable upDrawble=new SpriteDrawable(new Sprite(createTexture(100, 40, Color.RED, 1))); 
     SpriteDrawable downDrawble=new SpriteDrawable(new Sprite(createTexture(100, 40, Color.RED, 1))); 
     SpriteDrawable cheDrawble=new SpriteDrawable(new Sprite(createTexture(100, 40, Color.RED, 1))); 
     TextButtonStyle btStyle=new TextButtonStyle(upDrawble, downDrawble, cheDrawble, new BitmapFont()); 
     TextButton playButton = new TextButton("play",btStyle); 
     window.add(playButton).pad(10, 0, 10, 0).row(); 
     //playButton.addListener(listener); 
     // + options button 
     TextButton optionsButton = new TextButton("options",btStyle); 
     window.add(optionsButton).pad(10, 0, 10, 0).row(); 
     // optionsButton.addListener(listener); 
     // + leaders button 
     TextButton leadersButton = new TextButton("leaders",btStyle); 
     window.add(leadersButton).pad(10, 0, 10, 0).row(); 
     //leadersButton.addListener(listener); 
     /// if (Constants.DEBUG_FLAG_MENU_SCREEN) window.debug(); 
     window.pack(); 
     // window.getTitleTable() ; 
     return window; 
    } 



public static Texture createTexture(int width, int height, Color col, 
      float alfa) { 
     Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888); 
     Color color = col; 
     pixmap.setColor(color.r, color.g, color.b, alfa); 
     pixmap.fillRectangle(0, 0, width, height); 

     Texture pixmaptexture = new Texture(pixmap); 
     return pixmaptexture; 
    } 
相關問題