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.
 
 
 

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