XWMLReader.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "Crown.h"
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <GL/glew.h>
  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. class FruitInfo: public WithProperties
  22. {
  23. public:
  24. FruitInfo(Str name, float price):
  25. mName(name), mPrice(price)
  26. {
  27. AddProperty(new StrProperty("fruitName", &mName));
  28. AddProperty(new FloatProperty("fruitPrice", &mPrice));
  29. }
  30. virtual ~FruitInfo()
  31. {
  32. }
  33. Str ToStr() const
  34. {
  35. return mName;
  36. }
  37. public:
  38. Str mName;
  39. float mPrice;
  40. };
  41. class AllWindowsContext: public WindowContext
  42. {
  43. public:
  44. AllWindowsContext(WindowsManager* windowsManager):
  45. WindowContext(windowsManager)
  46. {
  47. mFruitsList = new List<Generic>();
  48. mFruitsList->Append(new FruitInfo("Pear", 1.38f));
  49. mFruitsList->Append(new FruitInfo("Apple", 2.41f));
  50. mFruitsList->Append(new FruitInfo("Peach", 1.89f));
  51. mFruitsList->Append(new FruitInfo("Tomato???", 0.87f));
  52. mFruitsList->Append(new FruitInfo("Orange", 1.40f));
  53. mFruitsList->Append(new FruitInfo("Pineapple", 2.15f));
  54. //AddProperty(new GenericListProperty("FruitsList", &mFruitsList));
  55. RegisterAction("IncreaseFruitPrice", CreateDelegate(this, &AllWindowsContext::IncreaseFruitPrice));
  56. RegisterAction("DecreaseFruitPrice", CreateDelegate(this, &AllWindowsContext::DecreaseFruitPrice));
  57. }
  58. virtual ~AllWindowsContext()
  59. {
  60. for(int i=0; i<mFruitsList->GetSize(); i++)
  61. {
  62. FruitInfo* fruitInfo = mFruitsList->GetElement(i).asType<FruitInfo>();
  63. delete fruitInfo;
  64. }
  65. }
  66. void OnLoad()
  67. {
  68. lwFruits = (ListView*)(GetAssociatedWindow()->FindChildByName("lwFruits"));
  69. }
  70. void IncreaseFruitPrice(Widget* src, List<Str>* /*args*/)
  71. {
  72. FruitPriceApplyDeltaToSelected(+1.0f);
  73. }
  74. void DecreaseFruitPrice(Widget* src, List<Str>* /*args*/)
  75. {
  76. FruitPriceApplyDeltaToSelected(-1.0f);
  77. }
  78. void FruitPriceApplyDeltaToSelected(float delta)
  79. {
  80. int selectedIndex = lwFruits->GetSelectedIndex();
  81. if (selectedIndex != -1)
  82. {
  83. FruitInfo* fruitInfo;
  84. mFruitsList->GetElement(selectedIndex).asType(&fruitInfo);
  85. fruitInfo->SetPropertyValue("fruitPrice", fruitInfo->mPrice + delta);
  86. }
  87. }
  88. private:
  89. ListView* lwFruits;
  90. List<Generic>* mFruitsList;
  91. };
  92. class MainScene: public Scene, public WeakReferenced
  93. {
  94. public:
  95. MainScene(Crown::uint windowWidth, Crown::uint windowHeight)
  96. {
  97. }
  98. virtual ~MainScene()
  99. {
  100. }
  101. void LoadXWMLAndLogResponse(Str xwmlFile)
  102. {
  103. XWMLReader xwml;
  104. Window* window;
  105. Filesystem* fs = GetFilesystem();
  106. FilesystemEntry info;
  107. // if (!fs->GetInfo(xwmlFile, info))
  108. // {
  109. // MessageWindow* mw = new MessageWindow(mWindowsManager, "Load XWML", "Could not find file '" + xwmlFile + "'", MWR_OK, NULL);
  110. // mWindowsManager->DoModalWindow(mw);
  111. // return;
  112. // }
  113. window = xwml.LoadFile(xwmlFile, mWindowsManager, new AllWindowsContext(mWindowsManager));
  114. if (window == NULL)
  115. Log::E("Could not load XWML file '" + info.osPath + "'");
  116. else
  117. Log::I("Successfully loaded XWML file '" + info.osPath + "'");
  118. }
  119. virtual void OnLoad()
  120. {
  121. Scene::OnLoad();
  122. Renderer* renderer = GetDevice()->GetRenderer();
  123. renderer->SetClearColor(Color4(0.6f, 0.6f, 0.6f, 1.0f));
  124. mWindowsManager = new WindowsManager(this);
  125. mWindowsManager->RegisterAction("LoadXWMLFromFilepath", CreateDelegate(this, &MainScene::LoadXWMLFromFilepath));
  126. //LoadXWMLAndLogResponse("res/window.xml", wm);
  127. //LoadXWMLAndLogResponse("res/window_listview.xml", wm);
  128. LoadXWMLAndLogResponse("res/window_loader.xml");
  129. }
  130. void LoadXWMLFromFilepath(Widget* src, List<Str>* /*args*/)
  131. {
  132. Window* window = src->GetWindow();
  133. TextBox* tbXWMLFilepath = (TextBox*)window->FindChildByName("tbXWMLFilepath");
  134. if (tbXWMLFilepath == NULL)
  135. {
  136. Log::E("LoadXWMLFromFilepath action: Could not find TextBox 'tbXWMLFilepath'");
  137. return;
  138. }
  139. LoadXWMLAndLogResponse(tbXWMLFilepath->GetText());
  140. }
  141. virtual void RenderScene()
  142. {
  143. GetDevice()->GetRenderer()->_SetBackfaceCulling(false);
  144. mWindowsManager->Render();
  145. GetDevice()->GetRenderer()->_SetBackfaceCulling(true);
  146. }
  147. private:
  148. WindowsManager* mWindowsManager;
  149. };
  150. int main(int argc, char** argv)
  151. {
  152. int wndW = 1024;
  153. int wndH = 768;
  154. if (argc == 3)
  155. {
  156. wndW = atoi(argv[1]);
  157. wndH = atoi(argv[2]);
  158. }
  159. Device* mDevice = GetDevice();
  160. if (!mDevice->Init(wndW, wndH, 32, false))
  161. {
  162. return 0;
  163. }
  164. WndCtrl ctrl;
  165. MainScene* mainScene = new MainScene(wndW, wndH);
  166. GetDevice()->GetSceneManager()->SelectNextScene(mainScene);
  167. mDevice->GetMainWindow()->SetTitle("Crown Engine v0.1 - XWMLReader Test");
  168. while (mDevice->IsRunning())
  169. {
  170. mDevice->Frame();
  171. }
  172. mDevice->Shutdown();
  173. return 0;
  174. }