OSDN Git Service

Moved libopenmetavrse outside the radegast folder, now we expected it to be in ....
[radegast/radegast.git] / Radegast / GUI / Consoles / AnimDetail.cs
1 // \r
2 // Radegast Metaverse Client\r
3 // Copyright (c) 2009, Radegast Development Team\r
4 // All rights reserved.\r
5 // \r
6 // Redistribution and use in source and binary forms, with or without\r
7 // modification, are permitted provided that the following conditions are met:\r
8 // \r
9 //     * Redistributions of source code must retain the above copyright notice,\r
10 //       this list of conditions and the following disclaimer.\r
11 //     * Redistributions in binary form must reproduce the above copyright\r
12 //       notice, this list of conditions and the following disclaimer in the\r
13 //       documentation and/or other materials provided with the distribution.\r
14 //     * Neither the name of the application "Radegast", nor the names of its\r
15 //       contributors may be used to endorse or promote products derived from\r
16 //       this software without specific prior written permission.\r
17 // \r
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 //\r
29 // $Id$\r
30 //\r
31 using System;\r
32 using System.Collections.Generic;\r
33 using System.ComponentModel;\r
34 using System.Drawing;\r
35 using System.Data;\r
36 using System.Text;\r
37 using System.Windows.Forms;\r
38 using System.IO;\r
39 using OpenMetaverse;\r
40 using OpenMetaverse.Assets;\r
41 \r
42 namespace Radegast\r
43 {\r
44     public partial class AnimDetail : UserControl\r
45     {\r
46         private RadegastInstance instance;\r
47         private Avatar av;\r
48         private UUID anim;\r
49         private int n;\r
50         private List<FriendInfo> friends;\r
51         private FriendInfo friend;\r
52 \r
53         public AnimDetail(RadegastInstance instance, Avatar av, UUID anim, int n)\r
54         {\r
55             InitializeComponent();\r
56             Disposed += new EventHandler(AnimDetail_Disposed);\r
57             this.instance = instance;\r
58             this.av = av;\r
59             this.anim = anim;\r
60             this.n = n;\r
61         }\r
62 \r
63         void AnimDetail_Disposed(object sender, EventArgs e)\r
64         {\r
65         }\r
66 \r
67         private void AnimDetail_Load(object sender, EventArgs e)\r
68         {\r
69             if (File.Exists(instance.AnimCacheDir + "/" + anim + ".fail"))\r
70             {\r
71                 Visible = false;\r
72                 return;\r
73             }\r
74 \r
75             groupBox1.Text = "Animation " + n + " (" + anim + ") for " + av.Name;\r
76 \r
77             friends = instance.Client.Friends.FriendList.FindAll(delegate(FriendInfo f) { return true; });\r
78 \r
79             pnlSave.Visible = false;\r
80             boxAnimName.Text = "Animation " + n;\r
81             cbFriends.DropDownStyle = ComboBoxStyle.DropDownList;\r
82 \r
83             foreach (FriendInfo f in friends)\r
84             {\r
85                 cbFriends.Items.Add(f);\r
86             }\r
87 \r
88             if (File.Exists(instance.AnimCacheDir + "/" + anim + ".sla"))\r
89             {\r
90                 pnlSave.Visible = true;\r
91                 return;\r
92             }\r
93 \r
94 \r
95             instance.Client.Assets.RequestAsset(anim, AssetType.Animation, true, Assets_OnAssetReceived);\r
96         }\r
97 \r
98         private void btnSave_Click(object sender, EventArgs e)\r
99         {\r
100             WindowWrapper mainWindow = new WindowWrapper(frmMain.ActiveForm.Handle);\r
101             System.Windows.Forms.SaveFileDialog dlg = new SaveFileDialog();\r
102             dlg.AddExtension = true;\r
103             dlg.RestoreDirectory = true;\r
104             dlg.Title = "Save animation as...";\r
105             dlg.Filter = "Second Life Animation (*.sla)|*.sla";\r
106             DialogResult res = dlg.ShowDialog();\r
107 \r
108             if (res == DialogResult.OK)\r
109             {\r
110                 try\r
111                 {\r
112                     byte[] data = File.ReadAllBytes(instance.AnimCacheDir + "/" + anim + ".sla");\r
113                     File.WriteAllBytes(dlg.FileName, data);\r
114                 }\r
115                 catch (Exception ex)\r
116                 {\r
117                     Logger.Log("Saving animation failed: " + ex.Message, Helpers.LogLevel.Debug);\r
118                 }\r
119             }\r
120         }\r
121 \r
122         void Assets_OnAssetReceived(AssetDownload transfer, Asset asset)\r
123         {\r
124             if (InvokeRequired)\r
125             {\r
126                 Invoke(new MethodInvoker(delegate()\r
127                 {\r
128                     Assets_OnAssetReceived(transfer, asset);\r
129                 }));\r
130                 return;\r
131             }\r
132 \r
133             if (transfer.Success)\r
134             {\r
135                 Logger.Log("Animation " + anim + " download success " + asset.AssetData.Length + " bytes.", Helpers.LogLevel.Debug);\r
136                 File.WriteAllBytes(instance.AnimCacheDir + "/" + anim + ".sla", asset.AssetData);\r
137                 pnlSave.Visible = true;\r
138             }\r
139             else\r
140             {\r
141                 Logger.Log("Animation " + anim + " download failed.", Helpers.LogLevel.Debug);\r
142                 FileStream f = File.Create(instance.AnimCacheDir + "/" + anim + ".fail");\r
143                 f.Close();\r
144                 Visible = false;\r
145             }\r
146         }\r
147 \r
148         private void playBox_CheckStateChanged(object sender, EventArgs e)\r
149         {\r
150             if (playBox.CheckState == CheckState.Checked)\r
151             {\r
152                 instance.Client.Self.AnimationStart(anim, true);\r
153             }\r
154             else\r
155             {\r
156                 instance.Client.Self.AnimationStop(anim, true);\r
157             }\r
158         }\r
159 \r
160         private void btnSend_Click(object sender, EventArgs e)\r
161         {\r
162             byte[] data = File.ReadAllBytes(instance.AnimCacheDir + "/" + anim + ".sla");\r
163             instance.Client.Inventory.RequestCreateItemFromAsset(data, boxAnimName.Text, "(No description)", AssetType.Animation, InventoryType.Animation, instance.Client.Inventory.FindFolderForType(AssetType.Animation), On_ItemCreated);\r
164             lblStatus.Text = "Uploading...";\r
165             cbFriends.Enabled = false;\r
166         }\r
167 \r
168         void On_ItemCreated(bool success, string status, UUID itemID, UUID assetID)\r
169         {\r
170             if (InvokeRequired)\r
171             {\r
172                 Invoke(new MethodInvoker(delegate()\r
173                 {\r
174                     On_ItemCreated(success, status, itemID, assetID);\r
175                 }\r
176                 ));\r
177                 return;\r
178             }\r
179 \r
180             if (!success)\r
181             {\r
182                 lblStatus.Text = "Failed.";\r
183                 Logger.Log("Failed creating asset", Helpers.LogLevel.Debug);\r
184                 return;\r
185             }\r
186             else\r
187             {\r
188                 Logger.Log("Created inventory item " + itemID.ToString(), Helpers.LogLevel.Info);\r
189 \r
190                 lblStatus.Text = "Sending to " + friend.Name;\r
191                 Logger.Log("Sending item to " + friend.Name, Helpers.LogLevel.Info);\r
192 \r
193                 InventoryItem item = (InventoryItem)instance.Client.Inventory.Store[itemID];\r
194                 instance.Client.Inventory.GiveItem(item.UUID, item.Name, item.AssetType, friend.UUID, false);\r
195                 lblStatus.Text = "Sent";\r
196             }\r
197 \r
198             cbFriends.Enabled = true;\r
199         }\r
200 \r
201 \r
202         private void cbFriends_SelectedValueChanged(object sender, EventArgs e)\r
203         {\r
204             if (cbFriends.SelectedIndex >= 0)\r
205             {\r
206                 btnSend.Enabled = true;\r
207                 friend = friends[cbFriends.SelectedIndex];\r
208             }\r
209             else\r
210             {\r
211                 btnSend.Enabled = false;\r
212             }\r
213         }\r
214     }\r
215 }\r