|
3 | 3 | if (!options.buttons)
|
4 | 4 | options.buttons = {"Yes":true,"No":false};
|
5 | 5 | var btns = Object.keys(options.buttons);
|
| 6 | + if (btns.length>6) throw new Error(">6 buttons"); |
6 | 7 | var btnPos;
|
7 | 8 | function draw(highlightedButton) {
|
8 | 9 | g.reset().setFontAlign(0,0);
|
|
13 | 14 | clearRect(0,Y,W-1,Y+4+title.h).
|
14 | 15 | drawString(title.text,W/2,Y+4+title.h/2);
|
15 | 16 | Y += title.h+4;
|
16 |
| - var BX = Math.min(2,btns.length), |
| 17 | + var BX = 0|"0123233"[btns.length], |
17 | 18 | BY = Math.ceil(btns.length / BX),
|
18 |
| - BW = W/BX, BH = 40; |
| 19 | + BW = (W-1)/BX, BH = (BY>1 || options.img)?40:50; |
19 | 20 | var H = R.y2-(Y + BY*BH);
|
20 | 21 | if (options.img) {
|
21 | 22 | var im = g.imageMetrics(options.img);
|
|
28 | 29 | drawString(msg.text,W/2,Y+H/2);
|
29 | 30 | btnPos = [];
|
30 | 31 | btns.forEach((btn,idx)=>{
|
31 |
| - var x = (idx&1)*BW + 2, y = R.y2-(BY-(idx>>1))*BH - 2, |
32 |
| - bw = BW-5, poly = [x+4,y, |
| 32 | + var ix=idx%BX,iy=0|(idx/BX),x = ix*BW + 2, y = R.y2-(BY-iy)*BH + 1, |
| 33 | + bw = BW-4, bh = BH-2, poly = [x+4,y, |
33 | 34 | x+bw-4,y,
|
34 | 35 | x+bw,y+4,
|
35 |
| - x+bw,y+BH-4, |
36 |
| - x+bw-4,y+BH, |
37 |
| - x+4,y+BH, |
38 |
| - x,y+BH-4, |
| 36 | + x+bw,y+bh-4, |
| 37 | + x+bw-4,y+bh, |
| 38 | + x+4,y+bh, |
| 39 | + x,y+bh-4, |
39 | 40 | x,y+4,
|
40 | 41 | x+4,y];
|
41 |
| - btnPos.push({x1:x-2, x2:x+bw, |
42 |
| - y1:y, y2:y+BH+2, |
43 |
| - poly: poly}); |
| 42 | + btnPos.push({x1:x-2, x2:x+BW-2, |
| 43 | + y1:y, y2:y+BH}); |
44 | 44 | var btnText = g.findFont(btn, {w:bw-4,h:BH-4,wrap:1});
|
45 | 45 | g.setColor(idx===highlightedButton ? g.theme.bgH : g.theme.bg2).fillPoly(poly).
|
46 |
| - setColor(idx===highlightedButton ? g.theme.fgH : g.theme.fg2).drawPoly(poly).drawString(btnText.text,x+bw/2,y+BH/2); |
| 46 | + setColor(idx===highlightedButton ? g.theme.fgH : g.theme.fg2).drawPoly(poly).drawString(btnText.text,x+bw/2,y+2+BH/2); |
47 | 47 | if (idx&1) y+=BH;
|
48 | 48 | });
|
49 | 49 | Bangle.setLCDPower(1); // ensure screen is on
|
|
57 | 57 | return new Promise(resolve=>{
|
58 | 58 | var ui = {mode:"custom", remove: options.remove, redraw: draw, back:options.back, touch:(_,e)=>{
|
59 | 59 | btnPos.forEach((b,i)=>{
|
60 |
| - if (e.x > b.x1 && e.x < b.x2 && |
61 |
| - e.y > b.y1 && e.y < b.y2) { |
| 60 | + if (e.x >= b.x1 && e.x <= b.x2 && |
| 61 | + e.y >= b.y1 && e.y <= b.y2 && !e.hit) { |
| 62 | + e.hit = true; // ensure we don't call twice if the buttons overlap |
62 | 63 | draw(i); // highlighted button
|
63 | 64 | g.flip(); // write to screen
|
64 | 65 | E.showPrompt(); // remove
|
|
0 commit comments