1 module soundtab.ui.pressurewidget;
2 
3 import dlangui.widgets.widget;
4 import soundtab.ui.noteutil;
5 
6 class PressureWidget : Widget {
7 
8     double _currentPressure = 0;
9     bool _currentProximity = false;
10 
11     uint _inactiveFrameColor = 0x808080;
12     uint _activeFrameColor = 0x00C000;
13     uint _gaugeColor = 0x00C000;
14 
15     this() {
16         super("pressure");
17         margins = Rect(5,5,5,5).pointsToPixels;
18         //styleId = "EDIT_LINE";
19     }
20 
21     /** 
22     Measure widget according to desired width and height constraints. (Step 1 of two phase layout). 
23     */
24     override void measure(int parentWidth, int parentHeight) {
25         int h = font.height * 150 / 100;
26         dstring label = "Pressure";
27         FontRef fnt = font;
28         Point sz = fnt.textSize(label);
29         measuredContent(parentWidth, parentHeight, sz.x + h / 2, h);
30     }
31 
32     /// Draw widget at its position to buffer
33     override void onDraw(DrawBuf buf) {
34         if (visibility != Visibility.Visible)
35             return;
36         Rect rc = _pos;
37         applyMargins(rc);
38         auto saver = ClipRectSaver(buf, rc, alpha);
39         DrawableRef bg = backgroundDrawable;
40         if (!bg.isNull) {
41             bg.drawTo(buf, rc, state);
42         }
43         applyPadding(rc);
44         _needDraw = false;
45 
46         dstring label = "Pressure";
47         FontRef fnt = font;
48         Point sz = fnt.textSize(label);
49         fnt.drawText(buf, rc.middlex - sz.x / 2, rc.middley - sz.y, label, 0x808080);
50 
51         rc.top = rc.middley + 2;
52 
53         buf.drawFrame(rc, _currentProximity ? (_currentPressure > 0 ? _activeFrameColor : _activeFrameColor) : _inactiveFrameColor, Rect(1,1,1,1), 
54                       0xE0202020);
55         rc.shrink(2, 2);
56 
57         if (_currentProximity && _currentPressure > 0) {
58             int x = cast(int)(rc.left + _currentPressure * rc.width);
59             buf.fillRect(Rect(rc.left, rc.top, x, rc.bottom), _gaugeColor);
60         }
61 
62     }
63 
64     void setPressure(double press, bool proximity) {
65         _currentPressure = press;
66         _currentProximity = proximity;
67         invalidate();
68     }
69 
70 
71 }
72