testdoor.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "red-green/door"
  6. "runtime/debug"
  7. "strconv"
  8. "strings"
  9. "time"
  10. // "net/http"
  11. // _ "net/http/pprof"
  12. "github.com/mitchellh/go-wordwrap"
  13. )
  14. //go:generate sh -c "font-util extract -f 'Amazon Cyan,Serpent,Unchained,Asylum,ArmageonRed,BrainDmgBlu,Boner,Descent,Remorse,Dungeon' ../TDFONTS.TDF ../TDFONTS2.TDF ../TDFONTS9.TDF > fonts.go"
  15. //go:generate sh -c "font-util extract -f Armageddon -c 7,1 -c 4,2 ../TDFONTS2.TDF > rgfont.go; sed -i 's/Armageddon/RedGreen/g' rgfont.go"
  16. func pctUpdate(pct *int64) func() int64 {
  17. return func() int64 {
  18. return *pct
  19. }
  20. }
  21. func press_keys(d *door.Door) {
  22. d.Write(door.Reset + door.CRNL + "Press some keys... <ENTER> to exit.")
  23. var r rune
  24. var ex door.Extended
  25. var err error
  26. for (r != 0x0d) && (err == nil) {
  27. r, ex, err = d.WaitKey(door.Inactivity)
  28. if ex == door.MOUSE {
  29. m, ok := d.GetMouse()
  30. if ok {
  31. // var m door.MouseInfo = door.Mouse
  32. d.Write(fmt.Sprintf("M %d (%d,%d) ", m.Button, m.X, m.Y))
  33. }
  34. } else {
  35. if ex == door.NOP {
  36. d.Write(fmt.Sprintf("%d (%x) ", r, r))
  37. } else {
  38. d.Write(fmt.Sprintf("<%s> ", ex.String()))
  39. }
  40. }
  41. }
  42. d.Write(door.Reset + door.CRNL)
  43. }
  44. func press_a_key(d *door.Door) error {
  45. var err error
  46. var ex door.Extended
  47. d.Write(door.Reset + door.CRNL + "Press a key, or LEFT mouse click to continue...")
  48. for {
  49. _, ex, err = d.WaitKey(door.Inactivity)
  50. if ex == door.MOUSE {
  51. m, ok := d.GetMouse()
  52. if ok {
  53. if m.Button == 1 {
  54. break
  55. }
  56. }
  57. } else {
  58. break
  59. }
  60. }
  61. d.Write(door.CRNL)
  62. return err
  63. }
  64. func about_test_door(d *door.Door) {
  65. var W int = 60
  66. var center_x int = (door.Width - W) / 2
  67. var center_y int = (door.Height - 16) / 2
  68. var about door.Panel = door.Panel{X: center_x,
  69. Y: center_y,
  70. Width: W,
  71. Style: door.SINGLE_DOUBLE,
  72. BorderColor: door.ColorText("BOLD YELLOW ON BLUE"),
  73. }
  74. about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, "About This Door"),
  75. DefaultColor: door.ColorText("BOLD CYAN ON BLUE")})
  76. about.Lines = append(about.Lines, about.Spacer())
  77. about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, "Test Door written in go, using go door.")})
  78. var copyright string = "(C) 2022 Bugz, Red Green Software"
  79. if door.Unicode {
  80. copyright = strings.Replace(copyright, "(C)", "\u00a9", -1)
  81. }
  82. about.Lines = append(about.Lines,
  83. door.Line{Text: copyright, Width: W,
  84. DefaultColor: door.ColorText("BOLD WHITE ON BLUE")})
  85. for _, text := range []string{"",
  86. "This door was written by Bugz.",
  87. "",
  88. "It is written in Go, understands CP437 and unicode, adapts",
  89. "to screen sizes, uses door32.sys, supports TheDraw Fonts",
  90. "(the fonts are compiled into the door), has NoMoreSecrets",
  91. "effect, and runs on Linux and Windows."} {
  92. about.Lines = append(about.Lines, door.Line{Text: text, Width: W})
  93. }
  94. var better door.NoMoreSecretsConfig = door.NoMoreSecretsDefault
  95. better.Jumble_Loop_Speed = 75 // 35
  96. better.Reveal_Loop_Speed = 100 // 50
  97. better.Color = door.ColorText("BRI CYAN ON BLUE")
  98. door.NoMoreSecrets(about.Output(), d, &better)
  99. }
  100. func MainMenu() door.Menu {
  101. // Make the main menu
  102. // Width was 45
  103. var menu door.Menu = door.Menu{Panel: door.Panel{Width: 35,
  104. X: 2,
  105. Y: 5,
  106. Style: door.DOUBLE,
  107. Title: "[ Main Menu: ]",
  108. TitleOffset: 3,
  109. BorderColor: door.ColorText("BRI CYAN ON BLA")}}
  110. menu.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
  111. door.ColorText("BOLD BLUE"),
  112. door.ColorText("BOLD WHITE"),
  113. door.ColorText("BOLD CYAN"))
  114. menu.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
  115. door.ColorText("BOLD WHI ON BLUE"),
  116. door.ColorText("BOLD YEL ON BLUE"),
  117. door.ColorText("BOLD CYAN ON BLUE"))
  118. menu.AddSelection("A", "ANSI Display")
  119. // m.AddSelection("C", "Crash")
  120. menu.AddSelection("D", "Display Information")
  121. menu.AddSelection("F", "Font Demo")
  122. menu.AddSelection("I", "Input Prompt Demo")
  123. menu.AddSelection("M", "Menu Demo")
  124. menu.AddSelection("P", "Progress Bars Demo")
  125. menu.AddSelection("S", "Show Panel")
  126. menu.AddSelection("T", "Test Door About")
  127. menu.AddSelection("W", "Screen Width")
  128. menu.AddSelection("Q", "Quit")
  129. var descriptions []string = []string{
  130. // 12345678901234567890123456789012345678901234567890
  131. "Display an ANSI file. It is compiled into the door itself.",
  132. // "Crash go, see a handled error.", // The error shows up in the logs.
  133. "Display dropfile information.",
  134. "Display TheDraw Fonts. Font information is compiled into the door.",
  135. "Input some values, while updating the time.",
  136. "Isn't this is a menu?",
  137. "Display various progress bar styles. Half step, display percentage, and gradient.",
  138. "Show multiple panels.",
  139. "Show more information about the door, using NoMoreSecrets effect.",
  140. "Examples using the full width of the screen.",
  141. "Exit this door.",
  142. }
  143. var widthLeft int = door.Width - (menu.Width + menu.X + 2)
  144. var panelWidth int = widthLeft - (2 + 2)
  145. var maxLines int = 1
  146. var maxLineLength int
  147. // Calculate the max lines needed for each line.
  148. for _, line := range descriptions {
  149. var wrapped string = wordwrap.WrapString(line, uint(panelWidth))
  150. var lines int = len(strings.Split(wrapped, "\n"))
  151. if lines > maxLines {
  152. maxLines = lines
  153. }
  154. if len(line) > maxLineLength {
  155. maxLineLength = len(line)
  156. }
  157. }
  158. if maxLines == 1 {
  159. // Ok! Everything fits into one line, SO use max line length as width of panel.
  160. panelWidth = maxLineLength
  161. }
  162. // Position the description panel beside the menu. m.Width + m.X + 2 (1 one to give it a space)
  163. var descPanel door.Panel = door.Panel{X: menu.Width + menu.X + 2 + 1, Y: menu.Y, Width: panelWidth, Style: door.SINGLE, BorderColor: door.ColorText("WHI ON BLU")}
  164. for x := 0; x < maxLines; x++ {
  165. descPanel.Lines = append(descPanel.Lines, door.Line{Text: "", Width: panelWidth})
  166. }
  167. menu.Activated = func(item int, d *door.Door) {
  168. var line string = descriptions[item]
  169. var lines []string = strings.Split(wordwrap.WrapString(line, uint(panelWidth)), "\n")
  170. for idx := range descPanel.Lines {
  171. if idx >= len(lines) {
  172. descPanel.Lines[idx].Text = ""
  173. } else {
  174. descPanel.Lines[idx].Text = lines[idx]
  175. }
  176. }
  177. d.Write(door.SavePos + descPanel.Output() + door.RestorePos)
  178. }
  179. return menu
  180. }
  181. func display_information(d *door.Door) {
  182. d.Write(door.Clrscr)
  183. var headerColor string = door.ColorText("BRI CYAN")
  184. var keyColor string = door.ColorText("BRI GREEN")
  185. var sepColor string = door.ColorText("BRI YEL")
  186. var valColor string = door.ColorText("CYAN")
  187. var nice_format func(string, string) string = func(key string, value string) string {
  188. return fmt.Sprintf("%s%-20s %s: %s%s", keyColor, key, sepColor, valColor, value) + door.CRNL
  189. }
  190. var offset string
  191. var header string = "DropFile: "
  192. offset = strings.Repeat(" ", len(header))
  193. d.Write(headerColor + header)
  194. d.Write(nice_format("Comm Type", strconv.Itoa(d.Config.Comm_type)))
  195. if d.Config.BBSID != "" {
  196. d.Write(offset + nice_format("BBS Software", d.Config.BBSID))
  197. }
  198. d.Write(offset + nice_format("Time Left", strconv.Itoa(d.Config.Time_left)))
  199. d.Write(offset + nice_format("Real Name", d.Config.Real_name))
  200. // d.Write(nice_format("Comm Handle", strconv.Itoa(d.Config.Comm_handle)))
  201. d.Write(offset + nice_format("Handle", d.Config.Handle))
  202. d.Write(offset + nice_format("User #", strconv.Itoa(d.Config.User_number)))
  203. d.Write(offset + nice_format("Security Level", strconv.Itoa(d.Config.Security_level)))
  204. d.Write(offset + nice_format("Node #", strconv.Itoa(d.Config.Node)))
  205. header = "Detected: "
  206. offset = strings.Repeat(" ", len(header))
  207. d.Write(door.CRNL + headerColor + header)
  208. d.Write(nice_format("Unicode", strconv.FormatBool(door.Unicode)))
  209. d.Write(offset + nice_format("CP437", strconv.FormatBool(door.CP437)))
  210. d.Write(offset + nice_format("Full CP437", strconv.FormatBool(door.Full_CP437)))
  211. d.Write(offset + nice_format("Screen Size", fmt.Sprintf("%d X %d", door.Width, door.Height)))
  212. var time time.Duration = d.TimeLeft()
  213. d.Write(offset + nice_format("Door Time Left", fmt.Sprintf("%d Hours, %d Minutes, %d Seconds", int(time.Hours()), int(time.Minutes())%60, int(time.Seconds())%60)))
  214. time = d.TimeUsed()
  215. d.Write(offset + nice_format("Door Time Used", fmt.Sprintf("%d Minutes, %d Seconds", int(time.Minutes()), int(time.Seconds())%60)))
  216. press_a_key(d)
  217. d.Write(door.Clrscr + door.CRNL + door.CRNL + door.CRNL)
  218. modules := GetModules()
  219. header = "Build: "
  220. offset = strings.Repeat(" ", len(header))
  221. d.Write(headerColor + header)
  222. gover, gitver, goarch, goos := GetVersion()
  223. d.Write(nice_format("go version", gover))
  224. d.Write(offset + nice_format("git commit", gitver))
  225. d.Write(offset + nice_format("Arch", goarch))
  226. d.Write(offset + nice_format("OS", goos))
  227. for mod, version := range modules {
  228. d.Write(offset + nice_format(mod, version))
  229. }
  230. }
  231. func display_ansi(d *door.Door) {
  232. var art []string = ANSIGrowl()
  233. d.Write(door.Clrscr)
  234. for _, line := range art {
  235. d.Write(line + door.CRNL)
  236. }
  237. }
  238. func font_demo(d *door.Door) {
  239. var output []string
  240. var l int
  241. var centering string
  242. var now time.Time = time.Now()
  243. d.Write(door.Clrscr + door.CRNL) // + door.CRNL + door.CRNL)
  244. var fac door.ColorFont = FontAmazonCyan()
  245. output, l = fac.Output(now.Format("Jan Mon"))
  246. /*
  247. if l > door.Width {
  248. output, l = fac.Output("Jan")
  249. }
  250. */
  251. if l < door.Width {
  252. centering = ""
  253. // centering = strings.Repeat(" ", (door.Width-l)/2)
  254. for _, o := range output {
  255. d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
  256. }
  257. d.Write(door.CRNL)
  258. }
  259. var patch door.ColorMap = fac.Scan(6)
  260. // log.Printf("Patch: %#v\n", patch)
  261. fac.Modify(4, patch)
  262. output, l = fac.Output(now.Format("Monday"))
  263. centering = strings.Repeat(" ", (door.Width-l)/2)
  264. for _, o := range output {
  265. d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
  266. }
  267. d.Write(door.CRNL)
  268. fac.Modify(1, patch)
  269. output, l = fac.Output(now.Format("Janunary")) // 3:04:05 PM"))
  270. centering = strings.Repeat(" ", (door.Width-l)/2)
  271. for _, o := range output {
  272. d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
  273. }
  274. d.Write(door.CRNL)
  275. press_a_key(d)
  276. // Anarchy Blue - no digits
  277. var fab door.ColorFont = FontSerpent()
  278. now = time.Now()
  279. output, l = fab.Output(now.Format("01/02/2006"))
  280. if l > door.Width {
  281. output, l = fab.Output("Hello")
  282. }
  283. if l < door.Width {
  284. centering = strings.Repeat(" ", (door.Width-l)/2)
  285. for _, o := range output {
  286. d.Write(centering + o + door.Reset + door.CRNL)
  287. }
  288. d.Write(door.CRNL)
  289. }
  290. var unchain door.ColorFont = FontUnchained()
  291. output, l = unchain.Output(now.Format("3:04:05 PM"))
  292. if l > door.Width {
  293. output, l = unchain.Output("Meow")
  294. }
  295. if l < door.Width {
  296. centering = strings.Repeat(" ", (door.Width-l)/2)
  297. for _, o := range output {
  298. d.Write(centering + o + door.Reset + door.CRNL)
  299. }
  300. d.Write(door.CRNL)
  301. }
  302. press_a_key(d)
  303. var asylum door.ColorFont = FontAsylum()
  304. output, l = asylum.Output("Bugz ROCKS")
  305. if l > door.Width {
  306. output, l = asylum.Output("Aslym")
  307. }
  308. if l < door.Width {
  309. centering = strings.Repeat(" ", (door.Width-l)/2)
  310. for _, o := range output {
  311. d.Write(centering + o + door.Reset + door.CRNL)
  312. }
  313. d.Write(door.CRNL)
  314. }
  315. var brain door.ColorFont = FontBrainDmgBlu()
  316. output, l = brain.Output("I'm so BLUE")
  317. if l > door.Width {
  318. output, l = brain.Output("Blue")
  319. }
  320. if l < door.Width {
  321. centering = strings.Repeat(" ", (door.Width-l)/2)
  322. for _, o := range output {
  323. d.Write(centering + o + door.Reset + door.CRNL)
  324. }
  325. d.Write(door.CRNL)
  326. }
  327. var boner door.ColorFont = FontBoner()
  328. output, l = boner.Output("Welcome!")
  329. if l < door.Width {
  330. centering = strings.Repeat(" ", (door.Width-l)/2)
  331. for _, o := range output {
  332. d.Write(centering + o + door.Reset + door.CRNL)
  333. }
  334. d.Write(door.CRNL)
  335. }
  336. press_a_key(d)
  337. var descent door.ColorFont = FontDescent()
  338. output, l = descent.Output("Meanwhile...")
  339. if l > door.Width {
  340. output, l = descent.Output("BUGZ")
  341. }
  342. if l < door.Width {
  343. centering = strings.Repeat(" ", (door.Width-l)/2)
  344. for _, o := range output {
  345. d.Write(centering + o + door.Reset + door.CRNL)
  346. }
  347. d.Write(door.CRNL)
  348. }
  349. var remorse door.ColorFont = FontRemorse()
  350. output, l = remorse.Output("Enjoy the fonts")
  351. if l > door.Width {
  352. output, l = remorse.Output("Amazing")
  353. }
  354. if l < door.Width {
  355. centering = strings.Repeat(" ", (door.Width-l)/2)
  356. for _, o := range output {
  357. d.Write(centering + o + door.Reset + door.CRNL)
  358. }
  359. d.Write(door.CRNL)
  360. }
  361. var dungeon door.ColorFont = FontDungeon()
  362. output, l = dungeon.Output("Until NEXT time")
  363. if l > door.Width {
  364. output, l = dungeon.Output("Beware")
  365. }
  366. if l < door.Width {
  367. centering = strings.Repeat(" ", (door.Width-l)/2)
  368. for _, o := range output {
  369. d.Write(centering + o + door.Reset + door.CRNL)
  370. }
  371. d.Write(door.CRNL)
  372. }
  373. /*
  374. redgreen := FontArmageddon()
  375. white := redgreen.Scan(7)
  376. blue := redgreen.Scan(4)
  377. redgreen.Modify(1, white)
  378. redgreen.Modify(2, blue)
  379. */
  380. redgreen := FontRedGreen()
  381. output, l = redgreen.Output("Red-Green")
  382. if l < door.Width {
  383. press_a_key(d)
  384. centering = strings.Repeat(" ", (door.Width-l)/2)
  385. for _, o := range output {
  386. d.Write(centering + o + door.Reset + door.CRNL)
  387. }
  388. d.Write(door.CRNL)
  389. output, l = redgreen.Output("Software")
  390. centering = strings.Repeat(" ", (door.Width-l)/2)
  391. for _, o := range output {
  392. d.Write(centering + o + door.Reset + door.CRNL)
  393. }
  394. d.Write(door.CRNL)
  395. }
  396. }
  397. func input_demo(d *door.Door) {
  398. var ticker *time.Ticker = time.NewTicker(time.Second)
  399. go func() {
  400. for t := range ticker.C {
  401. const tf = "January 2, 2006 03:04:05 PM MST"
  402. output := door.SavePos + door.Goto(5, 2) + door.ColorText("BRI WHI ON CYAN") + t.Format(tf) + door.RestorePos
  403. d.Write(output)
  404. }
  405. }()
  406. var inputColor string = door.ColorText("BRI WHI ON BLUE")
  407. var inputColor2 string = door.ColorText("BRI WHI ON GREEN")
  408. var prompt door.Line = door.Line{Text: "What is YOUR Name: "}
  409. prompt.RenderF = door.RenderBlueYellow
  410. d.Write(prompt.Output() + inputColor)
  411. var name string = d.Input(25)
  412. d.Write(door.Reset + door.CRNL)
  413. prompt.Text = "What is Your Quest: "
  414. d.Write(prompt.Output() + inputColor2)
  415. var quest string = d.Input(35)
  416. d.Write(door.Reset + door.CRNL)
  417. prompt.Text = "What is your Favorite CoLoR: "
  418. d.Write(prompt.Output() + inputColor)
  419. var color string = d.Input(15)
  420. d.Write(door.Reset + door.CRNL)
  421. ticker.Stop()
  422. d.Write(fmt.Sprintf("You're %s on the %s quest, and fond of %s."+door.CRNL, name, quest, color))
  423. }
  424. func progress_bars(d *door.Door) {
  425. d.Write(door.Clrscr)
  426. var bar door.BarLine = door.BarLine{Line: door.Line{DefaultColor: door.ColorText("BOLD YELLOW")}, Width: 20, Style: door.HALF_STEP}
  427. var bar2 door.BarLine = door.BarLine{Width: 30, Style: door.SOLID, PercentStyle: door.PERCENT_SPACE}
  428. bar2.ColorRange = []door.BarRange{
  429. {Percent: 2500, Color: door.ColorText("RED")},
  430. {Percent: 5000, Color: door.ColorText("BROWN")},
  431. {Percent: 7500, Color: door.ColorText("BOLD YEL")},
  432. {Percent: 9500, Color: door.ColorText("GREEN")},
  433. {Percent: 10100, Color: door.ColorText("BRI GRE")}}
  434. var bar3 door.BarLine = door.BarLine{Width: 15, Style: door.GRADIENT, Line: door.Line{DefaultColor: door.ColorText("CYAN")}}
  435. var percentage int64
  436. bar.UpdateP = pctUpdate(&percentage)
  437. bar2.UpdateP = pctUpdate(&percentage)
  438. bar3.UpdateP = pctUpdate(&percentage)
  439. update_bars := func() {
  440. bar.Update()
  441. bar2.Update()
  442. bar3.Update()
  443. }
  444. d.Write(door.Goto(3, 12) + "Half-Step")
  445. d.Write(door.Goto(25, 12) + "% with space and Color Range")
  446. d.Write(door.Goto(57, 12) + "Gradient")
  447. d.Write(door.HideCursor)
  448. bar_start := door.Goto(3, 15)
  449. for f := 0; f <= 100; f++ {
  450. d.Write(door.Goto(3, 10) + door.Reset + fmt.Sprintf("Value: %d", f))
  451. percentage = int64(f * 100)
  452. update_bars()
  453. d.Write(bar_start + bar.Output() + " " + door.Reset + bar2.Output() + door.Reset + " " + bar3.Output())
  454. if d.Disconnect() {
  455. // don't continue to sleep if we're disconnected
  456. break
  457. }
  458. time.Sleep(time.Millisecond * 100)
  459. }
  460. d.Write(door.ShowCursor)
  461. }
  462. func width_demo(d *door.Door) {
  463. var w int = door.Width
  464. var panel door.Panel = door.Panel{X: 1, Y: 1, Width: w}
  465. var lineColor string = door.ColorText("WHI")
  466. var line string
  467. for y := 1; y <= door.Height; y++ {
  468. if y%10 == 0 {
  469. line = strings.Repeat("1234567890", w/10)
  470. for x := len(line); x < w; x++ {
  471. line += strconv.Itoa((x + 1) % 10)
  472. }
  473. } else {
  474. line = ""
  475. for x := 1; x < w; x++ {
  476. if x%10 == 0 {
  477. line += strconv.Itoa(y % 10)
  478. } else {
  479. line += " "
  480. }
  481. }
  482. }
  483. var l door.Line = door.Line{Text: line, DefaultColor: lineColor}
  484. panel.Lines = append(panel.Lines, l)
  485. }
  486. var message string = fmt.Sprintf("Screen Size: %d X %d", door.Width, door.Height)
  487. d.Write(panel.Output())
  488. // Output alert on top of panel
  489. var cx, cy int
  490. cx = (door.Width - len(message) + 2) / 2
  491. cy = (door.Height - 3) / 2
  492. var alert []string = door.AlertBox(message, 1)
  493. d.Write(door.ColorText("BRI YEL ON BLUE"))
  494. for idx, ab := range alert {
  495. d.Write(door.Goto(cx, cy+idx) + ab)
  496. }
  497. d.Write(door.Reset + panel.GotoEnd())
  498. // Pause for key
  499. d.WaitKey(door.Inactivity)
  500. panel.Lines = make([]door.Line, 0)
  501. var background string = "BUGZ Test Door in GO "
  502. var bl int = len(background)
  503. for y := 1; y <= door.Height; y++ {
  504. offset := (y - 1) % bl
  505. line = background[offset:]
  506. for len(line) < w {
  507. if len(line)+bl <= w {
  508. line += background
  509. } else {
  510. line += background[0 : w-len(line)]
  511. }
  512. }
  513. var l door.Line = door.Line{Text: line, RenderF: door.RenderBlueYellow}
  514. panel.Lines = append(panel.Lines, l)
  515. }
  516. d.Write(panel.Output())
  517. d.WaitKey(door.Inactivity)
  518. }
  519. type TrackPanels struct {
  520. Panel *door.Panel
  521. XPos int
  522. YPos int
  523. BColor string
  524. }
  525. func FindPanel(m door.Mouse, panels []TrackPanels) int {
  526. for idx, p := range panels {
  527. hit, _, _ := p.Panel.Within(int(m.X), int(m.Y))
  528. if hit {
  529. return idx
  530. }
  531. }
  532. return -1
  533. }
  534. func panel_demo(d *door.Door) {
  535. var width int = 55
  536. var panel door.Panel = door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
  537. var moveColor string = door.ColorText("CYAN ON BLACK")
  538. var lineColor string = door.ColorText("BRIGHT WHI ON BLUE")
  539. // Add lines to the panel
  540. for _, line := range []string{"The BBS Door Panel Demo", "(C) 2021 Red Green Software, https://red-green.com"} {
  541. if door.Unicode {
  542. line = strings.Replace(line, "(C)", "\u00a9", -1)
  543. }
  544. var l door.Line = door.Line{Text: line, Width: width, DefaultColor: lineColor}
  545. panel.Lines = append(panel.Lines, l)
  546. }
  547. panel.Lines = append(panel.Lines, panel.Spacer())
  548. panel.Lines = append(panel.Lines, door.Line{Text: "Welcome to golang!", Width: width, DefaultColor: lineColor})
  549. width = 10
  550. var single door.Panel = door.Panel{X: 6, Y: 12, Width: width, Style: door.SINGLE, BorderColor: door.ColorText("WHITE ON BLUE"), Title: "< Single >"}
  551. single.Lines = append(single.Lines, door.Line{Text: "Example", Width: width, DefaultColor: door.ColorText("WHI ON BLACK")})
  552. single.Lines = append(single.Lines, single.Spacer())
  553. single.Lines = append(single.Lines, door.Line{Text: "More Text", Width: width, DefaultColor: door.ColorText("BRI GREEN ON BLACK")})
  554. width = 15
  555. 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}
  556. double_single.Lines = append(double_single.Lines, door.Line{Text: "Double / Single", Width: width, DefaultColor: door.ColorText("BRI WHI ON GREEN")})
  557. double_single.Lines = append(double_single.Lines, double_single.Spacer())
  558. double_single.Lines = append(double_single.Lines, door.Line{Text: "Some Other Text", Width: width, DefaultColor: door.ColorText("BRI CYAN ON GREEN")})
  559. var single_double door.Panel = door.Panel{X: 46, Y: 12, Width: width, Style: door.SINGLE_DOUBLE, BorderColor: door.ColorText("BRI YELL ON RED")}
  560. single_double.Lines = append(single_double.Lines, door.Line{Text: "Single / Double", Width: width, DefaultColor: door.ColorText("BRI WHI ON RED")})
  561. single_double.Lines = append(single_double.Lines, single_double.Spacer())
  562. single_double.Lines = append(single_double.Lines, door.Line{Text: "Text Goes Here ", Width: width, DefaultColor: door.ColorText("BRI GREEN ON RED")})
  563. d.Write(door.Clrscr)
  564. d.Write(panel.Output())
  565. d.Write(single.Output())
  566. d.Write(double_single.Output())
  567. d.Write(single_double.Output())
  568. d.Write(door.Goto(1, 20) + door.Reset + "Use MOUSE to click/drag panels, R to Reset, Q to quit...")
  569. var panels []TrackPanels = []TrackPanels{
  570. {&panel, panel.X, panel.Y, panel.BorderColor},
  571. {&single, single.X, single.Y, single.BorderColor},
  572. {&double_single, double_single.X, double_single.Y, single.BorderColor},
  573. {&single_double, single_double.X, single_double.Y, single_double.BorderColor}}
  574. var movingPanel *door.Panel
  575. var moveX, moveY int
  576. var panelColor string
  577. for {
  578. key, ex, err := d.WaitKey(door.Inactivity)
  579. if err != nil {
  580. return
  581. }
  582. if ex == door.MOUSE {
  583. m, ok := d.GetMouse()
  584. if ok {
  585. // Process Mouse Event
  586. if m.Button == 1 {
  587. idx := FindPanel(m, panels)
  588. if idx != -1 {
  589. movingPanel = panels[idx].Panel
  590. panelColor = movingPanel.BorderColor
  591. moveX = int(m.X)
  592. moveY = int(m.Y)
  593. } else {
  594. continue
  595. }
  596. // Should we do something to the panel? Yes!
  597. movingPanel.BorderColor = moveColor
  598. d.Update(movingPanel.Output())
  599. } else if m.Button == 4 {
  600. if movingPanel != nil {
  601. // Ok, panel is move!
  602. d.Update(movingPanel.Clear())
  603. movingPanel.BorderColor = panelColor
  604. movingPanel.X -= (moveX - int(m.X))
  605. movingPanel.Y -= (moveY - int(m.Y))
  606. // sanity checks
  607. if movingPanel.X < 0 {
  608. movingPanel.X = 0
  609. }
  610. var edgeX bool
  611. if movingPanel.X+movingPanel.Width >= door.Width {
  612. movingPanel.X = door.Width - movingPanel.Width - 1
  613. edgeX = true
  614. }
  615. if movingPanel.Y < 0 {
  616. movingPanel.Y = 0
  617. }
  618. var edgeY bool
  619. if movingPanel.Y+movingPanel.Length() >= door.Height {
  620. movingPanel.Y = door.Height - movingPanel.Length() - 1
  621. edgeY = true
  622. }
  623. // If panel is at the end of the screen -- it scrolls (syncterm)
  624. // This "fixes" it. (Maybe a better way would be to not use last
  625. // line on the screen?)
  626. if edgeX && edgeY {
  627. movingPanel.X--
  628. }
  629. d.Update(movingPanel.Output())
  630. }
  631. movingPanel = nil
  632. }
  633. }
  634. } else {
  635. if (key == 'Q') || (key == 'q') || (key == '\x1b') {
  636. return
  637. }
  638. if (key == 'R') || (key == 'r') {
  639. for _, p := range panels {
  640. d.Update(p.Panel.Clear())
  641. }
  642. for _, p := range panels {
  643. p.Panel.X = p.XPos
  644. p.Panel.Y = p.YPos
  645. p.Panel.BorderColor = p.BColor
  646. d.Update(p.Panel.Output())
  647. }
  648. }
  649. if key == '0' {
  650. d.Write(panels[0].Panel.GotoEnd())
  651. }
  652. if key == '1' {
  653. d.Write(panels[1].Panel.GotoEnd())
  654. }
  655. }
  656. }
  657. }
  658. func main() {
  659. var message string
  660. /*
  661. go func() {
  662. http.ListenAndServe(":6060", nil)
  663. }()
  664. */
  665. var d door.Door = door.Door{}
  666. d.Init("testdoor")
  667. defer func() {
  668. if err := recover(); err != nil {
  669. // This displays stack trace stderr
  670. debug.PrintStack()
  671. fmt.Println("ERROR:", err)
  672. log.Println("FAILURE:", err)
  673. // Display error to user
  674. d.Write(fmt.Sprintf(door.Reset+door.CRNL+"Exception: %v"+door.CRNL, err))
  675. }
  676. }()
  677. defer d.Close()
  678. // Updaters work best when the screen doesn't scroll, so start
  679. // us off at the very top.
  680. d.Write(door.Clrscr)
  681. // Start spinrite effects
  682. var ticker *time.Ticker = time.NewTicker(time.Millisecond * time.Duration(100))
  683. var spin door.SpinRiteMsg = door.SpinRiteMsgInit(15, 5,
  684. door.ColorText("RED ON GREEN"),
  685. []string{"RED", "GREEN", "SOFTWARE"})
  686. var spin2 door.SpinRite = door.SpinRiteInit(13, 5,
  687. door.ColorText("BRI CYA ON BLUE"))
  688. go func() {
  689. var output string
  690. for range ticker.C {
  691. output = door.SavePos + door.Goto(door.Width-16, 1) + spin.Output() +
  692. door.Goto(door.Width-15, 3) + spin2.Output() + door.RestorePos
  693. if !d.Disconnect() {
  694. d.Write(output)
  695. } else {
  696. ticker.Stop()
  697. return
  698. }
  699. }
  700. }()
  701. var wopr door.WOPR
  702. wopr.Init(d.StartTime, d.TimeOut, "") // TimeUsed(), d.TimeLeft(), "")
  703. wopr.ElapsedPanel.X = door.Width - 19
  704. wopr.ElapsedPanel.Y = door.Height - 15
  705. wopr.RemainingPanel.X = door.Width - 19
  706. wopr.RemainingPanel.Y = door.Height - 8
  707. wopr.Animate(&d)
  708. // bold := door.Color(1, 37, 40)
  709. var bolder string = door.ColorText("BOLD YEL ON BLUE")
  710. d.Write("Welcome to " + bolder + "go door go TestDoor." + door.Reset + door.CRNL + "..." + door.CRNL)
  711. d.EnableMouse(door.Normal)
  712. press_a_key(&d)
  713. d.Write(door.CRNL)
  714. var b []string
  715. if door.CP437 {
  716. b = door.AlertBox("Alert: go \xfb is in use!", 1)
  717. } else {
  718. b = door.AlertBox("Alert: go \u221a is in use!", 1)
  719. }
  720. warningColor := door.ColorText("BRI WHI ON GREEN")
  721. for _, line := range b {
  722. // Prevent color bleeding.
  723. d.Write(warningColor + line + door.Reset + door.CRNL)
  724. }
  725. d.Write(door.Reset + door.CRNL)
  726. var left time.Duration = d.TimeLeft()
  727. message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
  728. d.Write(message)
  729. press_a_key(&d)
  730. var mainmenu door.Menu = MainMenu()
  731. var choice int
  732. for choice >= 0 {
  733. d.Write(door.Clrscr + door.HideCursor)
  734. choice = mainmenu.Choose(&d)
  735. d.Write(door.ShowCursor)
  736. if choice < 0 {
  737. break
  738. }
  739. option := mainmenu.GetOption(choice)
  740. wopr.Stop()
  741. // Clear WOPR panels.
  742. d.Write(door.Reset + wopr.Clear())
  743. r, b := mainmenu.Panel.RightBottomPos()
  744. d.Write(door.Goto(r, b))
  745. // fmt.Printf("Choice: %d, Option: %c\n", choice, option)
  746. switch option {
  747. case 'A':
  748. display_ansi(&d)
  749. press_a_key(&d)
  750. case 'D':
  751. display_information(&d)
  752. press_a_key(&d)
  753. case 'F':
  754. font_demo(&d)
  755. press_a_key(&d)
  756. case 'I':
  757. d.Write(door.Reset + door.CRNL + door.CRNL)
  758. input_demo(&d)
  759. press_a_key(&d)
  760. case 'M':
  761. // Why is this so far down on the screen? (Scrolls)
  762. d.Write(door.Reset + door.CRNL + "TO DO: Provide menu of options to select from..." + door.CRNL)
  763. press_a_key(&d)
  764. case 'P':
  765. progress_bars(&d)
  766. press_a_key(&d)
  767. case 'S':
  768. panel_demo(&d)
  769. press_a_key(&d)
  770. case 'T':
  771. about_test_door(&d)
  772. press_a_key(&d)
  773. case 'W':
  774. width_demo(&d)
  775. case 'Q':
  776. // This is also far down on the screen ...
  777. choice = -1
  778. case 'C': // Disabled
  779. var a, z int
  780. z = 0
  781. a = 10 / z
  782. z = a
  783. _ = a
  784. _ = z
  785. }
  786. wopr.Animate(&d)
  787. }
  788. // d.Write("\x1b[?1000l") // disable mouse
  789. // d.Write("\x1b[?1002l")
  790. d.DisableMouse()
  791. d.Write(door.Reset + door.CRNL)
  792. if d.Config.BBSID != "" {
  793. message = fmt.Sprintf("Returning to the %s BBS..."+door.CRNL, d.Config.BBSID)
  794. } else {
  795. message = "Returning to the BBS..." + door.CRNL
  796. }
  797. d.Write(message)
  798. d.WaitKey(time.Second)
  799. left = d.TimeLeft()
  800. ticker.Stop()
  801. message = fmt.Sprintf("You had %0.2f minutes remaining!"+door.CRNL, left.Minutes())
  802. d.Write(message)
  803. left = d.TimeUsed()
  804. d.Write(fmt.Sprintf("You used %0.2f seconds / %0.2f minutes."+door.CRNL, left.Seconds(), left.Minutes()))
  805. fmt.Println("Ending testdoor.go")
  806. }