Add togglable console window

This commit is contained in:
Alessandro Proto 2023-02-12 17:32:43 +01:00
parent b477e74141
commit 1b2ad30759
4 changed files with 76 additions and 0 deletions

View file

@ -142,6 +142,11 @@ local function installOS()
promptKey() promptKey()
end end
local function toggleConsole()
local status = getConsole()
setConsole(not status)
end
term.setBlink(false) term.setBlink(false)
local function setupScreen() local function setupScreen()
@ -150,6 +155,10 @@ local function setupScreen()
"Open data folder", "Open data folder",
openDataFolder, openDataFolder,
}, },
{
"Toggle console window",
toggleConsole,
},
{ {
"Install default OS", "Install default OS",
installOS, installOS,

View file

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.Extensions.Bindings;
public partial class Common
{
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
[LibraryImport("kernel32.dll")]
public static partial IntPtr GetConsoleWindow();
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool IsWindowVisible(IntPtr hWnd);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool ShowWindow(IntPtr hWnd, int nCmdShow);
}

View file

@ -54,4 +54,19 @@ public static class GameWindowExtensions
{ {
return window.GetWindowFlags().HasFlag(WindowFlags.SDL_WINDOW_MAXIMIZED); return window.GetWindowFlags().HasFlag(WindowFlags.SDL_WINDOW_MAXIMIZED);
} }
public static void ToggleConsole(this GameWindow _, bool show)
{
var console = Common.GetConsoleWindow();
if (console != IntPtr.Zero)
Common.ShowWindow(console, show ? Common.SW_SHOW : Common.SW_HIDE);
}
public static bool IsConsoleVisible(this GameWindow _)
{
var console = Common.GetConsoleWindow();
if (console != IntPtr.Zero)
return false;
return Common.IsWindowVisible(console);
}
} }

View file

@ -1,5 +1,6 @@
using Capy64.API; using Capy64.API;
using Capy64.Eventing.Events; using Capy64.Eventing.Events;
using Capy64.Extensions;
using Capy64.Runtime.Libraries; using Capy64.Runtime.Libraries;
using KeraLua; using KeraLua;
using System; using System;
@ -113,6 +114,12 @@ internal class RuntimeManager : IPlugin
luaState.Thread.PushCFunction(L_Exit); luaState.Thread.PushCFunction(L_Exit);
luaState.Thread.SetGlobal("exit"); luaState.Thread.SetGlobal("exit");
luaState.Thread.PushCFunction(L_SetConsole);
luaState.Thread.SetGlobal("setConsole");
luaState.Thread.PushCFunction(L_GetConsole);
luaState.Thread.SetGlobal("getConsole");
var status = luaState.Thread.LoadFile("Assets/bios.lua"); var status = luaState.Thread.LoadFile("Assets/bios.lua");
if (status != LuaStatus.OK) if (status != LuaStatus.OK)
{ {
@ -203,6 +210,26 @@ internal class RuntimeManager : IPlugin
return 0; return 0;
} }
private static int L_SetConsole(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var status = L.ToBoolean(1);
_game.Window.ToggleConsole(status);
return 0;
}
private static int L_GetConsole(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var status = _game.Window.IsConsoleVisible();
L.PushBoolean(status);
return 1;
}
private void OnInit(object sender, EventArgs e) private void OnInit(object sender, EventArgs e)
{ {
Start(); Start();