summaryrefslogtreecommitdiff
path: root/ihl/guidebook/IHLGuidebookGui.java
blob: 343f52253783cbb71898f4e46055d0a3970a006f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package ihl.guidebook;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.lwjgl.opengl.GL11;
import org.xml.sax.SAXException;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import ihl.IHLMod;
import ihl.datanet.GuiInvisibleButton;
import ihl.utils.IHLRenderUtils;

public class IHLGuidebookGui extends GuiContainer
{
    public IHLGuidebookContainer container;
	private static final ResourceLocation background = new ResourceLocation("ihl", "textures/gui/GUIGuidebook.png");
	public Map<Integer, Integer[]> linksCoordinatesMap = new HashMap<Integer,Integer[]>();
	private GuiInvisibleButton prevPageAreaButton;
	private GuiInvisibleButton nextPageAreaButton;
	private String title;
	private String[] localisedContent=new String[2];;// By text block and row
	private ResourceLocation[] resourceLocationCache = new ResourceLocation[16];//By section number
	private int currentSection=0;
	private int textBlockNumberOnPageTurn=0;
	private int stringNumberOnPageTurn=0;
	private final int titleX=25;
	private final int titleY=7;
	private final int textBlockWidth=104;
	private final int textBlockX1=20;
	private final int textBlockY1=38;
	private final int textBlockX2=130;
	private final int textBlockY2=5;
	private final int textBlockMaxY=200;
	private int stringHeight=10;
	private int textRowInRightPage=0;
	private int textBlockInRightPage=0;
	private int textRowInNextPage=0;
	private int textBlockInNextPage=0;
	private int[] textRowStart=new int[2];
	private int textBlockStart=0;
	private int pictureWidth;
	private int pictureHeight;
	private int maxSection=0;
	
	public IHLGuidebookGui(IHLGuidebookContainer container1) {
		super(container1);
        this.container = container1;
        this.xSize=this.container.xSize;
        this.ySize=this.container.ySize;
	}
	
