[CubeScript][Tutorial] GUI Scrollboxes

[CubeScript][Tutorial] GUI Scrollboxes

Postby ZeroKnight » 12 Nov 2012, 00:10

Hello all. I recently figured out how to create scrollboxes for simple GUI implementations like CubeScript's GUI. It's actually not too hard when you figure out how they work, and I decided to share my findings and explain to the best of my ability how it's done. The tutorial itself is a working CFG that can be executed in Red Eclipse. The explanations are all documented in the CFG as comments. As with the rest of my work, I've thrown it on my GitHub, and it's in my archive thread on this forum as well.

Hope I was of some help :) Feel free to ask for help, post corrections, etc.

Link to tutorial on GitHub
{l Code}: {l Select All Code}
//////////////////////////////////////////////////////////////////////////////
/// CubeScript Tutorial: Scrollboxes
///
/// Tutorial that will guide you through how to create a fake scrollbox for
/// CubeScript GUIs. The fundamentals of this tutorial can be applied outside
/// of CubeScript as well!
///
/// Author: Alex "ZeroKnight" George
///
/// See a collection of my work! Check out:
/// http://forum.freegamedev.net/viewtopic.php?f=73&t=3592
/// https://github.com/ZeroKnight/ZeroKnight-Red-Eclipse-Archive.git
///
/// This work is licensed under a CC-BY-SA 3.0 License
/// http://creativecommons.org/licenses/by-sa/3.0/
//////////////////////////////////////////////////////////////////////////////

// First, let's start off with a list of items to throw into a scrollbox. We'll use a list of random names.
nameslist = [ "Bob" "Joe" "Frank" "Mike" "Dave" "Johnny" "Amanda" "Alex" "Chris" "Kurt" "Doug" "Nick" "Bill" "Jose" ]

// We'll need to specify how large we want our scrollbox to be, or in other words, how many items
// that the scrollbox will hold. We'll set this value with the variable below, which will make our
// scrollbox "border" at 5 items; in other words, when it needs to be scrolled.
sboxlength = 5

// Let's make a GUI to test with :)
newgui "testgui" [

    // This guilist creates the frame for the whole scrollbox: both the list of items and the scrollbar.
    guilist [

        // Here we will create the space for the scrollbox in the GUI using `sboxlength`, the
        // variable we declared earlier.
        // Note: The second parameter to guistrut, when true, will wrap the strut in a guilist.
        guistrut $sboxlength 1

        // We need to know the number of items the scrollbox will hold altogether.
        // We recalculate this number each time in case the list is changed, thus altering
        // the number of items, which will affect how far the scrollbox "scrolls".
        numitems = (listlen $nameslist)

        // `index` is the index in our list of items that the scrollbox will be showing.
        // List items $index through (+ $index $sboxlength) will be shown in the scrollbox.
        // The reason we recalculate `index` is to account for changes in the list, which will
        // alter the number of items, thus altering the maximum or minimum value of `index`.
        // With the line below, `index` will never be <0 or greater than the last value of `index`
        // set by the guislider that controls it. This prevents the scrollbox from going out of
        // bounds, or stopping short in the list. It is a safeguard.
        index = (min (max 0 (- $numitems $sboxlength)) $index)

        // This guilist creates the "list" of items for the scrollbox
        guilist [

            // We use a guistrut here to keep the list of the scrollbox at a fixed width that is
            // wide enough to fit the full length of each item's name. It avoids the scrollbox
            // from dynamically changing it's width as you scroll through it.
            guistrut 10 1

            // This loop is what is responsible for displaying the contents of the scrollbox's list.
            // Starting at the `index` item in the list, display the next $sboxlength items in our
            // list of items.
            loop i $sboxlength [
               
                // Iterator for our list of items. Remember that loops start at 0 and end with
                // the count of the loop minus 1; hence (+ $index $i)
                k = (+ $index $i)
sboxlistitem = (at $nameslist $k)

                // Finally, show the item in the scrollbox's list.
                guitext $sboxlistitem
            ]
        ]
        // This is our scrollbar. It controls the value of `index` and goes from 0 to the difference
        // between the number of items in our list of items and the number of items that the scrollbox
        // will show at once.
        guislider index 0 (max 0 (- $numitems $sboxlength)) [] 1 1
    ]
] [
    // This simply initializes our `index` variable when the GUI is created
    if (= $guipasses 0) [
index = 0
]
]
showgui "testgui"

// And there you have it! Creating these fake scrollboxes isn't very hard once you know what makes them
// work :) I sincerely hope this tutorial helped you, as I myself have struggled on fake scrollboxes for
// a long while. If you have any questions or need help, contact myself ("ZeroKnight") at xzeroknightx@gmail.com
// or ask on the Red Eclipse forums (also where this tutorial is originall hosted).
// So long, and happy scripting!
//
// ~ Zero
[ Github ][ WazuClan -- irc.wazuclan.com #wazuclan ][ Zero's Archive of RE Extensions, Scripts, WeapMods & More! ]
User avatar
ZeroKnight
 
Posts: 524
Joined: 08 Jun 2011, 01:24
Location: Ohio, United States

Re: [CubeScript][Tutorial] GUI Scrollboxes

Postby bonifarz » 12 Nov 2012, 08:17

Clean, clear and useful. I think this does not only help for this particular task, it also gives beginners some good example and explanations how to structure a gui.
User avatar
bonifarz
 
Posts: 379
Joined: 09 Apr 2012, 12:16
Location: switzerland

Re: [CubeScript][Tutorial] GUI Scrollboxes

Postby ZeroKnight » 13 Nov 2012, 13:55

Thanks bonifarz :) I was worried that my wording wasn't clear enough. In time I plan to do more tutorials, from general and basic things to more advanced stuff. I could also kill two birds with one stone and use the base-work of the tutorials as a primer for the CubeScript wiki pages that have been sitting red-linked for quite some time now.
[ Github ][ WazuClan -- irc.wazuclan.com #wazuclan ][ Zero's Archive of RE Extensions, Scripts, WeapMods & More! ]
User avatar
ZeroKnight
 
Posts: 524
Joined: 08 Jun 2011, 01:24
Location: Ohio, United States

Who is online

Users browsing this forum: No registered users and 1 guest