Post subject: Can I create a new window with Lua?
Joined: 3/19/2017
Posts: 4
I'm working on a Lua script and have a whole lot of frequently updating info I'd like to be able to display at once, but doing so on the emulator's game display makes the game nigh unplayable. I was hoping there was some sort of function that would let me create a new window of some sort separate from the game's display and print the information there. I found the emulator has a gui function called createcanvas which will make a window with the dimensions I specify appear, but I can't figure our any way to draw on that, or really interact with it in any meaningful way besides just creating it. I also thought maybe gui.DrawNew would let me set my information to display to the canvas or perhaps some other window that it would create, but after searching around a bit it looks like it might just be for drawing to the native emulator size? Honestly, I'm not sure what it's all about but the only values I could input into it without getting an error were "emu" and "native" and neither helped me. So yeah. Is this impossible or am I just overlooking something?
Active player (325)
Joined: 2/23/2005
Posts: 786
Try forms.newform, and take a look at the forms category. It doesn't have a lot of features, and you can't draw on them directly, but you can create labels that can give you additional information.
Masterjun
He/Him
Site Developer, Skilled player (1971)
Joined: 10/12/2010
Posts: 1179
Location: Germany
When create a new canvas you need to save the LuaCanvas it's returning, like so:
Language: lua

local myCoolCanvas = gui.createcanvas(640,480)
As to things you can do with that, looking at the code for the LuaCanvas, you can use SetTitle(), Clear(), Refresh(), DrawRectangle(), and DrawText(). So like:
Language: lua

myCoolCanvas.DrawText(10,50,"sup") -- default color is white so might be hard to see myCoolCanvas.DrawRectangle(64,32,16,16)
Warning: Might glitch to credits I will finish this ACE soon as possible (or will I?)
Joined: 3/19/2017
Posts: 4
I'll check out forms later, but for now what Masterjun explained about the canvas is exactly what I was looking for. Thanks a million.
Editor, Player (163)
Joined: 4/7/2015
Posts: 330
Location: Porto Alegre, RS, Brazil
Masterjun wrote:
default color is white so might be hard to see
Language: lua

-- Background colour in 0xaarrggbb format, use it first before drawing anything myCoolCanvas.Clear(0xFFE9C662) -- Setting a title for the window with any string myCoolCanvas.SetTitle("My Cool Canvas!!!") -- Refreshing to be able to draw each frame (or amount of time your loop takes). -- USE IT AFTER ALL CANVAS DRAWINGS myCoolCanvas.Refresh()
Also, you can print text with different sizes and fonts, just like gui.drawText
DrawText(int x, int y, string message, Color? color = null, int? fontsize = null, string fontfamily = null, string fontstyle = null)
Try different font sizes to see which draws your info better without taking too much space. And try fonts that come with Windows, but not all of the works so you have to test (is nothing is printed, then the font doesn't work here).
Games are basically math with a visual representation of this math, that's why I make the scripts, to re-see games as math. My things: YouTube, GitHub, Pastebin, Twitter
Editor, Player (163)
Joined: 4/7/2015
Posts: 330
Location: Porto Alegre, RS, Brazil
Finally,
Language: lua

left_gap = 144 right_gap = 100 top_gap = 20 bottom_gap = 50 client.SetGameExtraPadding(left_gap, top_gap, right_gap, bottom_gap)
creates these gaps used in the Super Mario World script. You can fit a lot of info there, specially for seeing sprites before they enter the screen or a map of the level, for example.
Games are basically math with a visual representation of this math, that's why I make the scripts, to re-see games as math. My things: YouTube, GitHub, Pastebin, Twitter
Joined: 3/19/2017
Posts: 4
So, I've been using a canvas to print all my text and everything has been working wonderfully. Unfortunately after trying to pretty up my display I've run into a showstopping problem: it seems to be impossible to draw an image from a file to a canvas. I briefly looked into using forms as suggested by CtrlAltDestroy, but these too seem unfit for drawing images. Is there any function I'm overlooking that would allow me to do this? If not, is there some other way to create a window separate from Bizhawk's emulator display that can display both text and .png images?
Editor, Player (163)
Joined: 4/7/2015
Posts: 330
Location: Porto Alegre, RS, Brazil
I asked them in the TASVideos Discord server and they did it! So you can use something like "yourcanvasname.drawImage(...) to draw an image inside it just like in the normal "gui" functions. You can wait until the next official release, or download the current state of development here.
Games are basically math with a visual representation of this math, that's why I make the scripts, to re-see games as math. My things: YouTube, GitHub, Pastebin, Twitter
Joined: 3/19/2017
Posts: 4
Much obliged, but actually I just remembered this thread and was coming back to post that I got it working. I was actually the one who copied those functions from the gui over to the canvas earlier today when I figured out more or less how they worked. Everything is going swimmingly again.
Pokota
He/Him
Joined: 2/5/2014
Posts: 779
Woo! I know at least two people who are going to love this change!
Adventures in Lua When did I get a vest?
Ludko444
He/Him
Joined: 11/6/2016
Posts: 8
Hi. Is possible to create canvas with a lot of text ? I want, that all text will appear when I will have window with small size (text is longer like size of window). Is possible to create window with Slider to down, up, left, right ?
Editor, Player (163)
Joined: 4/7/2015
Posts: 330
Location: Porto Alegre, RS, Brazil
Ludko444 wrote:
Hi. Is possible to create canvas with a lot of text ? I want, that all text will appear when I will have window with small size (text is longer like size of window). Is possible to create window with Slider to down, up, left, right ?
Currently, there's not much you can do, the closest I got was using forms instead of canvas:
Language: lua

local form_width, form_height = 300, 200 local TEST_FORM = forms.newform(form_width, form_height, "TEST") local TEST_STR = "Creates a textbox control on the given form. The caption property will be the initial value of the textbox (default is empty). Width and Height are option, if not specified they will be a default size of 100, 20. Type is an optional property to restrict the textbox input. The available options are HEX, SIGNED, and UNSIGNED. Passing it null or any other value will set it to no restriction. x, and y are the optional location parameters for the position of the textbox within the given form. The function returns the handle of the created textbox. If true, the multiline will enable the standard winform multi-line property. If true, the fixedWidth options will create a fixed width font. Scrollbars is an optional property to specify which scrollbars to display. The available options are Vertical, Horizontal, Both, and None. Scrollbars are only shown on a multiline textbox" forms.textbox(TEST_FORM, TEST_STR, form_width - 20, form_height - 40, nil, 2, 2, true, false, "Vertical")
which results in: The main problem is that this function is to insert/use text or values, so you can type and erase in this space. Also, apparently you can't use a "new line" symbol. Anyway, since there's not a direct way to do this, I suggest you request this feature at https://github.com/TASVideos/BizHawk/issues.
Games are basically math with a visual representation of this math, that's why I make the scripts, to re-see games as math. My things: YouTube, GitHub, Pastebin, Twitter