Android Open Source - block-composer Game Dialog Mode






From Project

Back to project page block-composer.

License

The source code is released under:

MIT License

If you think the Android project block-composer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package bwr.blockcomposer.modes;
//w  w  w. j  a va  2  s. c o m
import javax.microedition.khronos.opengles.GL10;

import bwr.blockcomposer.AnimationFinishedListener;
import bwr.blockcomposer.R;
import bwr.blockcomposer.types.FloatValue;
import bwr.blockcomposer.ui.UIElementText;

import android.view.MotionEvent;

public class GameDialogMode extends OverlayMode {

  public static interface OnResult {
    public void onResult();
  }
  
  private static final int ANIMATION_TIME = 200;
  private static final int OKAY_BUTTON = 1;
  
  private UIElementText okayButton;
  private UIElementText message;
  
  private FloatValue gameDialogY = new FloatValue();
  private FloatValue okayButtonAlpha = new FloatValue();
  
  private boolean finishedInitialAnimation = false;
  
  private final String text;
  private final OnResult resultHandler;
  
  public GameDialogMode(Mode mode, String text, OnResult resultHandler) {
    super(mode);
    this.text = text;
    this.resultHandler = resultHandler;
  }

  @Override
  public void onModeCreate(GL10 gl, int displayWidth, int displayHeight) {
    super.onModeCreate(gl, displayWidth, displayHeight);

    final int paddingX = (int) context.getResources().getDimension(R.dimen.buttonPaddingX);
    final int paddingY = (int) context.getResources().getDimension(R.dimen.buttonPaddingY);
    
    final int okayButtonWidth = 100;
    final int okayButtonHeight = 64;
    final int okayButtonX = displayWidth - okayButtonWidth - paddingX;
    final int okayButtonY = okayButtonHeight + paddingY;
        
    okayButton = new UIElementText(gameResources, context, gl, OKAY_BUTTON, "Okay", okayButtonX, okayButtonY, okayButtonWidth, okayButtonHeight, BUTTON_TEXT_SIZE);
    uiElements.add(okayButton);
    
    final int messageWidth = displayWidth * 2/3;
    final int messageRequiredHeight = UIElementText.calculateRequiredHeight(text, messageWidth, DIALOG_TEXT_SIZE);
    final int messageHeight = Math.max(messageRequiredHeight, displayHeight/3);
    final int messageX = (displayWidth - messageWidth)/2;
    final int messageY = displayHeight - paddingY;
    
    message = new UIElementText(gameResources, context, gl, 0, text, messageX, messageY, messageWidth, messageHeight, DIALOG_TEXT_SIZE);
  
    gameDialogY.setValue(messageHeight);
    gameDialogY.initLinearAnimation(-messageHeight, ANIMATION_TIME);
    
    okayButtonAlpha.setValue(0.0f);
    okayButtonAlpha.initLinearAnimation(1, ANIMATION_TIME, new AnimationFinishedListener() {
      public void onAnimationFinished() {
        finishedInitialAnimation = true;
      }
    });
  }

  @Override
  public void update(long dt) {
    super.update(dt);
    gameDialogY.update(dt);
    okayButtonAlpha.update(dt);
  }

  @Override
  public void render2D(GL10 gl) {
        gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    
        gl.glColor4f(1f, 1f, 1f, okayButtonAlpha.getValue());
        okayButton.render(gl);

        gl.glColor4f(1f, 1f, 1f, 1f);
        
    gl.glPushMatrix();
    gl.glTranslatef(0, gameDialogY.getValue(), 0);
    message.render(gl);
    gl.glPopMatrix();
    
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

  }

  @Override
  protected boolean handleUIEvent(int id, int action) {
    if(action == MotionEvent.ACTION_DOWN && id == OKAY_BUTTON && finishedInitialAnimation) {
      gameDialogY.initLinearAnimation(150, ANIMATION_TIME);
      
      okayButtonAlpha.initLinearAnimation(-1f, ANIMATION_TIME, new AnimationFinishedListener() {
        public void onAnimationFinished() {
          finishedInitialAnimation = false;
          modeController.popMode();
        }
      });
      return true;
    }
    return false;
  }

  @Override
  public void onModePop() {
    if(resultHandler != null) {
      resultHandler.onResult();
    }
  }
  
}




Java Source Code List

bwr.blockcomposer.AnimationFinishedListener.java
bwr.blockcomposer.BlockComposerActivity.java
bwr.blockcomposer.BlockComposerRenderer.java
bwr.blockcomposer.BlockComposer.java
bwr.blockcomposer.Camera.java
bwr.blockcomposer.GameResources.java
bwr.blockcomposer.OnUpdateAction.java
bwr.blockcomposer.gamedata.Block.java
bwr.blockcomposer.gamedata.GameEntity.java
bwr.blockcomposer.gamedata.GameState.java
bwr.blockcomposer.gamedata.LevelDimensions.java
bwr.blockcomposer.gamedata.LevelMask.java
bwr.blockcomposer.gamedata.LevelMetadata.java
bwr.blockcomposer.gamedata.LevelStore.java
bwr.blockcomposer.gamedata.Level.java
bwr.blockcomposer.gamedata.Move.java
bwr.blockcomposer.gamedata.StaticLevelStore.java
bwr.blockcomposer.gamedata.UserLevelStore.java
bwr.blockcomposer.misc.GLUtility.java
bwr.blockcomposer.misc.Timer.java
bwr.blockcomposer.models.AxisRenderer.java
bwr.blockcomposer.models.Model.java
bwr.blockcomposer.models.SelectBoxRenderer.java
bwr.blockcomposer.modes.EditMode.java
bwr.blockcomposer.modes.GameDialogMode.java
bwr.blockcomposer.modes.GameMode.java
bwr.blockcomposer.modes.LevelImportMode.java
bwr.blockcomposer.modes.LevelSelectMode.java
bwr.blockcomposer.modes.LogoDisplayMode.java
bwr.blockcomposer.modes.ModeController.java
bwr.blockcomposer.modes.Mode.java
bwr.blockcomposer.modes.OverlayMode.java
bwr.blockcomposer.types.FlipRotateFloat.java
bwr.blockcomposer.types.FloatValueVector3.java
bwr.blockcomposer.types.FloatValue.java
bwr.blockcomposer.types.IntVector.java
bwr.blockcomposer.types.RotatingFloat.java
bwr.blockcomposer.ui.TextRenderer.java
bwr.blockcomposer.ui.UIElementPicture.java
bwr.blockcomposer.ui.UIElementText.java
bwr.blockcomposer.ui.UIElement.java