/*
// -----------------------------------------------------------------------
// 
// (c) Copyright 2001-2008, MeVis Research GmbH, Bremen, Germany
// ALL RIGHTS RESERVED
// 
// THIS FILE CONTAINS CONFIDENTIAL AND PROPRIETARY INFORMATION OF MEVIS 
// RESEARCH GMBH. ANY DUPLICATION, MODIFICATION, DISTRIBUTION, OR 
// DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY PROHIBITED 
// WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF MEVIS RESEARCH GMBH.
// 
*/
//----------------------------------------------------------------------------------
//! Macro module View3Dext
/*!
// \file    View3Dext.js
// \author  Florian Link
// \date    2009-12-10
//
// 
*/
//----------------------------------------------------------------------------------



var _interactiveQualityFactor;

function wakeup()
{
//  ctx.field("renderer.geometryMode").value = "ViewportParallel_3D_Textures"; // oko: commented, field is missing
  modeChanged();
  updateInteractivityFactors();
  updateInteractivity();
}

function imageChanged() {
  triggerViewAll();
}

function lutTypeChanged() {
  var t = ctx.field("lutType").value;
  ctx.field("useLutEditor.on").value = (t=="Editor");
}

function modeChanged()
{
  var mode = ctx.field("mode").value;
  switch (mode) {
    case "VolumeRendering":
    ctx.field("renderer.blendMode").value = "Blend";
    ctx.field("renderer.renderMode").value = "Direct";
    break;
    case "MIP":
    ctx.field("renderer.blendMode").value = "MIP";
    ctx.field("renderer.renderMode").value = "Direct";
    break;
    case "Illuminated":
    if (!ctx.field("oldGraphics").value) {
      ctx.field("renderer.blendMode").value = "Blend";
      ctx.field("renderer.renderMode").value = "Illuminated";
    } else {
      ctx.field("renderer.blendMode").value = "Blend";
      ctx.field("renderer.renderMode").value = "Direct";
      ctx.field("mode").value = "VolumeRendering";
      MLAB.logError("Not supported on your hardware, you need a graphics card that supports 3D Textures");
    }
    break;
  }
}

function gvrGeometryModeChanged(field) {
  if (field.value == "AxisParallel_2D_Textures") {
    MLAB.log("Fallback to 2D Textures");
    ctx.field("oldGraphics").value = true;
    if (ctx.field("mode").value == "Illuminated") {
      ctx.field("mode").value = "VolumeRendering";
      MLAB.logError("Not supported on your hardware, you need a graphics card that supports 3D Textures");
    }
  }
}

function show(name)
{
  ctx.field("viewer.applyCameraOrientation").value = name;
}

function showLutEditorPanel() {
  ctx.showWindow("LutEditor");
}

function updateInteractivityFactors()
{
  var i = ctx.field("interactiveQuality").value;
  var quality = 1;
  var sampling = 1;
  switch (i) {
    case "VeryLow":
    quality = 0.1;
    sampling = 0.5;
    break;
    case "Low":
    quality = 0.1
    sampling = 1;
    break;
    case "Medium":
    quality  = 0.50
    sampling = 1;
    break;
    case "High":
    quality = 1
    sampling = 1;
    break;
  }
  _interactiveQualityFactor = quality;
  ctx.field("renderer.interactiveSamplingRate").value = sampling;
}

function updateInteractivity()
{
  var q = ctx.field("quality").value;
  ctx.field("renderer.interactiveQuality").value = _interactiveQualityFactor * q;
}

function doScreenshot() {
  
  var isIncrementalUpdate = ctx.field("renderer.incrementalUpdate").boolValue();
  ctx.field("renderer.incrementalUpdate").value = 0;
  for (i=0; i < 50;i++){
    MLAB.processInventorQueue();
  }
  
  // do the screenshot:
  var file = MLABFileManager.getUniqueFilename(MLABFileManager.getTmpDir(), "View3DTemp",".tif")
  ctx.control("viewer").createScreenshot(file);

  // ask user where to save it
  var target = MLABFileDialog.getSaveFileName(MLAB.readKey("View3D","LastScreenshot"),"Image files (*.tif *.tiff *.png *.jpg)",
              "Save Screenshot");

  if (target!="") {
    if (target.find(".")==-1) {
      target += ".png";
    }
    MLABGraphic.convertImage(file, target);
    MLAB.writeKey("View3D","LastScreenshot", target);
    MLAB.writeRegistry();
  }

  // remove tmp file
  MLABFileManager.remove(file);
  
  ctx.field("renderer.incrementalUpdate").value = isIncrementalUpdate;
}

function inventorInputOnChanged(field)
{
  var flag = !field.boolValue();
  ctx.field("inInvPreLUT").setHidden(flag);
  ctx.field("inInvPostLUT").setHidden(flag);
  ctx.field("inInvPostRenderer").setHidden(flag);
  ctx.updateLayout();
}

//! disable/enable postfiltering together with trace color activation/deactivation
function useTraceColorsChanged()
{
  ctx.field("renderer.filterVolumeData").setStringValue(ctx.field("useTraceColors").boolValue()?"FilterLinearPreClass":"FilterLinear");
}

function triggerViewAll() {
  if (ctx.field("autoViewAll").value) {
    ctx.field("viewer.viewAll").touch();
    ctx.field("clip.resetSurround").touch();
  }
}
//# MeVis signature v1
//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
//# owner: MeVis
//# date: 2009-06-10T04:12:25
//# hash: GR07ofKvUJxcWY2mZPAc+u8Uos0wOFSrGj71GYKLjshEsst7A2837GAdUyJwfkxMwiqDeUVcCaL8r4+m7JhPeA==
//# MeVis end

