Browse Source

Lua: Set `warn` function

Previously, warnings went nowhere.
pull/6769/head
Gleb Mazovetskiy 2 years ago committed by Anders Jenbo
parent
commit
dbfa204944
  1. 1
      CMake/Assets.cmake
  2. BIN
      Packaging/resources/assets/fonts/gamedialogyellow.trn
  3. 49
      Source/DiabloUI/ui_flags.hpp
  4. 7
      Source/engine/render/text_render.cpp
  5. 5
      Source/engine/render/text_render.hpp
  6. 11
      Source/lua/lua.cpp
  7. 11
      Source/lua/repl.cpp
  8. 13
      Source/panels/console.cpp
  9. 1
      Source/panels/console.hpp

1
CMake/Assets.cmake

@ -115,6 +115,7 @@ set(devilutionx_assets
fonts/buttonface.trn fonts/buttonface.trn
fonts/buttonpushed.trn fonts/buttonpushed.trn
fonts/gamedialogwhite.trn fonts/gamedialogwhite.trn
fonts/gamedialogyellow.trn
fonts/gamedialogred.trn fonts/gamedialogred.trn
fonts/golduis.trn fonts/golduis.trn
fonts/goldui.trn fonts/goldui.trn

BIN
Packaging/resources/assets/fonts/gamedialogyellow.trn

Binary file not shown.

49
Source/DiabloUI/ui_flags.hpp

@ -22,32 +22,33 @@ enum class UiFlags : uint32_t {
ColorUiGoldDark = 1 << 8, ColorUiGoldDark = 1 << 8,
ColorUiSilverDark = 1 << 9, ColorUiSilverDark = 1 << 9,
ColorDialogWhite = 1 << 10, ColorDialogWhite = 1 << 10,
ColorDialogRed = 1 << 11, ColorDialogYellow = 1 << 11,
ColorYellow = 1 << 12, ColorDialogRed = 1 << 12,
ColorGold = 1 << 13, ColorYellow = 1 << 13,
ColorBlack = 1 << 14, ColorGold = 1 << 14,
ColorWhite = 1 << 15, ColorBlack = 1 << 15,
ColorWhitegold = 1 << 16, ColorWhite = 1 << 16,
ColorRed = 1 << 17, ColorWhitegold = 1 << 17,
ColorBlue = 1 << 18, ColorRed = 1 << 18,
ColorOrange = 1 << 19, ColorBlue = 1 << 19,
ColorButtonface = 1 << 20, ColorOrange = 1 << 20,
ColorButtonpushed = 1 << 21, ColorButtonface = 1 << 21,
ColorButtonpushed = 1 << 22,
AlignCenter = 1 << 22,
AlignRight = 1 << 23, AlignCenter = 1 << 23,
VerticalCenter = 1 << 24, AlignRight = 1 << 24,
VerticalCenter = 1 << 25,
KerningFitSpacing = 1 << 25,
KerningFitSpacing = 1 << 26,
ElementDisabled = 1 << 26,
ElementHidden = 1 << 27, ElementDisabled = 1 << 27,
ElementHidden = 1 << 28,
PentaCursor = 1 << 28,
Outlined = 1 << 29, PentaCursor = 1 << 29,
Outlined = 1 << 30,
/** @brief Ensures that the if current element is active that the next element is also visible. */ /** @brief Ensures that the if current element is active that the next element is also visible. */
NeedsNextElement = 1 << 30, NeedsNextElement = 1U << 31U,
// clang-format on // clang-format on
}; };
use_enum_as_flags(UiFlags); use_enum_as_flags(UiFlags);

7
Source/engine/render/text_render.cpp

