im trying to code a simple list box for my self made GUI but i seem to be stuck at the scroll bar. I already finished with the calculation for the scroll bar size but i have no idea how i could calculate it's current vertical position depending on the current item on top of the list box.
My current code looks like this
ListBox struct:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct MenuListBox {
int PosX;
int PosY;
int Width;
int ItemsVisible; //Items visible at the same time
int CurrentItem;
int CurrentTopItem;
int CurrentScrollBarPosY; //Vertical position of the scroll bar (0 = directly under the scroll up button
int CurrentScrollBarHeight; //Size of the scroll bar
int index;
wchar_t* ItemArray[10000]; //Array of list box items
int TabToDrawOn; //Needed for the TabControl
MenuListBox* pNext;
};
I didn't look at your code.... but this is a pretty straightforward problem so I figure I'd just give you the idea and you can work it into your code your own way.
There are few things you need to know.
1) The maximum [logical, not graphical] scroll position. This would probably be itemsInList - itemsVisible;
2) The current [logical] scroll position
3) The Height (in pixels) of the trackbar. My terminology might be wrong... it's the thing you click and drag on.
4) The maximum (graphical) position to draw the trackbar. This will be: heightOfAreaWhereTrackBarCanBeDrawn - heightOfTrackBar;
From here it's just a matter of scaling the logical range to the graphical range:
1 2 3
graphicalPos = curScroll * maxGraphicPos / maxLogicalPos;
// ie: #2 * #4 / #1
// #3 is only necessary for calculating #4
@Disch
I agree it wasn't worthy of being reported, but I definitely understand why it was done. Lumpkin clearly stopped reading once he saw the word scroll.
Also I don't know why your post got reported. That seems absurd.
I reported it because Lumpkin/Fredbill30 has a history of responding to posts without actually reading them, supplying code that doesn't work and making inane suggestions. Since, he continues to do so, I don't see much point in tolerating him.
A link that has nothing to do with the subject he is talking about. He is talking about GUIs and you posted a link to screen scrolling for a shooter/RPG. I completely agree with cire on this one.
Lumpkin wrote:
Again OP what will the dimensions of the scrollbar be? Whats the scale?
I've not done GUIs for a while (not since Allegro 3.0.x WIP) and from what I remember the libraries should auto size the parts according to the size of the window you are placing it on. Depending on the library he is using of course as some may not automatically do that.
im trying to code a simple list box for my self made GUI but i seem to be stuck at the scroll bar. I already finished with the calculation for the scroll bar size but i have no idea how i could calculate it's current vertical position depending on the current item on top of the list box.
So... yeah.... it seemed pretty straightforward to me what he was asking. He's drawing the scrollbar himself and just had a question about how to determine the placement of the trackbar.
Also @ Lumpkin: inane is not quite the same thing as insane. Dunno if that was a typo or not.
@Disch
I was responding to Limpkin's question. Though, I did misread it, somehow I read he was using an existing lib to make his own GUI lib. My advice to the OP would be just that though, look at open source GUI libs and see how they do it. I've never even considered making my own GUI lib due to all the ones out there, but it does sound like a fun challenge.
@BHXSpecter
This GUI is a bit different because it is not a windows GUI. I use a game engines drawing functions to draw the GUI inside of the games window with basic functions for drawing 2d lines, rects, text and all that.
I also already looked at other GUI source codes but they all do it somehow different or they use some functions of libs i don't have or cannot use.
I also thank @Lumpkin for the tip with the array since im only coding in c++ for a bit more than a month now so i don't have the best solution for everything.
Still, my main question in this thread is still not answered bcause i want an answer specific to my source code so please don't post something like "Look at other open source GUI libs" because i already did and they didn't gave me an answer to my problem.
Additional info: I always have to subtract - 2 from the values before multiplying it with 16 because every list box item has a height of 16 px as well as the buttons for scrolling up and down on the top and bottom of the scrollbar have a size of 16px so i always have to subtract 2 * 16 to give the scroll bar the correct size / y-position.
@Lumpkin
I want the scrollbar to automatically have the correct size depending on the amount of items in the array AND the amount of items visible at the same time (items visible at same time = 10 means the listbox can display 10 items / is 10 items high). The items in the box are changing all the time so the scroll bar has to get autoscaled. As i already said it's important to remember that the buttons for scrolling up/down at the top and bottom right at the top and bottom of the scroll bar are also taking a height of 16 px each.
But as you may see i already have the code for calculating the size, my only problem is the vertical position. I need the calculations to calculate it's vertical position depending on the amount of items in the array and the current item on top of the list box AND the amount of items visible in the list box at the same time but i ALSO need the calculations to do that backwards->Calculate the current item on top of the list depending on the scroll bars vertical position.
I've never done anything like this, but my guess is
scroll_bar_height (pixels) /num_items_in_list = height_per_line (ammount of scrollbar to move for each line)
current_pos = current_item_index * height_per_line