227 wiersze
6.6 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. #pragma once
  14. //
  15. // The Scene setup class
  16. // ----------------
  17. //
  18. #include <string>
  19. namespace lol
  20. {
  21. //-----------------------------------------------------------------------------
  22. class SceneSetup
  23. {
  24. friend class SceneSetupLuaLoader;
  25. friend class SceneSetupLuaObject;
  26. public:
  27. //CTor/DTor
  28. SceneSetup() : SceneSetup("default") { }
  29. SceneSetup(std::string const& name);
  30. ~SceneSetup();
  31. static char const *GetName() { return "<scenesetup>"; }
  32. //--
  33. bool Startup();
  34. bool Shutdown(bool destroy=false);
  35. //-- Setup command
  36. void AddLight(LightType type);
  37. void SetupScene();
  38. //-- main funcs
  39. void SetPosition(vec3 const& v);
  40. void SetLookAt(vec3 const& v);
  41. void SetColor(vec4 const& c);
  42. //-------------------------------------------------------------------------
  43. struct DisplayBase : public StructSafeEnum
  44. {
  45. enum Type
  46. {
  47. Gizmo,
  48. Light,
  49. Max
  50. };
  51. protected:
  52. virtual bool BuildEnumMap(map<int64_t, std::string>& enum_map)
  53. {
  54. enum_map[Gizmo] = "Gizmo";
  55. enum_map[Light] = "Light";
  56. return true;
  57. }
  58. };
  59. typedef SafeEnum<DisplayBase> Display;
  60. void Show(Display const& d);
  61. void Hide(Display const& d);
  62. void Toggle(Display const& d);
  63. protected:
  64. void Set(Display const& d, DisplayFlag const& f);
  65. protected:
  66. //-------------------------------------------------------------------------
  67. struct CommandBase : public StructSafeEnum
  68. {
  69. enum Type
  70. {
  71. AddLight,
  72. SetupScene,
  73. Max
  74. };
  75. protected:
  76. virtual bool BuildEnumMap(map<int64_t, std::string>& enum_map)
  77. {
  78. enum_map[AddLight] = "AddLight";
  79. enum_map[SetupScene] = "SetupScene";
  80. return true;
  81. }
  82. };
  83. typedef SafeEnum<CommandBase> Command;
  84. public:
  85. //--
  86. std::string m_name;
  87. Command m_last_cmd;
  88. vec4 m_clear_color;
  89. array<Light *> m_lights;
  90. array<std::string, std::string> m_custom_cmd;
  91. bool m_show_gizmo;
  92. bool m_show_lights;
  93. };
  94. //-----------------------------------------------------------------------------
  95. class SceneSetupLuaObject : public LuaObject
  96. {
  97. public:
  98. //-------------------------------------------------------------------------
  99. SceneSetupLuaObject(std::string const& name);
  100. virtual ~SceneSetupLuaObject();
  101. //-------------------------------------------------------------------------
  102. static SceneSetupLuaObject* New(lua_State* l, int arg_nb);
  103. static const LuaObjectLibrary* GetLib();
  104. //-------------------------------------------------------------------------
  105. public:
  106. //-- Setup command
  107. static int AddLight(lua_State* l);
  108. static int SetupScene(lua_State* l);
  109. //-- main funcs
  110. static int SetPosition(lua_State* l);
  111. static int SetLookAt(lua_State* l);
  112. static int SetColor(lua_State* l);
  113. static int Show(lua_State* l);
  114. static int Hide(lua_State* l);
  115. static int Toggle(lua_State* l);
  116. //-- Setup command
  117. void AddLight(LightType type);
  118. void SetupScene();
  119. //-- main funcs
  120. void SetPosition(vec3& v);
  121. void SetLookAt(vec3& v);
  122. void SetColor(vec4& c);
  123. void Show(SceneSetup::Display const& d);
  124. void Hide(SceneSetup::Display const& d);
  125. void Toggle(SceneSetup::Display const& d);
  126. private:
  127. SceneSetup* m_setup = nullptr;
  128. };
  129. //-----------------------------------------------------------------------------
  130. class SceneSetupLuaLoader : public LuaLoader
  131. {
  132. friend class SceneSetupLuaObject;
  133. public:
  134. SceneSetupLuaLoader();
  135. virtual ~SceneSetupLuaLoader();
  136. //Virtual Store lua object ------------------------------------------------
  137. virtual void Store(LuaObject* obj);
  138. array<SceneSetupLuaObject*>& GetInstances();
  139. //-------------------------------------------------------------------------
  140. protected:
  141. static void RegisterSetup(SceneSetup* setup);
  142. static bool GetRegisteredSetups(map<std::string, SceneSetup*>& setups);
  143. public:
  144. bool GetLoadedSetups(map<std::string, SceneSetup*>& setups);
  145. private:
  146. static map<std::string, SceneSetup*> m_setups;
  147. };
  148. /*
  149. addlight { return token::T_ADDLIGHT; }
  150. position { return token::T_OBJPOSITION; }
  151. lookat { return token::T_OBJLOOKAT; }
  152. color { return token::T_OBJCOLOR; }
  153. clearcolor { return token::T_CLEARCOLOR; }
  154. showgizmo { return token::T_SHOWGIZMO; }
  155. showlight { return token::T_SHOWLIGHT; }
  156. custom { return token::T_CUSTOMCMD; }
  157. custom { return token::T_CUSTOMCMD; }
  158. light_command:
  159. T_ADDLIGHT { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT"; }
  160. | T_ADDLIGHT fv { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT";
  161. uc.m_sstp.m_lights.last()->SetType(LightType($2)); }
  162. | T_ADDLIGHT svv { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT";
  163. uc.m_sstp.m_lights.last()->SetType(FindValue<LightType>($2)); }
  164. ;
  165. setup_command:
  166. T_OBJPOSITION v3 { if (uc.m_last_cmd == "ADDLIGHT")
  167. uc.m_sstp.m_lights.last()->SetPosition(vec3($2[0], $2[1], $2[2])); }
  168. | T_OBJLOOKAT v3 { if (uc.m_last_cmd == "ADDLIGHT")
  169. {
  170. } }
  171. | T_OBJCOLOR v4{ if (uc.m_last_cmd == "ADDLIGHT")
  172. uc.m_sstp.m_lights.last()->SetColor(vec4($2[0], $2[1], $2[2], $2[3])); }
  173. | T_OBJCOLOR COLOR{ uint32_t x = $2;
  174. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  175. vec4 vv = vec4(v) * (1.f / 255.f);
  176. if (uc.m_last_cmd == "ADDLIGHT")
  177. uc.m_sstp.m_lights.last()->SetColor(vv); }
  178. ;
  179. scene_command:
  180. T_CLEARCOLOR v4{ uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], $2[3]); }
  181. T_CLEARCOLOR v3{ uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], 1.f); }
  182. | T_CLEARCOLOR COLOR{ uint32_t x = $2; ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  183. uc.m_sstp.m_clear_color = vec4(v) * (1.f / 255.f); }
  184. | T_SHOWGIZMO bv{ uc.m_sstp.m_show_gizmo = $2; }
  185. | T_SHOWLIGHT bv{ uc.m_sstp.m_show_lights = $2; }
  186. ;
  187. custom_command:
  188. T_CUSTOMCMD svv sv{ uc.m_sstp.m_custom_cmd.push($2, $3); }
  189. ;
  190. */
  191. } /* namespace lol */