1 /**
2 Grimoire
3 Copyright (c) 2017 Enalye
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising
7 from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute
11 it freely, subject to the following restrictions:
12 
13 	1. The origin of this software must not be misrepresented;
14 	   you must not claim that you wrote the original software.
15 	   If you use this software in a product, an acknowledgment
16 	   in the product documentation would be appreciated but
17 	   is not required.
18 
19 	2. Altered source versions must be plainly marked as such,
20 	   and must not be misrepresented as being the original software.
21 
22 	3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 module ui.checkbox;
26 
27 import std.conv: to;
28 
29 import core.all;
30 import common.all;
31 import render.sprite;
32 
33 import ui.widget;
34 import ui.label;
35 
36 class Checkbox: Widget {
37 	private {
38 		Sprite _uncheckedSprite, _checkedSprite;
39 		bool _isChecked;
40 	}
41 
42 	@property {
43 		Sprite uncheckedSprite() { return _uncheckedSprite; }
44 		Sprite uncheckedSprite(Sprite newSprite) {
45 			_uncheckedSprite = newSprite;
46 			_uncheckedSprite.fit(_size);
47 			return _uncheckedSprite;
48 		}
49 
50 		Sprite checkedSprite() { return _checkedSprite; }
51 		Sprite checkedSprite(Sprite newSprite) {
52 			_checkedSprite = newSprite;
53 			_checkedSprite.fit(_size);
54 			return _checkedSprite;
55 		}
56 
57 		bool isChecked() const { return _isChecked; }
58 		bool isChecked(bool newIsChecked) {
59 			_isChecked = newIsChecked;
60 			onCheck();
61 			return _isChecked;
62 		}
63 	}
64 
65 	this() {
66 		uncheckedSprite(fetch!Sprite("gui_unchecked"));
67 		checkedSprite(fetch!Sprite("gui_checked"));
68 	}
69 
70 	override void update(float deltaTime) {}
71 	
72 	override void onEvent(Event event) {}
73 
74 	override void draw() {
75 		if(_isChecked)
76 			_checkedSprite.draw(_position);
77 		else
78 			_uncheckedSprite.draw(_position);
79 	}
80 
81 	override void onSelect() {
82 		if(_isSelected || isLocked)
83 			return;
84 		isChecked = !_isChecked;
85 	}
86 
87     override void onSize() {
88         _uncheckedSprite.fit(_size);
89         _checkedSprite.fit(_size);
90     }
91 
92 	protected void onCheck() {}
93 }
94 
95 class TextCheckbox: Checkbox {
96 	Label label;
97 
98 	@property {
99 		alias color = label.color;
100 		alias text = label.text;
101 	}
102 
103 	this(string text) {
104 		label = new Label;
105 		label.text = text;
106 		onSize();
107 	}	
108 
109 	override void onPosition() {
110         label.position = _position + Vec2f((_size.x - label.size.x) / 2f, 0f);
111     }
112 
113 	override void onSize() {
114 		super.onSize();
115 		const Vec2f checkboxSize = Vec2f.one * label.size.y;
116 		_size = label.size + checkboxSize + Vec2f(25f, 0f);
117 		onPosition();
118 	}
119 
120 	override void update(float deltaTime) {}
121 
122 	override void draw() {
123 		const Vec2f checkboxPosition = _position - Vec2f((_size.x - label.size.y) / 2f, 0f);
124 
125 		label.draw();
126 		if(_isChecked)
127 			_checkedSprite.draw(checkboxPosition);
128 		else
129 			_uncheckedSprite.draw(checkboxPosition);
130 	}
131 }