package org.helioviewer.jhv.renderable.components;

import java.awt.Color;
import java.awt.Component;

import org.helioviewer.jhv.camera.GL3DViewport;
import org.helioviewer.jhv.display.Displayer;
import org.helioviewer.jhv.gui.ImageViewerGui;
import org.helioviewer.jhv.gui.UIGlobals;
import org.helioviewer.jhv.layers.Layers;
import org.helioviewer.jhv.renderable.gui.AbstractRenderable;

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.util.awt.TextRenderer;

public class RenderableTimeStamp extends AbstractRenderable {

    private float oldFontSize = -1;
    private static final double vpScale = 0.035;
    private TextRenderer textRenderer;

    private static final String name = "Timestamp";

    public RenderableTimeStamp() {
    }

    @Override
    public void render(GL2 gl, GL3DViewport vp) {
    }

    @Override
    public void renderFloat(GL2 gl, GL3DViewport vp) {
        if (!isVisible[vp.getIndex()])
            return;

        float fontSize = (int) (vp.getHeight() * vpScale);
        if (textRenderer == null || fontSize != oldFontSize) {
            oldFontSize = fontSize;
            if (textRenderer != null) {
                textRenderer.dispose();
            }
            textRenderer = new TextRenderer(UIGlobals.UIFontRoboto.deriveFont(fontSize), true, true);
            textRenderer.setUseVertexArrays(true);
            textRenderer.setSmoothing(false);
            textRenderer.setColor(Color.WHITE);
        }

        String text = Layers.getLastUpdatedTimestamp().toString();
        if (Displayer.multiview) {
            RenderableImageLayer im = ImageViewerGui.getRenderableContainer().getViewportRenderableImageLayer(vp.getIndex());
            if (im != null) {
                text = im.getTimeString();
            }
        }

        int delta = (int) (vp.getHeight() * 0.01);
        textRenderer.beginRendering(vp.getWidth(), vp.getHeight(), true);
        textRenderer.draw(text, delta, delta);
        textRenderer.endRendering();
    }

    @Override
    public void init(GL2 gl) {
    }

    @Override
    public void remove(GL2 gl) {
        dispose(gl);
    }

    @Override
    public Component getOptionsPanel() {
        return null;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getTimeString() {
        return null;
    }

    @Override
    public boolean isDeletable() {
        return false;
    }

    @Override
    public void dispose(GL2 gl) {
        if (textRenderer != null) {
            textRenderer.dispose();
            textRenderer = null;
        }
        oldFontSize = -1;
    }

    @Override
    public void renderMiniview(GL2 gl, GL3DViewport vp) {
    }

}

@@ -49,31 +49,8 @@
 s;%0A%0A
-import java.awt.Color;%0A
 impo
@@ -245,20 +245,20 @@
 jhv.
-gui.UIGlobal
+layers.Layer
 s;%0Ai
@@ -279,37 +279,37 @@
 oviewer.jhv.
-layers.Layers
+opengl.GLText
 ;%0Aimport org
@@ -507,44 +507,8 @@
  %7B%0A%0A
-    private float oldFontSize = -1;%0A
     
@@ -556,48 +556,8 @@
 35;%0A
-    private TextRenderer textRenderer;%0A%0A
     
@@ -609,50 +609,8 @@
 %22;%0A%0A
-    public RenderableTimeStamp() %7B%0A    %7D%0A%0A
     
@@ -809,518 +809,8 @@
 n;%0A%0A
-        float fontSize = (int) (vp.getHeight() * vpScale);%0A        if (textRenderer == null %7C%7C fontSize != oldFontSize) %7B%0A            oldFontSize = fontSize;%0A            if (textRenderer != null) %7B%0A                textRenderer.dispose();%0A            %7D%0A            textRenderer = new TextRenderer(UIGlobals.UIFontRoboto.deriveFont(fontSize), true, true);%0A            textRenderer.setUseVertexArrays(true);%0A            textRenderer.setSmoothing(false);%0A            textRenderer.setColor(Color.WHITE);%0A        %7D%0A%0A
     
@@ -1182,37 +1182,119 @@
  0.01);%0A        
-textR
+TextRenderer renderer = GLText.getRenderer((int) (vp.getHeight() * vpScale));%0A        r
 enderer.beginRen
@@ -1338,37 +1338,33 @@
  true);%0A        
-textR
+r
 enderer.draw(tex
@@ -1381,37 +1381,33 @@
 delta);%0A        
-textR
+r
 enderer.endRende
@@ -1911,149 +1911,8 @@
 ) %7B%0A
-        if (textRenderer != null) %7B%0A            textRenderer.dispose();%0A            textRenderer = null;%0A        %7D%0A        oldFontSize = -1;%0A
     

use cached TextRenderer