@Override
 public void onUpdate(final float pSecondsElapsed) {
   if (LevelWonLayer.this.mLayerBG.getY()
       < ((ResourceManager.getInstance().cameraHeight / 2f)
           + (LevelWonLayer.this.mLayerBG.getHeight() / 2f))) {
     LevelWonLayer.this.mLayerBG.setY(
         Math.min(
             LevelWonLayer.this.mLayerBG.getY()
                 + (pSecondsElapsed * ManagedLayer.mSLIDE_PIXELS_PER_SECONDS),
             (ResourceManager.getInstance().cameraHeight / 2f)
                 + (LevelWonLayer.this.mLayerBG.getHeight() / 2f)));
   } else {
     ResourceManager.getInstance().engine.unregisterUpdateHandler(this);
     SceneManager.getInstance().hideLayer();
     if (LevelWonLayer.this.mIsGoingToRestartLevel) {
       LevelWonLayer.this.mCurrentLevel.restartLevel();
       return;
     } else if (LevelWonLayer.this.mIsGoingToNextLevel) {
       if (Levels.isNextLevelInCurrentWorld(LevelWonLayer.this.mCurrentLevel.mLevelDef)) {
         LevelWonLayer.this.mCurrentLevel.startNextLevel();
         return;
       }
     }
     LevelWonLayer.this.mCurrentLevel.disposeLevel();
     SceneManager.getInstance().showMainMenu();
   }
 }
 @Override
 public void onUpdate(final float pSecondsElapsed) {
   if (OptionsLayer.this.LayerBG.getY()
       < ((ResourceManager.getInstance().cameraHeight / 2f)
           + (OptionsLayer.this.LayerBG.getHeight() / 2f))) {
     OptionsLayer.this.LayerBG.setY(
         Math.min(
             OptionsLayer.this.LayerBG.getY()
                 + (pSecondsElapsed * mSLIDE_PIXELS_PER_SECONDS),
             (ResourceManager.getInstance().cameraHeight / 2f)
                 + (OptionsLayer.this.LayerBG.getHeight() / 2f)));
   } else {
     ResourceManager.getInstance().engine.unregisterUpdateHandler(this);
     SceneManager.getInstance().hideLayer();
   }
 }
 @Override
 public void onUpdate(final float pSecondsElapsed) {
   if (LevelWonLayer.this.mLayerBG.getY() > 0f) {
     LevelWonLayer.this.mLayerBG.setY(
         Math.max(
             LevelWonLayer.this.mLayerBG.getY()
                 - (pSecondsElapsed * ManagedLayer.mSLIDE_PIXELS_PER_SECONDS),
             0f));
   } else {
     ResourceManager.getInstance().engine.unregisterUpdateHandler(this);
   }
 }
  @Override
  public void onShowLayer() {
    // Register the slide-in animation with the engine.
    ResourceManager.getInstance().engine.registerUpdateHandler(this.mSlideInUpdateHandler);

    this.mIsGoingToRestartLevel = false;
    this.mIsGoingToNextLevel = false;

    // Set the title of the layer
    this.mMainText.setText("LEVEL " + this.mCurrentLevel.mLevelDef.mLevelIndex + " SUCCESS!");
    this.mMainText.setScale(Math.min(390f / this.mMainText.getWidth(), 1f));

    // Show the current level's score

    this.mScoreText.setText("SCORE: " + this.mCurrentLevel.CurrentScore);
    this.mScoreText.setScale(Math.min(203f / this.mScoreText.getWidth(), 1f));

    // Show the highscore (or the current score if it is the highscore)
    final int PreviousHighScore =
        TimelessWarriorsActivity.getIntFromSharedPreferences(
            TimelessWarriorsActivity.SHARED_PREFS_LEVEL_HIGHSCORE
                + this.mCurrentLevel.mLevelDef.mLevelIndex);
    if (PreviousHighScore >= this.mCurrentLevel.CurrentScore) {
      this.mHighScoreText.setText("HIGHSCORE: " + PreviousHighScore);
    } else {
      TimelessWarriorsActivity.writeIntToSharedPreferences(
          TimelessWarriorsActivity.SHARED_PREFS_LEVEL_HIGHSCORE
              + this.mCurrentLevel.mLevelDef.mLevelIndex,
          this.mCurrentLevel.CurrentScore);
      this.mHighScoreText.setText("HIGHSCORE: " + this.mCurrentLevel.CurrentScore);
    }
    this.mHighScoreText.setScale(Math.min(203f / this.mHighScoreText.getWidth(), 1f));

    // Show the appropriate number of stars
    final int NumStars =
        (int)
            Math.min(
                (this.mCurrentLevel.CurrentScore / (this.mCurrentLevel.TotalScorePossible * 0.9f))
                    * (this.mStarsTiledSprite.getTileCount()),
                (this.mStarsTiledSprite.getTileCount() - 1));
    this.mStarsTiledSprite.setCurrentTileIndex(NumStars);
    if (NumStars
        > TimelessWarriorsActivity.getIntFromSharedPreferences(
            TimelessWarriorsActivity.SHARED_PREFS_LEVEL_STARS
                + this.mCurrentLevel.mLevelDef.mLevelIndex)) {
      TimelessWarriorsActivity.writeIntToSharedPreferences(
          TimelessWarriorsActivity.SHARED_PREFS_LEVEL_STARS
              + this.mCurrentLevel.mLevelDef.mLevelIndex,
          NumStars);
    }
    // this.mStarsTiledSprite.registerEntityModifier(new ScaleModifier(0.5f, 25f, 1f));
    // this.mStarsTiledSprite.registerEntityModifier(new AlphaModifier(0.5f, 0f, 1f));

    // Make the next level reachable
    final int currentMaxLevel =
        TimelessWarriorsActivity.getIntFromSharedPreferences(
            TimelessWarriorsActivity.SHARED_PREFS_LEVEL_MAX_REACHED);
    if (currentMaxLevel < this.mCurrentLevel.mLevelDef.mLevelIndex) {
      TimelessWarriorsActivity.writeIntToSharedPreferences(
          TimelessWarriorsActivity.SHARED_PREFS_LEVEL_MAX_REACHED,
          this.mCurrentLevel.mLevelDef.mLevelIndex);
    }
  }
  @Override
  public void onLoadLayer() {
    if (this.mHasLoaded) {
      return;
    }
    this.mHasLoaded = true;

    this.setTouchAreaBindingOnActionDownEnabled(true);
    this.setTouchAreaBindingOnActionMoveEnabled(true);

    final Rectangle fadableBGRect =
        new Rectangle(
            0f,
            0f,
            ResourceManager.getInstance().cameraWidth,
            ResourceManager.getInstance().cameraHeight,
            ResourceManager.getActivity().getVertexBufferObjectManager());
    fadableBGRect.setColor(0f, 0f, 0f, 0.5f);
    this.attachChild(fadableBGRect);

    this.attachChild(
        this.mLayerBG =
            new Sprite(
                0f,
                (ResourceManager.getInstance().cameraHeight / 2f)
                    + (ResourceManager.gameLevelLayerBGTR.getHeight() / 2f),
                ResourceManager.gameLevelLayerBGTR,
                ResourceManager.getActivity().getVertexBufferObjectManager()));
    this.mLayerBG.setScale(1.5f / ResourceManager.getInstance().cameraScaleFactorY);

    final GrowButton BackToLevelSelectButton =
        new GrowButton(128.2f, 75f, ResourceManager.gameLevelLayerButtonLevelSelectTR, false) {
          @Override
          public void onClick() {
            LevelWonLayer.this.mIsGoingToRestartLevel = false;
            LevelWonLayer.this.mIsGoingToNextLevel = false;
            LevelWonLayer.this.onHideLayer();
          }
        };
    this.mLayerBG.attachChild(BackToLevelSelectButton);
    this.registerTouchArea(BackToLevelSelectButton);

    final GrowButton RestartLevelButton =
        new GrowButton(256f, 75f, ResourceManager.gameLevelLayerButtonRestartLevelTR, false) {
          @Override
          public void onClick() {
            LevelWonLayer.this.mIsGoingToRestartLevel = true;
            LevelWonLayer.this.mIsGoingToNextLevel = false;
            LevelWonLayer.this.onHideLayer();
          }
        };
    this.mLayerBG.attachChild(RestartLevelButton);
    this.registerTouchArea(RestartLevelButton);

    final GrowButton NextLevelButton =
        new GrowButton(383f, 75f, ResourceManager.gameLevelLayerButtonNextLevelTR, false) {
          @Override
          public void onClick() {
            LevelWonLayer.this.mIsGoingToRestartLevel = false;
            LevelWonLayer.this.mIsGoingToNextLevel = true;
            LevelWonLayer.this.onHideLayer();
          }
        };
    this.mLayerBG.attachChild(NextLevelButton);
    this.registerTouchArea(NextLevelButton);

    this.mLayerBG.attachChild(
        new Sprite(
            365f,
            148f,
            ResourceManager.gameLevelLayerStarsBGTR,
            ResourceManager.getActivity().getVertexBufferObjectManager()));
    this.mLayerBG.attachChild(
        this.mStarsTiledSprite =
            new TiledSprite(
                365f,
                148f,
                ResourceManager.gameLevelLayerStarsTTR,
                ResourceManager.getActivity().getVertexBufferObjectManager()));

    this.mMainText =
        new Text(
            0f,
            0f,
            ResourceManager.fontDefaultMagneTank48,
            "LEVEL *** SUCCESS!",
            ResourceManager.getActivity().getVertexBufferObjectManager());
    this.mMainText.setScale(Math.min(390f / this.mMainText.getWidth(), 1f));
    this.mMainText.setPosition(256f, 205f);
    this.mMainText.setColor(0.31f, 0.35f, 0.31f);
    this.mLayerBG.attachChild(this.mMainText);

    this.mScoreText =
        new Text(
            0f,
            0f,
            ResourceManager.fontDefaultMagneTank48,
            "SCORE: *****",
            ResourceManager.getActivity().getVertexBufferObjectManager());
    this.mScoreText.setAnchorCenterX(0f);
    this.mScoreText.setScale(Math.min(203f / this.mScoreText.getWidth(), 1f));
    this.mScoreText.setPosition(80f, 155f);
    this.mScoreText.setColor(0.31f, 0.35f, 0.31f);
    this.mLayerBG.attachChild(this.mScoreText);

    this.mHighScoreText =
        new Text(
            0f,
            0f,
            ResourceManager.fontDefaultMagneTank48,
            "HIGHSCORE: *****",
            ResourceManager.getActivity().getVertexBufferObjectManager());
    this.mHighScoreText.setAnchorCenterX(0f);
    this.mHighScoreText.setScale(Math.min(203f / this.mHighScoreText.getWidth(), 1f));
    this.mHighScoreText.setPosition(80f, 128f);
    this.mHighScoreText.setColor(0.31f, 0.35f, 0.31f);
    this.mLayerBG.attachChild(this.mHighScoreText);

    this.setPosition(
        ResourceManager.getInstance().cameraWidth / 2f,
        ResourceManager.getInstance().cameraHeight / 2f);
  }
 // ====================================================
 // METHODS
 // ====================================================
 @Override
 public void onHideLayer() {
   // Register the slide-out animation with the engine.
   ResourceManager.getInstance().engine.registerUpdateHandler(this.mSlideOutUpdateHandler);
 }
  @Override
  public void onLoadLayer() {
    if (this.mHasLoaded) {
      return;
    }
    this.mHasLoaded = true;

    this.setTouchAreaBindingOnActionDownEnabled(true);
    this.setTouchAreaBindingOnActionMoveEnabled(true);

    final Rectangle fadableBGRect =
        new Rectangle(
            0f,
            0f,
            ResourceManager.getInstance().cameraWidth / 2,
            ResourceManager.getInstance().cameraHeight / 2,
            ResourceManager.getActivity().getVertexBufferObjectManager());
    fadableBGRect.setColor(0f, 0f, 0f, 0.8f);
    this.attachChild(fadableBGRect);

    this.attachChild(
        this.LayerBG =
            new Sprite(
                0f,
                (ResourceManager.getInstance().cameraHeight / 2f)
                    + (ResourceManager.gameLevelLayerBGTR.getHeight() / 2f),
                ResourceManager.gameLevelLayerBGTR,
                ResourceManager.getActivity().getVertexBufferObjectManager()));
    this.LayerBG.setScale(1.5f / ResourceManager.getInstance().cameraScaleFactorY);

    this.TitleText =
        new Text(
            0f,
            0f,
            ResourceManager.fontDefaultMagneTank48,
            "OPTIONS",
            ResourceManager.getActivity().getVertexBufferObjectManager());
    this.TitleText.setScale(Math.min(390f / this.TitleText.getWidth(), 1f));
    this.TitleText.setPosition(256f, 225f);
    this.TitleText.setColor(0.31f, 0.35f, 0.31f);
    this.LayerBG.attachChild(this.TitleText);

    this.SoundEnabledText =
        new GrowToggleTextButton(
            256f,
            197.2f,
            "SOUND: ENABLED",
            "SOUND: DISABLED",
            ResourceManager.fontDefaultMagneTank48,
            !SFXManager.isSoundMuted()) {
          @Override
          public boolean checkState() {
            return !SFXManager.isSoundMuted();
          }

          @Override
          public void onClick() {
            SFXManager.toggleSoundMuted();
          }
        };
    this.SoundEnabledText.setScales(
        Math.min(390f / this.SoundEnabledText.getWidth(), 0.85f),
        Math.min(390f / this.SoundEnabledText.getWidth(), 0.85f) * 1.4f);
    this.SoundEnabledText.setColor(0.212f, 0.275f, 0.212f);
    this.LayerBG.attachChild(this.SoundEnabledText);
    this.registerTouchArea(this.SoundEnabledText);

    this.MusicEnabledText =
        new GrowToggleTextButton(
            256f,
            169.4f,
            "MUSIC: ENABLED",
            "MUSIC: DISABLED",
            ResourceManager.fontDefaultMagneTank48,
            !SFXManager.isMusicMuted()) {
          @Override
          public boolean checkState() {
            return !SFXManager.isMusicMuted();
          }

          @Override
          public void onClick() {
            SFXManager.toggleMusicMuted();
          }
        };
    this.MusicEnabledText.setScales(
        Math.min(390f / this.MusicEnabledText.getWidth(), 0.85f),
        Math.min(390f / this.MusicEnabledText.getWidth(), 0.85f) * 1.2f);
    this.MusicEnabledText.setColor(0.212f, 0.275f, 0.212f);
    this.LayerBG.attachChild(this.MusicEnabledText);
    this.registerTouchArea(this.MusicEnabledText);

    this.GraphicsQualityText =
        new GrowToggleTextButton(
            256f,
            141.6f,
            "GRAPHICS: HIGH",
            "GRAPHICS: NORMAL",
            ResourceManager.fontDefaultMagneTank48,
            ResourceManager.isUsingHighQualityGraphics()) {
          @Override
          public boolean checkState() {
            return ResourceManager.isUsingHighQualityGraphics();
          }

          @Override
          public void onClick() {
            ResourceManager.getInstance().switchQuality();
          }
        };
    this.GraphicsQualityText.setScales(
        Math.min(390f / this.GraphicsQualityText.getWidth(), 0.85f),
        Math.min(390f / this.GraphicsQualityText.getWidth(), 0.85f) * 1.2f);
    this.GraphicsQualityText.setColor(0.212f, 0.275f, 0.212f);
    this.LayerBG.attachChild(this.GraphicsQualityText);
    this.registerTouchArea(this.GraphicsQualityText);

    this.ResetText =
        new GrowToggleTextButton(
            256f, 113.8f, "RESET DATA", "", ResourceManager.fontDefaultMagneTank48, true) {
          @Override
          public boolean checkState() {
            return true;
          }

          @Override
          public void onClick() {
            ResourceManager.getActivity()
                .runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        final AlertDialog.Builder builder =
                            new AlertDialog.Builder(ResourceManager.getActivity());
                        builder.setTitle("Reset Game Data?");
                        builder.setIcon(R.drawable.icon);
                        builder
                            .setMessage(
                                "This will reset all scores and stars that you've achieved. Continue?")
                            .setPositiveButton(
                                "Reset Data",
                                new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(final DialogInterface dialog, final int id) {
                                    final int ActivityStartCount =
                                        TimelessWarriorsActivity.getIntFromSharedPreferences(
                                            TimelessWarriorsActivity
                                                .SHARED_PREFS_ACTIVITY_START_COUNT);
                                    final int MusicMuted =
                                        TimelessWarriorsActivity.getIntFromSharedPreferences(
                                            TimelessWarriorsActivity.SHARED_PREFS_MUSIC_MUTED);
                                    final boolean Quality =
                                        TimelessWarriorsActivity.getBooleanFromSharedPreferences(
                                            TimelessWarriorsActivity
                                                .SHARED_PREFS_HIGH_QUALITY_GRAPHICS);
                                    final int MarketRated =
                                        TimelessWarriorsActivity.getIntFromSharedPreferences(
                                            TimelessWarriorsActivity.SHARED_PREFS_RATING_SUCCESS);
                                    final int SoundMuted =
                                        TimelessWarriorsActivity.getIntFromSharedPreferences(
                                            TimelessWarriorsActivity.SHARED_PREFS_SOUNDS_MUTED);
                                    ResourceManager.getActivity()
                                        .getSharedPreferences(
                                            TimelessWarriorsActivity.SHARED_PREFS_MAIN, 0)
                                        .edit()
                                        .clear()
                                        .putInt(
                                            TimelessWarriorsActivity
                                                .SHARED_PREFS_ACTIVITY_START_COUNT,
                                            ActivityStartCount)
                                        .putInt(
                                            TimelessWarriorsActivity.SHARED_PREFS_MUSIC_MUTED,
                                            MusicMuted)
                                        .putBoolean(
                                            TimelessWarriorsActivity
                                                .SHARED_PREFS_HIGH_QUALITY_GRAPHICS,
                                            Quality)
                                        .putInt(
                                            TimelessWarriorsActivity.SHARED_PREFS_RATING_SUCCESS,
                                            MarketRated)
                                        .putInt(
                                            TimelessWarriorsActivity.SHARED_PREFS_SOUNDS_MUTED,
                                            SoundMuted)
                                        .apply();
                                    MainMenu.getInstance().RefreshLevelStars();
                                  }
                                })
                            .setNegativeButton(
                                "Cancel",
                                new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(final DialogInterface dialog, final int id) {}
                                })
                            .setCancelable(false);
                        final AlertDialog alert = builder.create();
                        alert.show();
                      }
                    });
          }
        };
    this.ResetText.setScales(
        Math.min(390f / this.ResetText.getWidth(), 0.85f),
        Math.min(390f / this.ResetText.getWidth(), 0.85f) * 1.2f);
    this.ResetText.setColor(0.212f, 0.275f, 0.212f);
    this.LayerBG.attachChild(this.ResetText);
    this.registerTouchArea(this.ResetText);

    final GrowButton BackToTitleButton =
        new GrowButton(0f, 0f, ResourceManager.menuArrow2TR, false) {
          @Override
          public void onClick() {
            OptionsLayer.this.onHideLayer();
          }
        };
    BackToTitleButton.setFlippedHorizontal(true);
    this.LayerBG.attachChild(BackToTitleButton);
    BackToTitleButton.setPosition(256f, 66f);
    this.registerTouchArea(BackToTitleButton);

    this.setPosition(
        ResourceManager.getInstance().cameraWidth / 2f,
        ResourceManager.getInstance().cameraHeight / 2f);
  }