1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 /+
26  + TEST TEST TEST
27  +/
28 
29 module desgui.ready.droplist;
30 
31 import desgui.core.widget;
32 import desgui.core.label;
33 import desgui.core.layout;
34 import desgui.ready.button;
35 
36 void log(size_t line=__LINE__,T...)( string fmt, T args )
37 {
38     import std.stdio, std..string;
39     stderr.writefln( "line % 4d: %s", line, format( fmt, args ) );
40 }
41 
42 struct InertionChange(G=float)
43 {
44     G cur, next;
45     float T = 1.0f;
46 
47     void init( in G v, float t )
48     { init(v), T = t; }
49 
50     void init( in G v )
51     { cur = v; next = v; }
52 
53     auto opCall( float dt )
54     {
55         float cT = T;
56         if( dt / cT > 1.0f ) cT = dt;
57         cur += ( next - cur ) * ( dt / cT );
58         return cur;
59     }
60 }
61 
62 class DataButton(Type): DiButton
63 {
64     Type val;
65     Signal!Type onClickData;
66 
67     this( DiWidget par, in irect r, wstring str, Type v )
68     {
69         super( par, r, str, { onClickData(val); } );
70         val = v;
71     }
72 }
73 
74 class DiDropList: DiWidget
75 {
76     size_t cur_selected = 0;
77 
78     bool drop = false;
79 
80     size_t base_height;
81     size_t full_height;
82 
83     InertionChange!float in_h, h;
84 
85     void animUpdate( float dt )
86     {
87         h.next = drop ? full_height : base_height;
88         in_h.next = drop ? 0 : cur_selected * base_height;
89         bool check( in InertionChange!float ic, float v )
90         {
91             import std.math;
92             return abs( ic.cur - ic.next ) >= v;
93         }
94         if( check( h, 1 ) )
95             forceReshape( irect( rect.pos, rect.w, h(dt) ) );
96         if( check( in_h, 1 ) )
97             inner_offset.y = cast(int)-in_h(dt);
98 
99         parent.relayout();
100     }
101 
102     Signal!size_t onSelect;
103 
104     this( DiWidget par, in irect r, wstring[] list )
105     {
106         super( par );
107 
108         reshape( r );
109         size_lim.h.fix = true;
110         base_height = r.h;
111         full_height = r.h * list.length;
112 
113         in_h.init( 0, .1f );
114         h.init( base_height, .1f );
115 
116         auto react = ( size_t v ) 
117         { 
118             cur_selected = v; 
119             drop = !drop;
120             if( !drop ) onSelect( cur_selected );
121         };
122 
123         foreach( i, item; list )
124         {
125             auto buf = new DataButton!size_t( this, irect( 0, i*r.h, r.w, r.h ), item, i );
126             buf.onClickData.connect( react );
127         }
128 
129         auto ll = new DiLineLayout(DiLineLayout.Type.VERTICAL);
130         ll.stretchDirect = false;
131         layout = ll;
132 
133         import desutil.timer;
134         auto tm = new Timer;
135         idle.connect({ animUpdate( tm.cycle() ); });
136         update();
137     }
138 }