From 8ce4d4f7c7a39e0e4e218a655665050c0a7cb688 Mon Sep 17 00:00:00 2001 From: Miepee Date: Sat, 18 May 2024 12:44:02 +0200 Subject: [PATCH] Make logbook accept color Fixes #198 Co-Authored-By: Jesright73 <79653967+Jesright73@users.noreply.github.com> --- YAMS-LIB/Program.cs | 3 + YAMS-LIB/patches/ColoredLogBook.cs | 266 +++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 YAMS-LIB/patches/ColoredLogBook.cs diff --git a/YAMS-LIB/Program.cs b/YAMS-LIB/Program.cs index 4e1565a..1ecb198 100644 --- a/YAMS-LIB/Program.cs +++ b/YAMS-LIB/Program.cs @@ -159,6 +159,9 @@ public static void Main(string am2rPath, string outputAm2rPath, string jsonPath) gmData.Code.ByName("gml_RoomCC_rm_a0h01_3762_Create").AppendGMLInCode("instance_destroy()"); gmData.Code.ByName("gml_Room_rm_a0h01_Create").AppendGMLInCode("tile_layer_delete(-119)"); + // Make Logbook colored + ColoredLogBook.Apply(gmData, decompileContext, seedObject); + // For pause menu, draw now the same as equipment menu because doing determining what max total health/missiles/etc. are would be spoilery and insane to figure out UndertaleCode? ssDraw = gmData.Code.ByName("gml_Object_oSS_Fg_Draw_0"); ssDraw.ReplaceGMLInCode("(string(global.etanks) + \"/10\")", "( string(ceil(global.playerhealth)) + \"/\" + string(global.maxhealth) )"); diff --git a/YAMS-LIB/patches/ColoredLogBook.cs b/YAMS-LIB/patches/ColoredLogBook.cs new file mode 100644 index 0000000..f564376 --- /dev/null +++ b/YAMS-LIB/patches/ColoredLogBook.cs @@ -0,0 +1,266 @@ +using UndertaleModLib; +using UndertaleModLib.Decompiler; + +namespace YAMS_LIB.patches; + +public class ColoredLogBook +{ + public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileContext, SeedObject seedObject) + { + gmData.Scripts.AddScript("complex_text", + """ + var textqueue, textlength, currpos, temppos, currchar, codechar, codeword, printxstart, printystart, printx, printy, edge, textcol, temptext, tempchar, loop1, loop2, lineheight, newline, prevtextcol, colval; + printxstart = argument0 + printystart = argument1 + printx = printxstart + printy = printystart + textqueue = argument2 + textlength = string_length(textqueue) + currpos = 1 + temppos = 1 + currchar = "" + codechar = "" + codeword = "" + lineheight = argument3 + edge = printxstart + argument4 + textcol = draw_get_colour() + prevtextcol = textcol + colval[2] = 0 + colval[1] = 0 + colval[0] = 0 + loop1 = 0 + loop2 = 0 + newline = 0 + while (currpos <= (textlength)) + { + newline = 0 + currchar = string_char_at(textqueue, currpos) + if (currchar == " ") + { + temppos = currpos + 1 + temptext = "" + loop1 = 1 + while (loop1 == 1) + { + tempchar = string_char_at(textqueue, temppos) + if (tempchar == "{") + { + loop2 = 1 + while (loop2 == 1) + { + if (tempchar == "}") + loop2 = 0 + + temppos++ + tempchar = string_char_at(textqueue, temppos) + if temppos >= (textlength) + { + loop1 = 0 + loop2 = 0 + } + } + } + temptext += tempchar + if (temppos > (textlength) || tempchar == " " || tempchar == "#") + loop1 = 0 + else + temppos++ + } + if ((printx + string_width(temptext)) > edge) + { + //Set currchar to blank so the space isn't drawn after the new line. + currchar = "" + newline = 1 + } + } + else if (currchar == "#") + { + currchar = "" + newline = 1 + } + else if (currchar == "{") + { + currchar = "" + codeword = "" + loop1 = 1 + loop2 = 1 + while (currpos < (textlength + 1) && loop1 == 1) + { + currpos++ + codechar = string_char_at(textqueue, currpos) + if (codechar == "}") + loop1 = 0 + // CRASH if bracket is open to end of string + else if (codechar == "") + { + show_error("OPEN BRACKET", true) + } + else if (codechar == "C") + { + temppos = 2 + repeat (3) + { + codeword = "" + repeat (3) + { + currpos++ + codechar = string_char_at(textqueue, currpos) + // CRASH if not a number. + if (ord(codechar) < 48 || ord(codechar) > 57) + { + show_error("COLOR CODE: EXPECTED A NUMBER, GOT `" + codechar + "`", true) + } + codeword += codechar + } + colval[temppos] = min(real(codeword), 255) + temppos -= 1 + } + textcol = make_colour_rgb(colval[2], colval[1], colval[0]) + } + else + { + codeword += codechar + if (loop2 == 1) + { + //Default color set here, before the switch. Adding a Default case in the switch changed some valid colors to be white. + textcol = c_white + switch codeword + { + case "aqua": + textcol = c_aqua + loop2 = 0 + break + case "black": + textcol = c_black + loop2 = 0 + break + case "blue": + textcol = c_blue + loop2 = 0 + break + case "dkgray": + textcol = c_dkgray + loop2 = 0 + break + case "fuchsia": + textcol = c_fuchsia + loop2 = 0 + break + case "gray": + textcol = c_gray + loop2 = 0 + break + case "green": + textcol = c_green + loop2 = 0 + break + case "lime": + textcol = c_lime + loop2 = 0 + break + case "ltgray": + textcol = c_ltgray + loop2 = 0 + break + case "maroon": + textcol = c_maroon + loop2 = 0 + break + case "navy": + textcol = c_navy + loop2 = 0 + break + case "olive": + textcol = c_olive + loop2 = 0 + break + case "orange": + textcol = c_orange + loop2 = 0 + break + case "purple": + textcol = c_purple + loop2 = 0 + break + case "red": + textcol = c_red + loop2 = 0 + break + case "silver": + textcol = c_silver + loop2 = 0 + break + case "teal": + textcol = c_teal + loop2 = 0 + break + case "white": + textcol = c_white + loop2 = 0 + break + case "yellow": + textcol = c_yellow + loop2 = 0 + break + } + + } + } + } + } + // CRASH if right bracket has no corresponding left bracket. + else if (currchar == "}") + show_error("UNEXPECTED END BRACKET AT Pos:" + string(currpos), true) + else if ((printx + string_width(currchar)) > edge) + newline = 1 + if (newline == 1) + { + printx = printxstart + printy += lineheight + } + draw_set_colour(textcol) + //Only draw the current character if it is not blank + if (currchar != "") + { + draw_text(printx, printy, currchar) + printx += string_width(currchar) + } + currpos++ + } + //Reset draw colour to value from before this script ran. + draw_set_colour(prevtextcol) + + """); + + gmData.Scripts.AddScript("get_returned_text_of_complex_text", + """ + var tempWord, tempChar, openedBracket, textQueue, textLength, currPos; + textQueue = argument0; + textLength = string_length(textQueue); + currPos = 0; + tempWord = ""; + tempChar = ""; + openedBracket = false; + while (currPos <= (textLength)) + { + tempChar = string_char_at(textQueue, currPos); + // Technically should error if there's something like "{red{pink{}}" but I don't care, since complex_text would error on that. + if (tempChar == "{") + openedBracket = true; + + if (!openedBracket) + tempWord += tempChar; + + if (tempChar == "}") + openedBracket = false; + + currPos++; + } + return tempWord; + """); + + gmData.Code.ByName("gml_Object_oLogScreenControl_Other_10").ReplaceGMLInCode("draw_text_ext", "complex_text"); + gmData.Code.ByName("gml_Object_oLogScreenControl_Other_10").ReplaceGMLInCode("string_height_ext(logtext", "string_height_ext(get_returned_text_of_complex_text(logtext)"); + gmData.Code.ByName("gml_Object_oLogScreenControl_Other_11").ReplaceGMLInCode("draw_text_ext", "complex_text"); + } +}