OSDN Git Service

9f30933b42bbe39e9520a8d91126c3377ee6bdba
[radegast/radegast.git] / Radegast / GUI / Consoles / ExportCollada.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.ComponentModel;
34 using System.Drawing;
35 using System.Drawing.Imaging;
36 using System.Text;
37 #if (COGBOT_LIBOMV || USE_STHREADS)
38 using ThreadPoolUtil;
39 using Thread = ThreadPoolUtil.Thread;
40 using ThreadPool = ThreadPoolUtil.ThreadPool;
41 using Monitor = ThreadPoolUtil.Monitor;
42 #endif
43 using System.Threading;
44 using System.Windows.Forms;
45 using System.IO;
46 using OpenMetaverse;
47 using OpenMetaverse.StructuredData;
48 using OpenMetaverse.Assets;
49 using OpenMetaverse.Imaging;
50
51 namespace Radegast
52 {
53         public partial class ExportCollada : RadegastTabControl
54         {
55                 #region Private Variables
56         DAEExport Exporter;
57                 #endregion
58                 
59                 #region Constructor
60         public ExportCollada(RadegastInstance instance, Primitive prim)
61             : base(instance)
62                 {
63                         InitializeComponent();
64             Exporter = new DAEExport(instance, prim);
65             Exporter.Progress += new EventHandler<DAEStatutsEventArgs>(Exporter_Progress);
66             UpdateInfo();
67             cbImageType.Text = "TGA";
68                 }
69                 #endregion
70                 
71                 #region Private Methods
72                 void LogMessage(string format, params object[] args)
73                 {
74                         if (InvokeRequired)
75                         {
76                                 if (IsHandleCreated || !instance.MonoRuntime)
77                                         BeginInvoke(new MethodInvoker(() => LogMessage(format, args)));
78                                 return;
79                         }
80                         txtLog.AppendText(String.Format(format + "\r\n",args));
81                         txtLog.SelectionStart = txtLog.TextLength;
82                         txtLog.ScrollToCaret();
83                 }
84
85         void UpdateInfo()
86         {
87             Primitive root = Exporter.Prims[0];
88             if (root.Properties != null)
89             {
90                 objectName.Text = root.Properties.Name;
91             }
92             else
93             {
94                 objectName.Text = "Object";
95             }
96             objectUUID.Text = root.ID.ToString();
97             primCount.Text = Exporter.Prims.Count.ToString();
98             exportablePrims.Text = Exporter.ExportablePrims.ToString();
99             textureCount.Text = Exporter.Textures.Count.ToString();
100             exportableTextures.Text = Exporter.ExportableTextures.ToString();
101             texturesPanel.Controls.Clear();
102             foreach (UUID textureID in Exporter.Textures)
103             {
104                 var img = new SLImageHandler(instance, textureID, string.Empty);
105                 img.Height = 96;
106                 img.Width = 96;
107                 img.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
108                 texturesPanel.Controls.Add(img);
109             }
110         }
111                 
112                 void ValidatePath(string fname)
113                 {
114                         string path = Path.GetDirectoryName(fname);
115                         if (Directory.Exists(path))
116                                 btnExport.Enabled = true;
117                         else
118                                 btnExport.Enabled = false;
119                 }
120                 #endregion
121                 
122                 #region Event Handlers
123                 void TxtFileNameTextChanged(object sender, EventArgs e)
124                 {
125                         ValidatePath(txtFileName.Text);
126                 }
127                 
128                 void BtnBrowseClick(object sender, EventArgs e)
129                 {
130                         SaveFileDialog dlg = new SaveFileDialog();
131                         dlg.Title = "Export Collada File";
132             dlg.Filter = "Collada (*.dae)|*.dae|All Files (*.*)|*.*";
133             if (txtFileName.Text.Trim() == string.Empty)
134             {
135                 dlg.FileName = RadegastInstance.SafeFileName(objectName.Text);
136             }
137             else
138             {
139                 dlg.FileName =  Path.GetFileName(txtFileName.Text);
140             }
141
142                         DialogResult res = dlg.ShowDialog();
143                         
144                         if (res == DialogResult.OK)
145                         {
146                                 txtFileName.Text = dlg.FileName;
147                                 ValidatePath(dlg.FileName);
148                         }
149                 }
150                 
151                 void BtnExportClick(object sender, EventArgs e)
152                 {
153             Exporter.ImageFormat = cbImageType.Text;
154             try
155             {
156                 Exporter.Export(txtFileName.Text);
157             }
158             catch (Exception ex)
159             {
160                 LogMessage("Export failed: {0}", ex.ToString());
161             }
162                 }
163
164         private void cbExportTextures_CheckedChanged(object sender, EventArgs e)
165         {
166             Exporter.ExportTextures = cbImageType.Enabled = cbExportTextures.Checked;
167         }
168
169         void Exporter_Progress(object sender, DAEStatutsEventArgs e)
170         {
171             LogMessage(e.Message);
172         }
173         #endregion
174     }
175 }