OSDN Git Service

Final tweaks to 1.9 release
[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.Windows.Forms;\r
34 using System.IO;\r
35 using OpenMetaverse;\r
36 using OpenMetaverse.Assets;\r
37 \r
38 namespace Radegast\r
39 {\r
40     public partial class AnimDetail : UserControl\r
41     {\r
42         private RadegastInstance instance;\r
43         private Avatar av;\r
44         private UUID anim;\r
45         private int n;\r
46         private List<FriendInfo> friends;\r
47         private FriendInfo friend;\r
48 \r
49         public AnimDetail(RadegastInstance instance, Avatar av, UUID anim, int n)\r
50         {\r
51             InitializeComponent();\r
52             Disposed += new EventHandler(AnimDetail_Disposed);\r
53             this.instance = instance;\r
54             this.av = av;\r
55             this.anim = anim;\r
56             this.n = n;\r
57         }\r
58 \r
59         void AnimDetail_Disposed(object sender, EventArgs e)\r
60         {\r
61         }\r
62 \r
63         private void AnimDetail_Load(object sender, EventArgs e)\r
64         {\r
65             if (File.Exists(instance.AnimCacheDir + "/" + anim + ".fail"))\r
66             {\r
67                 Visible = false;\r
68                 return;\r
69             }\r
70 \r
71             groupBox1.Text = "Animation " + n + " (" + anim + ") for " + av.Name;\r
72 \r
73             friends = instance.Client.Friends.FriendList.FindAll(delegate(FriendInfo f) { return true; });\r
74 \r
75             pnlSave.Visible = false;\r
76             boxAnimName.Text = "Animation " + n;\r
77             cbFriends.DropDownStyle = ComboBoxStyle.DropDownList;\r
78 \r
79             foreach (FriendInfo f in friends)\r
80             {\r
81                 cbFriends.Items.Add(f);\r
82             }\r
83 \r
84             if (File.Exists(instance.AnimCacheDir + "/" + anim + ".sla"))\r
85             {\r
86                 pnlSave.Visible = true;\r
87                 return;\r
88             }\r
89 \r
90 \r
91             instance.Client.Assets.RequestAsset(anim, AssetType.Animation, true, Assets_OnAssetReceived);\r
92         }\r
93 \r
94         private void btnSave_Click(object sender, EventArgs e)\r
95         {\r
96             WindowWrapper mainWindow = new WindowWrapper(frmMain.ActiveForm.Handle);\r
97             System.Windows.Forms.SaveFileDialog dlg = new SaveFileDialog();\r
98             dlg.AddExtension = true;\r
99             dlg.RestoreDirectory = true;\r
100             dlg.Title = "Save animation as...";\r
101             dlg.Filter = "Second Life Animation (*.sla)|*.sla";\r
102             DialogResult res = dlg.ShowDialog();\r
103 \r
104             if (res == DialogResult.OK)\r
105             {\r
106                 try\r
107                 {\r
108                     byte[] data = File.ReadAllBytes(instance.AnimCacheDir + "/" + anim + ".sla");\r
109                     File.WriteAllBytes(dlg.FileName, data);\r
110                 }\r
111                 catch (Exception ex)\r
112                 {\r
113                     Logger.Log("Saving animation failed: " + ex.Message, Helpers.LogLevel.Debug);\r
114                 }\r
115             }\r
116         }\r
117 \r
118         void Assets_OnAssetReceived(AssetDownload transfer, Asset asset)\r
119         {\r
120             if (InvokeRequired)\r
121             {\r
122                 Invoke(new MethodInvoker(delegate()\r
123                 {\r
124                     Assets_OnAssetReceived(transfer, asset);\r
125                 }));\r
126                 return;\r
127             }\r
128 \r
129             if (transfer.Success)\r
130             {\r
131                 Logger.Log("Animation " + anim + " download success " + asset.AssetData.Length + " bytes.", Helpers.LogLevel.Debug);\r
132                 File.WriteAllBytes(instance.AnimCacheDir + "/" + anim + ".sla", asset.AssetData);\r
133                 pnlSave.Visible = true;\r
134             }\r
135             else\r
136             {\r
137                 Logger.Log("Animation " + anim + " download failed.", Helpers.LogLevel.Debug);\r
138                 FileStream f = File.Create(instance.AnimCacheDir + "/" + anim + ".fail");\r
139                 f.Close();\r
140                 Visible = false;\r
141             }\r
142         }\r
143 \r
144         private void playBox_CheckStateChanged(object sender, EventArgs e)\r
145         {\r
146             if (playBox.CheckState == CheckState.Checked)\r
147             {\r
148                 instance.Client.Self.AnimationStart(anim, true);\r
149             }\r
150             else\r
151             {\r
152                 instance.Client.Self.AnimationStop(anim, true);\r
153             }\r
154         }\r
155 \r
156         private void btnSend_Click(object sender, EventArgs e)\r
157         {\r
158             byte[] data = File.ReadAllBytes(instance.AnimCacheDir + "/" + anim + ".sla");\r
159             instance.Client.Inventory.RequestCreateItemFromAsset(data, boxAnimName.Text, "(No description)", AssetType.Animation, InventoryType.Animation, instance.Client.Inventory.FindFolderForType(AssetType.Animation), On_ItemCreated);\r
160             lblStatus.Text = "Uploading...";\r
161             cbFriends.Enabled = false;\r
162         }\r
163 \r
164         void On_ItemCreated(bool success, string status, UUID itemID, UUID assetID)\r
165         {\r
166             if (InvokeRequired)\r
167             {\r
168                 Invoke(new MethodInvoker(delegate()\r
169                 {\r
170                     On_ItemCreated(success, status, itemID, assetID);\r
171                 }\r
172                 ));\r
173                 return;\r
174             }\r
175 \r
176             if (!success)\r
177             {\r
178                 lblStatus.Text = "Failed.";\r
179                 Logger.Log("Failed creating asset", Helpers.LogLevel.Debug);\r
180                 return;\r
181             }\r
182             else\r
183             {\r
184                 Logger.Log("Created inventory item " + itemID.ToString(), Helpers.LogLevel.Info);\r
185 \r
186                 lblStatus.Text = "Sending to " + friend.Name;\r
187                 Logger.Log("Sending item to " + friend.Name, Helpers.LogLevel.Info);\r
188 \r
189                 instance.Client.Inventory.GiveItem(itemID, boxAnimName.Text, AssetType.Animation, friend.UUID, false);\r
190                 lblStatus.Text = "Sent";\r
191             }\r
192 \r
193             cbFriends.Enabled = true;\r
194         }\r
195 \r
196 \r
197         private void cbFriends_SelectedValueChanged(object sender, EventArgs e)\r
198         {\r
199             if (cbFriends.SelectedIndex >= 0)\r
200             {\r
201                 btnSend.Enabled = true;\r
202                 friend = friends[cbFriends.SelectedIndex];\r
203             }\r
204             else\r
205             {\r
206                 btnSend.Enabled = false;\r
207             }\r
208         }\r
209     }\r
210 }\r