Advertisement
Guest User

libgdx loader example

a guest
Feb 18th, 2012
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package com.nizarium.mygame;
  2.  
  3. import android.os.Handler.Callback;
  4. import android.os.Message;
  5.  
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.Input.Keys;
  8. import com.badlogic.gdx.files.FileHandle;
  9. import com.badlogic.gdx.graphics.Pixmap;
  10. import com.badlogic.gdx.graphics.Texture;
  11. import com.badlogic.gdx.graphics.TextureData;
  12. import com.badlogic.gdx.graphics.Pixmap.Format;
  13. import com.badlogic.gdx.graphics.TextureData.TextureDataType;
  14. import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
  15. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  16. import com.badlogic.gdx.scenes.scene2d.Actor;
  17. import com.badlogic.gdx.scenes.scene2d.Stage;
  18.  
  19. public class StageLoading extends GameStage
  20. {
  21.     private Callback onLoaded;
  22.    
  23.     public StageLoading(float width, float height, boolean stretch,
  24.             GameMain main)
  25.     {
  26.         super(width, height, stretch, main);
  27.        
  28.         init();
  29.     }
  30.  
  31.     private void init()
  32.     {
  33.         Pixmap blankPixmap = new Pixmap(1, 1, Format.RGBA8888);
  34.         blankPixmap.setColor(1, 1, 1, 1);
  35.         blankPixmap.drawPixel(0, 0);
  36.        
  37.         final Texture blank = new Texture(blankPixmap);
  38.        
  39.         this.addActor(new Actor("background")
  40.         {
  41.             @Override
  42.             public Actor hit(float x, float y)
  43.             {
  44.                 return null;
  45.             }
  46.  
  47.             @Override
  48.             public void draw(SpriteBatch batch, float parentAlpha)
  49.             {
  50.                 final String loadingStr = "Loading";
  51.  
  52.                 batch.draw(main.getGuiBackground(),
  53.                         this.getStage().centerX() - main.getGuiBackground().getWidth()
  54.                                 * 0.5f,
  55.                         this.getStage().centerY() - main.getGuiBackground().getHeight()
  56.                                 * 0.5f, main.getGuiBackground().getWidth(),
  57.                                 main.getGuiBackground().getHeight(), 0, 0, 1, 1);
  58.  
  59.                 TextBounds bounds = main.getFont().getBounds(loadingStr);
  60.                 main.getFont().draw(batch, loadingStr,
  61.                         this.getStage().centerX() - bounds.width * 0.5f,
  62.                         this.getStage().centerY() - bounds.height);
  63.                
  64.                 batch.draw(blank, 0, height() - 5, width() * main.getManager().getProgress(), 5);
  65.             }
  66.         });
  67.     }
  68.  
  69.     @Override
  70.     public void act(float delta)
  71.     {
  72.         super.act(delta);
  73.  
  74.         if (main.getManager().getProgress() == 1)
  75.         {
  76.             for (GameData.ObjectTypeData data : GameData.objects.values())
  77.             {
  78.                 data.init(main);
  79.             }
  80.            
  81.             if (onLoaded != null)
  82.                 onLoaded.handleMessage(new Message());
  83.         }
  84.     }
  85.  
  86.     @Override
  87.     public boolean keyDown(int keycode)
  88.     {
  89.         if (keycode == Keys.BACK)
  90.         {
  91.             System.exit(0);
  92.             return true;
  93.         }
  94.  
  95.         return super.keyDown(keycode);
  96.     }
  97.  
  98.     public Callback getOnLoaded()
  99.     {
  100.         return onLoaded;
  101.     }
  102.  
  103.     public void setOnLoaded(Callback onLoaded)
  104.     {
  105.         this.onLoaded = onLoaded;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement