<commit_before>/**
 * @file:   com.innovail.trouble.core - MenuScreen.java
 * @date:   Apr 15, 2012
 * @author: bweber
 */
package com.innovail.trouble.screen;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.collision.Ray;

import com.innovail.trouble.core.ApplicationSettings;
import com.innovail.trouble.core.TroubleApplicationState;
import com.innovail.trouble.utils.BackgroundImage;
import com.innovail.trouble.utils.GameInputAdapter;
import com.innovail.trouble.utils.MenuEntryMesh;
import com.innovail.trouble.utils.GameMesh;

/**
 * 
 */
public class MainMenuScreen extends TroubleScreen {
    private static final String TAG = "MainMenuScreen";
    private static final String AppPartName = TroubleApplicationState.MAIN_MENU;
    
    private final BitmapFont _menuFont;
    private final SpriteBatch _spriteBatch;
    private final BackgroundImage _backgroundImage;

    private final GameMesh _logo;
    private final Map <String, GameMesh> _menuEntriesMap;
    private final Collection <GameMesh> _menuEntries;
    
    private float[] _yRotationAngle;
    private int[] _yRotationDirection;
    private static final float _RotationMaxAngle = 2.0f;
    private static final float _RotationAngleIncrease = 0.2f;
    private static float _rotationDelta = 0.0f;
    private static final float _RotationMaxDelta = 0.05f;

    private final Matrix4 _viewMatrix;
    private final Matrix4 _transformMatrix;
    
    public MainMenuScreen ()
    {
        super ();
        
        Gdx.app.log (TAG, "MainMenuScreen()");
        
        _currentState = TroubleApplicationState.MAIN_MENU;
        
        _spriteBatch = new SpriteBatch ();
        _menuFont = ApplicationSettings.getInstance ().getGameFont (AppPartName).createBitmapFont ();
        _backgroundImage = ApplicationSettings.getInstance ().getBackgroundImage (AppPartName);
        _backgroundImage.createTexture ().setFilter (TextureFilter.Linear, TextureFilter.Linear);
        
        _logo = ApplicationSettings.getInstance ().getLogo ();
        
        _menuEntriesMap = ApplicationSettings.getInstance ().getMenuEntries (AppPartName);
        _menuEntries = _menuEntriesMap.values ();
        _yRotationAngle = new float[_menuEntriesMap.size ()];
        _yRotationDirection = new int[_menuEntriesMap.size ()];
        final Random rand = new Random ();
        for (int i = 0; i < _menuEntriesMap.size (); i++) {
            _yRotationAngle[i] = rand.nextFloat ();
            _yRotationAngle[i] *= rand.nextBoolean () ? 1.0f : -1.0f;
            _yRotationDirection[i] = rand.nextBoolean () ? 1 : -1;
        }
        
        _viewMatrix = new Matrix4 ();
        _transformMatrix = new Matrix4 ();
        
        final float aspectRatio = (float) Gdx.graphics.getWidth () /
                                   (float) Gdx.graphics.getHeight ();
        _camera = new PerspectiveCamera (100, 2f * aspectRatio, 2f);
        _camera.position.set (0, 0, 2);
        _camera.direction.set (0, 0, -4).sub (_camera.position).nor ();
    }
    
    protected void update (final float delta)
    {
        /* Nothing to do here. */
    }
    
    protected void render (final GL10 gl, final float delta)
    {
        _rotationDelta += delta;

        renderLogo (gl);
        renderMenu (gl);

        gl.glDisable (GL10.GL_CULL_FACE);
        gl.glDisable (GL10.GL_DEPTH_TEST);
    }