    @Override
    public void initGui()
    {
    	super.initGui();
        int x = (width - xSize) / 2;
        int y = (height - ySize) / 2;
        prevPageAreaButton = new GuiInvisibleButton(0, x, y, xSize/2, 162, linksCoordinatesMap, buttonList);
        nextPageAreaButton = new GuiInvisibleButton(1, x+xSize/2, y, xSize/2, 162, linksCoordinatesMap, buttonList);
        this.clear();
        try {
			IHLMod.xmlparser.setupGuidebookGUI(this, 0);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
	@Override
    public void actionPerformed(GuiButton button)
    {
        super.actionPerformed(button);
        switch(button.id)
        {
        	case 0:
        		currentSection--;
        		if(currentSection<0)
        		{
        			currentSection=this.maxSection;
        		}
    			this.textBlockStart=0;
    			this.textRowStart[0]=0;
    			this.textRowStart[1]=0;
    			break;
        	case 1:
        		if(textRowInNextPage==0 && textBlockInNextPage==0)
        		{
            		currentSection++;
        			this.textBlockStart=0;
        			this.textRowStart[0]=0;
        			this.textRowStart[1]=0;
        		}
        		else
        		{
        			this.textBlockStart=textBlockInNextPage;
        			this.textRowStart[textBlockStart]=textRowInNextPage;
        			textRowInNextPage=0;
        			textBlockInNextPage=0;
        		}
    			break;
        }
        this.clear();
        try {
			IHLMod.xmlparser.setupGuidebookGUI(this, currentSection);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

	public void clear()
	{
		for(int i=0;i<this.container.base.content.length;i++)
		{
			this.container.base.content[i]=null;
		}
		this.title=null;
		this.localisedContent[0]=null;
		this.localisedContent[1]=null;
	}
	
	public void setTitle(String title1) {
		this.title=StatCollector.translateToLocal(title1);
	}
	
	public void setPicture(String string,int width,int height)
	{
		IHLMod.log.debug("setting picture='"+string+"' \n height="+height+" \n width="+width);
		if(this.resourceLocationCache[this.currentSection]==null)
		{
			this.resourceLocationCache[this.currentSection]=new ResourceLocation("ihl",string);
		}
		this.pictureWidth=width;
		this.pictureHeight=height;
	}


	@Override
    protected void drawGuiContainerForegroundLayer(int par1, int par2) {
		IHLRenderUtils.instance.updateScreenSize();
		this.textRowInRightPage=0;
		this.textBlockInRightPage=0;
        this.drawPage(this.textBlockX1,this.textBlockY1);
    }
	
    private void drawPage(int startX, int startY)
    {
        int xOffset = (this.width - xSize) / 2;
        int yOffset = (this.height - ySize) / 2;
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.fontRendererObj.drawString(this.title, this.titleX, this.titleY, 0);
        int xPos=startX;
        int yPos=startY;
        if(this.textBlockStart==0)
        {
        	if(this.localisedContent[0]!=null)
        	{
        		List<String> splittedText = IHLRenderUtils.instance.splitStringByWidth(this.localisedContent[0], Math.round(textBlockWidth));
        		for(int i1=this.textRowStart[0]; i1 < splittedText.size(); i1++)
        		{
        			yPos+=stringHeight;
        			if(yPos<this.textBlockMaxY)
        			{
        				this.fontRendererObj.drawString(splittedText.get(i1), Math.round(xPos), Math.round(yPos), 0);
        			}
        			else if(xPos!=this.textBlockX2)
        			{
        				yPos=this.textBlockY2;
        				xPos=this.textBlockX2;
        				this.fontRendererObj.drawString(splittedText.get(i1), Math.round(xPos), Math.round(yPos), 0);
        			}
        			else
					{
        				textRowInNextPage=i1;
        				textBlockInNextPage=0;
        				return;
					}
        		}
        	}
			if(this.resourceLocationCache[this.currentSection]!=null)
			{
            	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            	if(yPos+this.pictureHeight<this.textBlockMaxY)
        		{
                	yPos+=stringHeight;
                	ITextureObject texture = this.mc.renderEngine.getTexture(this.resourceLocationCache[this.currentSection]);
                	this.mc.renderEngine.bindTexture(this.resourceLocationCache[this.currentSection]);
                	this.drawTexturedModalRect(Math.round(xPos), Math.round(yPos), 0, 0, this.pictureWidth, this.pictureHeight);
                	yPos+=Math.round(this.pictureHeight);
        		}
        		else
        		{
        			yPos=this.textBlockY2;
        			xPos=this.textBlockX2;
                	this.mc.renderEngine.bindTexture(this.resourceLocationCache[this.currentSection]);
                	this.drawTexturedModalRect(Math.round(xPos), Math.round(yPos), 0, 0, this.pictureWidth, this.pictureHeight);
                	yPos+=Math.round(this.pictureHeight);
        		}
        	}
    	
        }
        if(this.localisedContent[1]!=null)
        {
			List<String> splittedText = IHLRenderUtils.instance.splitStringByWidth(this.localisedContent[1], Math.round(textBlockWidth));
            for(int i1=this.textRowStart[1]; i1 < splittedText.size(); i1++)
            {
            	yPos+=stringHeight;
            	if(yPos<this.textBlockMaxY)
            	{
                    this.fontRendererObj.drawString(splittedText.get(i1), Math.round(xPos), Math.round(yPos), 0);
            	}
				else if(xPos!=this.textBlockX2)
				{
					yPos=this.textBlockY2;
					xPos=this.textBlockX2;
                    this.fontRendererObj.drawString(splittedText.get(i1), Math.round(xPos), Math.round(yPos), 0);
				}
				else
				{
					textRowInNextPage=i1;
					textBlockInNextPage=1;
					return;
				}
            }
        }
        GL11.glScalef(1f, 1f, 1f);
    }


    @Override
    protected void drawGuiContainerBackgroundLayer(float par1, int par2,
                    int par3) {
            //draw your Gui here, only thing you need to change is the path
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            int x = (width - xSize) / 2;
            int y = (height - ySize) / 2;
            this.mc.renderEngine.bindTexture(background);
            this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
            
        }

	public void setSectionNumber(int i) 
	{
		this.currentSection=0;
	}

	public void setMaxSectionNumber(int i) 
	{
		this.maxSection=Math.max(i,this.maxSection);
	}

	public int getMaxSectionNumber() 
	{
		return this.maxSection;
	}

	public void addItemStack(ItemStack itemStack) 
	{
		for(int i=0;i<this.container.base.content.length;i++)
		{
			if(this.container.base.content[i]==null)
			{
				this.container.base.content[i]=itemStack;
				break;
			}
		}
	}

	public void addTextBlock(String textContent) {
        for(int i=0; i < this.localisedContent.length; i++)
        {
        	if(this.localisedContent[i]==null)
        	{
            	this.localisedContent[i]=StatCollector.translateToLocal(textContent);
                break;
        	}
        }
	}
}