summaryrefslogtreecommitdiff
path: root/jeu-test/Lemmini/0.84/src/Game/TextDialog.java
blob: 973b06e29c06cfd4a2025ba3414c1a461a002bc0 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package Game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import Tools.ToolBox;

/*
 * Copyright 2009 Volker Oth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Class to create text screens which can be navigated with the mouse.
 * Uses {@link LemmFont} as bitmap font.
 *
 * @author Volker Oth
 */
public class TextDialog {
	/** list of buttons */
	private ArrayList<Button> buttons;
	/** image used as screen buffer */
	private BufferedImage screenBuffer;
	/** graphics object to draw in screen buffer */
	private Graphics2D gScreen;
	/** image used as 2nd screen buffer for offscreen drawing */
	private BufferedImage backBuffer;
	/** graphics object to draw in 2nd (offscreen) screen buffer */
	private Graphics2D gBack;
	/** width of screen in pixels */
	private int width;
	/** height of screen in pixels */
	private int height;
	/** horizontal center of the screen in pixels */
	private int centerX;
	/** vertical center of the screen in pixels */
	private int centerY;

	/**
	 * Create dialog text screen.
	 * @param w Width of screen to create
	 * @param h Height of screen to create
	 */
	public TextDialog(final int w, final int h) {
		width = w;
		height = h;
		centerX = width/2;
		centerY = height/2;
		screenBuffer = ToolBox.createImage(w, h, Transparency.OPAQUE);
		gScreen = screenBuffer.createGraphics();
		gScreen.setClip(0, 0, width, height);
		backBuffer = ToolBox.createImage(w, h, Transparency.OPAQUE);
		gBack = backBuffer.createGraphics();
		gBack.setClip(0, 0, width, height);
		buttons = new ArrayList<Button>();
	}

	/**
	 * Initialize/reset the text screen.
	 */
	public void init() {
		buttons.clear();
		gScreen.setBackground(Color.BLACK);
		gScreen.clearRect(0, 0, width, height);
	}

	/**
	 * Get image containing current (on screen) screenbuffer.
	 * @return image containing current (on screen) screenbuffer
	 */
	public BufferedImage getScreen() {
		return screenBuffer;
	}

	/**
	 * Fill brackground with tiles.
	 * @param tile Image used as tile
	 */
	public void fillBackground(final BufferedImage tile) {
		for (int x=0; x<width; x+=tile.getWidth()) {
			for (int y=0; y<width; y+= tile.getHeight())
				gBack.drawImage(tile,x,y,null);
		}
		gScreen.drawImage(backBuffer,0,0,null);
	}

	/**
	 * Copy back buffer to front buffer.
	 */
	public void copyToBackBuffer() {
		gBack.drawImage(screenBuffer,0,0,null);
	}

	/**
	 * Set Image as background. The image will appear centered.
	 * @param image Image to use as background
	 */
	public void setBackground(final BufferedImage image) {
		int x= (width-image.getWidth())/2;
		int y= (height-image.getHeight())/2;
		gBack.setBackground(Color.BLACK);
		gBack.clearRect(0, 0, width, height);
		gBack.drawImage(image,x,y,null);
		gScreen.drawImage(backBuffer,0,0,null);
	}

	/**
	 * Restore whole background from back buffer.
	 */
	public void restore() {
		gScreen.drawImage(backBuffer,0,0,null);
	}

	/**
	 * Restore a rectangle of the background from backbuffer.
	 * @param x x position of upper left corner of rectangle
	 * @param y y position of upper left corner of rectangle
	 * @param width width of rectangle
	 * @param height height of rectangle
	 */
	public void restoreRect(final int x, final int y, final int width, final int height) {
		gScreen.drawImage(backBuffer,x,y,x+width,y+height,x,y,x+width,y+height,null);
	}

