MxEngine
RuntimeEditor.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/STL/MxString.h"
32 #include "Utilities/Memory/Memory.h"
33 #include "Core/MxObject/MxObject.h"
34 #include "Core/Event/Events/KeyEvent.h"
35 #include <functional>
36 
37 namespace MxEngine
38 {
39  class GraphicConsole;
40 
41  #if defined(MXENGINE_USE_PYTHON)
42  class PythonEngine;
43  #endif
44 
45 
47  {
48  #if defined(MXENGINE_USE_PYTHON)
49  using ScriptEngine = PythonEngine;
50  #else
51  using ScriptEngine = int; // stub
52  #endif
53 
54  ScriptEngine* engine;
55  GraphicConsole* console;
56  Vector2 cachedWindowSize{ 0.0f };
57  bool shouldRender = false;
58  bool useDefaultFrameBufferCached = false;
59 
60  MxVector<std::function<void(MxObject&)>> componentEditorCallbacks;
61  MxVector<std::function<void(MxObject&)>> componentAdderCallbacks;
62  MxVector<const char*> componentNames;
63  public:
64  RuntimeEditor();
65  RuntimeEditor(const RuntimeEditor&) = delete;
66  RuntimeEditor(RuntimeEditor&&) = default;
67  ~RuntimeEditor();
68 
69  void Log(const MxString& message);
70  void ClearLog();
71  void PrintHistory();
72  void OnRender();
73  void SetSize(const Vector2& size);
74  void Toggle(bool isVisible = true);
75  void AddKeyBinding(KeyCode openKey);
76 
77  ScriptEngine& GetEngine();
78  Vector2 GetSize() const;
79  bool IsToggled() const;
80 
81  void ExecuteScript(const MxString& code);
82  bool HasErrorsInExecution() const;
83  const MxString& GetLastErrorMessage() const;
84 
85  template<typename T>
86  void RegisterComponentEditor(const char* name, std::function<void(T&)> callback)
87  {
88  this->componentEditorCallbacks.push_back([func = std::move(callback)](MxObject& object)
89  {
90  auto component = object.GetComponent<T>();
91  if (component.IsValid())
92  func(*component);
93  });
94  if constexpr (std::is_default_constructible_v<T>)
95  {
96  this->componentNames.push_back(name);
97  this->componentAdderCallbacks.push_back([](MxObject& object)
98  {
99  if(!object.HasComponent<T>())
100  object.AddComponent<T>();
101  });
102  }
103  }
104 
105  template<typename Func>
106  void RegisterComponentEditor(const char* name, Func&& callback)
107  {
108  this->RegisterComponentEditor(name, std::function{ std::forward<Func>(callback) });
109  }
110  };
111 }
Definition: MxObject.h:40
Definition: GraphicConsole.h:40
Definition: RuntimeEditor.h:46
Definition: Application.cpp:49