1 module ws.gui.button;
2 
3 import
4 	ws.event,
5 	ws.gl.draw,
6 	ws.gui.base,
7 	ws.gui.style,
8 	ws.gui.text,
9 	ws.gui.point;
10 
11 
12 class Button: Base {
13 	
14 	Event!() leftClick;
15 	Event!() rightClick;
16 
17 	Text title;
18 	bool pressed;
19 	bool mouseFocus;
20 	string m_font = "sans";
21 	int m_font_size = 12;
22 
23 	@property string font(string s=""){
24 		if(s.length){
25 			m_font = s;
26 			title.setFont(m_font, m_font_size);
27 		}
28 		return m_font;
29 	}
30 
31 	@property int fontSize(int i=0){
32 		if(i > 0){
33 			m_font_size = i;
34 			title.setFont(m_font, m_font_size);
35 		}
36 		return m_font_size;
37 	}
38 
39 	@property void text(string s){
40 		title.text = s;
41 	}
42 
43 	@property string text(){
44 		return title.text;
45 	}
46 
47 	this(string t){
48 		leftClick = new Event!();
49 		rightClick = new Event!();
50 		Style style;
51 		style.bg = Color(
52 				[0.1, 0.1, 0.1, 0.5],
53 				[0.2, 0.2, 0.2, 0.7],
54 				[0.2, 0.2, 0.2, 1],
55 		);
56 		style.fg = Color(
57 				[1, 1, 1, 0.9],
58 				[1, 1, 1, 1],
59 				[1, 1, 1, 1]
60 		);
61 		title = addNew!Text;
62 		title.text = t;
63 		setStyle(style);
64 	}
65 
66 
67 	override void setStyle(Style style){
68 		super.setStyle(style);
69 		title.style = style;
70 	}
71 
72 
73 	override void onDraw(){
74 		draw.setColor(pressed ? style.bg.active :
75 				(mouseFocus ? style.bg.hover
76 						: style.bg.normal));
77 		draw.rect(pos, size);
78 		/+
79 		draw.setColor(pressed ? style.fg.active :
80 				(mouseFocus ? style.fg.hover
81 						: style.fg.normal));
82 		+/
83 		super.onDraw();
84 	}
85 
86 	override void resize(int[2] size){
87 		title.resize(size);
88 		//fontSize = cast(int)(size.h/1.8);
89 		super.resize(size);
90 	}
91 
92 	override void onMouseButton(Mouse.button button, bool p, int x, int y){
93 		super.onMouseButton(button, p, x, y);
94 		if(!p && pressed){
95 			if(button == Mouse.buttonLeft)
96 				leftClick();
97 			else if(button == Mouse.buttonRight)
98 				rightClick();
99 			pressed = false;
100 		}
101 		pressed = p;
102 	}
103 	
104 	
105 	override void onMouseFocus(bool focus){
106 		mouseFocus = focus;
107 		if(!focus)
108 			pressed = false;
109 	}
110 
111 
112 }
113