|
| 1 | +class UIRenderer |
| 2 | +{ |
| 3 | + static rgb(col, a =1) |
| 4 | + { |
| 5 | + return "rgb(" + col + "/ "+a+")"; |
| 6 | + } |
| 7 | + |
| 8 | + static drawGauge(ctx, x, y,w,h, filled, total, colour) |
| 9 | + { |
| 10 | + let empy ="100 100 100"; |
| 11 | + let outline = "120 40 0"; |
| 12 | + let step = Math.floor(h/total); |
| 13 | + let spacing = 1; |
| 14 | + for(let i = 0; i<total;i++) |
| 15 | + { |
| 16 | + ctx.beginPath(); |
| 17 | + ctx.roundRect(x+0.5,y+0.5+step*i, w-1,step-1-spacing,2); |
| 18 | + ctx.lineWidth=1; |
| 19 | + ctx.strokeStyle=this.rgb(outline); |
| 20 | + if(i<(total-filled)) |
| 21 | + { |
| 22 | + ctx.fillStyle=this.rgb(empy); |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + ctx.fillStyle=this.rgb(colour); |
| 27 | + } |
| 28 | + ctx.fill(); |
| 29 | + ctx.stroke(); |
| 30 | + } |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + static drawFrame(ctx,x,y,w,h,colour="") |
| 35 | + { |
| 36 | + let main = colour == ""? "255 64 0" : colour; |
| 37 | + let outline = "120 40 0"; |
| 38 | + let outline2 ="255 128 0"; |
| 39 | + ctx.beginPath(); |
| 40 | + ctx.roundRect(x+0.5,y+0.5,w,h,7); |
| 41 | + ctx.fillStyle=this.rgb(main); |
| 42 | + ctx.fill(); |
| 43 | + ctx.strokeStyle=this.rgb(outline); |
| 44 | + ctx.lineWidth=5; |
| 45 | + ctx.stroke(); |
| 46 | + ctx.strokeStyle=this.rgb(outline2); |
| 47 | + ctx.lineWidth=3; |
| 48 | + ctx.stroke(); |
| 49 | + |
| 50 | + } |
| 51 | + static drawButton(ctx,x,y,w,h,text) |
| 52 | + { |
| 53 | + let main ="255 64 0"; |
| 54 | + let highlight="255 200 125"; |
| 55 | + let shadow="200 40 0"; |
| 56 | + let outline = "120 40 0"; |
| 57 | + let outline2 ="255 128 0"; |
| 58 | + let bottom="100 40 0"; |
| 59 | + let fontcolour ="255 230 120"; |
| 60 | + ctx.beginPath(); |
| 61 | + ctx.roundRect(x+0.5,y+0.5,w,h,7); |
| 62 | + ctx.fillStyle=this.rgb(bottom); |
| 63 | + ctx.fill(); |
| 64 | + ctx.strokeStyle=this.rgb(outline); |
| 65 | + ctx.lineWidth=5; |
| 66 | + ctx.stroke(); |
| 67 | + ctx.strokeStyle=this.rgb(outline2); |
| 68 | + ctx.lineWidth=3; |
| 69 | + ctx.stroke(); |
| 70 | + ctx.beginPath(); |
| 71 | + ctx.roundRect(x+2.5,y-2,w-5,h-3,5); |
| 72 | + ctx.fillStyle=this.rgb(main); |
| 73 | + ctx.fill(); |
| 74 | + let grad = ctx.createLinearGradient(x+(w/2),y-2,x+(w/2),y+h); |
| 75 | + grad.addColorStop(0,this.rgb(highlight)); |
| 76 | + grad.addColorStop(0.4,this.rgb(highlight,0.3)); |
| 77 | + grad.addColorStop(1,this.rgb(highlight,0.0)); |
| 78 | + ctx.fillStyle=grad; |
| 79 | + ctx.fill(); |
| 80 | + let grad2 = ctx.createLinearGradient(x+(w/2),y-2,x+(w/2),y+h); |
| 81 | + grad2.addColorStop(0,this.rgb(shadow,0.0)); |
| 82 | + grad2.addColorStop(0.7,this.rgb(shadow,0.3)); |
| 83 | + grad2.addColorStop(1,this.rgb(shadow)); |
| 84 | + ctx.fillStyle=grad2; |
| 85 | + ctx.fill(); |
| 86 | + ctx.strokeStyle=this.rgb(outline); |
| 87 | + ctx.lineWidth=1; |
| 88 | + ctx.stroke(); |
| 89 | + ctx.font = "small-caps bold 30px monospace"; |
| 90 | + ctx.textAlign="center"; |
| 91 | + ctx.textBaseline="middle"; |
| 92 | + ctx.strokeStyle=this.rgb(outline); |
| 93 | + ctx.fillStyle=this.rgb(fontcolour); |
| 94 | + ctx.fillText(text,x+w/2,y+h/2-2); |
| 95 | + ctx.strokeText(text,x+w/2,y+h/2-2); |
| 96 | + } |
| 97 | +} |
0 commit comments