Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

317 рядки
8.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // 2013 Sam Hocevar <sam@hocevar.net>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #if HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <lol/engine.h>
  15. #include "scenesetup.h"
  16. namespace lol
  17. {
  18. //-----------------------------------------------------------------------------
  19. //CTor/DTor
  20. SceneSetup::SceneSetup(String const& name)
  21. {
  22. m_name = name;
  23. m_clear_color = vec4(vec3::zero, 1.f);
  24. m_show_gizmo = true;
  25. m_show_lights = true;
  26. }
  27. //----
  28. SceneSetup::~SceneSetup()
  29. {
  30. Shutdown(true);
  31. }
  32. //-----------------------------------------------------------------------------
  33. bool SceneSetup::Startup()
  34. {
  35. for (int i = 0; i < m_lights.Count(); i++)
  36. Ticker::Ref(m_lights[i]);
  37. return true;
  38. }
  39. //-----------------------------------------------------------------------------
  40. bool SceneSetup::Shutdown(bool destroy)
  41. {
  42. for (int i = 0; i < m_lights.Count(); i++)
  43. if (m_lights[i]->IsTicked())
  44. Ticker::Unref(m_lights[i]);
  45. if (destroy)
  46. m_lights.Empty();
  47. return true;
  48. }
  49. //-----------------------------------------------------------------------------
  50. void SceneSetup::AddLight(LightType type)
  51. {
  52. m_last_cmd = SceneSetup::Command::AddLight;
  53. m_lights << new Light();
  54. m_lights.Last()->SetType(type);
  55. }
  56. //-----------------------------------------------------------------------------
  57. void SceneSetup::SetupScene()
  58. {
  59. m_last_cmd = SceneSetup::Command::SetupScene;
  60. }
  61. //-----------------------------------------------------------------------------
  62. void SceneSetup::SetPosition(vec3 const& v)
  63. {
  64. switch (m_last_cmd.ToScalar())
  65. {
  66. case SceneSetup::Command::AddLight:
  67. m_lights.Last()->SetPosition(v);
  68. break;
  69. }
  70. }
  71. //-----------------------------------------------------------------------------
  72. void SceneSetup::SetLookAt(vec3 const& v)
  73. {
  74. switch (m_last_cmd.ToScalar())
  75. {
  76. case SceneSetup::Command::AddLight:
  77. //TODO: Do better
  78. m_lights.Last()->SetPosition(v - m_lights.Last()->GetPosition());
  79. break;
  80. }
  81. }
  82. //-----------------------------------------------------------------------------
  83. void SceneSetup::SetColor(vec4 const& c)
  84. {
  85. switch (m_last_cmd.ToScalar())
  86. {
  87. case SceneSetup::Command::AddLight:
  88. m_lights.Last()->SetColor(c);
  89. break;
  90. case SceneSetup::Command::SetupScene:
  91. m_clear_color = c;
  92. break;
  93. }
  94. }
  95. //-----------------------------------------------------------------------------
  96. void SceneSetup::Show(SceneSetup::Display const& d)
  97. {
  98. Set(d, DisplayFlag::On);
  99. }
  100. //-----------------------------------------------------------------------------
  101. void SceneSetup::Hide(SceneSetup::Display const& d)
  102. {
  103. Set(d, DisplayFlag::Off);
  104. }
  105. //-----------------------------------------------------------------------------
  106. void SceneSetup::Toggle(SceneSetup::Display const& d)
  107. {
  108. Set(d, DisplayFlag::Toggle);
  109. }
  110. //-----------------------------------------------------------------------------
  111. void SceneSetup::Set(SceneSetup::Display const& d, DisplayFlag const& f)
  112. {
  113. bool value = f == DisplayFlag::On ? true : false;
  114. switch (m_last_cmd.ToScalar())
  115. {
  116. case SceneSetup::Command::SetupScene:
  117. switch (d.ToScalar())
  118. {
  119. case SceneSetup::Display::Gizmo:
  120. m_show_gizmo = f == DisplayFlag::Toggle ? !m_show_gizmo : value;
  121. break;
  122. case SceneSetup::Display::Light:
  123. m_show_lights = f == DisplayFlag::Toggle ? !m_show_lights : value;
  124. break;
  125. }
  126. break;
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. SceneSetupLuaObject::SceneSetupLuaObject(String& name) : LuaObject()
  131. {
  132. m_setup = new SceneSetup(name);
  133. SceneSetupLuaLoader::RegisterSetup(m_setup);
  134. }
  135. //-----------------------------------------------------------------------------
  136. SceneSetupLuaObject::~SceneSetupLuaObject()
  137. {
  138. }
  139. //-----------------------------------------------------------------------------
  140. SceneSetupLuaObject* SceneSetupLuaObject::New(LuaState* l, int arg_nb)
  141. {
  142. UNUSED(l);
  143. UNUSED(arg_nb);
  144. LuaStack s(l);
  145. LuaString n;
  146. s >> n;
  147. return new SceneSetupLuaObject(n());
  148. }
  149. //-- Setup command ------------------------------------------------------------
  150. int SceneSetupLuaObject::AddLight(LuaState* l)
  151. {
  152. LuaStack s(l);
  153. LuaSSetupPtr o;
  154. LuaString t;
  155. s >> o >> t;
  156. o->m_setup->AddLight(FindValue<LightType>(t().C()));
  157. return 0;
  158. }
  159. int SceneSetupLuaObject::SetupScene(LuaState* l)
  160. {
  161. LuaStack s(l);
  162. LuaSSetupPtr o;
  163. s >> o;
  164. o->m_setup->SetupScene();
  165. return 0;
  166. }
  167. //-- main funcs ---------------------------------------------------------------
  168. int SceneSetupLuaObject::SetPosition(LuaState* l)
  169. {
  170. LuaStack s(l);
  171. LuaSSetupPtr o;
  172. LuaVec3 c;
  173. s >> o >> c;
  174. o->m_setup->SetPosition(c);
  175. return 0;
  176. }
  177. int SceneSetupLuaObject::SetLookAt(LuaState* l)
  178. {
  179. LuaStack s(l);
  180. LuaSSetupPtr o;
  181. LuaVec3 c;
  182. s >> o >> c;
  183. o->m_setup->SetLookAt(c);
  184. return 0;
  185. }
  186. int SceneSetupLuaObject::SetColor(LuaState* l)
  187. {
  188. LuaStack s(l);
  189. LuaSSetupPtr o;
  190. LuaColor c;
  191. s >> o >> c;
  192. o->m_setup->SetColor(c);
  193. return 0;
  194. }
  195. int SceneSetupLuaObject::Show(LuaState* l)
  196. {
  197. LuaStack s(l);
  198. LuaSSetupPtr o;
  199. LuaDisplay e;
  200. s >> o >> e;
  201. o->m_setup->Show(e);
  202. return 0;
  203. }
  204. int SceneSetupLuaObject::Hide(LuaState* l)
  205. {
  206. LuaStack s(l);
  207. LuaSSetupPtr o;
  208. LuaDisplay e;
  209. s >> o >> e;
  210. o->m_setup->Hide(e);
  211. return 0;
  212. }
  213. int SceneSetupLuaObject::Toggle(LuaState* l)
  214. {
  215. LuaStack s(l);
  216. LuaSSetupPtr o;
  217. LuaDisplay e;
  218. s >> o >> e;
  219. o->m_setup->Toggle(e);
  220. return 0;
  221. }
  222. //-----------------------------------------------------------------------------
  223. const LuaObjectLib* SceneSetupLuaObject::GetLib()
  224. {
  225. typedef SceneSetupLuaObject SSLO;
  226. static const LuaObjectLib lib = LuaObjectLib(
  227. "SceneSetup",
  228. //Statics
  229. { { nullptr, nullptr } },
  230. //Methods
  231. {
  232. //-----------------------------------------------------------------
  233. { "AddLight", AddLight },
  234. { "Setup", SetupScene },
  235. //-- main funcs
  236. { "Position", SetPosition },
  237. { "LookAt", SetLookAt },
  238. { "Color", SetColor },
  239. { "Show", Show },
  240. { "Hide", Hide },
  241. { "Toggle", Toggle },
  242. },
  243. //Variables
  244. { { nullptr, nullptr, nullptr } });
  245. return &lib;
  246. }
  247. //-----------------------------------------------------------------------------
  248. map<String, SceneSetup*> SceneSetupLuaLoader::m_setups;
  249. SceneSetupLuaLoader::SceneSetupLuaLoader() : LuaLoader()
  250. {
  251. LuaState* l = GetLuaState();
  252. LuaObjectDef::Register<SceneSetupLuaObject>(l);
  253. }
  254. //-----------------------------------------------------------------------------
  255. SceneSetupLuaLoader::~SceneSetupLuaLoader() { }
  256. //-----------------------------------------------------------------------------
  257. static array<SceneSetupLuaObject*> g_instances;
  258. void SceneSetupLuaLoader::Store(LuaObject* obj)
  259. {
  260. SceneSetupLuaObject* ezm = static_cast<SceneSetupLuaObject*>(obj);
  261. g_instances << ezm;
  262. }
  263. //-----------------------------------------------------------------------------
  264. array<SceneSetupLuaObject*>& SceneSetupLuaLoader::GetInstances()
  265. {
  266. return g_instances;
  267. }
  268. //-----------------------------------------------------------------------------
  269. void SceneSetupLuaLoader::RegisterSetup(SceneSetup* setup)
  270. {
  271. m_setups[setup->m_name] = setup;
  272. }
  273. //-----------------------------------------------------------------------------
  274. bool SceneSetupLuaLoader::GetRegisteredSetups(map<String, SceneSetup*>& setups)
  275. {
  276. setups = m_setups;
  277. return !!m_setups.count();
  278. }
  279. //-----------------------------------------------------------------------------
  280. bool SceneSetupLuaLoader::GetLoadedSetups(map<String, SceneSetup*>& setups)
  281. {
  282. return GetRegisteredSetups(setups);
  283. }
  284. } /* namespace lol */