OSDN Git Service

Update copyright year
[radegast/radegast.git] / Radegast / GUI / Consoles / MasterTab.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, 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.Drawing;
34 using System.Windows.Forms;
35 using OpenMetaverse.StructuredData;
36 #if (COGBOT_LIBOMV || USE_STHREADS)
37 using ThreadPoolUtil;
38 using Thread = ThreadPoolUtil.Thread;
39 using ThreadPool = ThreadPoolUtil.ThreadPool;
40 using Monitor = ThreadPoolUtil.Monitor;
41 #endif
42 using System.Threading;
43
44 using Radegast.Netcom;
45 using OpenMetaverse;
46
47 namespace Radegast
48 {
49     public partial class MasterTab : UserControl
50     {
51         private RadegastInstance instance;
52         private GridClient client { get { return instance.Client; } }
53         private RadegastNetcom netcom { get { return instance.Netcom; } }
54         private Avatar avatar;
55         public UUID selectedID;
56         public Primitive selectedPrim;
57
58         public MasterTab(RadegastInstance instance, Avatar avatar)
59         {
60             InitializeComponent();
61             Disposed += new EventHandler(MasterTab_Disposed);
62
63             if (!instance.advancedDebugging)
64             {
65                 saveBtn.Visible = false;
66                 texturesBtn.Visible = false;
67             }
68
69             this.instance = instance;
70             this.avatar = avatar;
71             
72             // Callbacks
73             client.Avatars.ViewerEffectPointAt += new EventHandler<ViewerEffectPointAtEventArgs>(Avatars_ViewerEffectPointAt);
74             client.Objects.ObjectProperties += new EventHandler<ObjectPropertiesEventArgs>(Objects_ObjectProperties);
75         }
76
77         void MasterTab_Disposed(object sender, EventArgs e)
78         {
79             client.Avatars.ViewerEffectPointAt -= new EventHandler<ViewerEffectPointAtEventArgs>(Avatars_ViewerEffectPointAt);
80             client.Objects.ObjectProperties -= new EventHandler<ObjectPropertiesEventArgs>(Objects_ObjectProperties);
81         }
82
83         void Objects_ObjectProperties(object sender, ObjectPropertiesEventArgs e)
84         {
85             if (selectedPrim != null) {
86                 if (selectedPrim.ID == e.Properties.ObjectID) {
87                     selectedPrim.Properties = e.Properties;
88                     UpdateDisplay();
89                 }
90             }
91         }
92
93         void UpdateDisplay()
94         {
95             if (InvokeRequired) {
96                 Invoke(new MethodInvoker(delegate()
97                 {
98                     UpdateDisplay();
99                 }));
100                 return;
101             }
102             lastPrimName.Text = selectedPrim.Properties.Name;
103             lastPrimLocalID.Text = selectedPrim.LocalID.ToString();
104         }
105
106         void UpdateLLUUID()
107         {
108             if (InvokeRequired) {
109                 Invoke(new MethodInvoker(delegate()
110                 {
111                     UpdateLLUUID();
112                 }));
113                 return;
114             }
115             lastPrimLLUUID.Text = selectedID.ToString();
116             lastPrimLocalID.Text = selectedPrim.LocalID.ToString();
117             sitBitn.Enabled = true;
118             if (selectedPrim.ParentID != 0) {
119                 objInfoBtn.Enabled = true;
120             } else {
121                 objInfoBtn.Enabled = false;
122             }
123             touchBtn.Enabled = true;
124             if ((selectedPrim.Flags & PrimFlags.Money) != 0)
125             {
126                 payBtn.Enabled = true;
127             }
128             else
129             {
130                 payBtn.Enabled = false;
131             }
132             saveBtn.Enabled = true;
133             if (selectedPrim.Textures != null) {
134                 texturesBtn.Enabled = true;
135             }
136             btnPoint.Enabled = true;
137         }
138
139         void Avatars_ViewerEffectPointAt(object sender, ViewerEffectPointAtEventArgs e)
140         {
141             if (e.SourceID == avatar.ID && e.TargetID != UUID.Zero) {
142                 selectedID = e.TargetID;
143                 selectedPrim = client.Network.CurrentSim.ObjectsPrimitives.Find(
144                     delegate(Primitive prim) { return prim.ID == selectedID; }
145                 );
146                 if (selectedPrim != null) {
147                     client.Objects.SelectObject(client.Network.CurrentSim, selectedPrim.LocalID);
148                     UpdateLLUUID();
149                 }
150             }
151         }
152
153         private void objInfoBtn_Click(object sender, EventArgs e)
154         {
155             selectedPrim = client.Network.CurrentSim.ObjectsPrimitives.Find(
156                     delegate(Primitive prim) { return prim.LocalID == selectedPrim.ParentID; }
157             );
158             selectedID = selectedPrim.ID;
159             UpdateLLUUID();
160             client.Objects.SelectObject(client.Network.CurrentSim, selectedPrim.LocalID);
161         }
162
163         private void sitBitn_Click(object sender, EventArgs e)
164         {
165             if (selectedPrim != null) {
166                 instance.State.SetSitting(true, selectedPrim.ID);
167             }
168         }
169
170         private void standBtn_Click(object sender, EventArgs e)
171         {
172             instance.State.SetSitting(false, UUID.Zero);
173         }
174
175         private void touchBtn_Click(object sender, EventArgs e)
176         {
177             client.Self.Touch(selectedPrim.LocalID);
178         }
179
180         private void payBtn_Click(object sender, EventArgs e)
181         {
182             (new frmPay(instance, selectedPrim.ID, selectedPrim.Properties.Name, true)).ShowDialog();
183         }
184
185         private void texturesBtn_Click(object sender, EventArgs e)
186         {
187             pnlImages.Controls.Clear();
188
189             List<UUID> textures = new List<UUID>();
190
191             textures.Add(selectedPrim.Textures.DefaultTexture.TextureID);
192
193             for (int i = 0; i < selectedPrim.Textures.FaceTextures.Length; i++) {
194                 if (selectedPrim.Textures.FaceTextures[i] != null && (!textures.Contains(selectedPrim.Textures.FaceTextures[i].TextureID))) {
195                     textures.Add(selectedPrim.Textures.FaceTextures[i].TextureID);
196                 }
197             }
198
199             int nTextures = 0;
200
201             foreach (UUID textureID in textures) {
202                 SLImageHandler img = new SLImageHandler(instance, textureID, "Texture " + (nTextures + 1).ToString());
203                 img.Location = new Point(0, nTextures++ * img.Height);
204                 img.Dock = DockStyle.Top;
205                 img.Height = 450;
206                 pnlImages.Controls.Add(img);
207 //                nTextures++;
208             }
209
210             if (selectedPrim.Sculpt != null && selectedPrim.Sculpt.SculptTexture != UUID.Zero)
211             {
212                 SLImageHandler img = new SLImageHandler(instance, selectedPrim.Sculpt.SculptTexture, "Sculp Texture");
213                 img.Location = new Point(0, nTextures * img.Height);
214                 img.Dock = DockStyle.Top;
215                 img.Height = 450;
216                 pnlImages.Controls.Add(img);
217             }
218
219
220         }
221
222         private void saveBtn_Click(object sender, EventArgs e)
223         {
224             WindowWrapper mainWindow = new WindowWrapper(frmMain.ActiveForm.Handle);
225             System.Windows.Forms.SaveFileDialog dlg = new SaveFileDialog();
226             dlg.AddExtension = true;
227             dlg.RestoreDirectory = true;
228             dlg.Title = "Save object as...";
229             dlg.Filter = "XML file (*.xml)|*.xml";
230             DialogResult res = dlg.ShowDialog();
231
232             if (res == DialogResult.OK)
233             {
234                 Thread t = new Thread(new ThreadStart(delegate()
235                     {
236                         try
237                         {
238                             PrimSerializer s = new PrimSerializer(client);
239                             string primsXmls = s.GetSerializedPrims(client.Network.CurrentSim, selectedPrim.LocalID);
240                             System.IO.File.WriteAllText(dlg.FileName, primsXmls);
241                             s.CleanUp();
242                             s = null;
243                             MessageBox.Show(mainWindow, "Successfully saved " + dlg.FileName, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
244                         }
245                         catch (Exception excp)
246                         {
247                             MessageBox.Show(mainWindow, excp.Message, "Saving failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
248                         }
249                     }));
250                 t.IsBackground = true;
251                 t.Start();
252             }
253         }
254         private void loadBtn_Click(object sender, EventArgs e)
255         {
256             PrimDeserializer.ImportFromFile(client);
257         }
258
259         private void btnPoint_Click(object sender, EventArgs e)
260         {
261             if (instance.State.IsPointing)
262             {
263                 instance.State.UnSetPointing();
264                 btnPoint.Text = "Point at";
265             }
266             else
267             {
268                 instance.State.SetPointing(selectedPrim, 3);
269                 btnPoint.Text = "Unpoint";
270             }
271         }
272
273
274     }
275
276     public class WindowWrapper : System.Windows.Forms.IWin32Window
277     {
278         public WindowWrapper(IntPtr handle)
279         {
280             _hwnd = handle;
281         }
282
283         public IntPtr Handle
284         {
285             get { return _hwnd; }
286         }
287
288         private IntPtr _hwnd;
289     }
290
291 }