소스 검색

Added SpinRite animation.

Steve Thielemann 2 년 전
부모
커밋
8e5f6ad7d4
1개의 변경된 파일245개의 추가작업 그리고 33개의 파일을 삭제
  1. 245 33
      testdoor/testdoor.go

+ 245 - 33
testdoor/testdoor.go

@@ -29,6 +29,21 @@ func (d *door.Door) PressAKey() {
 }
 */
 
+func press_keys(d *door.Door) {
+	d.Write(door.Reset + door.CRNL + "Press some keys...")
+	var k int
+	for k != 0x0d {
+		k = d.Key()
+		if k == door.XKEY_MOUSE {
+			var m door.MouseInfo = door.Mouse
+			d.Write(fmt.Sprintf("M %d (%d,%d) ", m.Button, m.X, m.Y))
+		} else {
+			d.Write(fmt.Sprintf("%d (%x) ", k, k))
+		}
+	}
+	d.Write(door.Reset + door.CRNL)
+}
+
 func press_a_key(d *door.Door) int {
 	d.Write(door.Reset + door.CRNL + "Press a key to continue...")
 	var k int = d.Key()
@@ -532,7 +547,7 @@ func width_demo(d *door.Door) {
 
 func panel_demo(d *door.Door) {
 	var width int = 55
-	var p door.Panel = door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
+	var panel door.Panel = door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
 
 	var lineColor string = door.ColorText("BRIGHT WHI ON BLUE")
 	// Add lines to the panel
@@ -541,10 +556,10 @@ func panel_demo(d *door.Door) {
 			line = strings.Replace(line, "(C)", "\u00a9", -1)
 		}
 		var l door.Line = door.Line{Text: line, Width: width, DefaultColor: lineColor}
-		p.Lines = append(p.Lines, l)
+		panel.Lines = append(panel.Lines, l)
 	}
-	p.Lines = append(p.Lines, p.Spacer())
-	p.Lines = append(p.Lines, door.Line{Text: "Welcome to golang!", Width: width, DefaultColor: lineColor})
+	panel.Lines = append(panel.Lines, panel.Spacer())
+	panel.Lines = append(panel.Lines, door.Line{Text: "Welcome to golang!", Width: width, DefaultColor: lineColor})
 
 	width = 10
 	var single door.Panel = door.Panel{X: 6, Y: 12, Width: width, Style: door.SINGLE, BorderColor: door.ColorText("WHITE ON BLUE"), Title: "< Single >"}
@@ -553,23 +568,196 @@ func panel_demo(d *door.Door) {
 	single.Lines = append(single.Lines, door.Line{Text: "More Text", Width: width, DefaultColor: door.ColorText("BRI GREEN ON BLACK")})
 
 	width = 15
-	var ds door.Panel = door.Panel{X: 26, Y: 12, Width: width, Style: door.DOUBLE_SINGLE, BorderColor: door.ColorText("BRI CYAN ON GREEN"), Title: "Double", TitleOffset: 3}
-	ds.Lines = append(ds.Lines, door.Line{Text: "Double / Single", Width: width, DefaultColor: door.ColorText("BRI WHI ON GREEN")})
-	ds.Lines = append(ds.Lines, ds.Spacer())
-	ds.Lines = append(ds.Lines, door.Line{Text: "Some Other Text", Width: width, DefaultColor: door.ColorText("BRI CYAN ON GREEN")})
+	var double_single door.Panel = door.Panel{X: 26, Y: 12, Width: width, Style: door.DOUBLE_SINGLE, BorderColor: door.ColorText("BRI CYAN ON GREEN"), Title: "Double", TitleOffset: 3}
+	double_single.Lines = append(double_single.Lines, door.Line{Text: "Double / Single", Width: width, DefaultColor: door.ColorText("BRI WHI ON GREEN")})
+	double_single.Lines = append(double_single.Lines, double_single.Spacer())
+	double_single.Lines = append(double_single.Lines, door.Line{Text: "Some Other Text", Width: width, DefaultColor: door.ColorText("BRI CYAN ON GREEN")})
 
-	var sd door.Panel = door.Panel{X: 46, Y: 12, Width: width, Style: door.SINGLE_DOUBLE, BorderColor: door.ColorText("BRI YELL ON RED")}
-	sd.Lines = append(sd.Lines, door.Line{Text: "Single / Double", Width: width, DefaultColor: door.ColorText("BRI WHI ON RED")})
-	sd.Lines = append(sd.Lines, sd.Spacer())
-	sd.Lines = append(sd.Lines, door.Line{Text: "Text Goes Here ", Width: width, DefaultColor: door.ColorText("BRI GREEN ON RED")})
+	var single_double door.Panel = door.Panel{X: 46, Y: 12, Width: width, Style: door.SINGLE_DOUBLE, BorderColor: door.ColorText("BRI YELL ON RED")}
+	single_double.Lines = append(single_double.Lines, door.Line{Text: "Single / Double", Width: width, DefaultColor: door.ColorText("BRI WHI ON RED")})
+	single_double.Lines = append(single_double.Lines, single_double.Spacer())
+	single_double.Lines = append(single_double.Lines, door.Line{Text: "Text Goes Here ", Width: width, DefaultColor: door.ColorText("BRI GREEN ON RED")})
 
 	d.Write(door.Clrscr)
-	d.Write(p.Output())
+	d.Write(panel.Output())
 	d.Write(single.Output())
-	d.Write(ds.Output())
-	d.Write(sd.Output())
+	d.Write(double_single.Output())
+	d.Write(single_double.Output())
+}
+
+type SpinRite struct {
+	Width     uint8   // Width of the animation (must be odd)
+	Length    uint8   // Width of the worm (must be odd)
+	Color     string  // Color (default CYAN ON BLUE)
+	OutputB   []byte  // Output CP437/bytes (Width)
+	OutputR   []rune  // Output Unicode (Width)
+	Buffer    []uint8 // Output calculate buffer (Width)
+	Runes     []rune  // blank, center, top, bottom, both
+	Bytes     []byte  // blank, center, top, bottom, both
+	CenterPos uint8   // Center Position (Width - 1) / 2
+	StartPos  uint8   // Starting Position (Length -1) / 2
+	Index     uint8   // Index of current iteration
+}
+
+func SpinRiteInit(width uint8, length uint8, color string) SpinRite {
+	if color == "" {
+		color = door.ColorText("CYAN ON BLUE")
+	}
+	var center uint8 = ((width - 1) / 2)
+	var start uint8 = ((length - 1) / 2)
+	var result SpinRite = SpinRite{Width: width,
+		Length:    length,
+		Color:     color,
+		CenterPos: center,
+		StartPos:  start,
+		Buffer:    make([]uint8, width),
+		Index:     0}
+	if door.Unicode {
+		result.OutputR = make([]rune, width)
+		result.Runes = []rune{' ', '\u221e', '\u2580', '\u2584', '\u2588'}
+	} else {
+		result.OutputB = make([]byte, width)
+		result.Bytes = []byte{' ', 0xec, 0xdf, 0xdc, 0xdb}
+	}
+	return result
+}
+
+func (sr *SpinRite) PosToIndex(pos int8, top bool) (index uint8, level bool) {
+	if top {
+		if pos >= int8(sr.Width) {
+			// wrap around
+			level = !top
+			idx := int8(sr.Width) + (int8(sr.Width) - (pos + 1))
+			log.Println("idx", idx) // -13
+			index = uint8(idx)
+		} else {
+			level = top
+			index = uint8(pos)
+		}
+	} else {
+		// bottom
+		if pos < 0 {
+			level = !top
+			pos++
+			index = uint8(-pos)
+		} else {
+			level = top
+			index = uint8(pos)
+		}
+	}
+	return
+}
+
+func (sr *SpinRite) Calculate() {
+	for i := range sr.Buffer {
+		sr.Buffer[i] = 0
+	}
+	sr.Buffer[sr.CenterPos] = 1
+
+	// [    S C s    ]
+	var top int8 = int8(sr.CenterPos) - int8(sr.StartPos) + int8(sr.Index)
+	var bottom int8 = int8(sr.CenterPos) + int8(sr.StartPos) - int8(sr.Index)
+
+	var l int8
+	for l = 0; l < int8(sr.Length); l++ {
+		var index uint8
+		var level bool
+		index, level = sr.PosToIndex(top+l, true)
+		log.Println("TOP", l, top+l, index, level)
+		if level {
+			sr.Buffer[index] |= 0x02
+		} else {
+			sr.Buffer[index] |= 0x04
+		}
+		index, level = sr.PosToIndex(bottom-l, false)
+		log.Println("BOT", l, bottom-l, index, level)
+		if level {
+			sr.Buffer[index] |= 0x02
+		} else {
+			sr.Buffer[index] |= 0x04
+		}
+	}
+
+	log.Println("Index", sr.Index, "Buffer", sr.Buffer)
+
+	var reset bool = true
+
+	// Convert from buffer to output
+	if door.Unicode {
+		for l = 0; l < int8(sr.Width); l++ {
+			switch sr.Buffer[l] {
+			case 0:
+				sr.OutputR[l] = sr.Runes[0]
+			case 1:
+				sr.OutputR[l] = sr.Runes[1]
+				reset = false
+			case 2, 3:
+				sr.OutputR[l] = sr.Runes[2]
+				reset = false
+			case 4, 5:
+				sr.OutputR[l] = sr.Runes[3]
+				reset = false
+			case 6, 7:
+				sr.OutputR[l] = sr.Runes[4]
+			}
+		}
+	} else {
+		for l = 0; l < int8(sr.Width); l++ {
+			switch sr.Buffer[l] {
+			case 0:
+				sr.OutputB[l] = sr.Bytes[0]
+			case 1:
+				sr.OutputB[l] = sr.Bytes[1]
+				reset = false
+			case 2, 3:
+				sr.OutputB[l] = sr.Bytes[2]
+				reset = false
+			case 4, 5:
+				sr.OutputB[l] = sr.Bytes[3]
+				reset = false
+			case 6, 7:
+				sr.OutputB[l] = sr.Bytes[4]
+			}
+		}
+	}
+
+	if reset {
+		log.Println("RESET")
+		sr.Index = 0
+	}
 }
 
+func (sr *SpinRite) Output() string {
+	var result string
+
+	sr.Calculate()
+	if door.Unicode {
+		result = string(sr.OutputR)
+	} else {
+		result = string(sr.OutputB)
+	}
+	sr.Index++
+
+	return sr.Color + result
+}
+
+/*
+Or possibly: · ∞
+[    █████    ]
+[   ▄▄███▀▀   ]
+[  ▄▄▄▄█▀▀▀▀  ]
+[ ▄▄▄▄▄•▀▀▀▀▀ ]
+[▄▄▄▄▄ • ▀▀▀▀▀]
+[█▄▄▄  •  ▀▀▀█]
+[██▄   •   ▀██]
+[██▀   •   ▄██]
+[█▀▀▀  •  ▄▄▄█]
+[█▀▀▀▀ • ▄▄▄▄█]
+[ ▀▀▀▀▀•▄▄▄▄▄ ]
+[  ▀▀▀▀█▄▄▄▄  ]
+[   ▀▀███▄▄   ]
+*/
+
 func main() {
 	var message string
 
@@ -590,23 +778,17 @@ func main() {
 	}()
 
 	defer d.Close()
-	var ticker *time.Ticker = time.NewTicker(time.Second)
+	// var ticker *time.Ticker = time.NewTicker(time.Second)
+	var ticker *time.Ticker = time.NewTicker(time.Millisecond * time.Duration(100))
 
-	go func() {
-		var maxlen int = 0 // max length of timeinfo (sysops have 4 digits of mins)
-		for t := range ticker.C {
-			const tf = "03:04:05 PM"
-			var timeinfo string = " " + t.Format(tf) + " " + fmt.Sprintf("(%3.1f mins)", d.TimeLeft().Minutes()) + " "
-			if maxlen == 0 {
-				maxlen = len(timeinfo)
-			} else {
-				if len(timeinfo) < maxlen {
-					timeinfo += strings.Repeat(" ", maxlen-len(timeinfo))
-				}
-			}
+	var spin SpinRite = SpinRiteInit(13, 5, door.ColorText("RED ON GREEN"))
+	var spin2 SpinRite = SpinRiteInit(15, 5, door.ColorText("BRI CYA ON BLUE"))
 
-			// maxlen = 12 + 7 + 5 = 24
-			output := door.SavePos + door.Goto(door.Width-(maxlen+1), 0) + door.ColorText("BRI WHI ON BLUE") + timeinfo + door.RestorePos
+	go func() {
+		var output string
+		for _ = range ticker.C {
+			output = door.SavePos + door.Goto(door.Width-15, 1) + spin.Output() +
+				door.Goto(door.Width-16, 2) + spin2.Output() + door.RestorePos
 			if !d.Disconnect() {
 				d.Write(output)
 			} else {
@@ -616,10 +798,39 @@ func main() {
 		}
 	}()
 
+	if false {
+		go func() {
+			var maxlen int = 0 // max length of timeinfo (sysops have 4 digits of mins)
+			for t := range ticker.C {
+				const tf = "03:04:05 PM"
+				var timeinfo string = " " + t.Format(tf) + " " + fmt.Sprintf("(%3.1f mins)", d.TimeLeft().Minutes()) + " "
+				if maxlen == 0 {
+					maxlen = len(timeinfo)
+				} else {
+					if len(timeinfo) < maxlen {
+						timeinfo += strings.Repeat(" ", maxlen-len(timeinfo))
+					}
+				}
+
+				// maxlen = 12 + 7 + 5 = 24
+				output := door.SavePos + door.Goto(door.Width-(maxlen+1), 0) + door.ColorText("BRI WHI ON BLUE") + timeinfo + door.RestorePos
+				if !d.Disconnect() {
+					d.Write(output)
+				} else {
+					ticker.Stop()
+					return
+				}
+			}
+		}()
+	}
+
 	// bold := door.Color(1, 37, 40)
 	var bolder string = door.ColorText("BLI BOLD YEL ON BLUE")
 	d.Write("Welcome to " + bolder + "door32.sys" + door.Reset + door.CRNL + "..." + door.CRNL)
-	press_a_key(&d)
+	// d.Write("\x1b[?9h") // enable mouse X10 support
+	d.Write("\x1b[?1000h") // enable mouse
+	press_keys(&d)
+	// press_a_key(&d)
 	d.Write(door.CRNL)
 
 	var b []string = door.AlertBox("Warning: golang is in use!", 1)
@@ -634,7 +845,8 @@ func main() {
 	message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
 	d.Write(message)
 
-	press_a_key(&d)
+	press_keys(&d)
+	// press_a_key(&d)
 
 	var mainmenu door.Menu = MainMenu()
 	var choice int
@@ -702,7 +914,7 @@ func main() {
 
 	ticker.Stop()
 
-	message = fmt.Sprintf("You had %0.2f minutes / %0.2f seconds remaining!"+door.CRNL, left.Minutes(), left.Seconds())
+	message = fmt.Sprintf("You had %0.2f minutes remaining!"+door.CRNL, left.Minutes())
 	d.Write(message)
 
 	left = d.TimeUsed()