OSDN Git Service

08ab6d10b25b6afbcd72bc4bcc7cdf4b6a4cfe0d
[radegast/radegast.git] / Radegast / GUI / Consoles / AttachmentDetail.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2013, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
9 //     * Redistributions of source code must retain the above copyright notice,
10 //       this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above copyright
12 //       notice, this list of conditions and the following disclaimer in the
13 //       documentation and/or other materials provided with the distribution.
14 //     * Neither the name of the application "Radegast", nor the names of its
15 //       contributors may be used to endorse or promote products derived from
16 //       this software without specific prior written permission.
17 // 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Collections.Generic;
33 using System.Windows.Forms;
34 #if (COGBOT_LIBOMV || USE_STHREADS)
35 using ThreadPoolUtil;
36 using Thread = ThreadPoolUtil.Thread;
37 using ThreadPool = ThreadPoolUtil.ThreadPool;
38 using Monitor = ThreadPoolUtil.Monitor;
39 #endif
40 using System.Threading;
41
42 using OpenMetaverse;
43
44 namespace Radegast
45 {
46     public partial class AttachmentDetail : UserControl
47     {
48         private RadegastInstance instance;
49         private GridClient client { get { return instance.Client; } }
50         private Avatar av;
51         private Primitive attachment;
52
53         public AttachmentDetail(RadegastInstance instance, Avatar av, Primitive attachment)
54         {
55             InitializeComponent();
56             Disposed += new EventHandler(AttachmentDetail_Disposed);
57
58             this.instance = instance;
59             this.av = av;
60             this.attachment = attachment;
61
62             if (!instance.advancedDebugging)
63             {
64                 btnSave.Visible = false;
65                 boxID.Visible = false;
66                 lblAttachment.Visible = false;
67             }
68
69             // Callbacks
70             client.Objects.ObjectProperties += new EventHandler<ObjectPropertiesEventArgs>(Objects_ObjectProperties);
71         }
72
73         void AttachmentDetail_Disposed(object sender, EventArgs e)
74         {
75             client.Objects.ObjectProperties -= new EventHandler<ObjectPropertiesEventArgs>(Objects_ObjectProperties);
76         }
77
78         private void AttachmentDetail_Load(object sender, EventArgs e)
79         {
80             boxID.Text = attachment.ID.ToString();
81
82             if (attachment.Properties == null)
83             {
84                 client.Objects.SelectObject(client.Network.CurrentSim, attachment.LocalID);
85             }
86             else
87             {
88                 UpdateControls();
89             }
90         }
91
92         private void UpdateControls()
93         {
94             if (InvokeRequired)
95             {
96                 Invoke(new MethodInvoker(UpdateControls));
97                 return;
98             }
99
100             lblAttachmentPoint.Text = attachment.PrimData.AttachmentPoint.ToString();
101             boxName.Text = attachment.Properties.Name;
102
103             if ((attachment.Flags & PrimFlags.Touch) == 0)
104             {
105                 btnTouch.Visible = false;
106             }
107
108             List<Primitive> parts = client.Network.CurrentSim.ObjectsPrimitives.FindAll(
109                 delegate(Primitive prim)
110                 {
111                     return (prim.LocalID == attachment.LocalID || prim.ParentID == attachment.LocalID);
112                 }
113             );
114
115             lblPrimCount.Text = "Prims: " + parts.Count.ToString();
116         }
117
118         void Objects_ObjectProperties(object sender, ObjectPropertiesEventArgs e)
119         {
120             if (e.Properties.ObjectID == attachment.ID)
121             {
122                 attachment.Properties = e.Properties;
123                 UpdateControls();
124             }
125         }
126
127         private void btnTouch_Click(object sender, EventArgs e)
128         {
129             client.Self.Touch(attachment.LocalID);
130         }
131
132         private void btnSave_Click(object sender, EventArgs e)
133         {
134             WindowWrapper mainWindow = new WindowWrapper(frmMain.ActiveForm.Handle);
135             System.Windows.Forms.SaveFileDialog dlg = new SaveFileDialog();
136             dlg.AddExtension = true;
137             dlg.RestoreDirectory = true;
138             dlg.Title = "Save object as...";
139             dlg.Filter = "XML file (*.xml)|*.xml";
140             DialogResult res = dlg.ShowDialog();
141
142             if (res == DialogResult.OK)
143             {
144                 Thread t = new Thread(new ThreadStart(delegate()
145                     {
146                         try
147                         {
148                             PrimSerializer s = new PrimSerializer(client);
149                             string primsXmls = s.GetSerializedAttachmentPrims(client.Network.CurrentSim, attachment.LocalID);
150                             System.IO.File.WriteAllText(dlg.FileName, primsXmls);
151                             s.CleanUp();
152                             s = null;
153                             MessageBox.Show(mainWindow, "Successfully saved " + dlg.FileName, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
154                         }
155                         catch (Exception excp)
156                         {
157                             MessageBox.Show(mainWindow, excp.Message, "Saving failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
158                         }
159                     }
160                 ));
161                 t.IsBackground = true;
162                 t.Start();
163             }
164
165         }
166
167         private void button1_Click(object sender, EventArgs e)
168         {
169             Rendering.frmPrimWorkshop pw = new Rendering.frmPrimWorkshop(instance, attachment.LocalID);
170             pw.Show();
171             pw.SetView(new Vector3(0f, 0.5f, 0f), 0, 0, 90, -10);
172         }
173     }
174 }