1 module dinu.resultWindow;
2 
3 
4 import dinu;
5 
6 
7 __gshared:
8 
9 
10 class ResultWindow: ws.wm.Window {
11 
12 	immutable(Match!Command)[] matches;
13 
14 	double scrollCurrent = 0;
15 	double selectCurrent = 0;
16 	long lastUpdate;
17 
18 	//GlContext context;
19 
20 	this(){
21 		super(1.4.em*15, 500, "dinu results", true);
22 		draw.setFont(options.font, 12);
23 		//context.blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
24 		//context.enable(GL_BLEND);
25 	}
26 
27 	/+
28 	override void drawInit(){
29 		context = new GlContext(windowHandle);
30 		draw = new GlDraw(context);
31 	}
32 	+/
33 
34 	override void resized(int[2] size){
35 		super.resized(size);
36 		onDraw;
37 	}
38 
39 	void update(WindowMain windowMain){
40 		matches = commandBuilder.results;
41 		auto targetHeight = min(15, matches.length)*1.4.em+0.3.em;
42 		int targetWidth;
43 		foreach(match; matches){
44 			if(targetWidth < draw.width(match.data[0].filterText)+16)
45 				targetWidth = draw.width(match.data[0].filterText)+16;
46 		}
47 		auto sep = (windowMain.size.w*options.ratio).to!int;
48 		if(targetHeight != size.h || targetWidth != size.w)
49 			resize([targetWidth.min(windowMain.size.w-sep*2).to!int.max(1), targetHeight.max(1)]);
50 		auto targetX = windowMain.pos.x+(windowMain.size.w*options.ratio).to!int;
51 		targetX += draw.width(commandBuilder.finishedPart);
52 		if(pos != [targetX, windowMain.size.y+windowMain.pos.y])
53 			move([targetX, windowMain.size.y+windowMain.pos.y]);
54 		if(!matches.length && !hidden)
55 			hide;
56 		else if(matches.length && hidden)
57 			show;
58 		if(hidden)
59 			return;
60 
61 		auto cur = (now*1000).lround;
62 		auto delta = cur - lastUpdate;
63 		lastUpdate = cur;
64 
65 		auto scrollTarget = min(max(0, cast(long)matches.length-15),
66 								max(0, commandBuilder.selected-15/2));
67 		if(options.animations > 0){
68 			scrollCurrent = scrollCurrent.eerp(scrollTarget, delta/150.0/options.animations);
69 			selectCurrent = selectCurrent.eerp(commandBuilder.selected, delta/100.0/options.animations);
70 		}else{
71 			scrollCurrent = scrollTarget;
72 			selectCurrent = commandBuilder.selected;
73 		}
74 	}
75 
76 	override void onDraw(){
77 		if(hidden)
78 			return;
79 		auto padding = 0.4.em;
80 		auto entryHeight = 1.4.em;
81 		auto textOffset = ((entryHeight - draw.fontHeight)/2.0).lround.to!int;
82 		draw.setColor(options.colorBg);
83 		draw.rect([0,0], size);
84 		if(selectCurrent < 0 && (commandBuilder.command.length==1)){
85 			draw.setColor(options.colorHintBg);
86 			draw.rect([0,size.h-entryHeight], [size.w,entryHeight]);
87 		}
88 		draw.setColor(options.colorSelected);
89 		draw.rect([0,cast(int)(size.h - (selectCurrent-scrollCurrent)*entryHeight)-entryHeight], [size.w, entryHeight]);
90 		auto start = cast(size_t)scrollCurrent;
91 		foreach(int i, result; matches[start.min($-1).max(0)..min($, start+16)]){
92 			int x = padding;
93 			int y = cast(int)(size.h - entryHeight*(i-(scrollCurrent-start)) - entryHeight);
94 			x += result.data[0].draw(draw, [x, y+textOffset], start+i == commandBuilder.selected, result.positions);
95 			auto hint = result.data[0].hint;
96 			debug(Score){
97 				hint = result.score.to!string;
98 			}
99 			if(hint.length){
100 				draw.setColor(options.colorHint);
101 				draw.text([max(x, size.w-draw.width(hint)-1.4.em), y+textOffset], hint);
102 			}
103 		}
104 		int scrollbarWidth = padding;
105 		draw.setColor(options.colorBg);
106 		//draw.rect([size.w-0.2.em, 0], [0.2.em, size.h]);
107 		if(matches.length > 15){
108 			double scrollbarHeight = size.h/(max(1.0, (cast(long)matches.length-cast(long)14).log2));
109 			int scrollbarOffset = cast(int)((size.h - scrollbarHeight - 0.3.em + 1) * (scrollCurrent/(max(1.0, matches.length-15))));
110 			draw.setColor(options.colorHintBg);
111 			draw.rect([size.w-scrollbarWidth, size.h-scrollbarOffset-scrollbarHeight.to!int], [scrollbarWidth, cast(int)scrollbarHeight]);
112 		}
113 		super.onDraw;
114 	}
115 
116 }