25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

314 lines
8.7 KiB

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