Browse Source

Fixed wopr so it can be resumed.

Panel has code to detect whether or not it was clicked.
Steve Thielemann 2 years ago
parent
commit
1e567b40eb
2 changed files with 80 additions and 13 deletions
  1. 32 0
      door/panel.go
  2. 48 13
      door/wopr.go

+ 32 - 0
door/panel.go

@@ -182,3 +182,35 @@ func (p *Panel) CenterX() {
 func (p *Panel) CenterY() {
 	p.Y = (Height - len(p.Lines)) / 2
 }
+
+func (p *Panel) Clicked(m Mouse) (line int, clicked bool) {
+	mX := int(m.X)
+	mY := int(m.Y)
+
+	if p.Style == NONE {
+		if mY >= p.Y && mY < p.Y+len(p.Lines) &&
+			mX >= p.X && mX < p.X+p.Width {
+			return p.Y - mY + 1, true
+		}
+		return 0, false
+	} else {
+		if mY == p.Y || mY == p.Y+len(p.Lines)+1 {
+			if mX >= p.X && mX <= p.X+p.Width+1 {
+				// Top/Bottom border clicked
+				return 0, true
+			}
+		}
+		if mY > p.Y && mY < p.Y+len(p.Lines)+1 {
+			if mX == p.X || mX == p.X+p.Width+1 {
+				// Left/Right border clicked
+				return 0, true
+			}
+		}
+		if mY > p.Y && mY < p.Y+len(p.Lines)+1 {
+			if mY > p.X && mY <= p.X+p.Width+1 {
+				return p.Y - mY, true
+			}
+		}
+		return 0, false
+	}
+}

+ 48 - 13
door/wopr.go

@@ -27,13 +27,14 @@ Ok, Width = 16
 
 Width 16  (at pos 1, update time/sec increment)
 Black on Cyan
-
 */
 type WOPR struct {
 	ElapsedPanel   Panel
-	Elapsed        time.Duration
+	Elapsed        time.Time
+	ElapsedD       time.Duration
 	RemainingPanel Panel
-	Remaining      time.Duration
+	Remaining      time.Time
+	RemainingD     time.Duration
 	Color          string
 	Index          int
 	Ticker         *time.Ticker
@@ -42,7 +43,7 @@ type WOPR struct {
 // Initialize, Set X, Y on Panels, Animate()
 
 // Initialize, and create panels
-func (w *WOPR) Init(elapsed time.Duration, remaining time.Duration, color string) {
+func (w *WOPR) Init(elapsed time.Time, remaining time.Time, color string) {
 	if color == "" {
 		color = ColorText("BLACK ON CYAN")
 	}
@@ -57,7 +58,7 @@ func (w *WOPR) Init(elapsed time.Duration, remaining time.Duration, color string
 
 	var ehour Line = Line{}
 	ehour.UpdateF = func() string {
-		var hours int = int(w.Elapsed.Hours())
+		var hours int = int(w.ElapsedD.Hours())
 		return fmt.Sprintf("     %02d HRS     ", hours%100)
 	}
 	ehour.Text = ehour.UpdateF()
@@ -65,8 +66,8 @@ func (w *WOPR) Init(elapsed time.Duration, remaining time.Duration, color string
 
 	var eminsec Line = Line{}
 	eminsec.UpdateF = func() string {
-		var mins int = int(w.Elapsed.Minutes()) % 60
-		var secs int = int(w.Elapsed.Seconds()) % 60
+		var mins int = int(w.ElapsedD.Minutes()) % 60
+		var secs int = int(w.ElapsedD.Seconds()) % 60
 		return fmt.Sprintf(" %02d MIN  %02d SEC ", mins, secs)
 	}
 	eminsec.Text = eminsec.UpdateF()
@@ -96,7 +97,7 @@ func (w *WOPR) Init(elapsed time.Duration, remaining time.Duration, color string
 
 	var rhour Line = Line{}
 	rhour.UpdateF = func() string {
-		var hours int = int(w.Remaining.Hours())
+		var hours int = int(w.RemainingD.Hours())
 		return fmt.Sprintf("     %02d HRS     ", hours%100)
 	}
 	rhour.Text = rhour.UpdateF()
@@ -104,8 +105,8 @@ func (w *WOPR) Init(elapsed time.Duration, remaining time.Duration, color string
 
 	var rminsec Line = Line{}
 	rminsec.UpdateF = func() string {
-		var mins int = int(w.Remaining.Minutes()) % 60
-		var secs int = int(w.Remaining.Seconds()) % 60
+		var mins int = int(w.RemainingD.Minutes()) % 60
+		var secs int = int(w.RemainingD.Seconds()) % 60
 		return fmt.Sprintf(" %02d MIN  %02d SEC ", mins, secs)
 	}
 	rminsec.Text = rminsec.UpdateF()
@@ -131,21 +132,55 @@ func (w *WOPR) Init(elapsed time.Duration, remaining time.Duration, color string
 func (w *WOPR) Inc() {
 	w.Index++
 	if w.Index == 16 {
-		w.Remaining = time.Duration(w.Remaining.Seconds()-1) * time.Second
-		w.Elapsed = time.Duration(w.Elapsed.Seconds()+1) * time.Second
 		w.Index = 0
+		w.ElapsedPanel.Update()
+		w.RemainingPanel.Update()
+
+		w.RemainingD = time.Duration(w.RemainingD.Seconds()-1) * time.Second
+		w.ElapsedD = time.Duration(w.ElapsedD.Seconds()+1) * time.Second
+
 	}
 
 }
 func (w *WOPR) Animate(d *Door) {
-	w.Ticker = time.NewTicker(time.Microsecond * time.Duration(62500))
+	// til := time.Now().UnixNano() % int64(time.Second)
+
+	// w.Index = int((til / int64(time.Microsecond)) / 62500)
+	// either put the sync sleep in the go routine, or sleep and sync Index.
+
+	// time.Sleep(time.Duration(int64(time.Second) - til)) // time.Now().UnixMilli()%1000) * time.Millisecond)
+
+	// time.Second / 16
+	// w.Ticker = time.NewTicker(time.Microsecond * time.Duration(62500))
+
+	// w.Index = 0
+	// Convert time.Time to time.Duration
+	// This gives us consistency when resuming.
+	w.ElapsedD = time.Since(w.Elapsed)
+	w.RemainingD = time.Until(w.Remaining)
 
 	go func(d *Door) {
+		// til := time.Now().UnixNano() % int64(time.Second)
+
+		sec16 := int64(time.Second) / 16
+		// tilms := til % sec16
+		w.Index = 0 // int(til / sec16)
+		// log.Printf("til: %d, sec: %d, w.Index: %d\n", til, sec16, w.Index)
+
+		// either put the sync sleep in the go routine, or sleep and sync Index.
+
+		// time.Sleep(time.Second - time.Duration(til)) // sec16 - (til % sec16))) //int64(time.Second) - til)) // time.Now().UnixMilli()%1000) * time.Millisecond)
+
+		// time.Second / 16
+		w.Ticker = time.NewTicker(time.Duration(sec16)) // time.Microsecond * time.Duration(62500))
+
 		var output string
 
 		for range w.Ticker.C {
+
 			w.ElapsedPanel.Update()
 			w.RemainingPanel.Update()
+
 			output = SavePos + w.ElapsedPanel.Output() + w.RemainingPanel.Output() + RestorePos
 			if !d.Disconnect() {
 				d.Write(output)