Commit ad05bcb5 authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

Merge branch '109-fix-gl-canvas-size' into 'master'

Resolve "Fix GL Canvas size"

Closes #109

See merge request grp-fidentis/analyst2!120
parents 1c9a6f3d 1d737ddc
Loading
Loading
Loading
Loading
+36 −39
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@ import cz.fidentis.analyst.scene.Camera;
import cz.fidentis.analyst.scene.Scene;
import cz.fidentis.analyst.scene.SceneRenderer;
import java.awt.BorderLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JLayeredPane;
import javax.swing.JPanel;
@@ -30,9 +32,9 @@ public class Canvas extends JPanel {
    private final MouseRotationListener manipulator;
    
    // GUI elements:
    private final JLayeredPane layeredPane;
    private final JPanel canvasPanel;
    private final ControlButtons controlButtonsPanel;
    private JLayeredPane layeredPane;
    private JPanel canvasPanel;
    private ControlButtons controlButtonsPanel;
    private GLCanvas glCanvas;
    
    
@@ -44,23 +46,7 @@ public class Canvas extends JPanel {
        listener = new CanvasListener(this);
        manipulator = new MouseRotationListener(this);
        
        setLayout(new BorderLayout());

        initGLCanvas();
        canvasPanel = getCanvasPanel();
        canvasPanel.add(glCanvas);
        
        controlButtonsPanel = getButtonsPanel(this);
        
        layeredPane = getLayeredPane();
        layeredPane.add(canvasPanel, 1);
        //layeredPane.setLayer(controlButtonsPanel, JLayeredPane.MODAL_LAYER);
        layeredPane.add(controlButtonsPanel, 0);
        add(layeredPane, BorderLayout.CENTER);
        
        setDarkBackground(true);
        
        validate();   
        initComponents();
    }
    
    /**
@@ -158,28 +144,40 @@ public class Canvas extends JPanel {
        return ret;
    }
    
    private JLayeredPane getLayeredPane() {
        JLayeredPane pane = new JLayeredPane();
        pane.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override
            public void componentResized(java.awt.event.ComponentEvent evt) {
                canvasPanel.setBounds(0, 0, layeredPane.getWidth(), layeredPane.getHeight());
                glCanvas.setBounds(layeredPane.getX(), layeredPane.getY(), layeredPane.getWidth(), layeredPane.getHeight());
            }
    private void initComponents() {
        setLayout(new BorderLayout());

        initGLCanvas();
        canvasPanel = new JPanel();
        canvasPanel.setLayout(new BorderLayout());
        canvasPanel.add(glCanvas);
        
        controlButtonsPanel = getButtonsPanel(this);
        
        layeredPane = new JLayeredPane();
        layeredPane.add(canvasPanel, 1);
        layeredPane.add(controlButtonsPanel, 0);
        add(layeredPane, BorderLayout.CENTER);
        
        setDarkBackground(true);
        
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(java.awt.event.ComponentEvent evt) {
            public void componentResized(ComponentEvent e) {
                canvasPanel.setBounds(0, 0, layeredPane.getWidth(), layeredPane.getHeight());
                glCanvas.setBounds(layeredPane.getX(), layeredPane.getY(), layeredPane.getWidth(), layeredPane.getHeight());
                /*
                OutputWindow.print("Canvas resized event:  ");
                OutputWindow.print("- Canvas: " + getWidth() + "x" + getHeight());
                OutputWindow.print("- Layered Pane: " + layeredPane.getWidth() + "x" + layeredPane.getHeight());
                OutputWindow.print("- Canvas Panel: " + canvasPanel.getWidth() + "x" + canvasPanel.getHeight());
                OutputWindow.print("- GL Canvas: " + glCanvas.getWidth() + "x" + glCanvas.getHeight());
                OutputWindow.print("- Parent1: " + getParent().getWidth() + "x" + getParent().getHeight());
                OutputWindow.print("- Parent2: " + getParent().getParent().getWidth() + "x" + getParent().getParent().getHeight());
                OutputWindow.print("- Parent3: " + getParent().getParent().getParent().getWidth() + "x" + getParent().getParent().getParent().getHeight());
                */
            }
        });
        return pane;
    }
    
    private JPanel getCanvasPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBounds(0, 0, 0, 0);
        return panel;
    }
    
    private void initGLCanvas() {
@@ -193,10 +191,9 @@ public class Canvas extends JPanel {
        
        // enables glCanvas to react to events
        glCanvas.requestFocusInWindow();        
        glCanvas.setSize(getWidth() - getInsets().left - getInsets().right, getHeight() - getInsets().top - getInsets().bottom);
        //glCanvas.setSize(getWidth() - getInsets().left - getInsets().right, getHeight() - getInsets().top - getInsets().bottom);

        glCanvas.addGLEventListener(listener);

        glCanvas.addMouseListener(manipulator);
        glCanvas.addMouseMotionListener(manipulator);
        glCanvas.addMouseWheelListener(manipulator);        
+4 −2
Original line number Diff line number Diff line
@@ -38,13 +38,15 @@ public class CanvasListener implements GLEventListener {

    @Override
    public void display(GLAutoDrawable glad) {
        //System.out.println("CanvasListener.display " + this.toString());
        canvas.getSceneRenderer().renderScene(canvas.getCamera(), canvas.getScene().getAllDrawables());
        //OutputWindow.print("3D canvas has been rendered, window size " 
        //        + canvas.getWidth() + "x" + canvas.getHeight()
        //        +", GL canvas size " + canvas.getGLCanvas().getWidth() + "x" + canvas.getGLCanvas().getHeight()
        //);
    }

    @Override
    public void reshape(GLAutoDrawable glad, int x, int y, int width, int height) {
        //System.out.println("CanvasListener.reshape");
        canvas.getSceneRenderer().setViewport(x, y, width, height);
    }
    
+17 −2
Original line number Diff line number Diff line
@@ -4,7 +4,10 @@ import cz.fidentis.analyst.canvas.Canvas;
import cz.fidentis.analyst.face.HumanFace;
import cz.fidentis.analyst.toolbar.FaceToFaceToolBar;
import cz.fidentis.analyst.toolbar.RenderingToolBar;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.GroupLayout;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;
import org.openide.windows.TopComponent;

@@ -18,6 +21,7 @@ public class FaceToFaceTab extends TopComponent {
    private final Canvas canvas ;
    private final FaceToFaceToolBar renderingToolBar;
    private final TopControlPanel controlPanel;
    private final JScrollPane scrollPane;

    /**
     * Constructor.
@@ -30,8 +34,19 @@ public class FaceToFaceTab extends TopComponent {
        canvas.initScene(primary, secondary);
        controlPanel = new TopControlPanel();
        renderingToolBar = new FaceToFaceToolBar(canvas, controlPanel);
        
        scrollPane = new JScrollPane(controlPanel);
        
        setName(name);
        initComponents();
        
        // change the height so that it corresponds to the height of the OpenGL window
        canvas.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                scrollPane.setSize(ControlPanel.CONTROL_PANEL_WIDTH, canvas.getHeight());
            }
        });
    }
    
    @Override
@@ -50,7 +65,7 @@ public class FaceToFaceTab extends TopComponent {
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(renderingToolBar, GroupLayout.PREFERRED_SIZE, RenderingToolBar.WIDTH, GroupLayout.PREFERRED_SIZE)
                                .addComponent(
                                        controlPanel, 
                                        scrollPane,
                                        ControlPanel.CONTROL_PANEL_WIDTH, 
                                        ControlPanel.CONTROL_PANEL_WIDTH, 
                                        ControlPanel.CONTROL_PANEL_WIDTH
@@ -63,7 +78,7 @@ public class FaceToFaceTab extends TopComponent {
                                .addGroup(layout.createBaselineGroup(true, true)
                                        .addComponent(canvas)
                                        .addComponent(renderingToolBar)
                                        .addComponent(controlPanel)
                                        .addComponent(scrollPane)
                                ))
        );
    }
+28 −24
Original line number Diff line number Diff line
@@ -16,56 +16,58 @@ import org.openide.windows.WindowManager;
 */
public class OutputWindow {

    private final JTextArea textArea;
    private static JTextArea textArea; 
    
    private long lastTime = System.currentTimeMillis();
    
    private static OutputWindow instance;
    
    protected OutputWindow() {
        TopComponent outputWin = WindowManager.getDefault().findTopComponent("output");
        textArea = new JTextArea();
        JScrollPane sp = new JScrollPane(textArea);
        outputWin.add(sp, BorderLayout.CENTER);
        initTextArea();
    }
    
    /**
     * Prints the message. The duration indicator is set to [+00:00:000]
     * Prints the message.
     * 
     * @param msg Message to be printed
     */
    public static void print(String msg) {
        instance().textArea.setText(instance().textArea.getText() 
        JTextArea ta = initTextArea();
        ta.setText(ta.getText() 
                + new SimpleDateFormat("HH:mm:ss").format(System.currentTimeMillis())
                + " [+00:00:000]: "
                + " [duration unknown]: "
                + msg.trim() 
                + System.lineSeparator()
        );
    }

    /**
     * Resets the stopwatch for the duration calculation.
     * Starts measuring of some operation. 
     * Call {@link #printDuration(java.lang.String)} on returned OutputWindow
     * to print the measured duration.
     * 
     * @return An object used to print measured duration and message
     */
    public static void resetStopwatch() {
        instance().lastTime = System.currentTimeMillis();
    public static OutputWindow measureTime() {
        OutputWindow ret = new OutputWindow();
        ret.lastTime = System.currentTimeMillis();
        return ret;
    }
    
    /**
     * Prints the message about an operation and the duration of the operation. 
     * The duration is computed as the difference between current system time and 
     * the time of calling {@link #resetStopwatch()}. The difference is measured in 
     * the time of calling {@link #measureTime()}. The time difference is measured in 
     * milliseconds and printed as [+mm:ss.SSS] where mm=minutes, ss=seconds, 
     * and SSS=milliseconds.
     * 
     * @param msg Message to be printed
     */
    public static void printDuration(String msg) {
        Duration duration = Duration.ofMillis(System.currentTimeMillis() - instance().lastTime);
        instance().textArea.setText(instance().textArea.getText() 
    public void printDuration(String msg) {
        Duration duration = Duration.ofMillis(System.currentTimeMillis() - lastTime);
        textArea.setText(textArea.getText() 
                + new SimpleDateFormat("HH:mm:ss").format(System.currentTimeMillis())
                + " [+"
                + " [duration "
                + String.format(
                        "%d:%02d.%03d", 
                        "%02d:%02d.%03d", 
                        duration.toMinutes(), 
                        duration.toSecondsPart(),
                        duration.toMillisPart())
@@ -75,11 +77,13 @@ public class OutputWindow {
        );
    }
    
    protected static OutputWindow instance() {
        if (instance == null) {
            instance = new OutputWindow();
    protected static JTextArea initTextArea() {
        if (textArea == null) {
            TopComponent outputWin = WindowManager.getDefault().findTopComponent("output");
            textArea = new JTextArea();
            JScrollPane sp = new JScrollPane(textArea);
            outputWin.add(sp, BorderLayout.CENTER);
        }
        return instance;
        return textArea;
    }
    
}
+2 −2
Original line number Diff line number Diff line
@@ -470,10 +470,10 @@ public final class ProjectTopComp extends TopComponent {
        if (file == null) {
            System.out.print("No file chosen.");
        } else {
            OutputWindow.resetStopwatch();
            OutputWindow out = OutputWindow.measureTime();
            String faceId = HumanFaceFactory.instance().loadFace(file);
            HumanFace face = HumanFaceFactory.instance().getFace(faceId);
            OutputWindow.printDuration("Loaded model " + face.getShortName() +" with " + face.getMeshModel().getNumVertices() + " vertices");
            out.printDuration("Loaded model " + face.getShortName() +" with " + face.getMeshModel().getNumVertices() + " vertices");

            try {
                // simple hack:
Loading