11var data = require ( "Storage" ) . readJSON ( "mtnclock.json" , 1 ) || { } ;
22
3+ if ( ! Array . isArray ( data . rows ) ) {
4+ data . rows = [ ] ;
5+ require ( "Storage" ) . writeJSON ( "mtnclock.json" , data ) ;
6+ }
7+
8+ let showingSettings = false ;
9+ let clockinfosSettings = [ ] ;
10+ let clockinfosMain = [ ] ;
11+ let clockinfos = require ( "clock_info" ) . load ( ) ;
12+
313let weather ;
414try {
515 weather = require ( 'weather' ) ;
@@ -166,7 +176,7 @@ function draw(color) {
166176 }
167177 }
168178 //mountains
169- drawMtn ( { mtn1 :color . mtn2 , mtn2 :color . mtn1 } , { x :px ( 35 ) , y :py ( 30 ) } , { w :px ( 38 ) , h :py ( 17 ) } ) ;
179+ drawMtn ( { mtn1 :color . mtn2 , mtn2 :color . mtn1 } , { x :px ( 43 ) , y :py ( 28 ) } , { w :px ( 38 ) , h :py ( 19 ) } ) ;
170180 drawMtn ( { mtn1 :color . mtn2 , mtn2 :color . mtn1 } , { x :px ( 10 ) , y :py ( 20 ) } , { w :px ( 50 ) , h :py ( 30 ) } ) ;
171181 drawMtn ( { mtn1 :color . mtn1 , mtn2 :color . mtn2 } , { x :px ( 90 ) , y :py ( 20 ) } , { w :px ( 70 ) , h :py ( 30 ) } ) ;
172182 //lake
@@ -200,15 +210,33 @@ function draw(color) {
200210
201211 //clock text
202212 ( color . clock == undefined ) ? g . setColor ( 0xFFFF ) : g . setColor ( color . clock ) ;
203- g . setFont ( "Vector" , py ( 20 ) ) . setFontAlign ( - 1 , - 1 ) . drawString ( ( require ( "locale" ) . time ( new Date ( ) , 1 ) . replace ( " " , "" ) ) , px ( 2 ) , py ( 67 ) ) ;
204- g . setFont ( "Vector" , py ( 10 ) ) . drawString ( require ( 'locale' ) . dow ( new Date ( ) , 1 ) + " " + new Date ( ) . getDate ( ) + " " + require ( 'locale' ) . month ( new Date ( ) , 1 ) + ( ( data . temp == undefined ) ? "" : " | " + require ( 'locale' ) . temp ( Math . round ( data . temp - 273.15 ) ) . replace ( ".0" , "" ) ) , px ( 2 ) , py ( 87 ) ) ;
213+ const start = 87 ;
214+ for ( let r = 0 ; r < data . rows . length ; r ++ ) {
215+ let a = data . rows [ r ] . menuA ;
216+ let b = data . rows [ r ] . menuB ;
217+ if ( clockinfos [ a ] && clockinfos [ a ] . items [ b ] ) {
218+ let ci = clockinfos [ a ] . items [ b ] ;
219+ let text = ci . get ( ) . text ;
220+ if ( ! ( clockinfosMain [ a ] && clockinfosMain [ a ] [ b ] ) ) {
221+ clockinfosMain [ a ] = clockinfosMain [ a ] || [ ] ;
222+ clockinfosMain [ a ] [ b ] = true ;
223+ ci . show ( ) ;
224+ ci . on ( "redraw" , clockinfoRedraw ) ;
225+ }
226+ g . setFont ( "Vector" , py ( 10 ) ) . setFontAlign ( - 1 , - 1 ) . drawString ( text , px ( 2 ) , py ( ( start - ( data . rows . length - 1 ) * 10 ) + r * 10 ) ) ;
227+ }
228+ }
229+
230+ g . setFont ( "Vector" , py ( 20 ) ) . setFontAlign ( - 1 , - 1 ) . drawString ( ( require ( "locale" ) . time ( new Date ( ) , 1 ) . replace ( " " , "" ) ) , px ( 2 ) , py ( start - ( ( data . rows . length + 2 ) * 10 ) ) ) ;
231+ g . setFont ( "Vector" , py ( 10 ) ) . drawString ( require ( 'locale' ) . dow ( new Date ( ) , 1 ) + " " + new Date ( ) . getDate ( ) + " " + require ( 'locale' ) . month ( new Date ( ) , 1 ) + ( ( data . temp == undefined ) ? "" : " | " + require ( 'locale' ) . temp ( Math . round ( data . temp - 273.15 ) ) . replace ( ".0" , "" ) ) , px ( 2 ) , py ( start - data . rows . length * 10 ) ) ;
205232
206233 if ( data . showWidgets ) {
207234 Bangle . drawWidgets ( ) ;
208235 }
209236}
210237
211238function setWeather ( ) {
239+ if ( showingSettings ) return ;
212240 var a = { } ;
213241 //clear day/night is default weather
214242 if ( ( data . code >= 800 && data . code <= 802 ) || data . code == undefined ) {
@@ -370,7 +398,73 @@ global.GB = (event) => {
370398 if ( _GB ) setTimeout ( _GB , 0 , event ) ;
371399} ;
372400
401+ function drawClkinfoSettings ( ) {
402+ if ( drawTimeout ) clearTimeout ( drawTimeout ) ;
403+ g . clear ( ) ;
404+ g . setColor ( g . theme . fg ) ;
405+ g . setFont ( "Vector" , py ( 10 ) ) . setFontAlign ( - 1 , - 1 ) . drawString ( "<Back" , px ( 2 ) , py ( 9 ) ) ;
406+ g . drawRect ( 1 , 1 , px ( 33 ) , px ( 25 ) - 1 ) ;
407+ g . setFont ( "Vector" , py ( 10 ) ) . setFontAlign ( 0 , - 1 ) . drawString ( "-" , px ( 50 ) , py ( 9 ) ) ;
408+ g . drawRect ( px ( 33 ) , 1 , px ( 67 ) , px ( 25 ) - 1 ) ;
409+ g . setFont ( "Vector" , py ( 10 ) ) . setFontAlign ( 0 , - 1 ) . drawString ( "+" , px ( 83 ) , py ( 9 ) ) ;
410+ g . drawRect ( px ( 67 ) , 1 , px ( 100 ) - 1 , px ( 25 ) - 1 ) ;
411+
412+ for ( let r = 0 ; r < data . rows . length ; r ++ ) {
413+ let a = data . rows [ r ] . menuA ;
414+ let b = data . rows [ r ] . menuB ;
415+ let ci = clockinfos [ a ] . items [ b ] ;
416+ if ( clockinfosMain [ a ] && clockinfosMain [ a ] [ b ] ) {
417+ clockinfosMain [ a ] [ b ] = false ;
418+ ci . hide ( ) ;
419+ ci . removeListener ( "redraw" , clockinfoRedraw ) ;
420+ }
421+ addClockinfo ( r )
422+ }
423+ clockinfosMain = [ ] ;
424+ }
425+
426+ function addClockinfo ( r ) {
427+ let dr = data . rows [ r ] ;
428+ // Check if the saved clockinfo indices still exist
429+ let ma = ( dr && dr . menuA && clockinfos [ dr . menuA ] ) ? dr . menuA : 0 ;
430+ let mb = ( ma && dr && dr . menuB && clockinfos [ ma ] . items [ dr . menuB ] ) ? dr . menuB : 0 ;
431+ clockinfosSettings [ r ] = require ( "clock_info" ) . addInteractive ( clockinfos , {
432+ x : 2 , y : py ( ( r + 1 ) * 25 ) + 1 , w : px ( 100 ) - 4 , h : py ( 25 ) - 2 ,
433+ menuA : ma ,
434+ menuB : mb ,
435+ draw : ( itm , info , options ) => {
436+ g . reset ( ) . clearRect ( options . x - 1 , options . y , options . x + options . w + 1 , options . y + options . h ) ;
437+ if ( options . focus ) g . drawRect ( options . x - 1 , options . y , options . x + options . w + 1 , options . y + options . h ) ;
438+ g . setFont ( "Vector" , py ( 10 ) ) . setFontAlign ( - 1 , 0 ) . drawString ( info . text , options . x , options . y + options . h / 2 ) ;
439+ }
440+ } ) ;
441+ }
442+
443+ function saveClockinfos ( ) {
444+ data . rows = [ ] ;
445+ for ( let r = 0 ; r < clockinfosSettings . length ; r ++ ) {
446+ data . rows [ r ] = {
447+ menuA : clockinfosSettings [ r ] . menuA ,
448+ menuB : clockinfosSettings [ r ] . menuB
449+ }
450+ clockinfosSettings [ r ] . remove ( ) ;
451+ }
452+ require ( "Storage" ) . writeJSON ( "mtnclock.json" , data ) ;
453+ }
454+
373455var drawTimeout ;
456+ var redrawTimeout ;
457+
458+ function clockinfoRedraw ( ) {
459+ // Limit redraws to every second
460+ if ( ! redrawTimeout ) {
461+ redrawTimeout = setTimeout ( function ( ) {
462+ setWeather ( ) ;
463+ clearTimeout ( redrawTimeout ) ;
464+ redrawTimeout = undefined ;
465+ } , 1000 ) ;
466+ }
467+ }
374468
375469//update watchface in next minute
376470function queueDraw ( ) {
@@ -383,6 +477,41 @@ function queueDraw() {
383477 } , 60000 - ( Date . now ( ) % 60000 ) ) ;
384478}
385479
480+ function checkTouchBack ( xy ) {
481+ return xy . x <= px ( 33 ) && xy . y < py ( 25 ) ;
482+ }
483+
484+ function checkTouchMinus ( xy ) {
485+ return xy . x > px ( 33 ) && xy . x < px ( 67 ) && xy . y < px ( 25 ) ;
486+ }
487+
488+ function checkTouchPlus ( xy ) {
489+ return xy . x >= px ( 67 ) && xy . y < px ( 25 ) ;
490+ }
491+
492+ Bangle . on ( 'touch' , function ( b , xy ) {
493+ // Bangle.js 2 supports long touch (type 2)
494+ // On other devices, any touch will show the settings
495+ if ( ! showingSettings && ( xy . type == 2 || process . env . HWVERSION != 2 ) ) {
496+ drawClkinfoSettings ( ) ;
497+ showingSettings = true ;
498+ } else if ( checkTouchBack ( xy ) ) {
499+ showingSettings = false ;
500+ saveClockinfos ( ) ;
501+ queueDraw ( ) ;
502+ // call setWeather after a timeout because for some reason a clockinfo
503+ // can still draw for a little bit despite calling remove() on it
504+ setTimeout ( setWeather , 100 ) ;
505+ } else if ( checkTouchMinus ( xy ) && clockinfosSettings . length > 0 ) {
506+ let cl = clockinfosSettings [ clockinfosSettings . length - 1 ] ;
507+ cl . remove ( ) ;
508+ g . reset ( ) . clearRect ( cl . x , cl . y , cl . x + cl . w - 2 , cl . y + cl . h - 1 ) ;
509+ clockinfosSettings . pop ( ) ;
510+ } else if ( checkTouchPlus ( xy ) && clockinfosSettings . length < 3 ) {
511+ addClockinfo ( clockinfosSettings . length )
512+ }
513+ } ) ;
514+
386515queueDraw ( ) ;
387516readWeather ( ) ;
388517setWeather ( ) ;
0 commit comments