Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

235 rader
7.4 KiB

  1. //
  2. // Lol Engine — imGui integration
  3. //
  4. // Copyright © 2009—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #pragma once
  13. #define IM_VEC2_CLASS_EXTRA ImVec2(const lol::vec2 &v) { x = v.x; y = v.y; } \
  14. ImVec2(const lol::ivec2 &v) : ImVec2(lol::vec2(v)) { } \
  15. operator lol::vec2() const { return lol::vec2(x, y); } \
  16. operator lol::ivec2() const { return lol::ivec2(lol::vec2(x, y)); }
  17. #define IM_VEC4_CLASS_EXTRA ImVec4(const lol::vec4 &v) { x = v.x; y = v.y; z = v.z; w = v.w; } \
  18. ImVec4(const lol::ivec4 &v) : ImVec4(lol::vec4(v)) { } \
  19. operator lol::vec4() { return lol::vec4(x, y, z, w); } \
  20. operator lol::ivec4() const { return lol::ivec4(lol::vec4(x, y, z, w)); }
  21. #include "imgui.h"
  22. #undef IM_VEC2_CLASS_EXTRA
  23. #undef IM_VEC4_CLASS_EXTRA
  24. //Imgui extension ---------------------------------------------------------------------------------
  25. typedef int ImGuiSetDock; // condition flags for Set*() // enum ImGuiSetCond_
  26. enum ImGuiSetDock_
  27. {
  28. ImGuiSetDock_TopLeft = 0,
  29. ImGuiSetDock_TopRight = 1,
  30. ImGuiSetDock_BottomLeft = 2,
  31. ImGuiSetDock_BottomRight = 3,
  32. };
  33. namespace ImGui
  34. {
  35. IMGUI_API void SetNextWindowSizeAndDock(const ImVec2& size, ImGuiSetDock dock, ImGuiSetCond cond = 0);
  36. }
  37. //LolImGui ----------------------------------------------------------------------------------------
  38. namespace lol
  39. {
  40. class LolImGui : public Entity
  41. {
  42. typedef Entity super;
  43. //ImGuiKeyBase ------------------------------------------------------------
  44. struct LolImGuiKeyBase : public StructSafeEnum
  45. {
  46. enum Type
  47. {
  48. KEY_START,
  49. Tab = KEY_START,
  50. LeftArrow,
  51. RightArrow,
  52. UpArrow,
  53. DownArrow,
  54. Home,
  55. End,
  56. Delete,
  57. Backspace,
  58. Enter,
  59. Escape,
  60. A,
  61. C,
  62. V,
  63. X,
  64. Y,
  65. Z,
  66. LShift,
  67. RShift,
  68. LCtrl,
  69. RCtrl,
  70. KEY_END,
  71. MOUSE_KEY_START = KEY_END,
  72. LeftClick = MOUSE_KEY_START,
  73. RightClick,
  74. MiddleClick,
  75. Focus,
  76. MOUSE_KEY_END,
  77. MAX = MOUSE_KEY_END,
  78. };
  79. protected:
  80. virtual bool BuildEnumMap(map<int64_t, String>& enum_map)
  81. {
  82. enum_map[Tab] = g_name_key_Tab;
  83. enum_map[LeftArrow] = g_name_key_Left;
  84. enum_map[RightArrow] = g_name_key_Right;
  85. enum_map[UpArrow] = g_name_key_Up;
  86. enum_map[DownArrow] = g_name_key_Down;
  87. enum_map[Home] = g_name_key_Home;
  88. enum_map[End] = g_name_key_End;
  89. enum_map[Delete] = g_name_key_Delete;
  90. enum_map[Backspace] = g_name_key_Backspace;
  91. enum_map[Enter] = g_name_key_Return;
  92. enum_map[Escape] = g_name_key_Escape;
  93. enum_map[A] = g_name_key_A;
  94. enum_map[C] = g_name_key_C;
  95. enum_map[V] = g_name_key_V;
  96. enum_map[X] = g_name_key_X;
  97. enum_map[Y] = g_name_key_Y;
  98. enum_map[Z] = g_name_key_Z;
  99. enum_map[LShift] = g_name_key_LShift;
  100. enum_map[RShift] = g_name_key_RShift;
  101. enum_map[LCtrl] = g_name_key_LCtrl;
  102. enum_map[RCtrl] = g_name_key_RCtrl;
  103. enum_map[LeftClick] = g_name_mouse_key_left;
  104. enum_map[RightClick] = g_name_mouse_key_right;
  105. enum_map[MiddleClick] = g_name_mouse_key_middle;
  106. enum_map[Focus] = g_name_mouse_key_in_screen;
  107. return true;
  108. }
  109. };
  110. typedef SafeEnum<LolImGuiKeyBase> LolImGuiKey;
  111. //ImGuiKeyBase ------------------------------------------------------------
  112. struct LolImGuiAxisBase : public StructSafeEnum
  113. {
  114. enum Type
  115. {
  116. MOUSE_AXIS_START = 0,
  117. Scroll = MOUSE_AXIS_START,
  118. MOUSE_AXIS_END,
  119. MAX = MOUSE_AXIS_END,
  120. };
  121. protected:
  122. virtual bool BuildEnumMap(map<int64_t, String>& enum_map)
  123. {
  124. enum_map[Scroll] = g_name_mouse_axis_scroll;
  125. return true;
  126. }
  127. };
  128. typedef SafeEnum<LolImGuiAxisBase> LolImGuiAxis;
  129. public:
  130. //-------------------------------------------------------------------------
  131. LolImGui();
  132. ~LolImGui();
  133. char const *GetName() { return "<LolImGui>"; }
  134. //-------------------------------------------------------------------------
  135. static void Init();
  136. static void Shutdown();
  137. //-------------------------------------------------------------------------
  138. static String GetClipboard();
  139. protected:
  140. virtual void TickGame(float seconds);
  141. virtual void TickDraw(float seconds, Scene &scene);
  142. static void SetClipboardCallback(void *data, const char* text);
  143. static const char* GetClipboardCallback(void *data);
  144. static void RenderDrawLists(ImDrawData* draw_data);
  145. void RenderDrawListsMethod(ImDrawData* draw_data);
  146. struct Uniform
  147. {
  148. Uniform() { }
  149. Uniform(ShaderVar var) { m_var = var; }
  150. operator String() { return m_var; }
  151. operator ShaderVar() { return m_var; }
  152. operator ShaderUniform() { return m_uniform; }
  153. //--
  154. ShaderVar m_var;
  155. ShaderUniform m_uniform;
  156. };
  157. //-------------------------------------------------------------------------
  158. TextureImage* m_font = nullptr;
  159. ShaderBuilder m_builder = ShaderBuilder("imgui_shader", "120");
  160. Shader* m_shader = nullptr;
  161. Uniform m_ortho;
  162. Uniform m_texture;
  163. array<ShaderAttrib> m_attribs;
  164. VertexDeclaration* m_vdecl = nullptr;
  165. IndexBuffer* m_ibuff = nullptr;
  166. Controller* m_controller = nullptr;
  167. InputDevice* m_mouse = nullptr;
  168. InputDevice* m_keyboard = nullptr;
  169. InputProfile m_profile;
  170. //map<ImGuiKey_, LolImGuiKey> m_keys;
  171. String m_clipboard;
  172. };
  173. //-----------------------------------------------------------------------------
  174. class PrimitiveLolImGui : public PrimitiveRenderer
  175. {
  176. public:
  177. PrimitiveLolImGui() { }
  178. virtual void Render(Scene& scene, PrimitiveSource* primitive);
  179. };
  180. //bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks);
  181. //void ImGui_ImplGlfw_Shutdown();
  182. //void ImGui_ImplGlfw_NewFrame();
  183. //
  184. //// Use if you want to reset your rendering device without losing ImGui state.
  185. //void ImGui_ImplGlfw_InvalidateDeviceObjects();
  186. //bool ImGui_ImplGlfw_CreateDeviceObjects();
  187. //
  188. //// GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization)
  189. //// Provided here if you want to chain callbacks.
  190. //// You can also handle inputs yourself and use those as a reference.
  191. //void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
  192. //void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
  193. //void ImGui_ImplGlFw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
  194. //void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c);
  195. } /* namespace lol */