1 module ws.gui.list; 2 3 import 4 std.math, 5 std.algorithm, 6 ws.time, 7 ws.gl.draw, 8 ws.gui.base, 9 ws.gui.button; 10 11 12 class List: Base { 13 14 int padding = 10; 15 int entryHeight = 30; 16 double scroll = 0; 17 double scrollSpeed = 0; 18 double frameTime; 19 double frameLast; 20 21 override void resize(int[2] size){ 22 super.resize(size); 23 update; 24 } 25 26 void update(){ 27 auto height = ((entryHeight+padding)*cast(long)children.length-size.h+padding+entryHeight).max(0); 28 int y = cast(int)(pos.y + size.h - padding - entryHeight + scroll*entryHeight); 29 foreach(c; children){ 30 c.move([pos.x + padding, y]); 31 c.resize([size.w-padding*2, entryHeight]); 32 y -= c.size.y + padding; 33 } 34 onMouseMove(cursorPos.x, cursorPos.y); 35 36 } 37 38 override void onMouseButton(Mouse.button button, bool pressed, int x, int y){ 39 if(button == Mouse.wheelDown){ 40 if(pressed && scroll < children.length){ 41 scrollSpeed += 1.0; 42 return; 43 } 44 }else if(button == Mouse.wheelUp){ 45 if(pressed && scroll > 0){ 46 scrollSpeed -= 1.0; 47 return; 48 } 49 } 50 super.onMouseButton(button, pressed, x, y); 51 } 52 53 override void onDraw(){ 54 if(hidden) 55 return; 56 frameTime = now-frameLast; 57 frameLast = now; 58 if(scrollSpeed){ 59 scroll = (scroll + scrollSpeed*frameTime*0.06).min(children.length).max(0); 60 scrollSpeed = scrollSpeed.eerp(0, frameTime, frameTime*0.350, frameTime*0.350); 61 update; 62 } 63 draw.setColor(style.bg.normal); 64 draw.rect(pos, size); 65 super.onDraw(); 66 } 67 68 } 69 70 71 double eerp(double current, double target, double a, double b, double c){ 72 auto dir = current < target ? 1 : -1; 73 auto diff = (current-target).abs; 74 return current + (dir*(a*diff^^2 + b*diff + c)).min(diff).max(-diff); 75 } 76