    protected void renderBackground (final float width, final float height)
    {
        _viewMatrix.setToOrtho2D (0.0f, 0.0f, width, height);
        _spriteBatch.setProjectionMatrix (_viewMatrix);
        _spriteBatch.setTransformMatrix (_transformMatrix);
        _spriteBatch.begin ();
        _spriteBatch.disableBlending ();
        _spriteBatch.setColor (Color.WHITE);
        _spriteBatch.draw ((Texture)_backgroundImage.getImageObject (),
                          0, 0, width, height,
                          0, 0,
                          _backgroundImage.getWidth (),
                          _backgroundImage.getHeight (),
                          false, false);
        _spriteBatch.enableBlending ();
        _spriteBatch.setBlendFunction (GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        final String text = ApplicationSettings.getInstance ().getCopyRightNotice ();
        final float textWidth = _menuFont.getBounds (text).width;
        final float textHeight = _menuFont.getBounds (text).height;
        _menuFont.draw (_spriteBatch, text,
                        Gdx.graphics.getWidth () / 2 - textWidth / 2,
                        textHeight + 5);
        _spriteBatch.end ();
    }
    
    protected void setLighting (final GL10 gl)
    {
        final Color lightColor = _logo.getColor ();
        final float[] specular0 = {lightColor.r, lightColor.g, lightColor.b, lightColor.a};
        final float[] position1 = {-2.0f, -2.0f, 1.0f, 0.0f};
        final float[] ambient1 = {1.0f, 1.0f, 1.0f, 1.0f};
        final float[] diffuse1 = {1.0f, 1.0f, 1.0f, 1.0f};
        final float[] specular1 = {1.0f, 1.0f, 1.0f, 1.0f};
        
        gl.glEnable (GL10.GL_LIGHTING);
        gl.glLightModelf (GL10.GL_LIGHT_MODEL_TWO_SIDE, 0.0f);
        
        gl.glEnable (GL10.GL_LIGHT0);
        gl.glLightfv (GL10.GL_LIGHT0, GL10.GL_SPECULAR, specular0, 0);
        
        gl.glEnable (GL10.GL_LIGHT1);
        gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_AMBIENT, ambient1, 0);
        gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuse1, 0);
        gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_SPECULAR, specular1, 0);
        gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_POSITION, position1, 0);
        
        gl.glEnable (GL10.GL_COLOR_MATERIAL);
        gl.glEnable (GL10.GL_BLEND);
    }

    private void renderLogo (final GL10 gl)
    {
        final Color currentColor = _logo.getColor ();
        final int frontAndOrBack = GL10.GL_FRONT;
        final float[] matSpecular = {1.0f, 1.0f, 1.0f, 1.0f};
        final float[] matShininess = {7.0f};
        
        gl.glPushMatrix ();
        gl.glTranslatef (0.0f, 1.0f, 0.3f);
        gl.glRotatef (90.0f, 1.0f, 0.0f, 0.0f);
        gl.glColor4f (currentColor.r, currentColor.g, currentColor.b, currentColor.a);
        gl.glMaterialfv (frontAndOrBack, GL10.GL_SPECULAR, matSpecular, 0);
        gl.glMaterialfv (frontAndOrBack, GL10.GL_SHININESS, matShininess, 0);
        _logo.getMesh ().render (GL10.GL_TRIANGLES);
        gl.glPopMatrix ();
        //gl.glDisable (GL10.GL_COLOR_MATERIAL);
     }
    
    private void renderMenu (final GL10 gl)
    {
        final Iterator <GameMesh> currentMesh = _menuEntries.iterator ();
        float yLocation = 0.0f;
        int i = 0;
        while (currentMesh.hasNext ()) {
            final GameMesh thisMesh = currentMesh.next ();
            gl.glPushMatrix ();
            gl.glTranslatef (0.0f, yLocation, 0.0f);
            gl.glRotatef (90.0f, 1.0f, 0.0f, 0.0f);
            gl.glRotatef (_yRotationAngle[i], 0.0f, 1.0f, 0.0f);
            thisMesh.getMesh ().render (GL10.GL_TRIANGLES);
            gl.glPopMatrix ();
            
            final Matrix4 transform = new Matrix4();
            final Matrix4 tmp = new Matrix4();
            transform.setToTranslation (0.0f, yLocation, 0.0f);
            tmp.setToRotation (1.0f, 0.0f, 0.0f, 90.0f);
            transform.mul(tmp);
            tmp.setToRotation (0.0f, 1.0f, 0.0f, _yRotationAngle[i]);
            transform.mul(tmp);
            thisMesh.transformBoundingBox (transform);
            
            if (_DEBUG) {
                Gdx.gl10.glPolygonMode (GL10.GL_FRONT_AND_BACK, GL10.GL_LINE);
                gl.glPushMatrix ();
                thisMesh.getBBMesh ().render (GL10.GL_TRIANGLES);
                gl.glPopMatrix ();
                Gdx.gl10.glPolygonMode (GL10.GL_FRONT_AND_BACK, GL10.GL_FILL);<commit_msg>Fixing axis for meshes. Removing initial rotation.<commit_after>/**
 * @file:   com.innovail.trouble.core - MenuScreen.java
 * @date:   Apr 15, 2012
 * @author: bweber
 */
package com.innovail.trouble.screen;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.collision.Ray;

import com.innovail.trouble.core.ApplicationSettings;
import com.innovail.trouble.core.TroubleApplicationState;
import com.innovail.trouble.utils.BackgroundImage;
import com.innovail.trouble.utils.GameInputAdapter;
import com.innovail.trouble.utils.MenuEntryMesh;
import com.innovail.trouble.utils.GameMesh;

/**
 * 
 */
public class MainMenuScreen extends TroubleScreen {
    private static final String TAG = "MainMenuScreen";
    private static final String AppPartName = TroubleApplicationState.MAIN_MENU;
    
    private final BitmapFont _menuFont;
    private final SpriteBatch _spriteBatch;
    private final BackgroundImage _backgroundImage;

    private final GameMesh _logo;
    private final Map <String, GameMesh> _menuEntriesMap;
    private final Collection <GameMesh> _menuEntries;
    
    private float[] _yRotationAngle;
    private int[] _yRotationDirection;
    private static final float _RotationMaxAngle = 2.0f;
    private static final float _RotationAngleIncrease = 0.2f;
    private static float _rotationDelta = 0.0f;
    private static final float _RotationMaxDelta = 0.05f;

    private final Matrix4 _viewMatrix;
    private final Matrix4 _transformMatrix;
    
    public MainMenuScreen ()
    {
        super ();
        
        Gdx.app.log (TAG, "MainMenuScreen()");
        
        _currentState = TroubleApplicationState.MAIN_MENU;
        
        _spriteBatch = new SpriteBatch ();
        _menuFont = ApplicationSettings.getInstance ().getGameFont (AppPartName).createBitmapFont ();
        _backgroundImage = ApplicationSettings.getInstance ().getBackgroundImage (AppPartName);
        _backgroundImage.createTexture ().setFilter (TextureFilter.Linear, TextureFilter.Linear);
        
        _logo = ApplicationSettings.getInstance ().getLogo ();
        
        _menuEntriesMap = ApplicationSettings.getInstance ().getMenuEntries (AppPartName);
        _menuEntries = _menuEntriesMap.values ();
        _yRotationAngle = new float[_menuEntriesMap.size ()];
        _yRotationDirection = new int[_menuEntriesMap.size ()];
        final Random rand = new Random ();
        for (int i = 0; i < _menuEntriesMap.size (); i++) {
            _yRotationAngle[i] = rand.nextFloat ();
            _yRotationAngle[i] *= rand.nextBoolean () ? 1.0f : -1.0f;
            _yRotationDirection[i] = rand.nextBoolean () ? 1 : -1;
        }
        
        _viewMatrix = new Matrix4 ();
        _transformMatrix = new Matrix4 ();
        
        final float aspectRatio = (float) Gdx.graphics.getWidth () /
                                   (float) Gdx.graphics.getHeight ();
        _camera = new PerspectiveCamera (100, 2f * aspectRatio, 2f);
        _camera.position.set (0, 0, 2);
        _camera.direction.set (0, 0, -4).sub (_camera.position).nor ();
    }
    
    protected void update (final float delta)
    {
        /* Nothing to do here. */
    }
    
    protected void render (final GL10 gl, final float delta)
    {
        _rotationDelta += delta;
        
        Gdx.gl10.glPolygonMode (GL10.GL_FRONT_AND_BACK, GL10.GL_FILL);

        renderLogo (gl);
        renderMenu (gl);

        gl.glDisable (GL10.GL_CULL_FACE);
        gl.glDisable (GL10.GL_DEPTH_TEST);
    }

    protected void renderBackground (final float width, final float height)
    {
        _viewMatrix.setToOrtho2D (0.0f, 0.0f, width, height);
        _spriteBatch.setProjectionMatrix (_viewMatrix);
        _spriteBatch.setTransformMatrix (_transformMatrix);
        _spriteBatch.begin ();
        _spriteBatch.disableBlending ();
        _spriteBatch.setColor (Color.WHITE);
        _spriteBatch.draw ((Texture)_backgroundImage.getImageObject (),
                          0, 0, width, height,
                          0, 0,
                          _backgroundImage.getWidth (),
                          _backgroundImage.getHeight (),
                          false, false);
        _spriteBatch.enableBlending ();
        _spriteBatch.setBlendFunction (GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        final String text = ApplicationSettings.getInstance ().getCopyRightNotice ();
        final float textWidth = _menuFont.getBounds (text).width;
        final float textHeight = _menuFont.getBounds (text).height;
        _menuFont.draw (_spriteBatch, text,
                        Gdx.graphics.getWidth () / 2 - textWidth / 2,
                        textHeight + 5);
        _spriteBatch.end ();
    }
    
    protected void setLighting (final GL10 gl)
    {
        final Color lightColor = _logo.getColor ();
        final float[] specular0 = {lightColor.r, lightColor.g, lightColor.b, lightColor.a};
        final float[] position1 = {-2.0f, -2.0f, 1.0f, 0.0f};
        final float[] ambient1 = {1.0f, 1.0f, 1.0f, 1.0f};
        final float[] diffuse1 = {1.0f, 1.0f, 1.0f, 1.0f};
        final float[] specular1 = {1.0f, 1.0f, 1.0f, 1.0f};
        
        gl.glEnable (GL10.GL_LIGHTING);
        gl.glLightModelf (GL10.GL_LIGHT_MODEL_TWO_SIDE, 0.0f);
        
        gl.glEnable (GL10.GL_LIGHT0);
        gl.glLightfv (GL10.GL_LIGHT0, GL10.GL_SPECULAR, specular0, 0);
        
        gl.glEnable (GL10.GL_LIGHT1);
        gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_AMBIENT, ambient1, 0);
        gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuse1, 0);
        gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_SPECULAR, specular1, 0);
        gl.glLightfv (GL10.GL_LIGHT1, GL10.GL_POSITION, position1, 0);
        
        gl.glEnable (GL10.GL_COLOR_MATERIAL);
        gl.glEnable (GL10.GL_BLEND);
    }

    private void renderLogo (final GL10 gl)
    {
        final Color currentColor = _logo.getColor ();
        final int frontAndOrBack = GL10.GL_FRONT;
        final float[] matSpecular = {1.0f, 1.0f, 1.0f, 1.0f};
        final float[] matShininess = {7.0f};
        
        gl.glPushMatrix ();
        gl.glTranslatef (0.0f, 1.0f, 0.3f);
        gl.glRotatef (90.0f, 1.0f, 0.0f, 0.0f);
        gl.glColor4f (currentColor.r, currentColor.g, currentColor.b, currentColor.a);
        gl.glMaterialfv (frontAndOrBack, GL10.GL_SPECULAR, matSpecular, 0);
        gl.glMaterialfv (frontAndOrBack, GL10.GL_SHININESS, matShininess, 0);
        _logo.getMesh ().render (GL10.GL_TRIANGLES);
        gl.glPopMatrix ();
        //gl.glDisable (GL10.GL_COLOR_MATERIAL);
     }
    
    private void renderMenu (final GL10 gl)
    {
        final Iterator <GameMesh> currentMesh = _menuEntries.iterator ();
        float yLocation = 0.0f;
        int i = 0;
        while (currentMesh.hasNext ()) {
            final GameMesh thisMesh = currentMesh.next ();
            gl.glPushMatrix ();
            gl.glTranslatef (0.0f, yLocation, 0.0f);
            gl.glRotatef (_yRotationAngle[i], 0.0f, 0.0f, 1.0f);
            thisMesh.getMesh ().render (GL10.GL_TRIANGLES);
            gl.glPopMatrix ();
            
            final Matrix4 transform = new Matrix4();
            final Matrix4 tmp = new Matrix4();
            transform.setToTranslation (0.0f, yLocation, 0.0f);
            tmp.setToRotation (0.0f, 0.0f, 1.0f, _yRotationAngle[i]);
            transform.mul(tmp);
            thisMesh.transformBoundingBox (transform);
            
            if (_DEBUG) {
                Gdx.gl10.glPolygonMode (GL10.GL_FRONT_AND_BACK, GL10.GL_LINE);
                gl.glPushMatrix ();
                thisMesh.getBBMesh ().render (GL10.GL_TRIANGLES);
                gl.glPopMatrix ();
                Gdx.gl10.glPolygonMode (GL10.GL_FRONT_AND_BACK, GL10.GL_FILL);
{'fraction_non_alphanumeric': 0.10954954954954955, 'fraction_numerical': 0.03015015015015015, 'mean_word_length': 2.449554588771494, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5445691', 'n_tokens_mistral': 5644, 'n_tokens_neox': 5292, 'n_words': 1077}