Advertisement
Guest User

Untitled

a guest
May 29th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.82 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package mygame;
  6.  
  7. import com.jme3.app.Application;
  8. import com.jme3.app.SimpleApplication;
  9. import com.jme3.app.state.AbstractAppState;
  10. import com.jme3.app.state.AppStateManager;
  11. import com.jme3.asset.AssetManager;
  12. import com.jme3.asset.TextureKey;
  13. import com.jme3.font.BitmapFont;
  14. import com.jme3.font.BitmapFont.Align;
  15. import com.jme3.font.BitmapFont.VAlign;
  16. import com.jme3.font.LineWrapMode;
  17. import com.jme3.math.ColorRGBA;
  18. import com.jme3.math.Vector2f;
  19. import com.jme3.math.Vector4f;
  20. import com.jme3.texture.Texture;
  21. import tonegod.gui.controls.extras.Indicator;
  22. import tonegod.gui.controls.extras.android.Joystick;
  23. import tonegod.gui.controls.text.TextElement;
  24. import tonegod.gui.core.Screen;
  25. import tonegod.gui.effects.Effect;
  26.  
  27. /**
  28.  *
  29.  * @author Bob
  30.  */
  31. public class GuiManager extends AbstractAppState {
  32.  
  33.   private SimpleApplication app;
  34.   private AppStateManager   stateManager;
  35.   private AssetManager      assetManager;
  36.   private Indicator         fireInd;
  37.   private Indicator         woodInd;
  38.   private TextElement       scoreText;
  39.   private Joystick          stick;
  40.   private Screen            screen;
  41.   private BitmapFont        font;
  42.   private Player            player;
  43.  
  44.   @Override
  45.   public void initialize(AppStateManager stateManager, Application app){
  46.     super.initialize(stateManager, app);
  47.     this.app          = (SimpleApplication) app;
  48.     this.stateManager = this.app.getStateManager();
  49.     this.assetManager = this.app.getAssetManager();
  50.     this.player       = this.app.getStateManager().getState(PlayerManager.class).player;
  51.     font              = this.assetManager.loadFont("Interface/Fonts/Impact.fnt");
  52.     screen            = new Screen(app, "tonegod/gui/style/atlasdef/style_map.gui.xml");
  53.     screen.setUseTextureAtlas(true,"tonegod/gui/style/atlasdef/atlas.png");
  54.     screen.setUseMultiTouch(true);
  55.     this.app.getGuiNode().addControl(screen);
  56.     this.app.getInputManager().setSimulateMouse(true);
  57.     initFireLevel();
  58.     initWoodLevel();
  59.     initScoreDisplay();
  60.     initJoyStick();
  61.     }
  62.  
  63.  
  64.   private void initJoyStick(){
  65.     stick = new Joystick(screen, Vector2f.ZERO, (int)10) {
  66.     @Override
  67.     public void onUpdate(float tpf, float deltaX, float deltaY) {
  68.         float dzVal = 0.002f;  // Dead zone threshold
  69.             if (deltaX < -dzVal) {
  70.                 stateManager.getState(InteractionManager.class).left = true;
  71.                 stateManager.getState(InteractionManager.class).right = false;
  72.             } else if (deltaX > dzVal) {
  73.                 stateManager.getState(InteractionManager.class).left = false;
  74.                 stateManager.getState(InteractionManager.class).right = true;
  75.             } else {
  76.                 stateManager.getState(InteractionManager.class).left = false;
  77.                 stateManager.getState(InteractionManager.class).right = false;
  78.             }
  79.           }
  80.         };
  81.       // getGUIRegion returns region info “x=0|y=0|w=50|h=50″, etc
  82.       TextureKey key = new TextureKey("Textures/barrel/D.png");
  83.       Texture tex = assetManager.loadTexture(key);
  84.       stick.setTextureAtlasImage(tex, "x=20|y=20|w=120|h=35");
  85.       stick.getThumb().setTextureAtlasImage(tex, "x=20|y=20|w=120|h=35");
  86.       // Make the Joystick semi-transparent
  87.       stick.getElementMaterial().setColor("Color", new ColorRGBA(1,1,1,0.4f));
  88.       stick.getThumb().getElementMaterial().setColor("Color", new ColorRGBA(1,1,1,0.4f));
  89.       screen.addElement(stick, true);
  90.       stick.setPosition(screen.getWidth()/2 -  stick.getWidth()/2, screen.getHeight()/2 -  stick.getHeight()/2);
  91.       // Add some fancy effects
  92.       Effect fxIn = new Effect(Effect.EffectType.FadeIn, Effect.EffectEvent.Show,.5f);
  93.       stick.addEffect(fxIn);
  94.       Effect fxOut = new Effect(Effect.EffectType.FadeOut, Effect.EffectEvent.Hide,.5f);
  95.       stick.addEffect(fxOut);
  96.       System.out.println("Joystick Initialized: " + stick);
  97.       stick.showWithEffect();
  98.       }
  99.  
  100.  
  101.   //Sets up the score display
  102.   private void initScoreDisplay(){
  103.     scoreText = new TextElement(screen, "ScoreText", Vector2f.ZERO, new Vector2f(300,50), font) {
  104.     @Override
  105.     public void onUpdate(float tpf) { }
  106.     @Override
  107.     public void onEffectStart() { }
  108.     @Override
  109.     public void onEffectStop() { }
  110.     };
  111.    
  112.     //Sets up the details of the score display
  113.     scoreText.setIsResizable(false);
  114.     scoreText.setIsMovable(false);
  115.     scoreText.setTextWrap(LineWrapMode.NoWrap);
  116.     scoreText.setTextVAlign(VAlign.Center);
  117.     scoreText.setTextAlign(Align.Center);
  118.     scoreText.setFontSize(18);
  119.  
  120.     //Add the score display
  121.     screen.addElement(scoreText);
  122.    
  123.     scoreText.setText("Trees Murdered: " + player.score);
  124.     scoreText.setLocalTranslation(screen.getWidth() / 1.1f - scoreText.getWidth()/1.8f, screen.getHeight() / 1.05f - scoreText.getHeight()/2, -1);
  125.     }
  126.  
  127.   private void initFireLevel(){
  128.     fireInd = new Indicator(
  129.       screen,
  130.       "Fire Ind",
  131.       new Vector2f(12,12),
  132.       Indicator.Orientation.HORIZONTAL
  133.       ) {
  134.  
  135.     @Override
  136.       public void onChange(float currentValue, float currentPercentage) {
  137.         }
  138.       };
  139.    
  140.     fireInd.setMaxValue(20);
  141.     fireInd.setCurrentValue(0);
  142.     fireInd.setIndicatorColor(ColorRGBA.Red);
  143.     fireInd.setText("Fire Level");
  144.     fireInd.setTextWrap(LineWrapMode.NoWrap);
  145.     fireInd.setTextVAlign(VAlign.Center);
  146.     fireInd.setTextAlign(Align.Center);
  147.     fireInd.setFont("Interface/Fonts2/Impact.fnt");
  148.     fireInd.setBaseImage(screen.getStyle("Window").getString("defaultImg"));
  149.     fireInd.setIndicatorPadding(new Vector4f(7,7,7,7));
  150.     fireInd.setDimensions(150, 30);
  151.     screen.addElement(fireInd);
  152.     }
  153.  
  154.   private void initWoodLevel(){
  155.     woodInd = new Indicator(
  156.       screen,
  157.       "Wood Ind",
  158.       new Vector2f(12,50),
  159.       Indicator.Orientation.HORIZONTAL
  160.       ) {
  161.  
  162.     @Override
  163.       public void onChange(float currentValue, float currentPercentage) {
  164.         }
  165.       };
  166.    
  167.     woodInd.setMaxValue(10);
  168.     woodInd.setCurrentValue(0);
  169.     woodInd.setIndicatorColor(ColorRGBA.Brown);
  170.     woodInd.setText("Wood Level");
  171.     woodInd.setTextWrap(LineWrapMode.NoWrap);
  172.     woodInd.setTextVAlign(VAlign.Center);
  173.     woodInd.setTextAlign(Align.Center);
  174.     woodInd.setFont("Interface/Fonts2/Impact.fnt");
  175.     woodInd.setBaseImage(screen.getStyle("Window").getString("defaultImg"));
  176.     woodInd.setIndicatorPadding(new Vector4f(7,7,7,7));
  177.     woodInd.setDimensions(150, 30);
  178.     screen.addElement(woodInd);
  179.     }
  180.  
  181.   private void updateHud(){
  182.     scoreText.setText("Trees Murdered: " + player.score);
  183.     //fireInd.setCurrentValue(player.fireLevel);
  184.     //woodInd.setCurrentValue(player.wood);
  185.     }
  186.  
  187.   @Override
  188.   public void update(float tpf){
  189.     updateHud();
  190.     }
  191.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement