1 module ws.gui.tabs;
2 
3 import 
4 	std.algorithm,
5 	ws.list,
6 	ws.io,
7 	ws.gl.draw,
8 	ws.gui.base,
9 	ws.gui.button;
10 
11 
12 class TabButton: Button {
13 	this(string s){
14 		super(s);
15 	}
16 	bool active;
17 	void disable(){
18 		mouseFocus = false;
19 		active = false;
20 	}
21 	void activate(){
22 		mouseFocus = true;
23 		active = true;
24 	}
25 	override void onMouseFocus(bool focus){
26 		if(!active)
27 			mouseFocus = focus;
28 		if(!focus)
29 			pressed = false;
30 	}
31 }
32 
33 
34 class Tabs: Base {
35 
36 	Point buttonSize = Point(150, 30);
37 	long active = -1;
38 	double activeSmooth = 0;
39 	enum: int {
40 		top, bottom, left, right
41 	}
42 	int position;
43 	double offset = 0.5;
44 	Style buttonStyle;
45 
46 	this(int pos = top){
47 		position = pos;
48 		pages = new List!Page;
49 	}
50 
51 	override void setStyle(Style style){
52 		super.setStyle(style);
53 		swap(style.bg.normal, style.bg.hover);
54 		buttonStyle = style;
55 	}
56 
57 	protected string font = "Ubuntu-B";
58 	protected int fontSize = 12;
59 
60 	Page addPage(TabButton button, Base gui){
61 		add(button);
62 		add(gui);
63 		button.font = font;
64 		button.fontSize = fontSize;
65 		button.setStyle = buttonStyle;
66 		button.resize(buttonSize);
67 		size_t current = pages.length;
68 		button.leftClick ~= {
69 			if(active > -1){
70 				pages[cast(size_t)active].content.hide();
71 				pages[cast(size_t)active].button.disable();
72 			}
73 			button.activate();
74 			gui.show();
75 			setTop(gui);
76 			active = current;
77 			updateSize();
78 		};
79 		pages ~= Page(button, gui);
80 		gui.hide();
81 		updateSize();
82 		return pages.back;
83 	}
84 
85 	Page addPage(string name, Base gui){
86 		auto button = new TabButton(name);
87 		return addPage(button, gui);
88 	}
89 
90 	void updateSize(){
91 		int x = (position == left ? buttonSize.x : 0);
92 		int y = (position == bottom ? buttonSize.y : 0);
93 		if(active > -1){
94 			auto gui = pages[cast(size_t)active].content;
95 			gui.moveLocal([x, y]);
96 			gui.resize([
97 				size.w - (position == left || position == right ? buttonSize.x : 0),
98 				size.h - (position == top || position == bottom ? buttonSize.y : 0)
99 			]);
100 		}
101 		auto s = Point(0,0);
102 		foreach(p; pages)
103 			s += p.button.size;
104 		int[2] start = (size.a - s)*offset;
105 		foreach(p; pages){
106 			p.button.moveLocal([
107 				position == right ? size.x-buttonSize.x :
108 				position == left ? 0 :
109 				start.x,
110 				position == top ? size.y-buttonSize.y :
111 				position == bottom ? 0 :
112 				size.y-start.y-buttonSize.h
113 			]);
114 			start = start.a + p.button.size;
115 		}
116 	}
117 
118 	override void onDraw(){
119 		if(active > -1){
120 			draw.setColor(style.bg.normal);
121 			auto g = pages[cast(size_t)active].content;
122 			draw.rect(g.pos, g.size);
123 		}
124 		super.onDraw();
125 	}
126 
127 	override void resize(int[2] size){
128 		super.resize(size);
129 		updateSize();
130 	}
131 
132 	struct Page {
133 		TabButton button;
134 		Base content;
135 	}
136 
137 	List!Page pages;
138 
139 }