1- Bangle . loadWidgets ( ) ;
2-
31var s = Object . assign ( {
2+ display2 :true ,
43 counter0 :10 ,
54 counter1 :20 ,
65 max0 :15 ,
76 max1 :25 ,
7+ fullscreen :true ,
88 buzz : true ,
99 colortext : true ,
1010} , require ( 'Storage' ) . readJSON ( "counter2.json" , true ) || { } ) ;
1111
12+ var sGlob = Object . assign ( {
13+ timeout : 10 ,
14+ } , require ( 'Storage' ) . readJSON ( "setting.json" , true ) || { } ) ;
15+
1216const f1 = ( s . colortext ) ? "#f00" : "#fff" ;
1317const f2 = ( s . colortext ) ? "#00f" : "#fff" ;
1418const b1 = ( s . colortext ) ? g . theme . bg : "#f00" ;
1519const b2 = ( s . colortext ) ? g . theme . bg : "#00f" ;
1620
1721var drag ;
22+ var dragCurr = { x :0 , y :0 , dx :0 , dy :0 , b :false } ;
1823
1924const screenwidth = g . getWidth ( ) ;
2025const screenheight = g . getHeight ( ) ;
@@ -37,51 +42,111 @@ function saveSettings() {
3742}
3843
3944let ignoreonce = false ;
45+ let fastupdateoccurring = false ;
4046var dragtimeout ;
4147
4248function updateScreen ( ) {
43- g . setBgColor ( b1 ) ;
44- g . clearRect ( 0 , 0 , halfwidth , screenheight ) ;
45- g . setBgColor ( b2 ) ;
46- g . clearRect ( halfwidth , 0 , screenwidth , screenheight ) ;
47- g . setFont ( "Vector" , 60 ) . setFontAlign ( 0 , 0 ) ;
48- g . setColor ( f1 ) ;
49- g . drawString ( Math . floor ( counter [ 0 ] ) , halfwidth * 0.5 , halfheight ) ;
50- g . setColor ( f2 ) ;
51- g . drawString ( Math . floor ( counter [ 1 ] ) , halfwidth * 1.5 , halfheight ) ;
49+ if ( s . display2 ) {
50+ g . setBgColor ( b1 ) ;
51+ g . clearRect ( 0 , 0 , halfwidth , screenheight ) ;
52+ g . setBgColor ( b2 ) ;
53+ g . clearRect ( halfwidth , 0 , screenwidth , screenheight ) ;
54+ g . setFont ( "Vector" , 60 ) . setFontAlign ( 0 , 0 ) ;
55+ g . setColor ( f1 ) ;
56+ g . drawString ( Math . floor ( counter [ 0 ] ) , halfwidth * 0.5 , halfheight ) ;
57+ g . setColor ( f2 ) ;
58+ g . drawString ( Math . floor ( counter [ 1 ] ) , halfwidth * 1.5 , halfheight ) ;
59+ }
60+ else {
61+ g . setBgColor ( b2 ) ; // Using right counter's colors b/c blue looks nicer than red
62+ g . clearRect ( 0 , 0 , screenwidth , screenheight ) ;
63+ g . setFont ( "Vector" , 90 ) . setFontAlign ( 0 , 0 ) ;
64+ g . setColor ( f2 ) ;
65+ g . drawString ( Math . floor ( counter [ 0 ] ) , halfwidth , halfheight ) ;
66+ }
5267 saveSettings ( ) ;
5368 if ( s . buzz ) Bangle . buzz ( 50 , .5 ) ;
54- Bangle . drawWidgets ( ) ;
69+
70+ Bangle . loadWidgets ( ) ;
71+ if ( s . fullscreen ) {
72+ require ( "widget_utils" ) . hide ( ) ;
73+ }
74+ else {
75+ Bangle . drawWidgets ( ) ;
76+ }
5577}
5678
79+ // Clearing the timer on lock is likely uneeded, but just in case
80+ Bangle . on ( 'lock' , e => {
81+ drag = undefined ;
82+ var timeOutTimer = sGlob . timeout * 1000 ;
83+ Bangle . setOptions ( { backlightTimeout : timeOutTimer , lockTimeout : timeOutTimer } ) ;
84+ if ( dragtimeout ) clearTimeout ( dragtimeout ) ;
85+ fastupdateoccurring = false ;
86+ } ) ;
87+
5788Bangle . on ( "drag" , e => {
58- const c = ( e . x < halfwidth ) ? 0 : 1 ;
89+ const c = ( e . x >= halfwidth && s . display2 ) ? 1 : 0 ;
90+ dragCurr = e ;
5991 if ( ! drag ) {
6092 if ( ignoreonce ) {
6193 ignoreonce = false ;
6294 return ;
6395 }
6496 drag = { x : e . x , y : e . y } ;
65- dragtimeout = setTimeout ( function ( ) { resetcounter ( c ) ; } , 600 ) ; //if dragging for 500ms, reset counter
97+ dragtimeout = setTimeout ( function ( ) { fastupdatecounter ( c ) ; } , 600 ) ; //if dragging for 500ms, reset counter
6698 }
6799 else if ( drag && ! e . b ) { // released
68- let adjust = 0 ;
69- const dx = e . x - drag . x , dy = e . y - drag . y ;
70- if ( Math . abs ( dy ) > Math . abs ( dx ) + 30 ) {
71- adjust = ( dy > 0 ) ? - 1 : 1 ;
72- } else {
73- adjust = ( e . y > halfwidth ) ? - 1 : 1 ;
74- }
75- counter [ c ] += adjust ;
76- updateScreen ( ) ;
77- drag = undefined ;
100+ if ( ! fastupdateoccurring )
101+ updatecounter ( c ) ;
102+ drag = undefined ;
103+ if ( dragtimeout ) {
104+ let timeOutTimer = 1000 ;
105+ Bangle . setOptions ( { backlightTimeout : timeOutTimer , lockTimeout : timeOutTimer } ) ;
78106 clearTimeout ( dragtimeout ) ;
79107 }
108+ fastupdateoccurring = false ;
109+ }
80110} ) ;
81111
112+ function updatecounter ( which ) {
113+ let adjust = 0 ;
114+ const dx = dragCurr . x - drag . x , dy = dragCurr . y - drag . y ;
115+ if ( Math . abs ( dy ) > Math . abs ( dx ) + 30 ) {
116+ adjust = ( dy > 0 ) ? - 1 : 1 ;
117+ } else {
118+ adjust = ( dragCurr . y > halfheight ) ? - 1 : 1 ;
119+ }
120+ counter [ which ] += adjust ;
121+ updateScreen ( ) ;
122+ }
123+
124+ function fastupdatecounter ( which ) {
125+ fastupdateoccurring = true ;
126+ updatecounter ( which ) ;
127+ Bangle . setOptions ( { backlightTimeout : 0 , lockTimeout : 0 } ) ;
128+ dragtimeout = setTimeout ( function ( ) { fastupdatecounter ( which ) ; } , 10 ) ;
129+ }
130+
131+
82132function resetcounter ( which ) {
83- counter [ which ] = defaults [ which ] ;
84- console . log ( "resetting counter " , which ) ;
133+ // If which is null, reset all
134+ fastupdateoccurring = false ;
135+ if ( dragtimeout ) {
136+ let timeOutTimer = 1000 ;
137+ Bangle . setOptions ( { backlightTimeout : timeOutTimer , lockTimeout : timeOutTimer } ) ;
138+ clearTimeout ( dragtimeout ) ;
139+ }
140+ if ( which == null ) {
141+ for ( let iter = 0 ; iter < defaults . length ; iter ++ ) {
142+ counter [ iter ] = defaults [ iter ] ;
143+ }
144+ console . log ( "resetting all counters" ) ;
145+ }
146+ else {
147+ counter [ which ] = defaults [ which ] ;
148+ console . log ( "resetting counter " , which ) ;
149+ }
85150 updateScreen ( ) ;
86151 drag = undefined ;
87152 ignoreonce = true ;
@@ -91,5 +156,13 @@ function resetcounter(which) {
91156updateScreen ( ) ;
92157
93158setWatch ( function ( ) {
159+ for ( let which = 0 ; which < defaults . length ; which ++ ) {
160+ if ( counter [ which ] != defaults [ which ] ) {
161+ resetcounter ( null ) ;
162+ return ;
163+ }
164+ }
165+ var timeOutTimer = sGlob . timeout * 1000 ;
166+ Bangle . setOptions ( { backlightTimeout : timeOutTimer , lockTimeout : timeOutTimer } ) ;
94167 load ( ) ;
95168} , BTN1 , { repeat :true , edge :"falling" } ) ;
0 commit comments