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();
        
        performance.stop();
        
        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 + "");
        }
        
        if (Gdx.graphics.getFramesPerSecond() < 30) {
            if (lagDetector.canDoEvent()) {
                size += 0.01f;
                Gdx.app.log(getClass().getSimpleName(), "lag detected. adjusting: " +  performance.toString());
            }
        }
    }
    
    @Override
    public void resize(int width, int height) {
    }
    
}

@@ -235,16 +235,54 @@
 hUtils;%0A
+import com.badlogic.gdx.math.Vector2;%0A
 import c
@@ -319,24 +319,24 @@
 nceCounter;%0A
-
 import com.s
@@ -701,24 +701,97 @@
 ector;%0A    %0A
+    boolean isDrag = false;%0A    Vector2 clickStart = new Vector2();%0A    %0A
     public N
@@ -2081,16 +2081,17 @@
 (e %3E 0.4
+5
 f && e %3C
@@ -2093,16 +2093,17 @@
 & e %3C 0.
+5
 5f) %7B%0A  
@@ -2444,32 +2444,32 @@
 end();%0A        %0A
-
         performa
@@ -2481,32 +2481,584 @@
 top();%0A        %0A
+        if (crossSection) %7B%0A            if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) %7B%0A                if (!isDrag) %7B%0A                    if (!clickStart.epsilonEquals(Gdx.input.getX(), Gdx.input.getY(), 2)) %7B%0A                        clickStart.set(Gdx.input.getX(), Gdx.input.getY());%0A                        isDrag = true;%0A                    %7D%0A                %7D%0A            %7D else %7B%0A                isDrag = false;%0A            %7D%0A            if (isDrag)%0A                z = z + ((Gdx.input.getX() - clickStart.x) * 0.0002f);%0A        %7D%0A        %0A
         if (Gdx.

Update NoiseAnim.java