| 143 | |
| 144 | #ifndef NDK_SERVER |
| 145 | void Application::SetupConsole(WindowInfo& info) |
| 146 | { |
| 147 | std::unique_ptr<ConsoleOverlay> overlay = std::make_unique<ConsoleOverlay>(); |
| 148 | |
| 149 | Nz::Vector2ui windowDimensions; |
| 150 | if (info.window->IsValid()) |
| 151 | windowDimensions.Set(info.window->GetWidth(), info.window->GetHeight() / 4); |
| 152 | else |
| 153 | windowDimensions.MakeZero(); |
| 154 | |
| 155 | overlay->console = std::make_unique<Console>(*info.overlayWorld, Nz::Vector2f(windowDimensions), overlay->lua); |
| 156 | |
| 157 | Console& consoleRef = *overlay->console; |
| 158 | |
| 159 | // Redirect logs toward the console |
| 160 | overlay->logSlot.Connect(Nz::Log::OnLogWrite, [&consoleRef] (const Nz::String& str) |
| 161 | { |
| 162 | consoleRef.AddLine(str); |
| 163 | }); |
| 164 | |
| 165 | LuaAPI::RegisterClasses(overlay->lua); |
| 166 | |
| 167 | // Override "print" function to add a line in the console |
| 168 | overlay->lua.PushFunction([&consoleRef] (Nz::LuaState& state) |
| 169 | { |
| 170 | Nz::StringStream stream; |
| 171 | |
| 172 | unsigned int argCount = state.GetStackTop(); |
| 173 | state.GetGlobal("tostring"); |
| 174 | for (unsigned int i = 1; i <= argCount; ++i) |
| 175 | { |
| 176 | state.PushValue(-1); // tostring function |
| 177 | state.PushValue(i); // argument |
| 178 | state.Call(1, 1); |
| 179 | |
| 180 | std::size_t length; |
| 181 | const char* str = state.CheckString(-1, &length); |
| 182 | if (i > 1) |
| 183 | stream << '\t'; |
| 184 | |
| 185 | stream << Nz::String(str, length); |
| 186 | state.Pop(1); |
| 187 | } |
| 188 | |
| 189 | consoleRef.AddLine(stream); |
| 190 | return 0; |
| 191 | }); |
| 192 | overlay->lua.SetGlobal("print"); |
| 193 | |
| 194 | // Define a few base variables to allow our interface to interact with the application |
| 195 | overlay->lua.PushGlobal("Application", Ndk::Application::Instance()); |
| 196 | overlay->lua.PushGlobal("Console", consoleRef.CreateHandle()); |
| 197 | |
| 198 | // Setup a few event callback to handle the console |
| 199 | Nz::EventHandler& eventHandler = info.window->GetEventHandler(); |
| 200 | |
| 201 | overlay->eventSlot.Connect(eventHandler.OnEvent, [&consoleRef] (const Nz::EventHandler*, const Nz::WindowEvent& event) |
| 202 | { |
nothing calls this directly
no test coverage detected