Advertisement
Guest User

ciq-scrolling-text

a guest
Mar 28th, 2021
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. //v3
  2. using Toybox.WatchUi;
  3. using Toybox.Graphics;
  4.  
  5. class testappView extends WatchUi.View {
  6. var updateTimer;
  7. var scrollableText, scrollableText2;
  8.  
  9. var screenWidth, screenHeight, screenShape;
  10. function initialize() {
  11. View.initialize();
  12.  
  13. var settings = System.getDeviceSettings();
  14. screenWidth = settings.screenWidth;
  15. screenHeight = settings.screenHeight;
  16. screenShape = settings.screenShape;
  17.  
  18. updateTimer = new Timer.Timer();
  19.  
  20. // scroll across the full screen wdith
  21. scrollableText = new ScrollableText(
  22. 0, // x
  23. screenHeight/2, //y
  24. Graphics.FONT_LARGE, // font
  25. "the quick brown fox jumps over the lazy dog", //text
  26. screenWidth //scrollableWidth
  27. );
  28.  
  29. // scroll across approximately half the screen width
  30. scrollableText2 = new ScrollableText(
  31. screenWidth/3, // x
  32. screenHeight/4, //y
  33. Graphics.FONT_LARGE, // font
  34. "the quick brown fox jumps over the lazy dog", //text
  35. getWidth(screenHeight/4)/2 //scrollableWidth
  36. );
  37. }
  38.  
  39. // this example only works properly with rectangular and round devices
  40. function getWidth(y) {
  41. if (screenShape == System.SCREEN_SHAPE_RECTANGLE) {
  42. return screenWidth;
  43. } else { // semiround and round
  44. // this isn't quite right for semiround watches
  45.  
  46. var radius = screenWidth / 2;
  47. //var angle = 2 * Math.toDegrees(Math.acos(1 - (y.toFloat() / radius))); // Angle = 2 * arccos(1 - height(y) / radius)
  48. //return (2 * radius * Math.sin(Math.toRadians(angle) / 2)).toNumber();
  49. return (2 * radius * Math.sin(Math.toRadians(2 * Math.toDegrees(Math.acos(1 - (y.toFloat() / radius)))) / 2)).toNumber();
  50. }
  51. }
  52.  
  53. function triggerUpdate() {
  54. WatchUi.requestUpdate();
  55. }
  56.  
  57. // Update the view
  58. function onUpdate(dc) {
  59. dc.setColor(0xffffff, 0);
  60. dc.clear();
  61.  
  62. scrollableText.onUpdate(dc);
  63. scrollableText2.onUpdate(dc);
  64.  
  65. // 50 ms is the minimum timer interval for most (all?) devices
  66. updateTimer.start(method(:triggerUpdate), 50 /* ms */, false);
  67. }
  68. }
  69.  
  70. // this example only works with left justified and vertically centred text
  71. // TODO: support non-vertically centred text
  72. // TODO: support shortening text with ellipses ("...")
  73. class ScrollableText {
  74. var screenHeight;
  75. var currentPos = 0;
  76.  
  77. var x, y, font, text, scrollableWidth;
  78. var textWidth = null;
  79. var fontHeight = null;
  80. // See Graphics.drawText
  81. function initialize(x, y, font, text, scrollableWidth) {
  82. self.x = x;
  83. self.y = y;
  84. self.font = font;
  85. self.text = text;
  86. self.scrollableWidth = scrollableWidth;
  87.  
  88. var settings = System.getDeviceSettings();
  89. screenHeight = settings.screenHeight;
  90. }
  91.  
  92. function onUpdate(dc) {
  93. if (textWidth == null) {
  94. textWidth = dc.getTextWidthInPixels(text, font);
  95. }
  96. if (fontHeight == null) {
  97. fontHeight = dc.getFontHeight(font);
  98. }
  99.  
  100. dc.setClip(x, 0, scrollableWidth, screenHeight); //only available in CIQ 2.3.0 and higher
  101.  
  102. dc.drawText(x + currentPos, y, font, text, Graphics.TEXT_JUSTIFY_LEFT|Graphics.TEXT_JUSTIFY_VCENTER);
  103.  
  104. // scroll by 2 pixels. (1 is smoother, but a bit too slow)
  105. currentPos = currentPos - 2;
  106. if (currentPos < -textWidth) {
  107. currentPos = scrollableWidth-1;
  108. }
  109.  
  110. dc.clearClip(); //only available in CIQ 2.3.0 and higher
  111.  
  112. // draw rectangle to demonstrate bounds of scrolling region
  113. dc.drawRectangle(x, y - fontHeight/2, scrollableWidth, fontHeight + 2);
  114. }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement