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