This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Endergreen117's profile picture

Making a Windows-Like OS, how do you make windows (or panels)?

Started by Endergreen117, 19 October 2014 - 07:02 PM
Endergreen117 #1
Posted 19 October 2014 - 09:02 PM
Recently I started a project in which I am attempting to make a Windows 8 like OS for Computercraft and I have been trying to figure out how to make windows in Computercraft. I have read the wiki entry for the "window" API and the "term" API, but they dont give very much detail as to how you actually deal with windows after they are created, how you would have them move via a "mouse_drag" event, how you would be able to have them re-size via a similar method, and how would you add a title-bar and window buttons ([_], [-], [x], etc)?
Lyqyd #2
Posted 19 October 2014 - 10:26 PM
By programming these things. Have you tried reading through the code of existing OS projects to see how they do it?
Endergreen117 #3
Posted 20 October 2014 - 01:38 AM
By programming these things. Have you tried reading through the code of existing OS projects to see how they do it?

Hmm, that's a good idea. I was looking for a more direct approach, but I suppose that'll work just as fine. Thanks.
oeed #4
Posted 20 October 2014 - 03:04 AM
I made OneOS before the window API was released, so I've never really used it. Personally, what I've done is downloaded the ComputerCraft jar file and extracted it (just rename it to a .zip) then taken the Lua code out. This allows you to easily see how the APIs work and also helps immensely when sandboxing or customising functions (such as making your own os.loadAPI function).

If you're making an OS I'd also make sure you try and modularise everything if you can. When you have a program such as OneOS you just can't put all of it in one file, last time I checked it was about 20 thousand lines. It allows you to swap things around easier and make things quicker. You should also use a centralised drawing API, using term.write is just tacky and it also allows you to have buffers, do things like clipping, colour blending and draw blocks of colour with a single line rather than using a 10 or so line for loop.

You could also take a look at Bedrock, it’s got quite a special and specific way of doing things and takes a bit to learn, but it makes creating powerful GUIs really easy. It’s completely different from standard ComptuerCraft programming though.

To answer your question about windows though. I like doing an object oriented model, I think it’s a cleaner and easier way to do things, but it’s just my personal preference. So what I would do is create an object for a window container. You’d have properties such as the X, Y, width, height, title, etc. You’d also set the window API object. Using your click detection code, I’d then send the click event to the window container, if Y == 1 then you set another property, called something like “DragX” and you’d set this property to the X value (relative to the window) of the click event. You’d then start a timer of about a second and set the returned number to some variable. When the timer event fires you then set this variable to nil. Now any mouse_drag events you receive while that timer is not nil should be assumed to be a window drag. When this happens you restart the timer and move the window to the drag location minus the DragX value so the cursor position stays in the same place.

It’s a bit of a long winded explanation, but it’s pretty much exactly what I did with PearOS windows. Maybe take a look at the code for that on GitHub. I don’t actually think I have any window dragging in OneOS, but because it uses Bedrock it’s a bit more obscure to learn from.
Dragon53535 #5
Posted 20 October 2014 - 03:50 AM
To tack onto Oeed's little post, there's a github repo that holds the current lua code for CC Here.