	/**
	 * Restore a rectangle of the background from backbuffer that might be invalidated by
	 * a text starting at x,y and having a length of len characters.
	 * @param x0 x position of upper left corner of rectangle expressed in character widths
	 * @param y0 y position of upper left corner of rectangle expressed in character heights
	 * @param l Length of text
	 */
	public void restoreText(final int x0, final int y0, final int l) {
		int x = x0*LemmFont.getWidth();
		int y = y0*(LemmFont.getHeight()+4);
		int len = l*LemmFont.getWidth();
		int h = LemmFont.getHeight()+4;
		gScreen.drawImage(backBuffer,x,y,x+len,y+h,x,y,x+len,y+h,null);
	}

	/**
	 * Draw string.
	 * @param s String
	 * @param x0 X position relative to center expressed in character widths
	 * @param y0 Y position relative to center expressed in character heights
	 * @param col LemmFont color
	 */
	public void print(final String s, final int x0, final int y0, final LemmFont.Color col) {
		int x = x0*LemmFont.getWidth();
		int y = y0*(LemmFont.getHeight()+4);
		LemmFont.strImage(gScreen,s, centerX+x, centerY+y, col);
	}

	/**
	 * Draw string.
	 * @param s String
	 * @param x X position relative to center expressed in character widths
	 * @param y Y position relative to center expressed in character heights
	 */
	public void print(final String s, final int x, final int y) {
		print(s,x,y,LemmFont.Color.GREEN);
	}

	/**
	 * Draw string horizontally centered.
	 * @param s String
	 * @param y0 Y position relative to center expressed in character heights
	 * @param col LemmFont color
	 * @return Absolute x position
	 */
	public int printCentered(final String s, final int y0, final LemmFont.Color col) {
		int y = y0*(LemmFont.getHeight()+4);
		int x = centerX-s.length()*LemmFont.getWidth()/2;
		LemmFont.strImage(gScreen,s, x, centerY+y, col);
		return x;
	}

	/**
	 * Draw string horizontally centered.
	 * @param s String
	 * @param y Y position relative to center expressed in character heights
	 * @return Absolute x position
	 */
	public int printCentered(final String s, final int y) {
		return printCentered(s,y,LemmFont.Color.GREEN);
	}

	/**
	 * Draw Image.
	 * @param img Image
	 * @param x X position relative to center
	 * @param y Y position relative to center
	 */
	public void drawImage(final BufferedImage img, final int x, final int y) {
		gScreen.drawImage(img,centerX+x,centerY+y,null);
	}

	/**
	 * Draw Image horizontally centered.
	 * @param img Image
	 * @param y Y position relative to center
	 */
	public void drawImage(final BufferedImage img, final int y) {
		int x = centerX-img.getWidth()/2;
		gScreen.drawImage(img,x,centerY+y,null);
	}

	/**
	 * Add Button.
	 * @param x X position relative to center in pixels
	 * @param y Y position relative to center in pixels
	 * @param img  Button image
	 * @param imgSelected Button selected image
	 * @param id Button ID
	 */
	public void addButton(final int x, final int y, final BufferedImage img, final  BufferedImage imgSelected, final int id) {
		Button b = new Button(centerX+x,centerY+y,id);
		b.SetImage(img);
		b.SetImageSelected(imgSelected);
		buttons.add(b);
	}

	/**
	 * Add text button.
	 * @param x0 X position relative to center (in characters)
	 * @param y0 Y position relative to center (in characters)
	 * @param id Button ID
	 * @param t  Button text
	 * @param ts Button selected text
	 * @param col Button text color
	 * @param cols Button selected text color
	 */
	public void addTextButton(final int x0, final int y0, final int id, final String t, final String ts, final LemmFont.Color col, final LemmFont.Color cols) {
		int x = x0*LemmFont.getWidth();
		int y = y0*(LemmFont.getHeight()+4);
		TextButton b = new TextButton(centerX+x,centerY+y,id);
		b.setText(t, col);
		b.setTextSelected(ts, cols);
		buttons.add(b);
	}

	/**
	 * React on left click.
	 * @param x Absolute x position in pixels
	 * @param y Absolute y position in pixels
	 * @return Button ID if button clicked, else -1
	 */
	public int handleLeftClick(final int x, final int y) {
		for (int i=0; i<buttons.size(); i++) {
			Button b = buttons.get(i);
			if (b.inside(x, y))
				return b.id;
		}
		return -1;
	}

