1 /**
2 Copyright: Copyright (c) 2013 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.buttonbehavior;
8 
9 import anchovy.gui;
10 import anchovy.gui.interfaces.iwidgetbehavior;
11 
12 // version = Button_debug;
13 
14 class ButtonBehavior : IWidgetBehavior
15 {
16 public:
17 
18 	override void attachTo(Widget widget)
19 	{
20 		widget.addEventHandler(&pointerPressed);
21 		widget.addEventHandler(&pointerReleased);
22 		widget.addEventHandler(&pointerMoved);
23 		widget.addEventHandler(&pointerEntered);
24 		widget.addEventHandler(&pointerLeaved);
25 
26 		widget.setProperty!"isFocusable"(true);
27 		widget.setProperty!"style"("button");
28 	}
29 
30 	bool pointerPressed(Widget widget, PointerPressEvent event)
31 	{
32 		if (event.button == PointerButton.PB_LEFT)
33 		{
34 			widget.setProperty!"state"("pressed");
35 
36 			version(Button_debug) writeln("pressed");
37 		}
38 		return true;
39 	}
40 
41 	bool pointerReleased(Widget widget, PointerReleaseEvent event)
42 	{
43 		if (event.button == PointerButton.PB_LEFT)
44 		{
45 			widget.setProperty!"state"("hover");
46 
47 			version(Button_debug) writeln("hovered");
48 		}
49 		return true;
50 	}
51 
52 	bool pointerMoved(Widget widget, PointerMoveEvent event)
53 	{
54 		return true;
55 	}
56 
57 	bool pointerEntered(Widget widget, PointerEnterEvent event)
58 	{
59 		if (event.context.eventDispatcher.pressedWidget is this)
60 		{
61 			widget.setProperty!"state"("pressed");
62 
63 			version(Button_debug) writeln("pressed");
64 		}
65 		else
66 		{
67 			widget.setProperty!"state"("hover");
68 
69 			version(Button_debug) writeln("hovered");
70 		}
71 		return true;
72 	}
73 
74 	bool pointerLeaved(Widget widget, PointerLeaveEvent event)
75 	{
76 		widget.setProperty!"state"("normal");
77 
78 		version(Button_debug) writeln("normal");
79 
80 		return true;
81 	}
82 }
83