@ -46,7 +46,7 @@ constexpr std::array<int, 6> LineHeights = { 12, 26, 38, 42, 50, 22 };
constexpr int SmallFontTallLineHeight = 16; constexpr int SmallFontTallLineHeight = 16;
std::array<int, 6> BaseLineOffset = { -3, -2, -3, -6, -7, 3 }; std::array<int, 6> BaseLineOffset = { -3, -2, -3, -6, -7, 3 };
std::array<const char *, 18> ColorTranslations = { std::array<const char *, 19> ColorTranslations = {
"fonts\\goldui.trn", "fonts\\goldui.trn",
"fonts\\grayui.trn", "fonts\\grayui.trn",
"fonts\\golduis.trn", "fonts\\golduis.trn",
@ -68,10 +68,11 @@ std::array<const char *, 18> ColorTranslations = {
"fonts\\buttonface.trn", "fonts\\buttonface.trn",
"fonts\\buttonpushed.trn", "fonts\\buttonpushed.trn",
"fonts\\gamedialogwhite.trn", "fonts\\gamedialogwhite.trn",
"fonts\\gamedialogyellow.trn",
"fonts\\gamedialogred.trn", "fonts\\gamedialogred.trn",
}; };
std::array<std::optional<std::array<uint8_t, 256>>, 18> ColorTranslationsData; std::array<std::optional<std::array<uint8_t, 256>>, 19> ColorTranslationsData;
text_color GetColorFromFlags(UiFlags flags) text_color GetColorFromFlags(UiFlags flags)
{ {
@ -97,6 +98,8 @@ text_color GetColorFromFlags(UiFlags flags)
return ColorUiSilverDark; return ColorUiSilverDark;
if (HasAnyOf(flags, UiFlags::ColorDialogWhite)) if (HasAnyOf(flags, UiFlags::ColorDialogWhite))
return gbRunGame ? ColorInGameDialogWhite : ColorDialogWhite; return gbRunGame ? ColorInGameDialogWhite : ColorDialogWhite;
if (HasAnyOf(flags, UiFlags::ColorDialogYellow))
return ColorInGameDialogYellow;
if (HasAnyOf(flags, UiFlags::ColorDialogRed)) if (HasAnyOf(flags, UiFlags::ColorDialogRed))
return ColorInGameDialogRed; return ColorInGameDialogRed;
if (HasAnyOf(flags, UiFlags::ColorYellow)) if (HasAnyOf(flags, UiFlags::ColorYellow))

5
Source/engine/render/text_render.hpp

@ -55,8 +55,9 @@ enum text_color : uint8_t {
ColorButtonface, ColorButtonface,
ColorButtonpushed, ColorButtonpushed,
ColorInGameDialogWhite, // Dialog white in-game ColorInGameDialogWhite, // Dialog white in-game
ColorInGameDialogRed, // Dialog red in-game ColorInGameDialogYellow, // Dialog yellow in-game
ColorInGameDialogRed, // Dialog red in-game
}; };
constexpr GameFontTables GetFontSizeFromUiFlags(UiFlags flags) constexpr GameFontTables GetFontSizeFromUiFlags(UiFlags flags)

11
Source/lua/lua.cpp

@ -89,6 +89,16 @@ int LuaPrint(lua_State *state)
return 0; return 0;
} }
void LuaWarn(void *userData, const char *message, int continued)
{
static std::string warnBuffer;
warnBuffer.append(message);
if (continued != 0)
return;
LogWarn("{}", warnBuffer);
warnBuffer.clear();
}
bool CheckResult(sol::protected_function_result result, bool optional) bool CheckResult(sol::protected_function_result result, bool optional)
{ {
const bool valid = result.valid(); const bool valid = result.valid();
@ -141,6 +151,7 @@ void LuaInitialize()
.compiledScripts = {}, .compiledScripts = {},
}); });
sol::state &lua = CurrentLuaState->sol; sol::state &lua = CurrentLuaState->sol;
lua_setwarnf(lua.lua_state(), LuaWarn, /*ud=*/nullptr);
lua.open_libraries( lua.open_libraries(
sol::lib::base, sol::lib::base,
sol::lib::package, sol::lib::package,

11
Source/lua/repl.cpp

@ -20,6 +20,16 @@ namespace {
std::optional<sol::environment> replEnv; std::optional<sol::environment> replEnv;
void LuaConsoleWarn(void *userData, const char *message, int continued)
{
static std::string warnBuffer;
warnBuffer.append(message);
if (continued != 0)
return;
PrintWarningToConsole(warnBuffer);
warnBuffer.clear();
}
int LuaPrintToConsole(lua_State *state) int LuaPrintToConsole(lua_State *state)
{ {
std::string result; std::string result;
@ -41,6 +51,7 @@ void CreateReplEnvironment()
sol::state &lua = GetLuaState(); sol::state &lua = GetLuaState();
replEnv.emplace(lua, sol::create, lua.globals()); replEnv.emplace(lua, sol::create, lua.globals());
replEnv->set("print", LuaPrintToConsole); replEnv->set("print", LuaPrintToConsole);
lua_setwarnf(replEnv->lua_state(), LuaConsoleWarn, /*ud=*/nullptr);
} }
sol::environment &ReplEnvironment() sol::environment &ReplEnvironment()

13
Source/panels/console.cpp

@ -58,6 +58,7 @@ struct ConsoleLine {
Help, Help,
Input, Input,
Output, Output,
Warning,
Error Error
}; };
@ -98,6 +99,7 @@ bool FirstRender;
constexpr UiFlags TextUiFlags = UiFlags::FontSizeDialog; constexpr UiFlags TextUiFlags = UiFlags::FontSizeDialog;
constexpr UiFlags InputTextUiFlags = TextUiFlags | UiFlags::ColorDialogWhite; constexpr UiFlags InputTextUiFlags = TextUiFlags | UiFlags::ColorDialogWhite;
constexpr UiFlags OutputTextUiFlags = TextUiFlags | UiFlags::ColorDialogWhite; constexpr UiFlags OutputTextUiFlags = TextUiFlags | UiFlags::ColorDialogWhite;
constexpr UiFlags WarningTextUiFlags = TextUiFlags | UiFlags::ColorDialogYellow;
constexpr UiFlags ErrorTextUiFlags = TextUiFlags | UiFlags::ColorDialogRed; constexpr UiFlags ErrorTextUiFlags = TextUiFlags | UiFlags::ColorDialogRed;
constexpr int TextSpacing = 0; constexpr int TextSpacing = 0;
@ -210,6 +212,10 @@ void DrawConsoleLines(const Surface &out)
DrawString(out, line, { 0, lineYEnd }, DrawString(out, line, { 0, lineYEnd },
TextRenderOptions { .flags = OutputTextUiFlags, .spacing = TextSpacing }); TextRenderOptions { .flags = OutputTextUiFlags, .spacing = TextSpacing });
break; break;
case ConsoleLine::Warning:
DrawString(out, line, { 0, lineYEnd },
TextRenderOptions { .flags = WarningTextUiFlags, .spacing = TextSpacing });
break;
case ConsoleLine::Error: case ConsoleLine::Error:
DrawString(out, line, { 0, lineYEnd }, DrawString(out, line, { 0, lineYEnd },
TextRenderOptions { .flags = ErrorTextUiFlags, .spacing = TextSpacing }); TextRenderOptions { .flags = ErrorTextUiFlags, .spacing = TextSpacing });
@ -292,7 +298,7 @@ void NextInput()
bool IsHistoryOutputLine(const ConsoleLine &line) bool IsHistoryOutputLine(const ConsoleLine &line)
{ {
return !line.text.empty() return !line.text.empty()
&& (line.type == ConsoleLine::Output || line.type == ConsoleLine::Error) && (line.type == ConsoleLine::Output || line.type == ConsoleLine::Warning || line.type == ConsoleLine::Error)
&& (HistoryIndex == -1 && (HistoryIndex == -1
|| GetConsoleLineFromEnd(HistoryIndex).textWithoutPrompt() != line.text); || GetConsoleLineFromEnd(HistoryIndex).textWithoutPrompt() != line.text);
} }
@ -471,5 +477,10 @@ void PrintToConsole(std::string_view text)
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Output, .text = std::string(text) }); AddConsoleLine(ConsoleLine { .type = ConsoleLine::Output, .text = std::string(text) });
} }
void PrintWarningToConsole(std::string_view text)
{
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Warning, .text = std::string(text) });
}
} // namespace devilution } // namespace devilution
#endif // _DEBUG #endif // _DEBUG

1
Source/panels/console.hpp

@ -14,6 +14,7 @@ void OpenConsole();
bool ConsoleHandleEvent(const SDL_Event &event); bool ConsoleHandleEvent(const SDL_Event &event);
void DrawConsole(const Surface &out); void DrawConsole(const Surface &out);
void PrintToConsole(std::string_view text); void PrintToConsole(std::string_view text);
void PrintWarningToConsole(std::string_view text);
} // namespace devilution } // namespace devilution
#endif // _DEBUG #endif // _DEBUG

Loading…
Cancel
Save