Advertisement
Rochet2

C++ pagination example

Oct 21st, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. /*
  6. https://ideone.com/CHxrfS
  7. You want to design the system so that sender tells what to do with action.
  8. If you need some options that are not a part of the paginated menus then you
  9. need to reserve senders for them. For example reserving from the beignning or end of the uint32 range a few values.
  10.  
  11. In this system sender is either a reserved ID or the page ID.
  12. The action is either 0 or a valid item lowguid/ID.
  13. */
  14.  
  15. // This is a globally known value
  16. // we want to substract the "next" and "previous" buttons from max items.
  17. const int max_items = 32-2;
  18.  
  19. void ShowPage(int page, vector<int> const & items)
  20. {
  21.     // Clear menu()
  22.     if (page < 0) // show main page if invalid page
  23.         page = 0;
  24.     int count = 0; // added items count
  25.     for (int i = page*max_items; i < items.size(); ++i) // go through all items, we want to start from page beginning
  26.     {
  27.         // you want here that action is for example item lowguid.
  28.         // you want to pass the page here so if something goes wrong you can show the same pageid on gossip select.
  29.         printf("Some Item, sender: %i action: %i\n", page, items[i]);
  30.         ++count; // added an item, increase count of items
  31.         if (count >= max_items)
  32.         {
  33.             // we are at limit, add next button and stop adding items
  34.             printf("Next page, sender: %i action: %i\n", page+1, 0);
  35.             break;
  36.         }
  37.     }
  38.     if (page > 0)
  39.     {
  40.         if (count == 0)
  41.         {
  42.             // player trying to view a page that doesnt contain any items and isnt main page.
  43.             // We want to show the main menu here or show the previous page.
  44.             ShowPage(page-1, items);
  45.             return;
  46.         }
  47.         // we are at limit, add next button and stop adding items
  48.         printf("Previous page, sender: %i action: %i\n", page-1, 0);
  49.     }
  50.     // Send menu()
  51. }
  52.  
  53. int main() {
  54.     /*
  55.     Through for loops and such you have gathered all items the player has in a list called "items".
  56.     These items can be for example pointers to the items you get from GetItemByPos or similar.
  57.     These are all the valid items to be shown in the menu.
  58.     */
  59.     vector<int> items = {};
  60.     int number_of_example_items = 32;
  61.     for (int i = 1; i <= number_of_example_items; ++i)
  62.         items.push_back(i);
  63.  
  64.     ShowPage(0, items);
  65.     ShowPage(1, items);
  66.     ShowPage(100, items);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement