PixelDisplay.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. class PixelDisplay {
  2. views = [];
  3. constructor(screenY, sw=8, sh=8) {
  4. this.screenY = screenY;
  5. this.screenWidth = sw;
  6. this.screenHeight = sh;
  7. this.lastData = new Uint8Array(sw * sh);
  8. this._makeView();
  9. }
  10. _makeView() {
  11. const tileSize = Math.floor(192 / this.screenWidth);
  12. const ox = (192 - tileSize * this.screenWidth)/2;
  13. const oy = (490 - tileSize * this.screenHeight)/2;
  14. for(let i = 0; i < this.screenWidth * this.screenHeight; i++) {
  15. this.views[i] = hmUI.createWidget(hmUI.widget.FILL_RECT, {
  16. x: ox + tileSize * (i % this.screenWidth),
  17. y: oy + tileSize * Math.floor(i / this.screenWidth),
  18. w: tileSize,
  19. h: tileSize,
  20. color: 0x0
  21. });
  22. }
  23. }
  24. render(data) {
  25. for(let i = 0; i < this.screenWidth * this.screenHeight; i++) {
  26. let x = i % this.screenWidth;
  27. let y = Math.floor(i / this.screenWidth);
  28. let v = data[y * this.screenWidth + x];
  29. if(v === this.lastData[y * this.screenWidth + x]) continue;
  30. this.views[i].setProperty(hmUI.prop.MORE, {
  31. color: (v << 16) + (v << 8) + v
  32. });
  33. }
  34. this.lastData.set(data);
  35. }
  36. }