-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtonDemo.js
140 lines (107 loc) · 4.17 KB
/
ButtonDemo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
let ScreenWidth = g.getWidth();
let ScreenHeight = g.getHeight();
g.clear(true);
if (g.drawRoundedRect == null) {
g.drawRoundedRect = function drawRoundedRect (x1,y1, x2,y2, r) {
let x,y;
if (x1 > x2) { x = x1; x1 = x2; x2 = x; }
if (y1 > y2) { y = y1; y1 = y2; y2 = y; }
r = Math.min(r || 0, (x2-x1)/2, (y2-y1)/2);
let cx1 = x1+r, cx2 = x2-r;
let cy1 = y1+r, cy2 = y2-r;
this.drawLine(cx1,y1, cx2,y1);
this.drawLine(cx1,y2, cx2,y2);
this.drawLine(x1,cy1, x1,cy2);
this.drawLine(x2,cy1, x2,cy2);
x = r; y = 0;
let dx,dy, Error = 0;
while (y <= x) {
dy = 1 + 2*y; y++; Error -= dy;
if (Error < 0) {
dx = 1 - 2*x; x--; Error -= dx;
}
this.setPixel(cx1 - x, cy1 - y); this.setPixel(cx1 - y, cy1 - x);
this.setPixel(cx2 + x, cy1 - y); this.setPixel(cx2 + y, cy1 - x);
this.setPixel(cx2 + x, cy2 + y); this.setPixel(cx2 + y, cy2 + x);
this.setPixel(cx1 - x, cy2 + y); this.setPixel(cx1 - y, cy2 + x);
}
};
}
if (g.fillRoundedRect == null) {
g.fillRoundedRect = function fillRoundedRect (x1,y1, x2,y2, r) {
let x,y;
if (x1 > x2) { x = x1; x1 = x2; x2 = x; }
if (y1 > y2) { y = y1; y1 = y2; y2 = y; }
r = Math.min(r || 0, (x2-x1)/2, (y2-y1)/2);
let cx1 = x1+r, cx2 = x2-r;
let cy1 = y1+r, cy2 = y2-r;
this.fillRect(x1,cy1, x2,cy2);
x = r; y = 0;
let dx,dy, Error = 0;
while (y <= x) {
dy = 1 + 2*y; y++; Error -= dy;
if (Error < 0) {
dx = 1 - 2*x; x--; Error -= dx;
}
this.drawLine(cx1 - x, cy1 - y, cx2 + x, cy1 - y);
this.drawLine(cx1 - y, cy1 - x, cx2 + y, cy1 - x);
this.drawLine(cx1 - x, cy2 + y, cx2 + x, cy2 + y);
this.drawLine(cx1 - y, cy2 + x, cx2 + y, cy2 + x);
}
};
}
/**** Button ****/
function Button (Text, Options) {
function renderButton (Details) {
let x = Details.x, Width = Details.w, halfWidth = Width/2;
let y = Details.y, Height = Details.h, halfHeight = Height/2;
let Padding = Details.pad || 0;
let Hilite = Details.hilite || false;
if (Details.bgCol != null) {
g.setBgColor(Details.bgCol);
g.clearRect(x,y, x + Width-1,y + Height-1);
}
if (Hilite) {
g.setColor(g.theme.bgH); // no typo!
g.fillRoundedRect(x+Padding,y+Padding, x+Width-Padding-1,y+Height-Padding-1,8);
}
g.setColor (Hilite ? g.theme.fgH : Details.col || g.theme.fg);
g.setBgColor(Hilite ? g.theme.bgH : Details.bgCol || g.theme.bg);
if (Details.font != null) { g.setFont(Details.font); }
g.setFontAlign(0,0);
g.drawRoundedRect(x+Padding,y+Padding, x+Width-Padding-1,y+Height-Padding-1,8);
g.setClipRect(x+Padding,y+Padding, x+Width-Padding-1,y+Height-Padding-1);
g.drawString(Details.label, x+halfWidth,y+halfHeight);
g.drawString(Details.label, x+halfWidth+1,y+halfHeight);
g.drawString(Details.label, x+halfWidth,y+halfHeight+1);
g.drawString(Details.label, x+halfWidth+1,y+halfHeight+1);
}
let Result = Object.assign((
Options == null ? {} : Object.assign({}, Options.common || {}, Options)
), {
type:'custom', render:renderButton, label:Text || 'Tap'
});
let Padding = Result.pad || 0;
let TextMetrics;
if (! Result.width || ! Result.height) {
if (Result.font == null) {
Result.font = g.getFont();
} else {
g.setFont(Result.font);
}
TextMetrics = g.stringMetrics(Result.label);
}
Result.width = Result.width || TextMetrics.width + 2*10 + 2*Padding;
Result.height = Result.height || TextMetrics.height + 2*5 + 2*Padding;
return Result;
}
g.setFont12x20(); // does not seem to be respected in layout!
let commonSettings = { font:'12x20', width:100, height:40, pad:4 };
let Layout = require('Layout');
let Display = new Layout({
type:'v', c:[
Button('normal', { common:commonSettings }),
Button('hilited', { common:commonSettings, hilite:true }),
]
});
Display.render();