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

[API] MonAPI - Create 'sections' on monitors

Started by Shazz, 21 June 2013 - 01:39 PM
Shazz #1
Posted 21 June 2013 - 03:39 PM
This project has been discontinued due to the addition of the window API in ComputerCraft 1.6 which essentially does the same thing. However, you can check out multiMon which allows you to have multiple monitors!

Spoiler

MonAPI by Shazz

Hello guys! Today I will be releasing an API for monitors that I made a couple months ago. I've polished it up a bit so it can be released. This is my first time making an 'object-oriented' API. So please if you have any constructive critism, suggestion or question, feel free to post.

What?
This API allows you to divide up monitors into sections. Sections are basically like virtual monitors. All sections come with the standard functions that monitors can use. On top of that, this API adds a few extra functions for simple things such as drawing borders, filling up a section and creating hitboxes. The hitbox system is another very useful part of the API. The hitboxes will allow you to make your sections interactive by making it easier for you to create buttons.

Why?
I made this when I was playing on a modded server with my friends. Me and my friend had a base but we had to control everything manually. So I thought to myself, "Why not make a control panel using my favourite mod - ComputerCraft?" In the process of making the program, I realized that there were a lot of things updating and redrawing at the same time. For example, I had multiple graphs that were updating every 0.5 seconds. This made the program flickery and inefficient. I was clearing the whole monitor each time I wanted to redraw the graphs. So I thought, "Why not only clear the area where the graph is and then redraw it?" I tried that but it made the code more complicated and harder to read so I decided to turn it into an API.

Here is the result of the program when using the API. (Ignore the graph values - currently they are being fed dummy values.)
Spoilerhttp://quickscreen.e...557EC6C21F97B4B
http://quickscreen.e...555C40F2D7ED398
http://quickscreen.e...9DC03E4697E22C2
http://quickscreen.e...115670AE41EFBD2
"You are not allowed to use that image extension on this community."

Documentation:
- monAPI
+ wrapMonitor(string side) [[ Use this instead of peripheral.wrap; all the function are still available. ]]
+ getHitbox(int x, int y) [[ Returns the name of the hitbox and the sectionID the hitbox is in. Returns nil if no hitbox was detected at the specified co-ordinates. ]]
+ createSection(int startX, int startY, int endX, int endY) [[ Creates a section. ]]
+ drawBorder(string horChar, string verChar, string cornerChar) [[ Draws a border on the edges of the section. ]]
+ fill(string char) [[ Fills the whole section with the specified character (background colour can be set using setBackgroundColour beforehand). ]]
+ getSectionID() [[ Returns the sectionID. ]]
+ registerHitbox(string name, int startX, int startY, int endX, int endY) [[ Registers a hitbox at the specified positions. ]]
+ clearHitbox() [[ Clears all hitboxes (for that section). ]]
+ delete() [[ Deletes the section. ]]
| All of the standard functions that are available for monitors except scroll, setCursorBlink and setTextScale.
| All the standard functions for the monitor that you would get if you used peripheral.wrap(string side).

Example:
The documentation might've been a lot to take in so here is a simple example. This example creates 3 sections. One at the top corner, one in the middle and one near the bottom corner. I tried to demonstrate most of the functions availabe in this example including the hitbox system.
Here is the code:
http://pastebin.com/2fAkJV1K

Here is a short video demonstrating the program:
[media]http://www.youtube.com/watch?v=fqt4jTcR8Kw[/media]

Download:
pastebin get 2npxyPdn MonAPI

Changelog:
- v1.0 - Initial release.

To-do:
- Add support for scroll() (possibly)
- Add support for setCursorBlink()
Edited on 20 April 2014 - 04:54 PM
Tjakka5 #2
Posted 21 June 2013 - 03:42 PM
I like it :D/>
Shazz #3
Posted 21 June 2013 - 03:57 PM
I like it :D/>

Thank you, any feature you want to see added?
bjornir90 #4
Posted 21 June 2013 - 06:14 PM
Looks so cool ! Could you explain how you did this because I don't totally understand your code …. apparently you have made some virtual monitor from what I understood
Shazz #5
Posted 21 June 2013 - 06:25 PM
Looks so cool ! Could you explain how you did this because I don't totally understand your code …. apparently you have made some virtual monitor from what I understood

It's not that complicated but basically what I've done is everytime a section is created, it returns it's own list of functions which call the real monitor functions and do the things (wrapper functions). Obviously there is some validating done, so you can't draw outside the section's boundaries, etc. So yeah, each section is pretty much like a virtual monitor.
GuachaFTW #6
Posted 25 June 2013 - 09:06 PM
I´m sorry for this, but, I'm new to CC and Lua and Didn't Understand your API at all, and I love it, so I would like you to help me understanding your API which i'm so interested in, Maybe you can upload an example (Because the video Didn't explain anything at all), Thank you
Shazz #7
Posted 25 June 2013 - 10:26 PM
I´m sorry for this, but, I'm new to CC and Lua and Didn't Understand your API at all, and I love it, so I would like you to help me understanding your API which i'm so interested in, Maybe you can upload an example (Because the video Didn't explain anything at all), Thank you

