diff --git a/OpenMetaverse.GUI/AvatarList.cs b/OpenMetaverse.GUI/AvatarList.cs new file mode 100644 index 00000000..df777f9b --- /dev/null +++ b/OpenMetaverse.GUI/AvatarList.cs @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2007-2008, openmetaverse.org + * All rights reserved. + * + * - Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * - Neither the name of the openmetaverse.org nor the names + * of its contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenMetaverse; +using System; +using System.Collections.Generic; +using System.Windows.Forms; + +namespace OpenMetaverse.GUI +{ + public class AvatarList : ListView + { + private GridClient _Client; + private List _Avatars = new List(); + + public delegate void AvatarDoubleClickCallback(Avatar avatar); + public event AvatarDoubleClickCallback OnAvatarDoubleClick; + + /// + /// Gets or sets the GridClient associated with this control + /// + public GridClient Client + { + get { return _Client; } + set { if (value != null) InitializeClient(value); } + } + + /// + /// TreeView control for an unspecified client's nearby avatar list + /// + public AvatarList() + { + ColumnHeader header1 = this.Columns.Add("Name"); + header1.Width = this.Width - 20; + + ColumnHeader header2 = this.Columns.Add(" "); + header2.Width = 40; + + this.View = View.Details; + this.DoubleClick += new EventHandler(AvatarList_DoubleClick); + } + + /// + /// TreeView control for the specified client's nearby avatar list + /// + /// + public AvatarList(GridClient client) + { + InitializeClient(client); + new InventoryTree(); + } + + /// + /// Thread-safe method for clearing the TreeView control + /// + public void ClearNodes() + { + if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { ClearNodes(); }); + else this.Items.Clear(); + } + + private void UpdateAvatar(Avatar avatar) + { + if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { UpdateAvatar(avatar); }); + else + { + lock (_Avatars) + { + ListViewItem item; + if (_Avatars.Contains(avatar.LocalID)) + { + item = this.Items[avatar.LocalID.ToString()]; + item.SubItems[0].Text = (int)Vector3.Dist(_Client.Self.SimPosition, avatar.Position) + "m"; + } + else + { + _Avatars.Add(avatar.LocalID); + string key = avatar.LocalID.ToString(); + item = this.Items.Add(key, avatar.Name, null); + item.SubItems.Add((int)Vector3.Dist(_Client.Self.SimPosition, avatar.Position) + "m"); + } + item.Tag = avatar; + } + } + } + + private void RemoveAvatar(uint localID) + { + if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { RemoveAvatar(localID); }); + else + { + lock (_Avatars) + { + if (_Avatars.Contains(localID)) + { + _Avatars.Remove(localID); + string key = localID.ToString(); + int index = this.Items.IndexOfKey(key); + if (index > -1) this.Items.RemoveAt(index); + } + } + } + } + + private void InitializeClient(GridClient client) + { + _Client = client; + _Client.Network.OnCurrentSimChanged += new NetworkManager.CurrentSimChangedCallback(Network_OnCurrentSimChanged); + _Client.Objects.OnNewAvatar += new ObjectManager.NewAvatarCallback(Objects_OnNewAvatar); + _Client.Objects.OnObjectKilled += new ObjectManager.KillObjectCallback(Objects_OnObjectKilled); + } + + private void AvatarList_DoubleClick(object sender, EventArgs e) + { + ListView list = (ListView)sender; + if (list.SelectedItems.Count > 0) + { + if (list.SelectedItems[0].Tag is Avatar) + { + Avatar av = (Avatar)list.SelectedItems[0].Tag; + if (OnAvatarDoubleClick != null) + { + try { OnAvatarDoubleClick(av); } + catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, Client, ex); } + } + } + } + } + + private void Network_OnCurrentSimChanged(Simulator PreviousSimulator) + { + lock (_Avatars) _Avatars.Clear(); + ClearNodes(); + } + + private void Objects_OnNewAvatar(Simulator simulator, Avatar avatar, ulong regionHandle, ushort timeDilation) + { + lock (_Avatars) + { + if (!_Avatars.Contains(avatar.LocalID)) UpdateAvatar(avatar); + } + } + + private void Objects_OnObjectKilled(Simulator simulator, uint objectID) + { + lock (_Avatars) + { + if (_Avatars.Contains(objectID)) RemoveAvatar(objectID); + } + } + } + +} diff --git a/OpenMetaverse.GUI/InventoryTree.cs b/OpenMetaverse.GUI/InventoryTree.cs new file mode 100644 index 00000000..4b8c3437 --- /dev/null +++ b/OpenMetaverse.GUI/InventoryTree.cs @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2007-2008, openmetaverse.org + * All rights reserved. + * + * - Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * - Neither the name of the openmetaverse.org nor the names + * of its contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenMetaverse; +using System.Collections.Generic; +using System.Windows.Forms; + +namespace OpenMetaverse.GUI +{ + + /// + /// TreeView control for browsing a client's inventory + /// + public class InventoryTree : TreeView + { + private GridClient _Client; + + /// + /// Gets or sets the GridClient associated with this control + /// + public GridClient Client + { + get { return _Client; } + set { if (value != null) InitializeClient(value); } + } + + /// + /// TreeView control for an unspecified client's inventory + /// + public InventoryTree() + { + this.BeforeExpand += new TreeViewCancelEventHandler(this_BeforeExpand); + } + + /// + /// TreeView control for the specified client's inventory + /// + /// + public InventoryTree(GridClient client) + { + InitializeClient(client); + new InventoryTree(); + } + + /// + /// Thread-safe method for clearing the TreeView control + /// + public void ClearNodes() + { + if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { ClearNodes(); }); + else this.Nodes.Clear(); + } + + /// + /// Thread-safe method for collapsing a TreeNode in the control + /// + /// + public void CollapseNode(TreeNode node) + { + if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { CollapseNode(node); }); + else if (!node.IsExpanded) node.Collapse(); + } + + /// + /// Thread-safe method for expanding a TreeNode in the control + /// + /// + public void ExpandNode(TreeNode node) + { + if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { ExpandNode(node); }); + else if (!node.IsExpanded) node.Expand(); + } + + /// + /// Thread-safe method for updating the contents of the specified folder UUID + /// + /// + public void UpdateFolder(UUID folderID) + { + if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { UpdateFolder(folderID); }); + else + { + TreeNode node = null; + TreeNodeCollection children; + + if (folderID != Client.Inventory.Store.RootFolder.UUID) + { + TreeNode[] found = Nodes.Find(folderID.ToString(), true); + if (found.Length > 0) + { + node = found[0]; + children = node.Nodes; + } + else + { + Logger.Log("Received update for unknown TreeView node " + folderID, Helpers.LogLevel.Warning); + return; + } + } + else children = this.Nodes; + + children.Clear(); + List contents = Client.Inventory.Store.GetContents(folderID); + foreach (InventoryBase inv in contents) + { + string key = inv.UUID.ToString(); + children.Add(key, inv.Name); + children[key].Tag = inv; + if (inv is InventoryFolder) children[key].Nodes.Add(null, "Loading..."); + } + + if (node != null) ExpandNode(node); + + } + } + + private void InitializeClient(GridClient client) + { + _Client = client; + _Client.Inventory.OnFolderUpdated += new InventoryManager.FolderUpdatedCallback(Inventory_OnFolderUpdated); + _Client.Network.OnLogin += new NetworkManager.LoginCallback(Network_OnLogin); + } + + private void this_BeforeExpand(object sender, TreeViewCancelEventArgs e) + { + UUID folderID; + if (e.Node.Name != null && UUID.TryParse(e.Node.Name, out folderID)) + _Client.Inventory.RequestFolderContents(folderID, _Client.Self.AgentID, true, true, InventorySortOrder.FoldersByName); + } + + private void Inventory_OnFolderUpdated(UUID folderID) + { + UpdateFolder(folderID); + } + + private void Network_OnLogin(LoginStatus login, string message) + { + if (login == LoginStatus.Success) Client.Inventory.RequestFolderContents(Client.Inventory.Store.RootFolder.UUID, Client.Self.AgentID, true, true, InventorySortOrder.FoldersByName); + } + } + +} + diff --git a/OpenMetaverse.GUI/OpenMetaverse.GUI.csproj b/OpenMetaverse.GUI/OpenMetaverse.GUI.csproj new file mode 100644 index 00000000..359b0004 --- /dev/null +++ b/OpenMetaverse.GUI/OpenMetaverse.GUI.csproj @@ -0,0 +1,59 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {317DBED9-3457-4597-A65A-8B4575D87934} + Library + Properties + OpenMetaverse.GUI + OpenMetaverse.GUI + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + Component + + + Component + + + + + + {02F19508-E831-45BB-965E-A37E1E1C973C} + OpenMetaverse + + + + + \ No newline at end of file