Panda3D
maxEggImport.cxx
1 // Filename: maxEggImport.cxx
2 // Created by: jyelon (15Jul05)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 //
15 // This is the wrapper code for the max importer plugin.
16 // It includes:
17 //
18 // - user interface dialogs and popups
19 // - plugin initialization/registration
20 //
21 // It does not include the actual code to traverse the EggData.
22 //
23 ////////////////////////////////////////////////////////////////////
24 
25 // Include this before everything
26 #include "pandatoolbase.h"
27 
28 // MAX includes
29 #include "maxEggLoader.h"
30 #include "Max.h"
31 #include "maxImportRes.h"
32 #include "istdplug.h"
33 
34 // panda includes.
35 #include "notifyCategoryProxy.h"
36 
37 #include <iostream>
38 #include <sstream>
39 
40 class MaxEggImporter : public SceneImport
41 {
42 public:
43  // GUI-related methods
45  ~MaxEggImporter();
46  int ExtCount(); // Number of extensions supported
47  const TCHAR * Ext(int n); // Extension #n (i.e. "EGG")
48  const TCHAR * LongDesc(); // Long ASCII description (i.e. "Egg Importer")
49  const TCHAR * ShortDesc(); // Short ASCII description (i.e. "Egg")
50  const TCHAR * AuthorName(); // ASCII Author name
51  const TCHAR * CopyrightMessage();// ASCII Copyright message
52  const TCHAR * OtherMessage1(); // Other message #1
53  const TCHAR * OtherMessage2(); // Other message #2
54  unsigned int Version(); // Version number * 100 (i.e. v3.01 = 301)
55  void ShowAbout(HWND hWnd); // Show DLL's "About..." box
56  int DoImport(const TCHAR *name,ImpInterface *ei,Interface *i, BOOL suppressPrompts);
57 
58 public:
59  // GUI-related fields
60  static BOOL _merge;
61  static BOOL _importmodel;
62  static BOOL _importanim;
63 };
64 
65 BOOL MaxEggImporter::_merge = TRUE;
66 BOOL MaxEggImporter::_importmodel = TRUE;
67 BOOL MaxEggImporter::_importanim = FALSE;
68 
69 MaxEggImporter::MaxEggImporter()
70 {
71 }
72 
73 MaxEggImporter::~MaxEggImporter()
74 {
75 }
76 
77 int MaxEggImporter::ExtCount()
78 {
79  // Number of different extensions handled by this importer.
80  return 1;
81 }
82 
83 const TCHAR * MaxEggImporter::Ext(int n)
84 {
85  // Fetch the extensions handled by this importer.
86  switch(n) {
87  case 0: return _T("egg");
88  default: return _T("");
89  }
90 }
91 
92 const TCHAR * MaxEggImporter::LongDesc()
93 {
94  return _T("Panda3D Egg Importer");
95 }
96 
97 const TCHAR * MaxEggImporter::ShortDesc()
98 {
99  return _T("Panda3D Egg");
100 }
101 
102 const TCHAR * MaxEggImporter::AuthorName()
103 {
104  return _T("Joshua Yelon");
105 }
106 
107 const TCHAR * MaxEggImporter::CopyrightMessage()
108 {
109  return _T("Copyight (c) 2005 Josh Yelon");
110 }
111 
112 const TCHAR * MaxEggImporter::OtherMessage1()
113 {
114  return _T("");
115 }
116 
117 const TCHAR * MaxEggImporter::OtherMessage2()
118 {
119  return _T("");
120 }
121 
122 unsigned int MaxEggImporter::Version()
123 {
124  return 100;
125 }
126 
127 static INT_PTR CALLBACK AboutBoxDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
128 {
129  switch (msg) {
130  case WM_INITDIALOG:
131  CenterWindow(hWnd, GetParent(hWnd));
132  break;
133  case WM_COMMAND:
134  switch (LOWORD(wParam)) {
135  case IDOK:
136  EndDialog(hWnd, 1);
137  break;
138  }
139  break;
140  default:
141  return FALSE;
142  }
143  return TRUE;
144 }
145 
146 void MaxEggImporter::ShowAbout(HWND hWnd)
147 {
148  DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX),
149  hWnd, AboutBoxDlgProc, 0);
150 }
151 
152 
153 static INT_PTR CALLBACK ImportDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
154 {
155  MaxEggImporter *imp = (MaxEggImporter*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
156  switch (msg) {
157  case WM_INITDIALOG:
158  imp = (MaxEggImporter*)lParam;
159  SetWindowLongPtr(hWnd, GWLP_USERDATA, lParam);
160  CenterWindow(hWnd, GetParent(hWnd));
161  CheckDlgButton(hWnd, IDC_MERGE, imp->_merge);
162  CheckDlgButton(hWnd, IDC_IMPORTMODEL, imp->_importmodel);
163  CheckDlgButton(hWnd, IDC_IMPORTANIM, imp->_importanim);
164  break;
165  case WM_COMMAND:
166  switch (LOWORD(wParam)) {
167  case IDOK:
168  imp->_merge = IsDlgButtonChecked(hWnd, IDC_MERGE);
169  imp->_importmodel = IsDlgButtonChecked(hWnd, IDC_IMPORTMODEL);
170  imp->_importanim = IsDlgButtonChecked(hWnd, IDC_IMPORTANIM);
171  EndDialog(hWnd, 1);
172  break;
173  case IDCANCEL:
174  EndDialog(hWnd, 0);
175  break;
176  }
177  break;
178  default:
179  return FALSE;
180  }
181  return TRUE;
182 }
183 
184 int MaxEggImporter::
185 DoImport(const TCHAR *name, ImpInterface *ii, Interface *i, BOOL suppressPrompts) {
186  // Prompt the user with our dialogbox.
187  if (!DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_IMPORT_DLG),
188  i->GetMAXHWnd(), ImportDlgProc, (LPARAM)this)) {
189  return 1;
190  }
191 
192  std::ostringstream log;
193  Notify::ptr()->set_ostream_ptr(&log, false);
194 
195 #ifdef _UNICODE
196  char sname[2048];
197  sname[2047] = 0;
198  wcstombs(sname, name, 2047);
199  bool ok = MaxLoadEggFile(sname, _merge ? true:false, _importmodel ? true:false, _importanim ? true:false);
200 #else
201  bool ok = MaxLoadEggFile(name, _merge ? true:false, _importmodel ? true:false, _importanim ? true:false);
202 #endif
203 
204  string txt = log.str();
205  if (txt != "") {
206  MessageBoxA(NULL, txt.c_str(), "Panda3D Importer", MB_OK);
207  } else if (!ok) {
208  MessageBoxA(NULL, "Import Failed, unknown reason\n", "Panda3D Importer", MB_OK);
209  }
210 
211  Notify::ptr()->set_ostream_ptr(NULL, false);
212  return 1;
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////////////////////////
216 //
217 // Plugin Initialization
218 //
219 // The following code enables Max to load this DLL, get a list
220 // of the classes defined in this DLL, and provides a means for
221 // Max to create instances of those classes.
222 //
223 ////////////////////////////////////////////////////////////////////////////////////////////////////
224 
225 HINSTANCE hInstance;
226 
227 BOOL WINAPI DllMain(HINSTANCE hinstDLL,ULONG fdwReason,LPVOID lpvReserved) {
228  static int controlsInit = FALSE;
229  hInstance = hinstDLL;
230 
231  if (!controlsInit) {
232  controlsInit = TRUE;
233  // It appears that InitCustomControls is deprecated in 2012.
234  // I'm not sure if we can just remove it like this, but
235  // I've heard that it seems to work, so let's do it like this.
236 #if MAX_VERSION_MAJOR < 14
237  InitCustomControls(hInstance);
238 #endif
239  InitCommonControls();
240  }
241 
242  return (TRUE);
243 }
244 
245 #define PANDAEGGIMP_CLASS_ID1 0x377193ab
246 #define PANDAEGGIMP_CLASS_ID2 0x897afe12
247 
248 class MaxEggImporterClassDesc: public ClassDesc {
249 public:
250  int IsPublic() {return 1;}
251  void *Create(BOOL loading = FALSE) {return new MaxEggImporter;}
252  const TCHAR *ClassName() {return _T("MaxEggImporter");}
253  SClass_ID SuperClassID() {return SCENE_IMPORT_CLASS_ID;}
254  Class_ID ClassID() {return Class_ID(PANDAEGGIMP_CLASS_ID1,PANDAEGGIMP_CLASS_ID2);}
255  const TCHAR *Category() {return _T("Chrutilities");}
256 };
257 
258 static MaxEggImporterClassDesc MaxEggImporterDesc;
259 
260 __declspec( dllexport ) const TCHAR* LibDescription()
261 {
262  return _T("Panda3D Egg Importer");
263 }
264 
265 __declspec( dllexport ) int LibNumberClasses()
266 {
267  return 1;
268 }
269 
270 __declspec( dllexport ) ClassDesc* LibClassDesc(int i)
271 {
272  switch(i) {
273  case 0: return &MaxEggImporterDesc;
274  default: return 0;
275  }
276 }
277 
278 __declspec( dllexport ) ULONG LibVersion()
279 {
280  return VERSION_3DSMAX;
281 }
282 
static Notify * ptr()
Returns the pointer to the global Notify object.
Definition: notify.cxx:337
void set_ostream_ptr(ostream *ostream_ptr, bool delete_later)
Changes the ostream that all subsequent Notify messages will be written to.
Definition: notify.cxx:73