1 /**
2 Copyright: Copyright (c) 2013-2014 Andrey Penechko.
3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module anchovy.gui.behaviors.labelbehavior;
8 
9 import anchovy.gui;
10 import anchovy.gui.interfaces.iwidgetbehavior;
11 
12 
13 class LabelBehavior : IWidgetBehavior
14 {
15 public:
16 
17 	override void attachTo(Widget widget)
18 	{
19 		widget.removeEventHandlers!DrawEvent();
20 		widget.addEventHandler(&handleDraw);
21 
22 		if (widget.peekPropertyAs!("text", string) is null)
23 			widget.setProperty!"text"("");
24 		if (widget.peekPropertyAs!("fontName", string) is null)
25 			widget.setProperty!"fontName"("normal");
26 
27 		GuiContext context = widget.getPropertyAs!("context", GuiContext);
28 
29 		TextLine line = context.guiRenderer.createTextLine(widget.getPropertyAs!("fontName", string));
30 
31 		widget.setProperty!"line"(line);
32 		widget.setProperty!"prefSize"(line.size);
33 
34 		widget.property("text").valueChanged.connect(&onTextChanged);
35 	}
36 
37 	void onTextChanged(FlexibleObject obj, Variant newText)
38 	{
39 		auto str = newText.coerce!dstring;
40 
41 		TextLine line = obj.getPropertyAs!("line", TextLine);
42 
43 		line.text = str;
44 
45 		obj["prefSize"] = line.size;
46 		invalidateLayout(cast(Widget)obj);
47 	}
48 
49 	bool handleDraw(Widget widget, DrawEvent event)
50 	{
51 		if (event.sinking && widget["isVisible"] == true)
52 		{
53 			event.guiRenderer.renderer.setColor(Color(0, 0, 0, 255));
54 			event.guiRenderer.drawTextLine(widget.getPropertyAs!("line", TextLine),
55 				widget.getPropertyAs!("staticPosition", ivec2), AlignmentType.LEFT_TOP);
56 		}
57 
58 		return true;
59 	}
60 }