Editor.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "Crown.h"
  2. #include "CRWObjectModel.h"
  3. #include <cstdlib>
  4. #include <iostream>
  5. using namespace Crown;
  6. class WndCtrl: public KeyboardListener
  7. {
  8. public:
  9. WndCtrl()
  10. {
  11. GetDevice()->GetInputManager()->RegisterKeyboardListener(this);
  12. }
  13. virtual void KeyReleased(const KeyboardEvent& event)
  14. {
  15. if (event.key == KC_ESCAPE)
  16. {
  17. GetDevice()->StopRunning();
  18. }
  19. }
  20. };
  21. void LoadXWMLAndLogResponse(WindowsManager* windowsManager, Str xwmlFile, WindowContext* context)
  22. {
  23. XWMLReader xwml;
  24. Window* window;
  25. Filesystem* fs = GetFilesystem();
  26. FilesystemEntry info;
  27. if (!fs->GetInfo(xwmlFile, info))
  28. {
  29. MessageWindow* mw = new MessageWindow(windowsManager, "Load XWML", "Could not find file '" + xwmlFile + "'", MWR_OK, NULL);
  30. windowsManager->DoModalWindow(mw);
  31. return;
  32. }
  33. window = xwml.LoadFile(xwmlFile, windowsManager, context);
  34. if (window == NULL)
  35. Log::E("Could not load XWML file '" + info.osPath + "'");
  36. else
  37. Log::I("Successfully loaded XWML file '" + info.osPath + "'");
  38. }
  39. class CRWUtils
  40. {
  41. public:
  42. static void CreateExampleCRW(Str crwFilePath)
  43. {
  44. CRWDecoder mDecoder;
  45. Stream* testimage = GetFilesystem()->OpenStream("res/terrain.bmp", SOM_READ);
  46. //FileStream* testfile = new FileStream("../../res/testfile.txt", SOM_READ);
  47. Stream* testfile = GetFilesystem()->OpenStream("res/testfile.txt", SOM_READ);
  48. mDecoder.New(crwFilePath);
  49. mDecoder.AddRawData("/", "file 1.txt", testfile, RF_NORMAL);
  50. testfile->Seek(0, SM_SeekFromBegin);
  51. mDecoder.AddDirectory("/", "docs");
  52. mDecoder.AddRawData("/docs", "file 2.txt", testfile, RF_NORMAL);
  53. testfile->Seek(0, SM_SeekFromBegin);
  54. mDecoder.AddDirectory("/docs", "images");
  55. mDecoder.AddRawData("/docs/images", "terrain.bmp", testimage, RF_NORMAL);
  56. mDecoder.AddDirectory("/docs", "todo");
  57. mDecoder.AddDirectory("/docs/todo", "asd1");
  58. mDecoder.AddDirectory("/docs/todo", "asd2");
  59. mDecoder.AddDirectory("/docs/images", "lll");
  60. mDecoder.AddDirectory("/docs/todo", "asd3");
  61. mDecoder.AddDirectory("/docs/todo", "asd4");
  62. mDecoder.AddDirectory("/docs/todo", "asd5");
  63. mDecoder.AddDirectory("/docs/todo", "asd6");
  64. for (int i=0; i<5; i++)
  65. {
  66. Str s = "info" + Str(i) + ".txt";
  67. mDecoder.AddRawData("/", s.c_str(), testfile, RF_NORMAL);
  68. testfile->Seek(0, SM_SeekFromBegin);
  69. }
  70. GetFilesystem()->Close(testimage);
  71. GetFilesystem()->Close(testfile);
  72. mDecoder.Close();
  73. }
  74. };
  75. //----------------------------------
  76. // Windows
  77. //----------------------------------
  78. class EditorWindow: public Window
  79. {
  80. public:
  81. EditorWindow(WindowsManager* wm, int x, int y, int width, int height, const Str& crwFilePath):
  82. Window(wm, x, y, width, height, "Editor: " + crwFilePath)
  83. {
  84. mDecoder.Load(crwFilePath);
  85. mObjModel = new CRWObjectModel(&mDecoder);
  86. TreeView* tw = new TreeView(GetContentWidget());
  87. //The CRW Object Model exposes the CRW content through a hierarchy of IWithProperties objects
  88. //that is suitable to be viewed in a TreeView
  89. //Create a ListGenericWrapper to expose the list as a Generic List
  90. ListGenericWrapper<CRWDescriptor*>* wrapper = new ListGenericWrapper<CRWDescriptor*>(mObjModel->GetRoot());
  91. tw->SetItems(wrapper);
  92. }
  93. virtual ~EditorWindow()
  94. {
  95. delete mObjModel;
  96. }
  97. private:
  98. CRWDecoder mDecoder;
  99. CRWObjectModel* mObjModel;
  100. };
  101. class UpperCaseConverter: public Converter
  102. {
  103. public:
  104. virtual Generic Convert(Generic source)
  105. {
  106. Str str = source.asStr();
  107. str.MakeUpper();
  108. return str;
  109. }
  110. };
  111. class CrwIconConverter: public Converter
  112. {
  113. public:
  114. virtual Generic Convert(Generic source)
  115. {
  116. //source is a CRWDescriptor, return the correct icon ImageSource (a path, probably)
  117. ushort type;
  118. if (!source.asUShort(type))
  119. {
  120. return "<Unknown>";
  121. }
  122. if (type == RT_Directory)
  123. {
  124. return "res/crw_dir_icon.bmp";
  125. }
  126. if (type == RT_RawData)
  127. {
  128. return "res/crw_file_icon.bmp";
  129. }
  130. return "res/crw_unknown.bmp";
  131. }
  132. };
  133. class CrwDescriptorToFullPathConverter: public Converter
  134. {
  135. public:
  136. virtual Generic Convert(Generic source)
  137. {
  138. //source is a CRWDescriptor, return the correct icon ImageSource (a path, probably)
  139. CRWDescriptor* descr;
  140. if (!source.asType(&descr))
  141. {
  142. return "";
  143. }
  144. return descr->GetCRWObjectModel()->GetCRWLibraryPath() + descr->GetFullName();
  145. }
  146. };
  147. class EditorWindowContext: public WindowContext
  148. {
  149. public:
  150. EditorWindowContext(WindowsManager* windowsManager, const Str& crwFilePath):
  151. WindowContext(windowsManager), twCrwLibrary(NULL)
  152. {
  153. mDecoder.Load(crwFilePath);
  154. mObjModel = new CRWObjectModel(&mDecoder);
  155. AddProperty(new IWithPropertiesProperty("CrwObjectModel", (IWithProperties**)&mObjModel));
  156. RegisterAction("UpdateCrwFilePreview", CreateDelegate(this, &EditorWindowContext::UpdateCrwFilePreview));
  157. RegisterConverter("UpperCaseConverter", new UpperCaseConverter);
  158. RegisterConverter("CrwIconConverter", new CrwIconConverter);
  159. RegisterConverter("CrwDescriptorToFullPathConverter", new CrwDescriptorToFullPathConverter);
  160. }
  161. virtual ~EditorWindowContext()
  162. {
  163. delete mObjModel;
  164. }
  165. void OnLoad()
  166. {
  167. twCrwLibrary = (TreeView*)FindWidgetByName("twCrwLibrary");
  168. NotifyChangeEventArgs args("twCrwLibrary");
  169. NotifyChange(&args);
  170. }
  171. virtual Generic GetPropertyValue(const Str& name) const
  172. {
  173. if (name == "twCrwLibrary")
  174. {
  175. return twCrwLibrary;
  176. }
  177. else
  178. {
  179. return WindowContext::GetPropertyValue(name);
  180. }
  181. }
  182. void UpdateCrwFilePreview(Widget* src, List<Str>* /*args*/)
  183. {
  184. //if (twCrwLibrary->GetSelectedIndex() != 2)
  185. //{
  186. // twCrwLibrary->SetSelectedIndex(2);
  187. //}
  188. //twCrwLibrary->PrintLoop(0);
  189. }
  190. virtual Str ToStr() const
  191. {
  192. return "EditorWindowContext";
  193. }
  194. private:
  195. TreeView* twCrwLibrary;
  196. CRWDecoder mDecoder;
  197. CRWObjectModel* mObjModel;
  198. };
  199. class MainWindowContext: public WindowContext
  200. {
  201. public:
  202. MainWindowContext(WindowsManager* windowsManager):
  203. WindowContext(windowsManager)
  204. {
  205. RegisterAction("NewLibrary", CreateDelegate(this, &MainWindowContext::NewLibraryAction));
  206. RegisterAction("OpenLibrary", CreateDelegate(this, &MainWindowContext::OpenLibraryAction));
  207. }
  208. void OnLoad()
  209. {
  210. btnClose = (Button*)FindWidgetByName("btnClose");
  211. btnNewLibrary = (Button*)FindWidgetByName("btnNewLibrary");
  212. btnOpenLibrary = (Button*)FindWidgetByName("btnOpenLibrary");
  213. }
  214. void NewLibraryAction(Widget* src, List<Str>* /*args*/)
  215. {
  216. Window* wnd = new TextInputWindow(
  217. GetWindowsManager(),
  218. "New CRW", "Please insert the new CRW name:",
  219. "library",
  220. new Delegate2<MainWindowContext, void, bool, Str>(this, &MainWindowContext::NewLibraryCallback, false, Str(""))
  221. );
  222. GetWindowsManager()->DoModalWindow(wnd);
  223. }
  224. void OpenLibraryAction(Widget* src, List<Str>* /*args*/)
  225. {
  226. Window* wnd = new TextInputWindow(
  227. GetWindowsManager(),
  228. "Open CRW", "Please insert the CRW name to open:",
  229. "library",
  230. new Delegate2<MainWindowContext, void, bool, Str>(this, &MainWindowContext::OpenCrwCallback, false, Str(""))
  231. );
  232. GetWindowsManager()->DoModalWindow(wnd);
  233. }
  234. void NewLibraryCallback(bool cancelled, Str name)
  235. {
  236. if (!cancelled)
  237. {
  238. Log::I("Creating new CRW: " + name + ".crw");
  239. Str crwFilePath = "res/" + name + ".crw";
  240. CRWUtils::CreateExampleCRW(crwFilePath);
  241. new EditorWindow(GetWindowsManager(), 100, 15, 250, 200, crwFilePath);
  242. }
  243. else
  244. Log::I("New CRW creation cancelled.");
  245. }
  246. void OpenCrwCallback(bool cancelled, Str name)
  247. {
  248. if (!cancelled)
  249. {
  250. Log::I("Opening CRW: " + name + ".crw");
  251. Str crwFilePath = "res/" + name + ".crw";
  252. //Verify the existence of crwFilePath
  253. if (!GetFilesystem()->Exists(crwFilePath))
  254. {
  255. Log::I("CRW file specified does not exist");
  256. }
  257. else
  258. {
  259. //new EditorWindow(GetWindowsManager(), 100, 15, 250, 200, crwFilePath);
  260. WindowsManager* windowsManager = GetWindowsManager();
  261. LoadXWMLAndLogResponse(windowsManager, "res/editor_editorwindow.xml", new EditorWindowContext(windowsManager, crwFilePath));
  262. }
  263. }
  264. else
  265. Log::I("Opening CRW cancelled.");
  266. }
  267. private:
  268. Button* btnClose;
  269. Button* btnNewLibrary;
  270. Button* btnOpenLibrary;
  271. };
  272. class MainScene: public Scene
  273. {
  274. public:
  275. MainScene(Crown::uint windowWidth, Crown::uint windowHeight)
  276. {
  277. }
  278. virtual ~MainScene()
  279. {
  280. }
  281. virtual void OnLoad()
  282. {
  283. Renderer* renderer = GetDevice()->GetRenderer();
  284. renderer->SetClearColor(Color4(0.6f, 0.6f, 0.6f, 1.0f));
  285. //Mat4 matrix(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f);
  286. //renderer->SetMatrix(MT_MODEL, matrix);
  287. //Mat4 matrix1;
  288. //glGetFloatv(GL_MODELVIEW_MATRIX, matrix1.ToFloatPtr());
  289. mWindowsManager = new WindowsManager(this);
  290. LoadXWMLAndLogResponse(mWindowsManager, "res/editor_mainwindow.xml", new MainWindowContext(mWindowsManager));
  291. }
  292. virtual void RenderScene()
  293. {
  294. Scene::RenderScene();
  295. }
  296. private:
  297. WindowsManager* mWindowsManager;
  298. };
  299. int main(int argc, char** argv)
  300. {
  301. int wndW = 1024;
  302. int wndH = 768;
  303. if (argc == 3)
  304. {
  305. wndW = atoi(argv[1]);
  306. wndH = atoi(argv[2]);
  307. }
  308. Device* mDevice = GetDevice();
  309. if (!mDevice->Init(wndW, wndH, 32, false))
  310. {
  311. return 0;
  312. }
  313. WndCtrl ctrl;
  314. MainScene* mainScene = new MainScene(wndW, wndH);
  315. GetDevice()->GetSceneManager()->SelectNextScene(mainScene);
  316. mDevice->GetMainWindow()->SetTitle("Crown Engine v0.1 - Editor Test");
  317. mDevice->Run();
  318. mDevice->Shutdown();
  319. return 0;
  320. }