Jump to content

Recommended Posts

Posted

Hi,

 

I'm currently working on a custom book and want to create new pages on fly for it.

I've got myself and index which includes all chapters the book has and instead of using a scrollbar when more than 10 chapters

exists for this book, I want to add a new page to show the rest of the chapters.

So each site on the index page shows only 10 chapters and when you've got 100 chapters the index part of the book will be 10 pages long.

 

Here is what I've currently got:

 

        if(!this.chapters.isEmpty()) {
            int buttons = this.buttonList.size() - 1;
            int y = (this.top + 20) / this.chapters.size() + 10;
            for(int i = 0; i < this.chapters.size(); i++) {
                y += 10;
                GuiButtonChapter button = new GuiButtonChapter(i + buttons, this.left + 20, y, 100, 10, this.chapters.get(i));
                button.displayString = I18n.format(this.chapters.get(i).getTitle());
                this.buttonList.add(button);
            }
        }

 

GuiButtonChapter is the button used to display the chapter and to actually choose to read it.

Chapters is a ArrayList which get's loaded in from my API as other mods might want to add chapters to the book. This happens when the Gui object is created.

The top and left variables are used to determine the start point of the GUI.

 

Thx in advance.

Bektor

Developer of Primeval Forest.

Posted (edited)

Anyone got an idea as I'm stuck on this problem.

 

Just to mention, also tried this piece of code... well, it resulted in an index out of bounds exception

for(int z = 0; z < this.maxPages; z++) {
            for(int i = 0; i < this.chapters.size(); i++) {
                y += 10;
                if(!(this.chapters.size() > i + z)) {
                    System.out.println("max: " + maxPages);
                    System.out.println("chapters: " + chapters.size());
                    System.out.println("i: " + i);
                    System.out.println("z: " + z);
                    break;
                }
                GuiButtonChapter button = new GuiButtonChapter(i + buttons, this.left + 20, y, 100, 10, this.chapters.get(i + z));
                button.displayString = I18n.format(this.chapters.get(i).getTitle());
                this.buttonList.add(button);
            }
}

 

Quote

max: 2 // yeah, it should be 3 as chapters is 21 and only 10 per page...

chapters: 21

i: 20

z: 1

 

Also it does not work. Somehow it even seems to do some weird stuff when going a page back...

Edited by Bektor

Developer of Primeval Forest.

Posted (edited)

Math.ceil(chapters.size()/10f) should return the required amount of pages for the chapters.
But without an Idea how the rest of your code looks and how you handle the pages we cannot tell you anything more.

Edited by JTK222
Posted
4 minutes ago, JTK222 said:

Math.ceil(chapters.size()/10f) should return the required amount of pages for the chapters.
But without an Idea how the rest of your code looks and how you handle the pages we cannot tell you anything more.

Ah, ok. That works. (I guess my idea of fixing this would have been more resource intensive ^^)

 

I'm handling the pages by actually having an integer variable which counts on which page we are currenty.

Thats basically how I handle the pages.

    private int page = 0;
    private int maxPages = 0;
    private ArrayList<Chapter> chapters = new ArrayList<>(APIMod.chapters);

    protected void initGui() {
        if(!this.chapters.isEmpty()) {
            int buttons = this.buttonList.size() - 1;
            int y = (this.top + 20) / this.chapters.size() + 10;
            for(int i = 0; i < this.chapters.size(); i++) {
                y += 10;
                GuiButtonChapter button = new GuiButtonChapter(i + buttons, this.left + 20, y, 100, 10, this.chapters.get(i));
                button.displayString = I18n.format(this.chapters.get(i).getTitle());
                this.buttonList.add(button);
            }
        }
    }

public void drawScreen(int mouseX, int mouseY, float partialTicks) {
  super.drawScreen(mouseX, mouseY, partialTicks);
}

 

Developer of Primeval Forest.

Posted

So I guess you use the pages only for this?
And not for the chapters themselves?

If yes you should create a list of 10 buttons. and add a method to them on to change for which chapter they are.
And as you switch pages you would have just to change the chapters for the buttons.

Posted
17 hours ago, JTK222 said:

So I guess you use the pages only for this?
And not for the chapters themselves?

If yes you should create a list of 10 buttons. and add a method to them on to change for which chapter they are.
And as you switch pages you would have just to change the chapters for the buttons.

Yes, the chapters are a different GUI.

 

Hm... I'm just wondering if this is efficient to do it with a list as every button would require to update on the actionPerformed method:

  • displayString and translate it
  • the chapter from the list

 This would mean, on each click on the forward button or the backwards button the hole button and chapters list would have to be iterated over.

Developer of Primeval Forest.

Posted

Yes, you would need to iterate trough the buttons, but let's be serious even in case your chapters lists gets a few hundred entries you wouldn't even notice it. (Using this system for a Crafting System I have done).
But you wouldn't have to iterate trough the chapters, chapters.get(10 * page + buttonOffset); should be enough, you would just have to set buttonOffset for each button (first button 0 and 10th button 9).
Maybe with a small if statement to prevent an IndexOutOfBounds exception and it would be done.

	button.chapter = chapters.size > 10 * page + buttonOffset ? chapters.get(10*page +buttonOffset) : null;
	

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.