1 module pixelperfectengine.concrete.popup.popuplabel;
2 
3 import pixelperfectengine.concrete.popup.base;
4 
5 /**
6  *
7  */
8 public class PopUpLabel : PopUpElement {
9 	protected int maxWidth, maxHeight;
10 	protected int scroll;
11 	public this (Text text, string source, int maxWidth = 256, int maxHeight = 256) {
12 		this.text = text;
13 		this.source = source;
14 		this.maxWidth = maxWidth;
15 		this.maxHeight = maxHeight;
16 	}
17 	public override void draw(){
18 		import pixelperfectengine.graphics.draw;
19 		
20 		StyleSheet ss = getStyleSheet();
21 		Text[] outputTextInLines = text.breakTextIntoMultipleLines(maxWidth);
22 		int height, finalHeight;
23 		foreach (line ; outputTextInLines) {
24 			height += line.getHeight();
25 		}
26 		finalHeight = height + 2 * ss.drawParameters["PopUpLabelVertPadding"];
27 		if (parent !is null) {
28 			const int maxScreenHeight = parent.getRasterSizes()[1];
29 			//Priority of height limiting: locally set height, screen height
30 			//If locally set height limit is greater than the screen, then the max height will be overwritten.
31 			if (maxScreenHeight < maxHeight)
32 				maxHeight = maxScreenHeight;
33 			if (finalHeight > maxHeight)
34 				finalHeight = maxHeight;
35 			else
36 				scroll = 0;
37 		}
38 		if (output is null) {
39 			output = new BitmapDrawer(maxWidth + 2 * ss.drawParameters["PopUpLabelHorizPadding"], 
40 					finalHeight);
41 			position = Box.bySize(0, 0, output.output.width, output.output.height);
42 		}
43 		with (output) {
44 			drawFilledBox(Box.bySize(0, 0, position.width, position.height), ss.getColor("window"));
45 			drawLine(position.cornerUL, position.cornerUR, ss.getColor("windowascent"));
46 			drawLine(position.cornerUL, position.cornerLL, ss.getColor("windowascent"));
47 			drawLine(position.cornerLL, position.cornerLR, ss.getColor("windowdescent"));
48 			drawLine(position.cornerUR, position.cornerLR, ss.getColor("windowdescent"));
49 			drawMultiLineText(Box(ss.drawParameters["PopUpLabelHorizPadding"], ss.drawParameters["PopUpLabelVertPadding"],
50 					position.width - ss.drawParameters["PopUpLabelHorizPadding"] - 1, 
51 					position.height - ss.drawParameters["PopUpLabelVertPadding"] - 1), outputTextInLines, 0 , scroll);
52 		}
53 	}
54 	public override void passMCE(MouseEventCommons mec, MouseClickEvent mce) {
55 		parent.endPopUpSession(this);
56 	}
57 	public override void passMME(MouseEventCommons mec, MouseMotionEvent mme) {
58 		if (!position.isBetween(mme.x, mme.y))
59 			parent.endPopUpSession(this);
60 	}
61 	public override void passMWE(MouseEventCommons mec, MouseWheelEvent mwe) {
62 		scroll += mwe.y;
63 		draw();
64 	}
65 }