From f2dde25d2c9272c45ddf8692bee29dce289ccbc0 Mon Sep 17 00:00:00 2001 From: Arham Jain Date: Wed, 24 Mar 2021 22:03:45 -0400 Subject: [PATCH] fixed bug with listbox Also might fix the font undefined issue on the switch --- src/listbox.moon | 16 +++++++++++----- src/script.moon | 1 - src/text/text.moon | 3 ++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/listbox.moon b/src/listbox.moon index 979117e..dcf9562 100644 --- a/src/listbox.moon +++ b/src/listbox.moon @@ -1,12 +1,17 @@ local * pad = 10 -- can also provide "data" as a part of choices +remove_color = => + w = "" + if type(@) == "string" then return @ + else + for i = 2, #@, 2 + w ..= @[i] + return w create_listbox = => @selected = @selected or 1 if @choices[@selected].onchange @choices[@selected].onchange(@choices[@selected]) - for choice in *@choices - choice.text = lg.newText(font, choice.text) @closable = @closable or false @allow_menu = @allow_menu or false @onclose = @onclose or -> @@ -14,7 +19,8 @@ create_listbox = => local * dispatch "pause" font_height = (text) -> - return math.max(text\getHeight!, @media) + _, count = string.gsub(remove_color(text), "\n", "\n") + return math.max(font\getHeight! * (1 + count), @media) close = -> input_event\remove! draw_event\remove! @@ -41,7 +47,7 @@ create_listbox = => if chosen.left then chosen.left(chosen) return false draw_event = on "draw_choice", -> - w = 3 * pad + _.max([c.text\getWidth! for c in *@choices]) + @media + w = 3 * pad + _.max([font\getWidth(remove_color(c.text)) for c in *@choices]) + @media h, y_selected = pad, 0 for i, c in ipairs @choices h += font_height(c.text) + pad @@ -55,7 +61,7 @@ create_listbox = => lg.setColor(1, 1, 1) if c.media then c.media(x+pad, text_y) if i == @selected then lg.setColor(.506, .631, .757) - lg.draw(c.text, x + 2*pad + @media, text_y) + lg.print(c.text, x + 2*pad + @media, text_y) text_y += pad + font_height(c.text) return false return {:create_listbox} diff --git a/src/script.moon b/src/script.moon index b74eae6..244bc2c 100644 --- a/src/script.moon +++ b/src/script.moon @@ -19,7 +19,6 @@ find_script = (s, file) -> read_file = (s, script_file) -> file = find_script(s, script_file) data = s.fs("#{s.base_dir}script/#{file}") - print file if data == nil then error("Cannot read #{script_file}! Please check the scripts folder/zip") ins = {} for line in string.gmatch(data, "[^\n]+") diff --git a/src/text/text.moon b/src/text/text.moon index 3e25240..04f1fca 100644 --- a/src/text/text.moon +++ b/src/text/text.moon @@ -77,7 +77,8 @@ on "input", => dispatch "next_ins", ins break else if @ == "up" - choices = [text: t, action: -> for t in *backlog] + choices = {} + -- choices = [text: t, action: -> for t in *backlog] for line in *backlog if line.file table.insert(choices, {