Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

201 righe
5.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2017—2019 Sam Hocevar <sam@hocevar.net>
  5. // © 2009—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  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. #include <string>
  15. #include <map>
  16. #include <memory>
  17. //
  18. // The Imgui integration
  19. //
  20. #define IM_VEC2_CLASS_EXTRA \
  21. ImVec2(const lol::vec2 &v) { x = v.x; y = v.y; } \
  22. ImVec2(const lol::ivec2 &v) : ImVec2(lol::vec2(v)) { } \
  23. operator lol::vec2() const { return lol::vec2(x, y); } \
  24. operator lol::ivec2() const { return lol::ivec2(lol::vec2(x, y)); }
  25. #define IM_VEC4_CLASS_EXTRA \
  26. ImVec4(const lol::vec4 &v) { x = v.x; y = v.y; z = v.z; w = v.w; } \
  27. ImVec4(const lol::ivec4 &v) : ImVec4(lol::vec4(v)) { } \
  28. operator lol::vec4() const { return lol::vec4(x, y, z, w); } \
  29. operator lol::ivec4() const { return lol::ivec4(lol::vec4(x, y, z, w)); }
  30. #include "imgui.h"
  31. #undef IM_VEC2_CLASS_EXTRA
  32. #undef IM_VEC4_CLASS_EXTRA
  33. namespace lol
  34. {
  35. class gui : public Entity
  36. {
  37. public:
  38. gui(ImFontAtlas *shared_font_atlas);
  39. ~gui();
  40. std::string GetName() const { return "<gui>"; }
  41. static void init(ImFontAtlas *shared_font_atlas = nullptr);
  42. static void shutdown();
  43. static std::string clipboard();
  44. static void refresh_fonts();
  45. private:
  46. typedef Entity super;
  47. //ImGuiKeyBase ------------------------------------------------------------
  48. struct key_base : public StructSafeEnum
  49. {
  50. enum Type
  51. {
  52. KEY_START,
  53. Tab = KEY_START,
  54. LeftArrow,
  55. RightArrow,
  56. UpArrow,
  57. DownArrow,
  58. Home,
  59. End,
  60. Delete,
  61. Backspace,
  62. Enter,
  63. Escape,
  64. A, C, V, X, Y, Z,
  65. LShift, RShift, LCtrl, RCtrl,
  66. KEY_END,
  67. MOUSE_KEY_START = KEY_END,
  68. LeftClick = MOUSE_KEY_START,
  69. RightClick,
  70. MiddleClick,
  71. Focus,
  72. MOUSE_KEY_END,
  73. MAX = MOUSE_KEY_END,
  74. };
  75. protected:
  76. virtual bool BuildEnumMap(std::map<int64_t, std::string>& enum_map)
  77. {
  78. enum_map[Tab] = g_name_key_Tab;
  79. enum_map[LeftArrow] = g_name_key_Left;
  80. enum_map[RightArrow] = g_name_key_Right;
  81. enum_map[UpArrow] = g_name_key_Up;
  82. enum_map[DownArrow] = g_name_key_Down;
  83. enum_map[Home] = g_name_key_Home;
  84. enum_map[End] = g_name_key_End;
  85. enum_map[Delete] = g_name_key_Delete;
  86. enum_map[Backspace] = g_name_key_Backspace;
  87. enum_map[Enter] = g_name_key_Return;
  88. enum_map[Escape] = g_name_key_Escape;
  89. enum_map[A] = g_name_key_A;
  90. enum_map[C] = g_name_key_C;
  91. enum_map[V] = g_name_key_V;
  92. enum_map[X] = g_name_key_X;
  93. enum_map[Y] = g_name_key_Y;
  94. enum_map[Z] = g_name_key_Z;
  95. enum_map[LShift] = g_name_key_LShift;
  96. enum_map[RShift] = g_name_key_RShift;
  97. enum_map[LCtrl] = g_name_key_LCtrl;
  98. enum_map[RCtrl] = g_name_key_RCtrl;
  99. enum_map[LeftClick] = g_name_mouse_key_left;
  100. enum_map[RightClick] = g_name_mouse_key_right;
  101. enum_map[MiddleClick] = g_name_mouse_key_middle;
  102. enum_map[Focus] = g_name_mouse_key_in_screen;
  103. return true;
  104. }
  105. };
  106. typedef SafeEnum<key_base> key_enum;
  107. //ImGuiKeyBase ------------------------------------------------------------
  108. struct axis_base : public StructSafeEnum
  109. {
  110. enum Type
  111. {
  112. MOUSE_AXIS_START = 0,
  113. Scroll = MOUSE_AXIS_START,
  114. MOUSE_AXIS_END,
  115. MAX = MOUSE_AXIS_END,
  116. };
  117. protected:
  118. virtual bool BuildEnumMap(std::map<int64_t, std::string>& enum_map)
  119. {
  120. enum_map[Scroll] = g_name_mouse_axis_scroll;
  121. return true;
  122. }
  123. };
  124. typedef SafeEnum<axis_base> axis_enum;
  125. protected:
  126. virtual void tick_game(float seconds);
  127. virtual void tick_draw(float seconds, Scene &scene);
  128. static void static_set_clipboard(void *data, const char* text);
  129. static const char* static_get_clipboard(void *data);
  130. static void static_render_draw_lists(ImDrawData* draw_data);
  131. void render_draw_lists(ImDrawData* draw_data);
  132. struct Uniform
  133. {
  134. Uniform() { }
  135. Uniform(ShaderVar var) { m_var = var; }
  136. std::string tostring() const { return m_var.tostring(); }
  137. operator ShaderVar() const { return m_var; }
  138. operator ShaderUniform() const { return m_uniform; }
  139. //--
  140. ShaderVar m_var;
  141. ShaderUniform m_uniform;
  142. };
  143. //-------------------------------------------------------------------------
  144. TextureImage* m_font = nullptr;
  145. ShaderBuilder m_builder = ShaderBuilder("imgui_shader", "120");
  146. std::shared_ptr<Shader> m_shader = nullptr;
  147. Uniform m_ortho;
  148. Uniform m_texture;
  149. array<ShaderAttrib> m_attribs;
  150. std::shared_ptr<VertexDeclaration> m_vdecl;
  151. Controller* m_controller = nullptr;
  152. InputDevice* m_mouse = nullptr;
  153. InputDevice* m_keyboard = nullptr;
  154. InputProfile m_profile;
  155. //std::map<ImGuiKey_, key_enum> m_keys;
  156. std::string m_clipboard;
  157. class primitive : public PrimitiveRenderer
  158. {
  159. public:
  160. primitive() { }
  161. virtual void Render(Scene& scene, std::shared_ptr<PrimitiveSource> primitive);
  162. };
  163. };
  164. } /* namespace lol */