You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
8.5 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(lua_State* l, int arg_nb)
  141. {
  142. UNUSED(arg_nb);
  143. auto s = LuaStack::Begin(l);
  144. auto n = s.Get<String>();
  145. return new SceneSetupLuaObject(n);
  146. }
  147. //-- Setup command ------------------------------------------------------------
  148. int SceneSetupLuaObject::AddLight(lua_State* l)
  149. {
  150. auto s = LuaStack::Begin(l);
  151. auto o = s.GetPtr<SceneSetupLuaObject>();
  152. auto t = s.Get<String>();
  153. o->m_setup->AddLight(FindValue<LightType>(t.C()));
  154. return s.End();
  155. }
  156. int SceneSetupLuaObject::SetupScene(lua_State* l)
  157. {
  158. auto s = LuaStack::Begin(l);
  159. auto o = s.GetPtr<SceneSetupLuaObject>();
  160. o->m_setup->SetupScene();
  161. return s.End();
  162. }
  163. //-- main funcs ---------------------------------------------------------------
  164. int SceneSetupLuaObject::SetPosition(lua_State* l)
  165. {
  166. auto s = LuaStack::Begin(l);
  167. auto o = s.GetPtr<SceneSetupLuaObject>();
  168. auto c = s.Get<vec3>();
  169. o->m_setup->SetPosition(c);
  170. return s.End();
  171. }
  172. int SceneSetupLuaObject::SetLookAt(lua_State* l)
  173. {
  174. auto s = LuaStack::Begin(l);
  175. auto o = s.GetPtr<SceneSetupLuaObject>();
  176. auto c = s.Get<vec3>();
  177. o->m_setup->SetLookAt(c);
  178. return s.End();
  179. }
  180. int SceneSetupLuaObject::SetColor(lua_State* l)
  181. {
  182. auto s = LuaStack::Begin(l);
  183. auto o = s.GetPtr<SceneSetupLuaObject>();
  184. auto c = s.Get<vec4>();
  185. o->m_setup->SetColor(c);
  186. return s.End();
  187. }
  188. int SceneSetupLuaObject::Show(lua_State* l)
  189. {
  190. auto s = LuaStack::Begin(l);
  191. auto o = s.GetPtr<SceneSetupLuaObject>();
  192. auto e = s.GetEnum<SceneSetup::DisplayBase>();
  193. o->m_setup->Show(e);
  194. return s.End();
  195. }
  196. int SceneSetupLuaObject::Hide(lua_State* l)
  197. {
  198. auto s = LuaStack::Begin(l);
  199. auto o = s.GetPtr<SceneSetupLuaObject>();
  200. auto e = s.GetEnum<SceneSetup::DisplayBase>();
  201. o->m_setup->Hide(e);
  202. return s.End();
  203. }
  204. int SceneSetupLuaObject::Toggle(lua_State* l)
  205. {
  206. auto s = LuaStack::Begin(l);
  207. auto o = s.GetPtr<SceneSetupLuaObject>();
  208. auto e = s.GetEnum<SceneSetup::DisplayBase>();
  209. o->m_setup->Toggle(e);
  210. return s.End();
  211. }
  212. //-----------------------------------------------------------------------------
  213. const LuaObjectLibrary* SceneSetupLuaObject::GetLib()
  214. {
  215. typedef SceneSetupLuaObject SSLO;
  216. static const LuaObjectLibrary lib = LuaObjectLibrary(
  217. "SceneSetup",
  218. //Statics
  219. { { nullptr, nullptr } },
  220. //Methods
  221. {
  222. //-----------------------------------------------------------------
  223. { "AddLight", AddLight },
  224. { "Setup", SetupScene },
  225. //-- main funcs
  226. { "Position", SetPosition },
  227. { "LookAt", SetLookAt },
  228. { "Color", SetColor },
  229. { "Show", Show },
  230. { "Hide", Hide },
  231. { "Toggle", Toggle },
  232. },
  233. //Variables
  234. { { nullptr, nullptr, nullptr } });
  235. return &lib;
  236. }
  237. //-----------------------------------------------------------------------------
  238. map<String, SceneSetup*> SceneSetupLuaLoader::m_setups;
  239. SceneSetupLuaLoader::SceneSetupLuaLoader() : LuaLoader()
  240. {
  241. lua_State* l = GetLuaState();
  242. LuaObjectHelper::Register<SceneSetupLuaObject>(l);
  243. }
  244. //-----------------------------------------------------------------------------
  245. SceneSetupLuaLoader::~SceneSetupLuaLoader() { }
  246. //-----------------------------------------------------------------------------
  247. static array<SceneSetupLuaObject*> g_instances;
  248. void SceneSetupLuaLoader::Store(LuaObject* obj)
  249. {
  250. SceneSetupLuaObject* ezm = static_cast<SceneSetupLuaObject*>(obj);
  251. g_instances << ezm;
  252. }
  253. //-----------------------------------------------------------------------------
  254. array<SceneSetupLuaObject*>& SceneSetupLuaLoader::GetInstances()
  255. {
  256. return g_instances;
  257. }
  258. //-----------------------------------------------------------------------------
  259. void SceneSetupLuaLoader::RegisterSetup(SceneSetup* setup)
  260. {
  261. m_setups[setup->m_name] = setup;
  262. }
  263. //-----------------------------------------------------------------------------
  264. bool SceneSetupLuaLoader::GetRegisteredSetups(map<String, SceneSetup*>& setups)
  265. {
  266. setups = m_setups;
  267. return !!m_setups.count();
  268. }
  269. //-----------------------------------------------------------------------------
  270. bool SceneSetupLuaLoader::GetLoadedSetups(map<String, SceneSetup*>& setups)
  271. {
  272. return GetRegisteredSetups(setups);
  273. }
  274. } /* namespace lol */