From 8a52b8fb527dc926a478a08021f342831e17d38b Mon Sep 17 00:00:00 2001 From: Latif Khalifa Date: Wed, 17 Mar 2010 12:30:30 +0000 Subject: [PATCH] Converted TerrainManager to the new event model. git-svn-id: http://libopenmetaverse.googlecode.com/svn/libopenmetaverse/trunk@3285 52acb1d6-8a22-11de-b505-999d5b087335 --- OpenMetaverse/TerrainManager.cs | 83 +++++++++++++++------ Programs/PrimWorkshop/frmBrowser.cs | 16 ++-- Programs/examples/Heightmap/frmHeightmap.cs | 28 +++---- 3 files changed, 84 insertions(+), 43 deletions(-) diff --git a/OpenMetaverse/TerrainManager.cs b/OpenMetaverse/TerrainManager.cs index 9809148a..adfb33b0 100644 --- a/OpenMetaverse/TerrainManager.cs +++ b/OpenMetaverse/TerrainManager.cs @@ -32,21 +32,33 @@ namespace OpenMetaverse { public class TerrainManager { - /// - /// - /// - /// - /// - /// - /// - /// - public delegate void LandPatchCallback(Simulator simulator, int x, int y, int width, float[] data); - - /// - public event LandPatchCallback OnLandPatch; + #region EventHandling + /// The event subscribers. null if no subcribers + private EventHandler m_LandPatchReceivedEvent; + + /// Raises the LandPatchReceived event + /// A LandPatchReceivedEventArgs object containing the + /// data returned from the simulator + protected virtual void OnLandPatchReceived(LandPatchReceivedEventArgs e) + { + EventHandler handler = m_LandPatchReceivedEvent; + if (handler != null) + handler(this, e); + } + + /// Thread sync lock object + private readonly object m_LandPatchReceivedLock = new object(); + + /// Raised when the simulator responds sends + public event EventHandler LandPatchReceived + { + add { lock (m_LandPatchReceivedLock) { m_LandPatchReceivedEvent += value; } } + remove { lock (m_LandPatchReceivedLock) { m_LandPatchReceivedEvent -= value; } } + } + #endregion public InternalDictionary SimPatches = new InternalDictionary(); - public InternalDictionary WindSpeeds = new InternalDictionary(); + public InternalDictionary WindSpeeds = new InternalDictionary(); private GridClient Client; @@ -132,11 +144,8 @@ namespace OpenMetaverse count++; - if (OnLandPatch != null) - { - try { OnLandPatch(simulator, x, y, group.PatchSize, heightmap); } - catch (Exception e) { Logger.Log(e.Message, Helpers.LogLevel.Error, Client, e); } - } + try { OnLandPatchReceived(new LandPatchReceivedEventArgs(simulator, x, y, group.PatchSize, heightmap)); } + catch (Exception e) { Logger.Log(e.Message, Helpers.LogLevel.Error, Client, e); } if (Client.Settings.STORE_LAND_PATCHES) { @@ -183,9 +192,9 @@ namespace OpenMetaverse Vector2[] windSpeeds; lock (WindSpeeds.Dictionary) { - if (!WindSpeeds.TryGetValue(handle,out windSpeeds)) + if (!WindSpeeds.TryGetValue(handle, out windSpeeds)) { - windSpeeds = WindSpeeds[handle] = new Vector2[256]; + windSpeeds = WindSpeeds[handle] = new Vector2[256]; } } for (int i = 0; i < 256; i++) @@ -214,7 +223,7 @@ namespace OpenMetaverse switch (type) { case TerrainPatch.LayerType.Land: - if (OnLandPatch != null || Client.Settings.STORE_LAND_PATCHES) + if (m_LandPatchReceivedEvent != null || Client.Settings.STORE_LAND_PATCHES) DecompressLand(e.Simulator, bitpack, header); break; case TerrainPatch.LayerType.Water: @@ -232,4 +241,36 @@ namespace OpenMetaverse } } } + + #region EventArgs classes + // Provides data for LandPatchReceived + public class LandPatchReceivedEventArgs : EventArgs + { + private readonly Simulator m_Simulator; + private readonly int m_X; + private readonly int m_Y; + private readonly int m_PatchSize; + private readonly float[] m_HeightMap; + + /// Simulator from that sent tha data + public Simulator Simulator { get { return m_Simulator; } } + /// Sim coordinate of the patch + public int X { get { return m_X; } } + /// Sim coordinate of the patch + public int Y { get { return m_Y; } } + /// Size of tha patch + public int PatchSize { get { return m_PatchSize; } } + /// Heightmap for the patch + public float[] HeightMap { get { return m_HeightMap; } } + + public LandPatchReceivedEventArgs(Simulator simulator, int x, int y, int patchSize, float[] heightMap) + { + this.m_Simulator = simulator; + this.m_X = x; + this.m_Y = y; + this.m_PatchSize = patchSize; + this.m_HeightMap = heightMap; + } + } + #endregion } diff --git a/Programs/PrimWorkshop/frmBrowser.cs b/Programs/PrimWorkshop/frmBrowser.cs index d5f88beb..63022938 100644 --- a/Programs/PrimWorkshop/frmBrowser.cs +++ b/Programs/PrimWorkshop/frmBrowser.cs @@ -162,7 +162,7 @@ namespace PrimWorkshop Client.Network.SimChanged += Network_OnCurrentSimChanged; Client.Network.EventQueueRunning += Network_OnEventQueueRunning; Client.Objects.ObjectUpdate += Objects_OnNewPrim; - Client.Terrain.OnLandPatch += new TerrainManager.LandPatchCallback(Terrain_OnLandPatch); + Client.Terrain.LandPatchReceived += new EventHandler(Terrain_LandPatchReceived); Client.Parcels.SimParcelsDownloaded += new EventHandler(Parcels_SimParcelsDownloaded); Client.Assets.OnImageRecieveProgress += new AssetManager.ImageReceiveProgressCallback(Assets_OnImageRecieveProgress); @@ -1010,19 +1010,19 @@ namespace PrimWorkshop lock (RenderPrimList) RenderPrimList[prim.LocalID] = render; } - - private void Terrain_OnLandPatch(Simulator simulator, int x, int y, int width, float[] data) + + private void Terrain_LandPatchReceived(object sender, LandPatchReceivedEventArgs e) { - if (Client != null && Client.Network.CurrentSim == simulator) + if (Client != null && Client.Network.CurrentSim == e.Simulator) { - Heightmap[y, x].Data = data; + Heightmap[e.Y, e.X].Data = e.HeightMap; } // Find the new max height - for (int i = 0; i < data.Length; i++) + for (int i = 0; i < e.HeightMap.Length; i++) { - if (data[i] > MaxHeight) - MaxHeight = data[i]; + if (e.HeightMap[i] > MaxHeight) + MaxHeight = e.HeightMap[i]; } } diff --git a/Programs/examples/Heightmap/frmHeightmap.cs b/Programs/examples/Heightmap/frmHeightmap.cs index af19b7d5..2bd731e4 100644 --- a/Programs/examples/Heightmap/frmHeightmap.cs +++ b/Programs/examples/Heightmap/frmHeightmap.cs @@ -78,7 +78,7 @@ namespace Heightmap private void frmHeightmap_Load(object sender, EventArgs e) { - Client.Terrain.OnLandPatch += new TerrainManager.LandPatchCallback(Terrain_OnLandPatch); + Client.Terrain.LandPatchReceived += new EventHandler(Terrain_LandPatchReceived); // Only needed so we can do lookups with TerrainHeightAtPoint Client.Settings.STORE_LAND_PATCHES = true; @@ -125,17 +125,17 @@ namespace Heightmap Client.Self.Movement.UpdateFromHeading(heading, false); } - void Terrain_OnLandPatch(Simulator simulator, int x, int y, int width, float[] data) + void Terrain_LandPatchReceived(object sender, LandPatchReceivedEventArgs e) { - if (x >= 16 || y >= 16) + if (e.X >= 16 || e.Y >= 16) { - Console.WriteLine("Bad patch coordinates, x = " + x + ", y = " + y); + Console.WriteLine("Bad patch coordinates, x = " + e.X + ", y = " + e.Y); return; } - if (width != 16) + if (e.PatchSize != 16) { - Console.WriteLine("Unhandled patch size " + width + "x" + width); + Console.WriteLine("Unhandled patch size " + e.PatchSize + "x" + e.PatchSize); return; } @@ -145,20 +145,20 @@ namespace Heightmap { for (int xp = 0; xp < 16; xp++) { - float height = data[(15-yp) * 16 + xp]; // data[0] is south west + float height = e.HeightMap[(15-yp) * 16 + xp]; // data[0] is south west Color color; - if (height >= simulator.WaterHeight) + if (height >= e.Simulator.WaterHeight) { - float maxVal = (float)Math.Log(Math.Abs(512+1-simulator.WaterHeight),2); - float lgHeight = (float)Math.Log(Math.Abs(height + 1 - simulator.WaterHeight), 2); - int colorVal1 = Utils.FloatToByte(lgHeight, simulator.WaterHeight, maxVal); - int colorVal2 = Utils.FloatToByte(height, simulator.WaterHeight, 25.0f); + float maxVal = (float)Math.Log(Math.Abs(512+1-e.Simulator.WaterHeight),2); + float lgHeight = (float)Math.Log(Math.Abs(height + 1 - e.Simulator.WaterHeight), 2); + int colorVal1 = Utils.FloatToByte(lgHeight, e.Simulator.WaterHeight, maxVal); + int colorVal2 = Utils.FloatToByte(height, e.Simulator.WaterHeight, 25.0f); color = Color.FromArgb(255, colorVal2, colorVal1); } else { const float minVal = -5.0f; - float maxVal = simulator.WaterHeight; + float maxVal = e.Simulator.WaterHeight; int colorVal1 = Utils.FloatToByte(height, -5.0f, minVal + (maxVal - minVal) * 1.5f); int colorVal2 = Utils.FloatToByte(height, -5.0f, maxVal); color = Color.FromArgb(colorVal1, colorVal2, 255); @@ -167,7 +167,7 @@ namespace Heightmap } } - Boxes[x, 15-y].Image = (System.Drawing.Image)patch; + Boxes[e.X, 15-e.Y].Image = (System.Drawing.Image)patch; } private void frmHeightmap_FormClosing(object sender, FormClosingEventArgs e)