I just realized that I put the "Here is the code:" part but never actually added the code. Derp. Anyway, I've added the code for the example program now! Hope that helps.
GuachaFTW #8
Posted 26 June 2013 - 04:39 PM
I´m sorry for this, but, I'm new to CC and Lua and Didn't Understand your API at all, and I love it, so I would like you to help me understanding your API which i'm so interested in, Maybe you can upload an example (Because the video Didn't explain anything at all), Thank you

I just realized that I put the "Here is the code:" part but never actually added the code. Derp. Anyway, I've added the code for the example program now! Hope that helps.

Thank you a lot for this minimal correction that may seem very Derp (and it is), But was very helpful, even with my minimal experience, I could understand the example very clearly, I really love your API and I'll always use it in my APi's folder, thank you and continue the good work
Shazz #9
Posted 26 June 2013 - 06:11 PM
I´m sorry for this, but, I'm new to CC and Lua and Didn't Understand your API at all, and I love it, so I would like you to help me understanding your API which i'm so interested in, Maybe you can upload an example (Because the video Didn't explain anything at all), Thank you

I just realized that I put the "Here is the code:" part but never actually added the code. Derp. Anyway, I've added the code for the example program now! Hope that helps.

Thank you a lot for this minimal correction that may seem very Derp (and it is), But was very helpful, even with my minimal experience, I could understand the example very clearly, I really love your API and I'll always use it in my APi's folder, thank you and continue the good work

Thank you and sorry for the derp earlier.
svdragster #10
Posted 02 July 2013 - 01:42 PM
Really great work!
Shazz #11
Posted 02 July 2013 - 06:46 PM
Really great work!

Thank you very much! Any feature you would like to see?
BlankWolf #12
Posted 04 July 2013 - 04:15 AM
This is so awesome.
I hope you don't mind me to borrow your idea and try to make this by my self.
Shazz #13
Posted 04 July 2013 - 12:23 PM
This is so awesome.
I hope you don't mind me to borrow your idea and try to make this by my self.

I don't mind at all. It's good that you want to learn it yourself!
BlankWolf #14
Posted 04 July 2013 - 01:00 PM
This is so awesome.
I hope you don't mind me to borrow your idea and try to make this by my self.

I don't mind at all. It's good that you want to learn it yourself!

Thanks. And maybe I post here a link to my program or somthing like that, when I'm ready.
UMayBleed #15
Posted 04 July 2013 - 03:59 PM
Very useful * Evil face * I will run the entire CC list of programs ever on 1 monitor >:D/> (Would break)
Shazz #16
Posted 04 July 2013 - 07:41 PM
Very useful * Evil face * I will run the entire CC list of programs ever on 1 monitor > :D/> (Would break)

You could possibly do that and have 'dual monitors'.
coaster3000 #17
Posted 07 July 2013 - 05:25 PM
A nice feature would be to use the api on the same computer (No peripheral wrap) So that way I can divide my screen up :D/>

It would be useful for windows in a program I think.

Also the ability to move sections around.


Question: Can I attempt to recreate api? I am not gonna publish it though. Its for learning process. I can however give you link if I manage to make it.

I am trying to practice making things for Survival world use If I don't have access to internet :)/>
Shazz #18
Posted 08 July 2013 - 01:14 AM
Go ahead, recreate it if you want. It's better for yourself as you'll learn more. You don't have to ask especially if you're not going to publish it.

About your suggestion, I might just port this API so that it is compatible with computers.
Galactica4 #19
Posted 11 July 2013 - 06:44 AM
Go ahead, recreate it if you want. It's better for yourself as you'll learn more. You don't have to ask especially if you're not going to publish it.

About your suggestion, I might just port this API so that it is compatible with computers.
Really cool API I really like it
Can I PLEEEEEEEEEEEEEASE add it in my new OS I am making? I will credit you accordingly in the inbuilt credits file as well as on the forums when I post it
Shazz #20
Posted 11 July 2013 - 08:02 PM
Go ahead, recreate it if you want. It's better for yourself as you'll learn more. You don't have to ask especially if you're not going to publish it.

About your suggestion, I might just port this API so that it is compatible with computers.
Really cool API I really like it
Can I PLEEEEEEEEEEEEEASE add it in my new OS I am making? I will credit you accordingly in the inbuilt credits file as well as on the forums when I post it

Sure go ahead :)/>
Cutecurtain #21
Posted 12 July 2013 - 04:20 AM
Really cool API! I'll use this! :)/>
deathseeker #22
Posted 13 July 2013 - 04:03 AM
This could be really useful
Shazz #23
Posted 13 July 2013 - 01:23 PM
Thank you for the feedback! :D/>