	/**
	 * React on mouse hover.
	 * @param x Absolute x position
	 * @param y Absolute y position
	 */
	public void handleMouseMove(final int x, final int y) {
		for (int i=0; i<buttons.size(); i++) {
			Button b = buttons.get(i);
			if (b.inside(x, y))
				b.selected = true;
			else
				b.selected = false;
		}
	}

	/**
	 * Draw buttons on screen.
	 */
	public void drawButtons() {
		for (int i=0; i<buttons.size(); i++)
			buttons.get(i).draw(gScreen);
	}

	/**
	 * React on right click.
	 * @param x Absolute x position
	 * @param y Absolute y position
	 * @return Button ID if button clicked, else -1
	 */
	public int handleRightClick(final int x, final int y) {
		for (int i=0; i<buttons.size(); i++) {
			Button b = buttons.get(i);
			if (b.inside(x, y))
				return b.id;
		}
		return -1;
	}

}

/**
 * Button class for TextDialog.
 * @author Volker Oth
 */
class Button {
	/** x coordinate in pixels */
	private int x;
	/** y coordinate in pixels */
	private int y;
	/** width in pixels */
	protected int width;
	/** height in pixels */
	protected int height;
	/** button identifier */
	protected int id;
	/** true if button is selected */
	protected boolean selected;
	/** normal button image */
	protected BufferedImage image;
	/** selected button image */
	protected BufferedImage imgSelected;

	/**
	 * Constructor
	 * @param xi x position in pixels
	 * @param yi y position in pixels
	 * @param idi identifier
	 */
	Button(final int xi, final int yi, final int idi) {
		x = xi;
		y = yi;
		id = idi;
	}

	/**
	 * Set normal button image.
	 * @param img image
	 */
	void SetImage(final BufferedImage img) {
		image = img;
		if (image.getHeight() > height)
			height = image.getHeight();
		if (image.getWidth() > width)
			width = image.getWidth();
	}

	/**
	 * Set selected button image.
	 * @param img image
	 */
	void SetImageSelected(final BufferedImage img) {
		imgSelected = img;
		if (imgSelected.getHeight() > height)
			height = imgSelected.getHeight();
		if (imgSelected.getWidth() > width)
			width = imgSelected.getWidth();
	}

	/**
	 * Return current button image (normal or selected, depending on state).
	 * @return current button image
	 */
	BufferedImage getImage() {
		if (selected)
			return imgSelected;
		else
			return image;
	}

	/**
	 * Draw the button.
	 * @param g graphics object to draw on
	 */
	void draw(final Graphics2D g) {
		g.drawImage(getImage(),x,y,null);
	}

	/**
	 * Check if a (mouse) position is inside this button.
	 * @param xi
	 * @param yi
	 * @return true if the coordinates are inside this button, false if not
	 */
	boolean inside(final int xi, final int yi) {
		return (xi>=x && xi<x+width && yi>=y && yi<y+height);
	}

}

/**
 * Button class for TextDialog.
 * @author Volker Oth
 */
class TextButton extends Button {
	/**
	 * Constructor
	 * @param xi x position in pixels
	 * @param yi y position in pixels
	 * @param idi identifier
	 */
	TextButton(final int xi, final int yi, final int idi) {
		super(xi, yi, idi);
	}

	/**
	 * Set text which is used as button.
	 * @param s String which contains the button text
	 * @param color Color of the button (LemmFont color!)
	 */
	void setText(final String s, final LemmFont.Color color) {
		image = LemmFont.strImage(s, color);
		if (image.getHeight() > height)
			height = image.getHeight();
		if (image.getWidth() > width)
			width = image.getWidth();
	}

	/**
	 * Set text for selected button.
	 * @param s String which contains the selected button text
	 * @param color Color of the button (LemmFont color!)
	 */
	void setTextSelected(final String s, final LemmFont.Color color) {
		imgSelected = LemmFont.strImage(s, color);
		if (imgSelected.getHeight() > height)
			height = imgSelected.getHeight();
		if (imgSelected.getWidth() > width)
			width = imgSelected.getWidth();
	}
}