1 module pixelperfectengine.concrete.elements.encoder;
2 
3 public import pixelperfectengine.concrete.elements.base;
4 
5 import std.math;
6 
7 /** 
8  * Implements a sliding encoder for various data-entry purposes.
9  */
10 public class SlidingEncoder : WindowElement {
11 	protected Box			track;				///The track, where the slider resides.
12 	protected Bitmap8Bit	slider;				///The bitmap for the slider.
13 	protected Bitmap8Bit	background;			///The bitmap for the background.
14 	protected uint			_maxValue;			///The maximum value of this encoder.
15 	protected uint			_value;				///The current value of this encoder.
16 	protected uint			trackLength;		///The total usable length of the encoder in pixels.
17 	protected double		valRatio;			///Pixel-to-value ratio of the encoder.
18 	public EventDeleg		onValueChange;		///Called when the value is changed.
19 	/** 
20 	 * Creates an instance of a sliding encoder
21 	 * Params:
22 	 *   source = source string for events.
23 	 *   position = Determines where the encoder should be drawn on the window.
24 	 *   slider = Specifies the slider of the encoder.
25 	 *   background = Specifies the background of the encoder. should have the exact sizes as position.
26 	 *   track = Specifies where the track of the encoder is. Is relative toupper-left corner of position.
27 	 *   _maxValue = Maximum value that can be reached by this encoder.
28 	 */
29 	public this(string source, Box position, Bitmap8Bit slider, Bitmap8Bit background, Box track, uint _maxValue) {
30 		this.source = source;
31 		this.position = position;
32 		this.slider = slider;
33 		this.background = background;
34 		this.track = track;
35 		trackLength = track.height - (slider.height & ~1);
36 		this._maxValue = _maxValue;
37 		valRatio = trackLength / _maxValue;
38 	}
39 	public @property uint maxValue() @nogc @safe pure nothrow const {
40 		return _maxValue;
41 	}
42 	public @property uint maxValue(uint val) {
43 		_maxValue = val;
44 		valRatio = trackLength / _maxValue;
45 		if (_maxValue < _value)
46 			_value = _maxValue;
47 		draw();
48 		return _maxValue;
49 	}
50 	public @property uint value() @nogc @safe pure nothrow const {
51 		return _value;
52 	}
53 	public @property uint value(uint val) {
54 		_value = val;
55 		if (_maxValue < _value)
56 			_value = _maxValue;
57 		draw();
58 		return _value;
59 	}
60 	override public void draw() {
61 		parent.bitBLT(position.cornerUL, background);
62 		const Point sliderpos = Point(position.left + track.left, position.top + track.top + cast(uint)(valRatio * _value));
63 		parent.bitBLT(sliderpos, slider);
64 		StyleSheet ss = getStyleSheet();
65 		if (isFocused) {
66 			const int textPadding = ss.drawParameters["horizTextPadding"];
67 			parent.drawBoxPattern(position - textPadding, ss.pattern["blackDottedLine"]);
68 		}
69 		if (state == ElementState.Disabled) {
70 			parent.bitBLTPattern(position, ss.getImage("ElementDisabledPtrn"));
71 		}
72 		if (onDraw !is null) {
73 			onDraw();
74 		}
75 	}
76 	public override void passMCE(MouseEventCommons mec, MouseClickEvent mce) {
77 		if (mce.button == MouseButton.Left && track.isBetween(mce.x - position.left, mce.y - position.top)) {
78 			const int position = mce.x - position.left - (slider.height / 2);
79 			parent.requestFocus(this);
80 			if (position <= 0) value = 0;
81 			else if (position >= trackLength) value = _maxValue;
82 			else {
83 				value = cast(uint)nearbyint(value * valRatio);
84 			}
85 			if (onValueChange !is null)
86 				onValueChange(new Event(this, EventType.MouseScroll, SourceType.WindowElement));
87 		} else {
88 			super.passMCE(mec, mce);
89 		}
90 	}
91 	public override void passMME(MouseEventCommons mec, MouseMotionEvent mme) {
92 		if (onValueChange !is null)
93 			onValueChange(new Event(this, EventType.MouseScroll, SourceType.WindowElement));
94 	}
95 	public override void passMWE(MouseEventCommons mec, MouseWheelEvent mwe) {
96 		const int diff = mwe.x + cast(int)nearbyint(mwe.y / valRatio);
97 		value(_value + diff);
98 		if (onValueChange !is null)
99 			onValueChange(new Event(this, EventType.MouseScroll, SourceType.WindowElement));
100 	}
101 }
102 public class RotaryEncoder : WindowElement {
103 	protected Bitmap8Bit		dot;
104 	protected Bitmap8Bit		knob;
105 
106 	override public void draw() {
107 
108 	}
109 }