home *** CD-ROM | disk | FTP | other *** search
/ Mundo do CD-ROM 119 / cdrom119.iso / internet / wwind / World_Wind_1.3.5_Full.exe / Plugins / ImageOverlay / ImageOverlay_1.1.cs < prev   
Encoding:
Text File  |  2006-05-01  |  47.7 KB  |  1,626 lines

  1. //----------------------------------------------------------------------------
  2. // NAME: Image Overlay 4
  3. // VERSION: 1.03
  4. // DESCRIPTION: Add custom image overlays in World Wind. Modified to use world files if available.
  5. // DEVELOPER: B. Reppen(Mashi), Modified by D. Deneau (geodan)
  6. // WEBSITE: http://www.mashiharu.com
  7. //----------------------------------------------------------------------------
  8. //
  9. // This file is in the Public Domain, and comes with no warranty.
  10. //
  11. //
  12. // Changes:
  13. //
  14. //   1.02 (2004-06-21): Fix to work with new texture load function in World Wind
  15. //   1.03 (2005-11-02): Modified to use world files if available, i.e. jgw,pgw,gfw,tfw
  16. //
  17. using Microsoft.DirectX;
  18. using Microsoft.DirectX.Direct3D;
  19. using System;
  20. using System.Globalization;
  21. using System.Diagnostics;
  22. using System.IO;
  23. using System.Windows.Forms;
  24. using System.Drawing;
  25. using WorldWind;
  26. using WorldWind.Renderable;
  27. using System.Collections;
  28. using System.ComponentModel;
  29.  
  30.  
  31. namespace Mashiharu.PluginSample
  32. {
  33.     public class ImageOverlayPlugin : WorldWind.PluginEngine.Plugin
  34.     {
  35.         public string Name = "Image Overlay 4";
  36.         ImageOverlayList overlay;
  37.         MenuItem menuOverlay;
  38.  
  39.         /// <summary>
  40.         /// Plugin entry point - All plugins must implement this function
  41.         /// </summary>
  42.         public override void Load()
  43.         {
  44.             // Add us to the ImageOverlayList of renderable objects - this puts us in the render loop
  45.             overlay = new ImageOverlayList(this);
  46.             Application.WorldWindow.CurrentWorld.RenderableObjects.Add(overlay);
  47.  
  48.             // Build menu
  49.             menuOverlay = new MenuItem(Name);
  50.             Application.PluginsMenu.MenuItems.Add(menuOverlay);
  51.             MenuItem menuAdd = new MenuItem("Add", new EventHandler(overlay.AddImage));
  52.             menuOverlay.MenuItems.Add(menuAdd);
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Unloads our plugin
  57.         /// </summary>
  58.         public override void Unload()
  59.         {
  60.             Application.WorldWindow.CurrentWorld.RenderableObjects.Remove(overlay.Name);
  61.             Application.PluginsMenu.MenuItems.Remove(menuOverlay);
  62.         }
  63.     }
  64.  
  65.     public enum Corner
  66.     {
  67.         None,
  68.         UL,
  69.         UR,
  70.         LL,
  71.         LR,
  72.         MM,
  73.         MR
  74.     }
  75.  
  76.     /// <summary>
  77.     /// ImageOverlay Example : Display images in ww
  78.     /// </summary>
  79.     public class ImageOverlayList : RenderableObjectList
  80.     {
  81.         public static MainApplication ParentApplication;
  82.         public static float Radius;
  83.         public ImageOverlayPlugin Plugin;
  84.         internal ImageOverlay m_selectedOverlay;
  85.         internal Texture CornerTexture;
  86.         internal Sprite Sprite;
  87.         internal Corner DragCorner;
  88.         ImageOverlayDialog properties;
  89.         ContextMenu menu;
  90.         string defaultImage;
  91.         int mouseDownX;
  92.         int mouseDownY;
  93.         DrawArgs drawArgs;
  94.  
  95.         /// <summary>
  96.         /// Set to open context menu
  97.         /// </summary>
  98.         int showMenu;
  99.  
  100.         public ImageOverlayList(ImageOverlayPlugin plugin) : base(plugin.Name)
  101.         {
  102.             this.Plugin = plugin;
  103.             ParentApplication = plugin.ParentApplication;
  104.             Radius = (float)ParentApplication.WorldWindow.CurrentWorld.EquatorialRadius;
  105.  
  106.             ImageOverlay.Parent = this;
  107.  
  108.             Load();
  109.  
  110.             // true to make this layer active on startup, this is equal to the checked state in layer manager
  111.             IsOn = true;
  112.  
  113.             ParentApplication.WorldWindow.MouseUp += new MouseEventHandler(WorldWindow_MouseUp);
  114.             ParentApplication.WorldWindow.MouseDown += new MouseEventHandler(WorldWindow_MouseDown);
  115.             ParentApplication.WorldWindow.MouseMove += new MouseEventHandler(WorldWindow_MouseMove);
  116.  
  117.             defaultImage = Path.Combine( plugin.PluginDirectory, "default.png" );
  118.         }
  119.  
  120.         public ImageOverlay SelectedOverlay
  121.         {
  122.             get
  123.             {
  124.                 return m_selectedOverlay;
  125.             }
  126.             set
  127.             {
  128.                 m_selectedOverlay = value;
  129.                 if(properties!=null)
  130.                     properties.SelectedOverlay = value;
  131.             }
  132.         }
  133.  
  134.         /// <summary>
  135.         /// RenderableObject abstract member (needed)
  136.         /// OBS: Worker thread (don't update UI directly from this thread)
  137.         /// </summary>
  138.         public override void Initialize(DrawArgs drawArgs)
  139.         {
  140.             Dispose();
  141.             this.drawArgs = drawArgs;
  142.             Sprite = new Sprite(drawArgs.device);
  143.  
  144.             // Create the menu
  145.             menu = new ContextMenu();
  146.             menu.MenuItems.Add( new MenuItem("&Remove", new EventHandler(Menu_Remove)));
  147.             menu.MenuItems.Add( new MenuItem("&Properties", new EventHandler(Menu_Properties)));
  148.  
  149.             if(CornerTexture == null)
  150.             {
  151.                 try
  152.                 {
  153.                     string texturePath = Path.Combine( Plugin.PluginDirectory, "corner.png");
  154.                     CornerTexture = ImageHelper.LoadTexture(drawArgs.device, texturePath);
  155.                 }
  156.                 catch( Exception caught )
  157.                 {
  158.                     MessageBox.Show(caught.Message, name);
  159.                     isOn = false;
  160.                     return;
  161.                 }
  162.             }
  163.  
  164.             isInitialized = true;
  165.         }
  166.  
  167.         /// <summary>
  168.         /// This is where we do our rendering
  169.         /// Called from UI thread = UI code safe in this function
  170.         /// </summary>
  171.         public override void Render(DrawArgs drawArgs)
  172.         {
  173.             if(!IsOn)
  174.                 return;
  175.             if(!isInitialized)
  176.                 return;
  177.             base.Render(drawArgs);
  178.             if(showMenu>0)
  179.             {
  180.                 // Let any re-selection be rendered and visible before we show the menu
  181.                 showMenu++;
  182.                 if(showMenu > 2)
  183.                 {
  184.                     // Open the context menu
  185.                     menu.Show(ParentApplication.WorldWindow, new Point(
  186.                         DrawArgs.LastMousePosition.X,
  187.                         DrawArgs.LastMousePosition.Y ));
  188.                     showMenu = 0;
  189.                 }
  190.             }
  191.         }
  192.  
  193.         /// <summary>
  194.         /// Handle mouse click
  195.         /// </summary>
  196.         public override bool PerformSelectionAction(DrawArgs drawArgs)
  197.         {
  198.             ImageOverlay previousSelected = SelectedOverlay;
  199.             if(SelectedOverlay != null)
  200.             {
  201.                 // Let the selected layer handle the click first.
  202.                 if(SelectedOverlay.PerformSelectionAction(drawArgs))
  203.                     return true;
  204.             }
  205.  
  206.             foreach(RenderableObject ro in ChildObjects)
  207.             {
  208.                 if(!ro.IsOn || !ro.isSelectable)
  209.                     continue;
  210.  
  211.                 if(ro==previousSelected)
  212.                     continue;
  213.  
  214.                 if (ro.PerformSelectionAction(drawArgs))
  215.                     return true;
  216.             }
  217.             return false;
  218.         }
  219.  
  220.         /// <summary>
  221.         /// RenderableObject abstract member (needed)
  222.         /// OBS: Worker thread (don't update UI directly from this thread)
  223.         /// </summary>
  224.         public override void Dispose()
  225.         {
  226.             isInitialized = false;
  227.  
  228.             Save();
  229.  
  230.             base.Dispose();
  231.             if(CornerTexture!=null)
  232.             {
  233.                 CornerTexture.Dispose();
  234.                 CornerTexture = null;
  235.             }
  236.  
  237.             if(Sprite!=null)
  238.             {
  239.                 Sprite.Dispose();
  240.                 Sprite = null;
  241.             }
  242.  
  243.             if(menu!=null)
  244.             {
  245.                 menu.Dispose();
  246.                 menu = null;
  247.             }
  248.         }
  249.  
  250.         /// <summary>
  251.         /// Creates a new image layer and adds to our overlay list
  252.         /// </summary>
  253.         public void AddImage( object sender, EventArgs e )
  254.         {
  255.  
  256.  
  257.  
  258.    //MessageBox.Show("add image");
  259.             ImageOverlay io = new ImageOverlay();
  260.             io.Name = "New Image";
  261.             io.Url = defaultImage;
  262.         io.FitToScreen = true;
  263.             Add(io);
  264.             SelectedOverlay = io;
  265.             Menu_Properties(this, EventArgs.Empty);
  266.         }
  267.  
  268.         string SaveFilePath
  269.         {
  270.             get
  271.             {
  272.                 return Path.Combine(Plugin.PluginDirectory, "overlays.txt");
  273.             }
  274.         }
  275.  
  276.         void Load()
  277.         {
  278.             if(!File.Exists(SaveFilePath))
  279.                 return;
  280.             using(StreamReader sr = File.OpenText(SaveFilePath))
  281.             {
  282.                 while(true)
  283.                 {
  284.                     string line = sr.ReadLine();
  285.                     if(line==null)
  286.                         break;
  287.                     ImageOverlay io = ImageOverlay.FromString(line);
  288.                     if(io != null)
  289.                         Add(io);
  290.                 }
  291.             }
  292.         }
  293.  
  294.         void Save()
  295.         {
  296.             using(StreamWriter sw = File.CreateText(SaveFilePath))
  297.             {
  298.                 foreach(ImageOverlay o in ChildObjects)
  299.                     sw.WriteLine( o.ToString() );
  300.             }
  301.         }
  302.  
  303.         private void WorldWindow_MouseDown(object sender, MouseEventArgs e)
  304.         {
  305.             mouseDownX = e.X;
  306.             mouseDownY = e.Y;
  307.         }
  308.  
  309.         private void WorldWindow_MouseUp(object sender, MouseEventArgs e)
  310.         {
  311.             if(e.Button != MouseButtons.Right)
  312.                 return;
  313.  
  314.             int dx = e.X-mouseDownX;
  315.             int dy = e.Y-mouseDownY;
  316.             double dist = Math.Sqrt(dx*dx + dy*dy);
  317.             if(dist > 3)
  318.                 // User dragged the mouse
  319.                 return;
  320.  
  321.             if(SelectedOverlay != null)
  322.             {
  323.                 if(SelectedOverlay.IsMouseInside)
  324.                 {
  325.                     showMenu++;
  326.                     return;
  327.                 }
  328.             }
  329.  
  330.             PerformSelectionAction(drawArgs);
  331.  
  332.             if(SelectedOverlay == null)
  333.                 return;
  334.  
  335.             if(SelectedOverlay.IsMouseInside)
  336.             {
  337.                 showMenu++;
  338.                 return;
  339.             }
  340.         }
  341.  
  342.         private void WorldWindow_MouseMove(object sender, MouseEventArgs e)
  343.         {
  344.             if(DragCorner==Corner.None)
  345.                 return;
  346.  
  347.             if(SelectedOverlay==null)
  348.                 return;
  349.  
  350.             SelectedOverlay.MouseMove(e);
  351.         }
  352.  
  353.         /// <summary>
  354.         /// Display the properties dialog
  355.         /// </summary>
  356.         public void ShowProperties( ImageOverlay ro)
  357.         {
  358.             if(properties!=null)
  359.                 properties.Dispose();
  360.  
  361.             properties = new ImageOverlayDialog(ro);
  362.             properties.Icon = ParentApplication.Icon;
  363.             properties.Show();
  364.         }
  365.  
  366.         /// <summary>
  367.         /// Removes an image from our overlay list
  368.         /// </summary>
  369.         private void Menu_Remove(object sender, EventArgs e)
  370.         {
  371.             if(SelectedOverlay==null)
  372.                 return;
  373.  
  374.             string msg = "Are you sure you want to remove " + SelectedOverlay.Name+"?";
  375.             if(MessageBox.Show(msg, "Remove image",
  376.                 MessageBoxButtons.YesNo, MessageBoxIcon.Question)!=DialogResult.Yes)
  377.                 return;
  378.  
  379.             // Remove the image
  380.             ChildObjects.Remove(SelectedOverlay);
  381.             SelectedOverlay.Dispose();
  382.             SelectedOverlay = null;
  383.         }
  384.  
  385.         private void Menu_Properties(object sender, EventArgs e)
  386.         {
  387.             if(SelectedOverlay==null)
  388.                 return;
  389.             ShowProperties(SelectedOverlay);
  390.         }
  391.     }
  392.  
  393.     /// <summary>
  394.     /// ImageOverlay Example : Display images in ww
  395.     /// </summary>
  396.     public class ImageOverlay : ImageLayer
  397.     {
  398.         DrawArgs drawArgs;
  399.         Point offset = new Point(4,4);
  400.         float closestDistanceSquared;
  401.         Corner closestPointcorner;
  402.         Vector3 ul;
  403.         Vector3 ur;
  404.         Vector3 ll;
  405.         Vector3 lr;
  406.         Vector3 mm;
  407.         Vector3 mr;
  408.  
  409.  
  410.         double[,] pos_p ;
  411.   double[] a ;
  412.   double[] b ;
  413.  
  414.  
  415.  
  416.         double angle ;
  417.         bool isMouseOverDragHandle;
  418.         public static ImageOverlayList Parent;
  419.         public bool FitToScreen;
  420.  
  421.         public ImageOverlay() : base("",
  422.             ImageOverlayList.Radius)
  423.         {
  424.             pos_p = new double[6,2]  ;
  425.    a = new double[6];
  426.    b = new double[6];
  427.    MinLat = -10;
  428.             MaxLat = 10;
  429.             MinLon = -10;
  430.             MaxLon = 10;
  431.  
  432.    pos_p[0,0] =    10 ;
  433.    pos_p[0,1] =    -10 ;
  434.  
  435.    pos_p[1,0] =    10 ;
  436.    pos_p[1,1] =    10 ;
  437.  
  438.    pos_p[2,0] =    -10 ;
  439.    pos_p[2,1] =    10 ;
  440.  
  441.    pos_p[3,0] =    -10 ;
  442.    pos_p[3,1] =    10 ;
  443.  
  444.    pos_p[4,0] =    0 ;
  445.    pos_p[4,1] =    0 ;
  446.  
  447.    pos_p[5,0] =    10 ;
  448.    pos_p[5,1] =    0 ;
  449.  
  450.             Opacity = 128;
  451.  
  452.             // We want to be drawn on top of everything else
  453.             //RenderPriority = RenderPriority.AtmosphericImages;
  454.             DisableZBuffer = true;
  455.             isSelectable = true;
  456.    //This drapes the image on the terrain.
  457.             this._terrainAccessor=ImageOverlayList.ParentApplication.WorldWindow.CurrentWorld.TerrainAccessor;
  458.             // true to make this layer active on startup, this is equal to the checked state in layer manager
  459.             IsOn = true;
  460.  
  461.             // Half resolution (since there might be many of us)
  462.             // TODO: Calculate resolution based on size (lat/lon) and terrain on/off
  463.             meshPointCount = 32; //32;
  464.         }
  465.  
  466.         /// <summary>
  467.         /// Filename or URL pointing to overlay bitmap
  468.         /// </summary>
  469.         public string Url
  470.         {
  471.             get
  472.             {
  473.                 if(ImagePath!=null)
  474.                     return ImagePath;
  475.                 return ImageUrl;
  476.             }
  477.             set
  478.             {
  479.                 if(value == null)
  480.                 {
  481.                     ImageUrl = null;
  482.                     ImagePath = null;
  483.                     return;
  484.                 }
  485.  
  486.                 if(value.ToLower().StartsWith("http://"))
  487.                 {
  488.                     if(ImageUrl==value)
  489.                         return;
  490.                     ImagePath = null;
  491.                     ImageUrl = value;
  492.                 }
  493.                 else
  494.                 {
  495.                     if(!Path.IsPathRooted(value))
  496.                         value = Path.Combine(Parent.Plugin.PluginDirectory, value);
  497.  
  498.                     // File
  499.                     ImageUrl = null;
  500.                     ImagePath = value;
  501.                 }
  502.                 Dispose();
  503.             }
  504.         }
  505.  
  506.            public void UpdateAngle(double percent)
  507.         {
  508. //            Debug.Assert(percent <= 10);
  509. //            Debug.Assert(percent >= 0);
  510.  
  511.             this.angle = percent;
  512.  
  513.             this.isInitialized = false;
  514.             CreateMesh();
  515.             this.isInitialized = true;
  516.         }
  517.  
  518.   public void get_ab ()
  519.     {
  520. /*    double[,] A_moins1 = {{   1.00000,      0.00000,      0.00000,      0.00000,      0.00000,     -0.00000},
  521.                           {   -2.00000,      2.00000,     -1.00000,      1.00000,      4.00000,     -4.00000},
  522.                           {   -1.00000,     -2.00000,      1.00000,     -2.00000,      0.00000,      4.00000 },
  523.                           {   1.00000,     -1.00000,      1.00000,     -1.00000,     -4.00000,      4.00000 } ,
  524.                           {    0.00000,      2.00000,      0.00000,      2.00000,      0.00000,     -4.00000},
  525.                            {     1.00000,     -1.00000,     -1.00000,      1.00000,      0.00000,     -0.00000} };*/
  526.     double[,] A_moins1 = {{       1.00000,     -2.00000,     -1.00000,      1.00000,      0.00000,      1.00000  },
  527.     {  0.00000,      2.00000,     -2.00000,     -1.00000,      2.00000,     -1.00000},
  528.     {  0.00000,     -1.00000,      1.00000,      1.00000,      0.00000,     -1.00000  },
  529.     {  0.00000,      1.00000,     -2.00000,     -1.00000,      2.00000,      1.00000    },
  530.     {  0.00000,      4.00000,      0.00000,     -4.00000,      0.00000,      0.00000      },
  531.     { -0.00000,     -4.00000,      4.00000,      4.00000,     -4.00000,     -0.00000        }};
  532.      a[0] = A_moins1[0,0] * pos_p[0,0] + A_moins1[1,0] * pos_p[1,0] + A_moins1[2,0] * pos_p[2,0] +
  533.             A_moins1[3,0] * pos_p[3,0] + A_moins1[4,0] * pos_p[4,0] + A_moins1[5,0] * pos_p[5,0] ;
  534.      a[1] = A_moins1[0,1] * pos_p[0,0] + A_moins1[1,1] * pos_p[1,0] + A_moins1[2,1] * pos_p[2,0] +
  535.             A_moins1[3,1] * pos_p[3,0] + A_moins1[4,1] * pos_p[4,0] + A_moins1[5,1] * pos_p[5,0] ;
  536.      a[2] = A_moins1[0,2] * pos_p[0,0] + A_moins1[1,2] * pos_p[1,0] + A_moins1[2,2] * pos_p[2,0] +
  537.             A_moins1[3,2] * pos_p[3,0] + A_moins1[4,2] * pos_p[4,0] + A_moins1[5,2] * pos_p[5,0] ;
  538.      a[3] = A_moins1[0,3] * pos_p[0,0] + A_moins1[1,3] * pos_p[1,0] + A_moins1[2,3] * pos_p[2,0] +
  539.             A_moins1[3,3] * pos_p[3,0] + A_moins1[4,3] * pos_p[4,0] + A_moins1[5,3] * pos_p[5,0] ;
  540.      a[4] = A_moins1[0,4] * pos_p[0,0] + A_moins1[1,4] * pos_p[1,0] + A_moins1[2,4] * pos_p[2,0] +
  541.             A_moins1[3,4] * pos_p[3,0] + A_moins1[4,4] * pos_p[4,0] + A_moins1[5,4] * pos_p[5,0] ;
  542.      a[5] = A_moins1[0,5] * pos_p[0,0] + A_moins1[1,5] * pos_p[1,0] + A_moins1[2,5] * pos_p[2,0] +
  543.             A_moins1[3,5] * pos_p[3,0] + A_moins1[4,5] * pos_p[4,0] + A_moins1[5,5] * pos_p[5,0] ;
  544.  
  545.      b[0] = A_moins1[0,0] * pos_p[0,1] + A_moins1[1,0] * pos_p[1,1] + A_moins1[2,0] * pos_p[2,1] +
  546.             A_moins1[3,0] * pos_p[3,1] + A_moins1[4,0] * pos_p[4,1] + A_moins1[5,0] * pos_p[5,1] ;
  547.      b[1] = A_moins1[0,1] * pos_p[0,1] + A_moins1[1,1] * pos_p[1,1] + A_moins1[2,1] * pos_p[2,1] +
  548.             A_moins1[3,1] * pos_p[3,1] + A_moins1[4,1] * pos_p[4,1] + A_moins1[5,1] * pos_p[5,1] ;
  549.      b[2] = A_moins1[0,2] * pos_p[0,1] + A_moins1[1,2] * pos_p[1,1] + A_moins1[2,2] * pos_p[2,1] +
  550.             A_moins1[3,2] * pos_p[3,1] + A_moins1[4,2] * pos_p[4,1] + A_moins1[5,2] * pos_p[5,1] ;
  551.      b[3] = A_moins1[0,3] * pos_p[0,1] + A_moins1[1,3] * pos_p[1,1] + A_moins1[2,3] * pos_p[2,1] +
  552.             A_moins1[3,3] * pos_p[3,1] + A_moins1[4,3] * pos_p[4,1] + A_moins1[5,3] * pos_p[5,1] ;
  553.      b[4] = A_moins1[0,4] * pos_p[0,1] + A_moins1[1,4] * pos_p[1,1] + A_moins1[2,4] * pos_p[2,1] +
  554.             A_moins1[3,4] * pos_p[3,1] + A_moins1[4,4] * pos_p[4,1] + A_moins1[5,4] * pos_p[5,1] ;
  555.      b[5] = A_moins1[0,5] * pos_p[0,1] + A_moins1[1,5] * pos_p[1,1] + A_moins1[2,5] * pos_p[2,1] +
  556.             A_moins1[3,5] * pos_p[3,1] + A_moins1[4,5] * pos_p[4,1] + A_moins1[5,5] * pos_p[5,1] ;
  557.  
  558.  
  559.     }
  560.  
  561.    public double get_lat2 ( double Tu,double Tv)
  562.      {
  563.      return (a[0] + a[1]*Tu + a[2]*Tv + a[3]*Tu*Tu + a[4]*Tv*Tv + a[5]*Tu*Tv) ;
  564.      }
  565.  
  566.    public double get_lon2 ( double Tu,double Tv)
  567.      {
  568.      return (b[0] + b[1]*Tu + b[2]*Tv + b[3]*Tu*Tu + b[4]*Tv*Tv + b[5]*Tu*Tv) ;
  569.  
  570.      }
  571.  
  572.  
  573.    protected override void CreateMesh()
  574.         {
  575.             int upperBound = meshPointCount - 1;
  576.             double scale2 = (double)1/(upperBound*upperBound);
  577.    float scaleFactor = (float)1/upperBound;
  578.             float latrange = (float)Math.Abs(maxLat - minLat);
  579.             float lonrange;
  580.             double lat;
  581.             double lon;
  582.    if(minLon < maxLon)
  583.                 lonrange = (float)(maxLon - minLon);
  584.             else
  585.                 lonrange = (float)(360.0f + maxLon - minLon);
  586.  
  587.             int opacityColor = System.Drawing.Color.FromArgb(this._opacity,0,0,0).ToArgb();
  588.    int opacityColor2 = System.Drawing.Color.FromArgb(250,0,0,0).ToArgb();
  589.    vertices = new CustomVertex.PositionColoredTextured[meshPointCount * meshPointCount];
  590.             get_ab();
  591.  
  592.    for(int i = 0; i < meshPointCount; i++)
  593.             {
  594.                 for(int j = 0; j < meshPointCount; j++)
  595.                 {
  596.                     double height = 0;
  597.  
  598. /*     lat = scale2 *( (pos_p[0,0]* (upperBound-i)*(upperBound-j) ) +
  599.                      (pos_p[2,0]* (i) * (upperBound-j) )  +
  600.                      (pos_p[1,0]* (upperBound-i)*(j) )    +
  601.                      (pos_p[3,0]* (i)*(j) ) );
  602.      lon = scale2 *( (pos_p[0,1]* (upperBound-i)*(upperBound-j) ) +
  603.                      (pos_p[2,1]* (i) * (upperBound-j) )  +
  604.                      (pos_p[1,1]* (upperBound-i)*(j) )    +
  605.                      (pos_p[3,1]* (i)*(j) ) );*/
  606.  
  607.     lat = get_lat2 (  j*scaleFactor , i*scaleFactor);
  608.     lon = get_lon2 (  j*scaleFactor , i*scaleFactor);
  609. //                    MessageBox.Show("Lat :"+lat.ToString()+" Lon : "+lon.ToString() );
  610. //            Vector3 tmp_p = MathEngine.SphericalToCartesian(lat,lon, layerRadius);
  611. //            Vector3 pointScreenXy = drawArgs.WorldCamera.Project(tmp_p);
  612. //            Parent.Sprite.Draw2D(Parent.CornerTexture, offset, 0, new Point((int)pointScreenXy.X, (int)pointScreenXy.Y), -1);
  613.      if(this._terrainAccessor != null)
  614.                         height = this.verticalExaggeration * this._terrainAccessor.GetElevationAt(
  615.                     lat,    //    (double)maxLat - Math.Cos(angle)*(scaleFactor * latrange * i)+ Math.Sin(angle)*(scaleFactor * lonrange * j),
  616.                     lon,    //    (double)minLon + Math.Sin(angle)*(scaleFactor * latrange * i)+ Math.Cos(angle)*(scaleFactor * lonrange * j),
  617.                             (double)upperBound / latrange);
  618.  
  619.                     Vector3 pos = MathEngine.SphericalToCartesian(
  620.                         lat,//maxLat - Math.Cos(angle)*(scaleFactor * latrange * i)+ Math.Sin(angle)*(scaleFactor * lonrange * j), //- scaleFactor*latrange*i,
  621.                         lon,//minLon + Math.Sin(angle)*(scaleFactor * latrange * i)+ Math.Cos(angle)*(scaleFactor * lonrange * j), //+ scaleFactor*lonrange*j,
  622.                         layerRadius + height);
  623.  
  624.                     vertices[i*meshPointCount + j].X = pos.X;
  625.                     vertices[i*meshPointCount + j].Y = pos.Y;
  626.                     vertices[i*meshPointCount + j].Z = pos.Z;
  627.  
  628.                     vertices[i*meshPointCount + j].Tu = j*scaleFactor;
  629.                     vertices[i*meshPointCount + j].Tv = i*scaleFactor;
  630.                     vertices[i*meshPointCount + j].Color = opacityColor;
  631.                 }
  632.             }
  633.         for(int i = 0; i < upperBound; i++)
  634.           {
  635.     vertices[i*meshPointCount + upperBound].Color = opacityColor2;
  636.     vertices[i*meshPointCount + 0].Color = opacityColor2;
  637.     vertices[i*meshPointCount + upperBound-1].Color = opacityColor2;
  638.     vertices[i*meshPointCount + 1].Color = opacityColor2;
  639.  
  640.  
  641.     vertices[0*meshPointCount + i].Color = opacityColor2;
  642.     vertices[1*meshPointCount + i].Color = opacityColor2;
  643.     vertices[(upperBound-1)*meshPointCount + i].Color = opacityColor2;
  644.     vertices[upperBound*meshPointCount + i].Color = opacityColor2;
  645.  
  646.     }
  647.  
  648.  
  649.  
  650.             indices = new short[2 * upperBound * upperBound * 3];
  651.             for(int i = 0; i < upperBound; i++)
  652.             {
  653.                 for(int j = 0; j < upperBound; j++)
  654.                 {
  655.                     indices[(2*3*i*upperBound) + 6*j] = (short)(i*meshPointCount + j);
  656.                     indices[(2*3*i*upperBound) + 6*j + 1] = (short)((i+1)*meshPointCount + j);
  657.                     indices[(2*3*i*upperBound) + 6*j + 2] = (short)(i*meshPointCount + j+1);
  658.  
  659.                     indices[(2*3*i*upperBound) + 6*j + 3] = (short)(i*meshPointCount + j+1);
  660.                     indices[(2*3*i*upperBound) + 6*j + 4] = (short)((i+1)*meshPointCount + j);
  661.                     indices[(2*3*i*upperBound) + 6*j + 5] = (short)((i+1)*meshPointCount + j+1);
  662.                 }
  663.             }
  664.         }
  665.  
  666.  
  667.  
  668.         /// <summary>
  669.         /// This is where we do our rendering
  670.         /// Called from UI thread = UI code safe in this function
  671.         /// </summary>
  672.         public override void Render(DrawArgs drawArgs)
  673.         {
  674.             try
  675.             {
  676.                 if(!isInitialized)
  677.                     return;
  678.  
  679.                 if(FitToScreen)
  680.                 {
  681.                     MakeVisible();
  682.                     FitToScreen = false;
  683.                 }
  684.  
  685.                 closestDistanceSquared = float.MaxValue;
  686.                 if(Parent.SelectedOverlay == this)
  687.                 {
  688.                     Parent.Sprite.Begin(SpriteFlags.None);
  689.                     DoCorner(drawArgs, Corner.UL, ul);
  690.                     DoCorner(drawArgs, Corner.UR, ur);
  691.                     DoCorner(drawArgs, Corner.LL, ll);
  692.                     DoCorner(drawArgs, Corner.LR, lr);
  693.      DoCorner(drawArgs, Corner.MM, mm);
  694.      DoCorner(drawArgs, Corner.MR, mr);
  695.                     Parent.Sprite.End();
  696.  
  697.                     if(Parent.DragCorner != Corner.None)
  698.                     {
  699.                         DrawArgs.MouseCursor = CursorType.Cross;
  700.                         return;
  701.                     }
  702.  
  703.                     if(closestDistanceSquared < 30)
  704.                     {
  705.                         DrawArgs.MouseCursor = CursorType.Cross;
  706.                         isMouseOverDragHandle = true;
  707.                     }
  708.                     else
  709.                         isMouseOverDragHandle = false;
  710.                 }
  711.             }
  712.             finally
  713.             {
  714.                 base.Render(drawArgs);
  715.             }
  716.         }
  717.  
  718.         void DoCorner(DrawArgs drawArgs, Corner corner, Vector3 worldPoint )
  719.         {
  720.             if(!drawArgs.WorldCamera.ViewFrustum.ContainsPoint(worldPoint))
  721.                 return;
  722.             Vector3 pointScreenXy = drawArgs.WorldCamera.Project(worldPoint);
  723.             float dx = pointScreenXy.X - DrawArgs.LastMousePosition.X;
  724.             float dy = pointScreenXy.Y - DrawArgs.LastMousePosition.Y;
  725.             float distSq = dx*dx + dy*dy;
  726.             if(distSq < closestDistanceSquared)
  727.             {
  728.                 closestDistanceSquared = distSq;
  729.                 closestPointcorner = corner;
  730.             }
  731.  
  732.             Parent.Sprite.Draw2D(Parent.CornerTexture, offset, 0, new Point((int)pointScreenXy.X, (int)pointScreenXy.Y), -1);
  733.         }
  734.  
  735.         /// <summary>
  736.         /// RenderableObject abstract member (needed)
  737.         /// OBS: Worker thread (don't update UI directly from this thread)
  738.         /// </summary>
  739.         public override void Initialize(DrawArgs drawArgs)
  740.         {
  741.             this.drawArgs = drawArgs;
  742.             base.Initialize(drawArgs);
  743.  
  744.             UpdateCorners();
  745.         }
  746.  
  747.         /// <summary>
  748.         /// RenderableObject abstract member (needed)
  749.         /// OBS: Worker thread (don't update UI directly from this thread)
  750.         /// </summary>
  751.         public override void Dispose()
  752.         {
  753.             base.Dispose();
  754.         }
  755.  
  756.         /// <summary>
  757.         /// Determine if mouse cursor is inside the image boundaries
  758.         /// </summary>
  759.  
  760.   public double min(double f1,double f2)
  761.     {
  762.     if (f1<f2) return f1;
  763.           else return f2;
  764.     }
  765.  
  766.   public double max(double f1,double f2)
  767.     {
  768.     if (f1>f2) return f1;
  769.           else return f2;
  770.     }
  771.  
  772.  
  773.   public void GetMinMax()
  774.     {
  775.     minLat = (float)min (min(pos_p[0,0],pos_p[1,0]),min(pos_p[2,0],pos_p[3,0]));
  776.     maxLat = (float)max (max(pos_p[0,0],pos_p[1,0]),max(pos_p[2,0],pos_p[3,0]));
  777.  
  778.     minLon = (float)min(min(pos_p[0,1],pos_p[1,1]),min(pos_p[2,1],pos_p[3,1])  ) ;
  779.     maxLon = (float)max(max(pos_p[0,1],pos_p[1,1]),max(pos_p[2,1],pos_p[3,1]) ) ;
  780.  
  781.  
  782.     }
  783.  
  784.  
  785.   public bool IsMouseInside
  786.         {
  787.             get
  788.             {
  789.  
  790.     Angle latitude, longitude;
  791.                 drawArgs.WorldCamera.PickingRayIntersection(
  792.                     DrawArgs.LastMousePosition.X,
  793.                     DrawArgs.LastMousePosition.Y,
  794.                     out latitude,
  795.                     out longitude );
  796.  
  797.                 if(Angle.IsNaN(latitude))
  798.                     return false;
  799.  
  800.                 if(Angle.IsNaN(longitude))
  801.                     return false;
  802.  
  803.                 bool clickedInside = true;
  804.                 if(latitude.Degrees < minLat)
  805.                     clickedInside = false;
  806.                 else if(latitude.Degrees > maxLat)
  807.                     clickedInside = false;
  808.                 else if(longitude.Degrees < minLon)
  809.                     clickedInside = false;
  810.                 else if(longitude.Degrees > maxLon)
  811.                     clickedInside = false;
  812.                 return clickedInside;
  813.             }
  814.         }
  815.  
  816.         /// <summary>
  817.         /// RenderableObject abstract member (needed)
  818.         /// Called from UI thread = UI code safe in this function
  819.         /// </summary>
  820.         public override bool PerformSelectionAction(DrawArgs drawArgs)
  821.         {
  822.             if(!isInitialized)
  823.                 return false;
  824.  
  825.             if(Parent.DragCorner != Corner.None)
  826.             {
  827.                 // Drag complete
  828.                 Parent.DragCorner = Corner.None;
  829.                 return true;
  830.             }
  831.  
  832.  
  833.             // Make image selected if mouse is clicked over it
  834.             if(Parent.SelectedOverlay==null && IsMouseInside)
  835.             {
  836.                 Parent.SelectedOverlay = this;
  837.                 return true;
  838.             }
  839.  
  840.             if(Parent.SelectedOverlay == this)
  841.             {
  842.                 // De-select if clicked outside
  843.                 if(!isMouseOverDragHandle)
  844.                 {
  845.                     Parent.SelectedOverlay = null;
  846.                     return false;
  847.                 }
  848.             }
  849.  
  850.             if(!isMouseOverDragHandle)
  851.                 return false;
  852.  
  853.             Parent.DragCorner = closestPointcorner;
  854.             return true;
  855.         }
  856.  
  857.         /// <summary>
  858.         /// Add items for our special context menu
  859.         /// </summary>
  860.         /// <param name="menu"></param>
  861.         public override void BuildContextMenu(ContextMenu menu)
  862.         {
  863.             base.BuildContextMenu(menu);
  864.             menu.MenuItems.Add("Overlay Properties", new EventHandler(OnProperties));
  865.         }
  866.  
  867.         void OnProperties(object sender, EventArgs e)
  868.         {
  869.             Parent.ShowProperties(this);
  870.         }
  871.  
  872.         void UpdateCorners()
  873.         {
  874.             if(minLon > maxLon)
  875.             {
  876.                 float tmp = (float)minLon;
  877.                 minLon = maxLon;
  878.                 maxLon = tmp;
  879.             }
  880.             if(minLat > maxLat)
  881.             {
  882.                 float tmp = (float)minLat;
  883.                 minLat = maxLat;
  884.                 maxLat = tmp;
  885.             }
  886.  
  887.    float dlat = (float)(minLat-maxLat);
  888.    float dlon = (float)(maxLon - minLon);
  889.  
  890.   float theHeightUL = this._terrainAccessor.GetElevationAt(pos_p[0,0],pos_p[0,1],meshPointCount);
  891.   float theHeightUR = this._terrainAccessor.GetElevationAt(pos_p[1,0],pos_p[1,1],meshPointCount);
  892.   float theHeightLL = this._terrainAccessor.GetElevationAt(pos_p[2,0],pos_p[2,1],meshPointCount);
  893.   float theHeightLR = this._terrainAccessor.GetElevationAt(pos_p[3,0],pos_p[3,1],meshPointCount);
  894.   float theHeightMM = this._terrainAccessor.GetElevationAt(pos_p[4,0],pos_p[4,1],meshPointCount);
  895.   float theHeightMR = this._terrainAccessor.GetElevationAt(pos_p[5,0],pos_p[5,1],meshPointCount);
  896.  
  897.  
  898.  
  899.  
  900.    ul = MathEngine.SphericalToCartesian(pos_p[0,0],pos_p[0,1], layerRadius+10+(theHeightUL*verticalExaggeration));
  901.             ur = MathEngine.SphericalToCartesian(pos_p[1,0],pos_p[1,1], layerRadius+10+(theHeightUR*verticalExaggeration));
  902.             ll = MathEngine.SphericalToCartesian(pos_p[2,0],pos_p[2,1], layerRadius+10+(theHeightLL*verticalExaggeration));
  903.             lr = MathEngine.SphericalToCartesian(pos_p[3,0],pos_p[3,1], layerRadius+10+(theHeightUR*verticalExaggeration));
  904.             mm = MathEngine.SphericalToCartesian(pos_p[4,0],pos_p[4,1], layerRadius+10+(theHeightLL*verticalExaggeration));
  905.             mr = MathEngine.SphericalToCartesian(pos_p[5,0],pos_p[5,1], layerRadius+10+(theHeightUR*verticalExaggeration));
  906.  
  907.  
  908.   }
  909.  
  910.         void MakeVisible()
  911.         {
  912.             try
  913.             {
  914.                 int centerX = drawArgs.screenWidth / 2;
  915.                 int centerY = drawArgs.screenHeight / 2;
  916.                 int margin = 50;
  917.  
  918.  
  919.                 this.maxLat = (float)drawArgs.WorldCamera.Latitude.Degrees + 20f;
  920.                 this.minLat = (float)drawArgs.WorldCamera.Latitude.Degrees - 20f;
  921.                 this.minLon = (float)drawArgs.WorldCamera.Longitude.Degrees - 20f;
  922.                 this.maxLon = (float)drawArgs.WorldCamera.Longitude.Degrees + 20f;
  923.  
  924.                 Angle latitude, longitude;
  925.                 drawArgs.WorldCamera.PickingRayIntersection(centerX, margin, out latitude, out longitude);
  926.                 if(Angle.IsNaN(latitude))
  927.                     return;
  928.                 maxLat = (float)latitude.Degrees;
  929.  
  930.                 drawArgs.WorldCamera.PickingRayIntersection(centerX, drawArgs.screenHeight - margin, out latitude, out longitude);
  931.                 if(Angle.IsNaN(latitude))
  932.                     return;
  933.                 minLat = (float)latitude.Degrees;
  934.  
  935.                 drawArgs.WorldCamera.PickingRayIntersection(margin, centerY, out latitude, out longitude);
  936.                 if(Angle.IsNaN(longitude))
  937.                     return;
  938.                 minLon = (float)longitude.Degrees;
  939.  
  940.                 drawArgs.WorldCamera.PickingRayIntersection(drawArgs.screenWidth - margin, centerY, out latitude, out longitude);
  941.                 if(Angle.IsNaN(longitude))
  942.                     return;
  943.                 maxLon = (float)longitude.Degrees;
  944.  
  945.     drawArgs.WorldCamera.PickingRayIntersection(margin, margin, out latitude, out longitude);
  946.     pos_p[0,1] = (double)longitude.Degrees;
  947.     pos_p[0,0] = (double)latitude.Degrees;
  948.  
  949.                 drawArgs.WorldCamera.PickingRayIntersection(drawArgs.screenWidth - margin, margin, out latitude, out longitude);
  950.     pos_p[1,1] = (double)longitude.Degrees;
  951.     pos_p[1,0] = (double)latitude.Degrees;
  952.  
  953.                 drawArgs.WorldCamera.PickingRayIntersection(drawArgs.screenWidth - margin,  drawArgs.screenHeight - margin, out latitude, out longitude);
  954.     pos_p[3,1] = (double)longitude.Degrees;
  955.     pos_p[3,0] = (double)latitude.Degrees;
  956.  
  957.              drawArgs.WorldCamera.PickingRayIntersection(margin,  drawArgs.screenHeight - margin, out latitude, out longitude);
  958.     pos_p[2,1] = (double)longitude.Degrees;
  959.     pos_p[2,0] = (double)latitude.Degrees;
  960.  
  961.              drawArgs.WorldCamera.PickingRayIntersection(drawArgs.screenWidth/2,  drawArgs.screenHeight /2, out latitude, out longitude);
  962.     pos_p[4,1] = (double)longitude.Degrees;
  963.     pos_p[4,0] = (double)latitude.Degrees;
  964.  
  965.              drawArgs.WorldCamera.PickingRayIntersection(drawArgs.screenWidth-margin,  drawArgs.screenHeight /2, out latitude, out longitude);
  966.     pos_p[5,1] = (double)longitude.Degrees;
  967.     pos_p[5,0] = (double)latitude.Degrees;
  968.  
  969.  
  970.       }
  971.             finally
  972.             {
  973.                 UpdateCorners();
  974.                 CreateMesh();
  975.             }
  976.         }
  977.  
  978.         public void MouseMove(MouseEventArgs e)
  979.         {
  980.             Angle latitude, longitude;
  981.             drawArgs.WorldCamera.PickingRayIntersection(e.X, e.Y, out latitude, out longitude);
  982.             switch(Parent.DragCorner)
  983.             {
  984.                 case Corner.UL:
  985.                     pos_p[0,1] = (double)longitude.Degrees;
  986.                     pos_p[0,0] = (double)latitude.Degrees;
  987.                     break;
  988.                 case Corner.UR:
  989.                     pos_p[1,1] = (double)longitude.Degrees;
  990.                     pos_p[1,0] = (double)latitude.Degrees;
  991.                     break;
  992.                 case Corner.LL:
  993.                     pos_p[2,1] = (double)longitude.Degrees;
  994.                     pos_p[2,0] = (double)latitude.Degrees;
  995.                     break;
  996.                 case Corner.LR:
  997.                     pos_p[3,1] = (double)longitude.Degrees;
  998.                     pos_p[3,0] = (double)latitude.Degrees;
  999.                     break;
  1000.                 case Corner.MM:
  1001.                     pos_p[4,1] = (double)longitude.Degrees;
  1002.                     pos_p[4,0] = (double)latitude.Degrees;
  1003.                     break;
  1004.                 case Corner.MR:
  1005.                     pos_p[5,1] = (double)longitude.Degrees;
  1006.                     pos_p[5,0] = (double)latitude.Degrees;
  1007.                     break;
  1008.  
  1009.  
  1010.  
  1011.             }
  1012.             UpdateCorners();
  1013.             CreateMesh();
  1014.         }
  1015.  
  1016.         /// <summary>
  1017.         /// Delete this image
  1018.         /// </summary>
  1019.         public override void Delete()
  1020.         {
  1021.             Parent.ChildObjects.Remove(this);
  1022.             Dispose();
  1023.             Parent.SelectedOverlay = null;
  1024.         }
  1025.  
  1026.         /// <summary>
  1027.         /// Deserialize overlay
  1028.         /// </summary>
  1029.         public static ImageOverlay FromString(string line)
  1030.         {
  1031.             string[] fields = line.Split('\t');
  1032.     //        if(fields.Length!=14)
  1033.     //            return null;
  1034.  
  1035.             ImageOverlay io = new ImageOverlay();
  1036.             io.name = fields[0];
  1037.             io.Url = fields[1];
  1038.             //io.MinLat = float.Parse( fields[2], CultureInfo.InvariantCulture);
  1039.             //io.MaxLat = float.Parse( fields[3], CultureInfo.InvariantCulture);
  1040.             //io.MinLon = float.Parse( fields[4], CultureInfo.InvariantCulture);
  1041.             //io.MaxLon = float.Parse( fields[5], CultureInfo.InvariantCulture);
  1042.             io.pos_p[0,0] = double.Parse( fields[2], CultureInfo.InvariantCulture);
  1043.    io.pos_p[0,1] = double.Parse( fields[3], CultureInfo.InvariantCulture);
  1044.    io.pos_p[1,0] = double.Parse( fields[4], CultureInfo.InvariantCulture);
  1045.    io.pos_p[1,1] = double.Parse( fields[5], CultureInfo.InvariantCulture);
  1046.    io.pos_p[2,0] = double.Parse( fields[6], CultureInfo.InvariantCulture);
  1047.    io.pos_p[2,1] = double.Parse( fields[7], CultureInfo.InvariantCulture);
  1048.    io.pos_p[3,0] = double.Parse( fields[8], CultureInfo.InvariantCulture);
  1049.    io.pos_p[3,1] = double.Parse( fields[9], CultureInfo.InvariantCulture);
  1050.    io.pos_p[4,0] = double.Parse( fields[10], CultureInfo.InvariantCulture);
  1051.    io.pos_p[4,1] = double.Parse( fields[11], CultureInfo.InvariantCulture);
  1052.    io.pos_p[5,0] = double.Parse( fields[12], CultureInfo.InvariantCulture);
  1053.    io.pos_p[5,1] = double.Parse( fields[13], CultureInfo.InvariantCulture);
  1054.  
  1055.  
  1056.    io.Opacity = byte.Parse( fields[14], CultureInfo.InvariantCulture);
  1057.    io.GetMinMax();
  1058.             return io;
  1059.         }
  1060.  
  1061.         /// <summary>
  1062.         /// Serialize overlay to string
  1063.         /// </summary>
  1064.         public override string ToString()
  1065.         {
  1066.             string url = Url == null ? "" : Url;
  1067.             url = url.Replace(Parent.Plugin.PluginDirectory,"");
  1068.             if(url.StartsWith(@"\"))
  1069.                 url = url.Substring(1);
  1070.             string res = string.Format(CultureInfo.InvariantCulture,
  1071.                 "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}",
  1072.                 name,
  1073.                 url,
  1074.                 pos_p[0,0],
  1075.                 pos_p[0,1],
  1076.                 pos_p[1,0],
  1077.                 pos_p[1,1],
  1078.                 pos_p[2,0],
  1079.                 pos_p[2,1],
  1080.                 pos_p[3,0],
  1081.              pos_p[3,1],
  1082.     pos_p[4,0],
  1083.              pos_p[4,1],
  1084.     pos_p[5,0],
  1085.              pos_p[5,1],
  1086.  
  1087.                 Opacity );
  1088.             return res;
  1089.         }
  1090.     }
  1091.  
  1092.  
  1093.     /// <summary>
  1094.     /// Properties dialog
  1095.     /// </summary>
  1096.     public class ImageOverlayDialog : System.Windows.Forms.Form
  1097.     {
  1098.         private System.Windows.Forms.TrackBar opacity;
  1099.         private System.Windows.Forms.Label label2;
  1100.         private System.Windows.Forms.Label label3;
  1101.         private System.Windows.Forms.GroupBox groupBox1;
  1102.         public ImageOverlay m_selectedOverlay;
  1103.         private System.Windows.Forms.TextBox url;
  1104.         private System.Windows.Forms.TextBox name;
  1105.         private System.Windows.Forms.Button buttonBrowse;
  1106.         private System.Windows.Forms.Label label1;
  1107.         private System.Windows.Forms.Label label4;
  1108.         private System.Windows.Forms.Button buttonFitScreen;
  1109.         private System.Windows.Forms.Button buttonClose;
  1110.  
  1111.         /// <summary>
  1112.         /// Required designer variable.
  1113.         /// </summary>
  1114.         private System.ComponentModel.Container components = null;
  1115.  
  1116.         public ImageOverlayDialog( ImageOverlay overlay )
  1117.         {
  1118.             //
  1119.             // Required for Windows Form Designer support
  1120.             //
  1121.             InitializeComponent();
  1122.  
  1123.             SelectedOverlay = overlay;
  1124.         }
  1125.  
  1126.         public ImageOverlay SelectedOverlay
  1127.         {
  1128.             get
  1129.             {
  1130.                 return m_selectedOverlay;
  1131.             }
  1132.             set
  1133.             {
  1134.                 m_selectedOverlay = value;
  1135.                 bool enabled = m_selectedOverlay != null;
  1136.  
  1137.                 opacity.Enabled = enabled;
  1138.                 name.Enabled = enabled;
  1139.                 url.Enabled = enabled;
  1140.  
  1141.                 if(m_selectedOverlay == null)
  1142.                     return;
  1143.  
  1144.                 // Limit transparency minimum
  1145.                 int opacityValue = (int)(100*SelectedOverlay.OpacityPercent);
  1146.                 opacity.Value = Math.Max(opacityValue, opacity.Minimum);
  1147.  
  1148.                 name.Text = SelectedOverlay.Name;
  1149.                 url.Text = SelectedOverlay.Url;
  1150.             }
  1151.         }
  1152.  
  1153.         /// <summary>
  1154.         /// Clean up any resources being used.
  1155.         /// </summary>
  1156.         protected override void Dispose( bool disposing )
  1157.         {
  1158.             if( disposing )
  1159.             {
  1160.                 if(components != null)
  1161.                 {
  1162.                     components.Dispose();
  1163.                 }
  1164.             }
  1165.             base.Dispose( disposing );
  1166.         }
  1167.  
  1168.         #region Windows Form Designer generated code
  1169.         /// <summary>
  1170.         /// Required method for Designer support - do not modify
  1171.         /// the contents of this method with the code editor.
  1172.         /// </summary>
  1173.         private void InitializeComponent()
  1174.         {
  1175.             this.opacity = new System.Windows.Forms.TrackBar();
  1176.             this.label2 = new System.Windows.Forms.Label();
  1177.             this.label3 = new System.Windows.Forms.Label();
  1178.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  1179.             this.url = new System.Windows.Forms.TextBox();
  1180.             this.name = new System.Windows.Forms.TextBox();
  1181.             this.buttonBrowse = new System.Windows.Forms.Button();
  1182.             this.label1 = new System.Windows.Forms.Label();
  1183.             this.label4 = new System.Windows.Forms.Label();
  1184.             this.buttonFitScreen = new System.Windows.Forms.Button();
  1185.             this.buttonClose = new System.Windows.Forms.Button();
  1186.             ((System.ComponentModel.ISupportInitialize)(this.opacity)).BeginInit();
  1187.             this.groupBox1.SuspendLayout();
  1188.             this.SuspendLayout();
  1189.             // 
  1190.             // opacity
  1191.             // 
  1192.             this.opacity.Location = new System.Drawing.Point(5, 20);
  1193.             this.opacity.Maximum = 100;
  1194.             this.opacity.Minimum = 7;
  1195.             this.opacity.Name = "opacity";
  1196.             this.opacity.Size = new System.Drawing.Size(273, 45);
  1197.             this.opacity.TabIndex = 0;
  1198.             this.opacity.TickFrequency = 8;
  1199.             this.opacity.Value = 100;
  1200.             this.opacity.Scroll += new System.EventHandler(this.opacity_Scroll);
  1201.             // 
  1202.             // label2
  1203.             // 
  1204.             this.label2.Location = new System.Drawing.Point(8, 56);
  1205.             this.label2.Name = "label2";
  1206.             this.label2.Size = new System.Drawing.Size(48, 18);
  1207.             this.label2.TabIndex = 1;
  1208.             this.label2.Text = "&Clear";
  1209.             // 
  1210.             // label3
  1211.             // 
  1212.             this.label3.Location = new System.Drawing.Point(229, 56);
  1213.             this.label3.Name = "label3";
  1214.             this.label3.Size = new System.Drawing.Size(48, 18);
  1215.             this.label3.TabIndex = 2;
  1216.             this.label3.Text = "&Opaque";
  1217.             // 
  1218.             // groupBox1
  1219.             //
  1220.             this.groupBox1.Controls.Add(this.label2);
  1221.             this.groupBox1.Controls.Add(this.label3);
  1222.             this.groupBox1.Controls.Add(this.opacity);
  1223.             this.groupBox1.Location = new System.Drawing.Point(5, 119);
  1224.             this.groupBox1.Name = "groupBox1";
  1225.             this.groupBox1.Size = new System.Drawing.Size(282, 81);
  1226.             this.groupBox1.TabIndex = 5;
  1227.             this.groupBox1.TabStop = false;
  1228.             this.groupBox1.Text = "Transparency";
  1229.             // 
  1230.             // url
  1231.             // 
  1232.             this.url.Location = new System.Drawing.Point(16, 87);
  1233.             this.url.Name = "url";
  1234.             this.url.Size = new System.Drawing.Size(198, 20);
  1235.             this.url.TabIndex = 3;
  1236.             this.url.Text = "";
  1237.             this.url.Leave += new System.EventHandler(this.url_Leave);
  1238.             this.url.TextChanged += new System.EventHandler(this.url_Change);
  1239.             //
  1240.             // name
  1241.             // 
  1242.             this.name.Location = new System.Drawing.Point(17, 31);
  1243.             this.name.Name = "name";
  1244.             this.name.Size = new System.Drawing.Size(266, 20);
  1245.             this.name.TabIndex = 1;
  1246.             this.name.Text = "";
  1247.             this.name.TextChanged += new System.EventHandler(this.name_TextChanged);
  1248.             //
  1249.             // buttonBrowse
  1250.             // 
  1251.             this.buttonBrowse.Location = new System.Drawing.Point(217, 85);
  1252.             this.buttonBrowse.Name = "buttonBrowse";
  1253.             this.buttonBrowse.Size = new System.Drawing.Size(67, 23);
  1254.             this.buttonBrowse.TabIndex = 4;
  1255.             this.buttonBrowse.Text = "&Browse...";
  1256.             this.buttonBrowse.Click += new System.EventHandler(this.buttonBrowse_Click);
  1257.             // 
  1258.             // label1
  1259.             //
  1260.             this.label1.Location = new System.Drawing.Point(12, 71);
  1261.             this.label1.Name = "label1";
  1262.             this.label1.Size = new System.Drawing.Size(100, 16);
  1263.             this.label1.TabIndex = 2;
  1264.             this.label1.Text = "Filename or URL";
  1265.             //
  1266.             // label4
  1267.             // 
  1268.             this.label4.Location = new System.Drawing.Point(11, 15);
  1269.             this.label4.Name = "label4";
  1270.             this.label4.Size = new System.Drawing.Size(100, 15);
  1271.             this.label4.TabIndex = 0;
  1272.             this.label4.Text = "Name";
  1273.             // 
  1274.             // buttonFitScreen
  1275.             // 
  1276.             this.buttonFitScreen.Location = new System.Drawing.Point(9, 214);
  1277.             this.buttonFitScreen.Name = "buttonFitScreen";
  1278.             this.buttonFitScreen.Size = new System.Drawing.Size(99, 27);
  1279.             this.buttonFitScreen.TabIndex = 6;
  1280.             this.buttonFitScreen.Text = "&Fit to screen";
  1281.             this.buttonFitScreen.Click += new System.EventHandler(this.buttonFitScreen_Click);
  1282.             // 
  1283.             // buttonClose
  1284.             //
  1285.             this.buttonClose.Location = new System.Drawing.Point(186, 214);
  1286.             this.buttonClose.Name = "buttonClose";
  1287.             this.buttonClose.Size = new System.Drawing.Size(99, 27);
  1288.             this.buttonClose.TabIndex = 7;
  1289.             this.buttonClose.Text = "&Close";
  1290.             this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
  1291.             //
  1292.             // ImageOverlayDialog
  1293.             //
  1294.             this.AcceptButton = this.buttonClose;
  1295.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  1296.             this.ClientSize = new System.Drawing.Size(292, 249);
  1297.             this.Controls.Add(this.buttonClose);
  1298.             this.Controls.Add(this.buttonFitScreen);
  1299.             this.Controls.Add(this.label4);
  1300.             this.Controls.Add(this.label1);
  1301.             this.Controls.Add(this.buttonBrowse);
  1302.             this.Controls.Add(this.name);
  1303.             this.Controls.Add(this.url);
  1304.             this.Controls.Add(this.groupBox1);
  1305.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  1306.             this.Name = "ImageOverlayDialog";
  1307.             this.Text = "Image Overlay Properties";
  1308.             ((System.ComponentModel.ISupportInitialize)(this.opacity)).EndInit();
  1309.             this.groupBox1.ResumeLayout(false);
  1310.             this.ResumeLayout(false);
  1311.  
  1312.         }
  1313.         #endregion
  1314.  
  1315.         private void opacity_Scroll(object sender, System.EventArgs e)
  1316.         {
  1317.             if(m_selectedOverlay == null)
  1318.                 return;
  1319.             m_selectedOverlay.UpdateOpacity(opacity.Value / 100f);
  1320.  
  1321.         }
  1322.  
  1323.         private void buttonBrowse_Click(object sender, System.EventArgs e)
  1324.         {
  1325.             float minLat;
  1326.             float minLon;
  1327.             float maxLat;
  1328.             float maxLon;
  1329.             Image MyImage;
  1330.             bool goodImage=true;
  1331.  
  1332.  
  1333.             if(m_selectedOverlay == null)
  1334.                 return;
  1335.             OpenFileDialog openFileDialog = new OpenFileDialog();
  1336.             if(!url.Text.ToLower().StartsWith("http://"))
  1337.                 openFileDialog.FileName = url.Text;
  1338.             openFileDialog.RestoreDirectory = true;
  1339.             if(openFileDialog.ShowDialog()==DialogResult.OK)
  1340.             {
  1341.  
  1342.                 url.Text = openFileDialog.FileName;
  1343.                 //Access as an image, to get the height and width dimensions
  1344.                 MyImage=new Bitmap(url.Text);
  1345.  
  1346.                 //Get the file name minus the extension
  1347.                 String fileNameNoExt = url.Text.Remove(url.Text.Length-4,4);
  1348.  
  1349.                 //Parse out the name, so people don't need to change the name everytime.
  1350.                 string baseName="";
  1351.                 int lastBackSlash = fileNameNoExt.LastIndexOf("\\");
  1352.                 if (lastBackSlash!=-1)
  1353.                 {
  1354.                     //int dotIndex = m_selectedOverlay.Url.LastIndexOf(".");
  1355.                     int fileNameLength = fileNameNoExt.Length-lastBackSlash;
  1356.                     baseName=fileNameNoExt.Substring(lastBackSlash+1,fileNameLength-1);
  1357.                     //MessageBox.Show(baseName);
  1358.                 }
  1359.                 else
  1360.                 {
  1361.                     int lastForwardSlash = fileNameNoExt.LastIndexOf("//");
  1362.                     int fileNameLength = fileNameNoExt.Length-lastForwardSlash;
  1363.                     baseName=fileNameNoExt.Substring(lastForwardSlash+1,fileNameLength-1);
  1364.  
  1365.                 }
  1366.  
  1367.                 this.name.Text=baseName;
  1368.  
  1369.                 String worldFileExt="";
  1370.                 //If it is a jpg
  1371.                 if (url.Text.EndsWith(".jpg"))
  1372.                 {
  1373.                     worldFileExt = ".jgw";
  1374.                 }
  1375.                 else if (url.Text.EndsWith(".png"))
  1376.                 {
  1377.                     worldFileExt = ".pgw";
  1378.                 }
  1379.                 else if (url.Text.EndsWith(".gif"))
  1380.                 {
  1381.                      worldFileExt = ".gfw";
  1382.                 }
  1383.                 else if (url.Text.EndsWith(".tif"))
  1384.                 {
  1385.                      worldFileExt = ".tfw";
  1386.                 }
  1387.                 else
  1388.                 {
  1389.                     goodImage=false;
  1390.                 }
  1391.                 if (goodImage!=false)
  1392.                 {
  1393.                     //Check to see if a world file exists
  1394.                     if (File.Exists(fileNameNoExt+worldFileExt))
  1395.                     {
  1396.                         //Read the world file, get the pixel size and upper left coordinate, calculated the lower right coordinate
  1397.                         System.IO.StreamReader worldFile = new System.IO.StreamReader(fileNameNoExt+worldFileExt);
  1398.                         String pixelSize = worldFile.ReadLine();
  1399.                         worldFile.ReadLine();//Skip
  1400.                         worldFile.ReadLine();//These
  1401.                         worldFile.ReadLine();//Lines
  1402.                         minLon=float.Parse(worldFile.ReadLine());
  1403.                         maxLat=float.Parse(worldFile.ReadLine());
  1404.                         maxLon=minLon + ((float.Parse(pixelSize)) * (MyImage.Width));
  1405.                         minLat=maxLat - ((float.Parse(pixelSize)) * (MyImage.Height));
  1406.                         //MessageBox.Show(maxLon.ToString());
  1407.                         //MessageBox.Show(minLat.ToString());
  1408.                         m_selectedOverlay.MinLon=minLon;
  1409.                         m_selectedOverlay.MaxLat=maxLat;
  1410.                         m_selectedOverlay.MaxLon=maxLon;
  1411.                         m_selectedOverlay.MinLat=minLat;
  1412.                     }
  1413.                 }
  1414.                 else
  1415.                 {
  1416.                     MessageBox.Show("This plugin only accepts png/jpg/gif/tif");
  1417.                 }
  1418.             }
  1419.             
  1420.             m_selectedOverlay.UpdateTexture(url.Text);
  1421.             m_selectedOverlay.Url = url.Text;
  1422.         }
  1423.  
  1424.         private void url_Change(object sender, System.EventArgs e)
  1425.         {
  1426.             /*string baseName="";
  1427.             int lastBackSlash = m_selectedOverlay.Url.LastIndexOf("\\");
  1428.             //MessageBox.Show(lastBackSlash.ToString());
  1429.             if (lastBackSlash!=-1) 
  1430.             {
  1431.                 //int dotIndex = m_selectedOverlay.Url.LastIndexOf(".");
  1432.                 int fileNameLength = m_selectedOverlay.Url.Length-lastBackSlash;
  1433.  
  1434.                 baseName=m_selectedOverlay.Url.Substring(lastBackSlash+1,fileNameLength-5);
  1435.                 //MessageBox.Show(baseName);
  1436.             } 
  1437.             else 
  1438.             {
  1439.                 int lastForwardSlash = m_selectedOverlay.Url.LastIndexOf("//");
  1440.                 int fileNameLength = m_selectedOverlay.Url.Length-lastForwardSlash;
  1441.                 baseName=m_selectedOverlay.Url.Substring(lastForwardSlash+1,fileNameLength-5);
  1442.  
  1443.             }
  1444.  
  1445.             this.name.Text = baseName;
  1446. */
  1447.         }
  1448.  
  1449.         private void url_Leave(object sender, System.EventArgs e)
  1450.         {
  1451.             //MessageBox.Show(url.Text);
  1452.             if(m_selectedOverlay == null)
  1453.                 return;
  1454.             if(m_selectedOverlay.Url == url.Text)
  1455.                 return;
  1456.  
  1457.             float minLat;
  1458.             float minLon;
  1459.             float maxLat;
  1460.             float maxLon;
  1461.             Image MyImage;
  1462.             bool goodImage=true;
  1463.             string bitmapPath="";
  1464.             //Access as an image, to get the height and width dimensions
  1465.             if (url.Text.ToLower().StartsWith("http://"))
  1466.             {
  1467.                 bitmapPath=url.Text.Substring(5);
  1468.  
  1469.             } 
  1470.             else 
  1471.             {
  1472.                 bitmapPath=url.Text;
  1473.             }
  1474.             //MessageBox.Show(bitmapPath);
  1475.             MyImage=new Bitmap(bitmapPath);
  1476.             
  1477.             //Get the file name minus the extension
  1478.             String fileNameNoExt = bitmapPath.Remove(bitmapPath.Length-4,4);
  1479.             String worldFileExt="";
  1480.             //If it is a jpg
  1481.             if (bitmapPath.EndsWith(".jpg"))
  1482.             {
  1483.                 worldFileExt = ".jgw";
  1484.             } 
  1485.             else if (bitmapPath.EndsWith(".png"))
  1486.             {
  1487.                 worldFileExt = ".pgw";
  1488.             }
  1489.             else if (bitmapPath.EndsWith(".gif"))
  1490.             {
  1491.                 worldFileExt = ".gfw";
  1492.             }
  1493.             else if (bitmapPath.EndsWith(".tif"))
  1494.             {
  1495.                 worldFileExt = ".tfw";
  1496.             } 
  1497.             else 
  1498.             {
  1499.                 goodImage=false;
  1500.             }
  1501.             if (goodImage!=false) 
  1502.             {
  1503.                 //Check to see if a world file exists
  1504.                 if (File.Exists(fileNameNoExt+worldFileExt))
  1505.                 {
  1506.                     //Read the world file, get the pixel size and upper left coordinate, calculated the lower right coordinate
  1507.                     System.IO.StreamReader worldFile = new System.IO.StreamReader(fileNameNoExt+worldFileExt);
  1508.                     String pixelSize = worldFile.ReadLine();
  1509.                     worldFile.ReadLine();//Skip
  1510.                     worldFile.ReadLine();//These
  1511.                     worldFile.ReadLine();//Lines
  1512.                     minLon=float.Parse(worldFile.ReadLine());
  1513.                     maxLat=float.Parse(worldFile.ReadLine());
  1514.                     maxLon=minLon + ((float.Parse(pixelSize)) * (MyImage.Width));
  1515.                     minLat=maxLat - ((float.Parse(pixelSize)) * (MyImage.Height));
  1516.                     //MessageBox.Show(maxLon.ToString());
  1517.                     //MessageBox.Show(minLat.ToString());
  1518.                     m_selectedOverlay.MinLon=minLon;
  1519.                     m_selectedOverlay.MaxLat=maxLat;
  1520.                     m_selectedOverlay.MaxLon=maxLon;
  1521.                     m_selectedOverlay.MinLat=minLat;
  1522.                 }
  1523.             } 
  1524.             else 
  1525.             {
  1526.                 MessageBox.Show("This plugin only accepts png/jpg/gif/tif");
  1527.             }
  1528.  
  1529.             m_selectedOverlay.Url = bitmapPath;
  1530.         }
  1531.  
  1532.         private void name_TextChanged(object sender, System.EventArgs e)
  1533.         {
  1534.             if(m_selectedOverlay == null)
  1535.                 return;
  1536.             m_selectedOverlay.Name = name.Text;
  1537.         }
  1538.  
  1539.         private void buttonFitScreen_Click(object sender, System.EventArgs e)
  1540.         {
  1541.             if(m_selectedOverlay == null)
  1542.                 return;
  1543.             m_selectedOverlay.FitToScreen = true;
  1544.         }
  1545.  
  1546.         private void buttonClose_Click(object sender, System.EventArgs e)
  1547.         {
  1548.             Close();
  1549.         }
  1550.     }
  1551.  
  1552.     /// <summary>
  1553.     ///    THE PROGRESS BAR
  1554.     /// </summary>
  1555.     public class Win32Form2 : System.Windows.Forms.Form
  1556.     {
  1557.         /// <summary>
  1558.         ///    Required by the Win Forms designer
  1559.         /// </summary>
  1560.         private System.ComponentModel.Container components;
  1561.         public  System.Windows.Forms.ProgressBar progressBar1;
  1562.  
  1563.         public Win32Form2() 
  1564.         {
  1565.  
  1566.             // Required for Win Form Designer support
  1567.             InitializeComponent();
  1568.  
  1569.             // TODO: Add any constructor code after InitializeComponent call
  1570.  
  1571.         }
  1572.  
  1573.         /// <summary>
  1574.         ///    Clean up any resources being used
  1575.         /// </summary>
  1576.         protected override void Dispose( bool disposing )
  1577.         {
  1578.             if( disposing )
  1579.             {
  1580.                 if (components != null)
  1581.                 {
  1582.                     components.Dispose();
  1583.                 }
  1584.             }
  1585.             base.Dispose( disposing );
  1586.         }
  1587.  
  1588.         /// <summary>
  1589.         ///    Required method for Designer support - do not modify
  1590.         ///    the contents of this method with an editor
  1591.         /// </summary>
  1592.         private void InitializeComponent()
  1593.         {
  1594.             this.progressBar1 = new System.Windows.Forms.ProgressBar();
  1595.             this.SuspendLayout();
  1596.             // 
  1597.             // progressBar1
  1598.             // 
  1599.             this.progressBar1.Location = new System.Drawing.Point(72, 24);
  1600.             this.progressBar1.Maximum = 10;
  1601.             this.progressBar1.Name = "progressBar1";
  1602.             this.progressBar1.Size = new System.Drawing.Size(312, 40);
  1603.             this.progressBar1.Step = 1;
  1604.             this.progressBar1.TabIndex = 0;
  1605.             // 
  1606.             // Win32Form2
  1607.             // 
  1608.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  1609.             this.ClientSize = new System.Drawing.Size(456, 85);
  1610.             this.Controls.Add(this.progressBar1);
  1611.             this.Name = "Win32Form2";
  1612.             this.Text = "Loading...";
  1613.             this.Click += new System.EventHandler(this.Win32Form2_Click);
  1614.             this.ResumeLayout(false);
  1615.  
  1616.         }
  1617.  
  1618.         protected void Win32Form2_Click(object sender, System.EventArgs e)
  1619.         {
  1620.  
  1621.         }
  1622.  
  1623.  
  1624.     }
  1625. }
  1626.