1 module armos.utils.gui.widgets.slider;
2 
3 import armos.utils.gui.widgets.widget;
4 
5 import armos.events;
6 
7 /++
8 値を操作するSliderを表す,Widgetを継承したclassです.
9 +/
10 class Slider(T) : Widget{
11     import std.format:format;
12     import std.conv;
13     public{
14         /++
15             初期化を行います.Slider固有の名前と操作する値,その上限下限を設定します.
16         +/
17         this(string name, ref T var, T min, T max){
18             _var = &var;
19             _varMin = min;
20             _varMax = max;
21             _name = name;
22             _height = 32;
23 
24             // static import armos.app;
25             // armos.events.addListener(armos.app.currentEvents.mouseMoved, this, &this.mouseMoved);
26             // armos.events.addListener(armos.app.currentEvents.mouseReleased, this, &this.mouseReleased);
27             // armos.events.addListener(armos.app.currentEvents.mousePressed, this, &this.mousePressed);
28 
29 
30             import armos.events;
31             import armos.app;
32             import rx;
33 
34             currentObservables.mouseMoved.doSubscribe!(event => this.mouseMoved(event));
35             currentObservables.mouseReleased.doSubscribe!(event => this.mouseReleased(event));
36             currentObservables.mousePressed.doSubscribe!(event => this.mousePressed(event));
37         };
38 
39         /++
40         +/
41         override void draw(){
42             import armos.graphics.renderer:color,
43                                            drawRectangle;
44 
45             color(_style.colors["background"]);
46             drawRectangle(0, 0, _style.width, _style.font.height*4);
47 
48             string varString = "";
49             static if(__traits(isIntegral, *_var)){
50                 varString = format("%d", *_var).to!string;
51             }else if(__traits(isFloating, *_var)){
52                 varString = format("%f", *_var).to!string;
53             }
54             _style.font.material.uniform("diffuse", _style.colors["font1"]);
55             
56             _style.font.draw(_name ~ " : " ~ varString, _style.font.width, 0);
57 
58             import armos.math:map;
59             immutable int currentValueAsSliderPosition = map( ( *_var ).to!float, _varMin.to!float, _varMax.to!float, 0.0, _style.width.to!float - _style.font.width.to!float*2.0).to!int;
60             color(_style.colors["base1"]);
61             drawRectangle(_style.font.width, _style.font.height, _style.width - _style.font.width*2, _style.font.height*2);
62             color(_style.colors["base2"]);
63             drawRectangle(_style.font.width, _style.font.height, currentValueAsSliderPosition.to!int, _style.font.height*2);
64         };
65 
66         /++
67         +/
68         override void mousePressed(ref armos.events.MousePressedEvent message){
69             _isPressing = isOnMouse(message.x, message.y);
70         }
71 
72         /++
73         +/
74         override void mouseMoved(ref armos.events.MouseMovedEvent message){
75             if(_isPressing){
76                 import armos.math:map;
77                 *_var = map( 
78                         ( message.x-_position[0]).to!float,
79                         _style.font.width.to!float, _style.width.to!float - _style.font.width.to!float, 
80                         _varMin.to!float, _varMax.to!float
81                         , true).to!T;
82             }
83         }
84 
85         /++
86         +/
87         override void mouseReleased(ref armos.events.MouseReleasedEvent message){
88             if(_isPressing){
89                 import armos.math:map;
90                 *_var = map( 
91                         ( message.x-_position[0]).to!float,
92                         _style.font.width.to!float, _style.width.to!float - _style.font.width.to!float, 
93                         _varMin.to!float, _varMax.to!float
94                         , true).to!T;
95                 _isPressing = false;
96             }
97         }
98     }//public
99 
100     private{
101         string _name;
102         T* _var;
103         T _varMin;
104         T _varMax;
105         bool _isPressing = false;
106 
107         bool isOnMouse(int x, int y){
108             immutable int localX = x-_position[0];
109             immutable int localY = y-_position[1];
110             if(_style.font.width<localX && localX<_style.width - _style.font.width){
111                 if(_style.font.height<localY && localY<_height - _style.font.height){
112                     return true;
113                 }
114             }
115             return false;
116         }
117     }//private
118 }//class Slider