package com.spaceproject.screens.animations;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.PerformanceCounter;
import com.spaceproject.math.MyMath;
import com.spaceproject.noise.OpenSimplexNoise;
import com.spaceproject.utility.IndependentTimer;

public class NoiseAnim extends TitleAnimation {
    float z;
    float zDelta;
    float size;
    float scale;
    boolean crossSection;
    OpenSimplexNoise noise;
    
    PerformanceCounter performance;
    IndependentTimer lagDetector;
    
    public NoiseAnim(float z, float zDelta, float size, float scale, boolean crossSection) {
        this.z = z;
        this.zDelta = zDelta;
        this.size = size * Gdx.graphics.getDensity();
        this.scale = scale;
        this.crossSection = crossSection;
        noise = new OpenSimplexNoise(MathUtils.random(Long.MAX_VALUE));
        
        //one DIP is one pixel on an approximately 160 dpi screen. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.
        Gdx.app.log(getClass().getSimpleName(), "density: " + Gdx.graphics.getDensity());
        lagDetector = new IndependentTimer(2000, true);
        performance = new PerformanceCounter("anim");
    }
    
    public NoiseAnim() {
        this(0, 0.2f, 16.6f, 0.110f, false);
    }
    
    
    @Override
    public void render(float delta, ShapeRenderer shape) {
        performance.start();
        
        z += zDelta * delta;

        shape.begin(ShapeRenderer.ShapeType.Filled);
        for (int x = 0; x <= Gdx.graphics.getWidth() / size; x++) {
            for (int y = 0; y <= Gdx.graphics.getHeight() / size; y++) {
                float e = MyMath.inverseLerp(-1, 1, (float) noise.eval(x * scale, y * scale, z));
                if (crossSection) {
                    if (e > 0.4f && e < 0.5f) {
                        shape.setColor(Color.BLACK);
                        shape.rect(x * size, y * size, size, size);
                    }
                } else {
                    shape.setColor(e, e, e, 1);
                    shape.rect(x * size, y * size, size, size);
                }
            }
        }
        shape.end();
        
        
        if (Gdx.input.isKeyPressed(Input.Keys.MINUS)) {
            scale += 0.001f;
            Gdx.app.log(getClass().getSimpleName(), scale + "");
        }
        if (Gdx.input.isKeyPressed(Input.Keys.EQUALS)) {
            scale -= 0.001f;
            Gdx.app.log(getClass().getSimpleName(), scale + "");
        }
        
        
        performance.stop();
        
        if (Gdx.graphics.getFramesPerSecond() < 30) {
            if (lagDetector.canDoEvent()) {
                size += 0.01f;
                //lagDetector.setInterval(5, true);
                Gdx.app.log(getClass().getSimpleName(), "lag detected. adjusting: " +  performance.toString());
                //Gdx.app.log(getClass().getSimpleName(), "lag detected. adjusting: " + size);
            }
            
            //Gdx.app.log(this.getClass().getSimpleName(), lagDetector.toString());
            //Gdx.graphics.getDensity();
        }
    }
    
    @Override
    public void resize(int width, int height) {
    }
    
}

@@ -1470,21 +1470,16 @@
  %7D%0A    %0A
-    %0A
     @Ove
@@ -1476,32 +1476,32 @@
  %0A    @Override%0A
+
     public void 
@@ -2331,32 +2331,60 @@
 end();%0A        %0A
+        performance.stop();%0A
         %0A       
@@ -2528,32 +2528,41 @@
  %22%22);%0A        %7D%0A
+        %0A
         if (Gdx.
@@ -2719,54 +2719,8 @@
    %0A
-        %0A        performance.stop();%0A        %0A
     
@@ -2848,60 +2848,8 @@
 1f;%0A
-                //lagDetector.setInterval(5, true);%0A
     
@@ -2972,242 +2972,9 @@
     
-    //Gdx.app.log(getClass().getSimpleName(), %22lag detected. adjusting: %22 + size);%0A            %7D%0A            %0A            //Gdx.app.log(this.getClass().getSimpleName(), lagDetector.toString());%0A            //Gdx.graphics.getDensity();
+%7D
 %0A   

Update NoiseAnim.java