OSDN Git Service

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