1package ui
 2
 3import (
 4	"fmt"
 5	"math"
 6	"pihonclient/extras"
 7	"pihonclient/phnparser"
 8	"pihonclient/renderer"
 9	"pihonclient/screendriver"
10	"strconv"
11)
12
13var BuildVersion string
14
15var UIDisplayedMenu = 0 // 0 - Boot ; 1 - Book Select ; 2 - Book reading
16var UISelectionIndex = 0
17var UISelectionIndexBuffer = 0
18var UITopBarTitle string
19var UIConnectedToInternet = false
20
21var UIMargin int
22var UITopBarHeight int
23var UILineHeight int
24var UIFontHorizontalSpacing int
25
26var UIMaxBookLines int
27
28var UIFrameCount = 0
29
30func RenderUI() {
31	renderer.PerformanceTrackerStart()
32	renderer.BlankScreen(0)
33	switch UIDisplayedMenu {
34	case 0:
35		renderer.DrawPihonLogo()
36		renderer.AddTextAllDisplays(UIMargin, renderer.ScreenHeight-UILineHeight-UIMargin+1, 1, BuildVersion)
37	case 1:
38		DrawTopBar(UIMargin, UITopBarHeight)
39		// Book Info
40		// Title
41		renderer.AddTextAllDisplays(UIMargin, UIMargin*2+UITopBarHeight, 1, phnparser.LibraryTitles[UISelectionIndex])
42		// Author
43		renderer.AddTextAllDisplays(UIMargin, UIMargin*3+UILineHeight+UITopBarHeight, 1, phnparser.LibraryAuthors[UISelectionIndex])
44		// Format / Size
45		renderer.AddTextAllDisplays(UIMargin, renderer.ScreenHeight-UILineHeight-UIMargin+1, 1, phnparser.LibraryFormats[UISelectionIndex]+" "+fmt.Sprintf("%.2fMB", float64(phnparser.LibraryFileSizes[UISelectionIndex])/float64(1024*1024)))
46		// Hash
47		renderer.AddTextAllDisplays(UIMargin, renderer.ScreenHeight-2*UILineHeight-2*UIMargin+1, 1, phnparser.LibraryHashes[UISelectionIndex][:phnparser.MaxLengthOfLine])
48		// Reading progress
49		//renderer.AddTextAllDisplays(renderer.ScreenWidth*renderer.ScreenCount-len("1/123")*UIFontHorizontalSpacing-UIMargin+3, renderer.ScreenHeight-UIMargin-UILineHeight+1, 1, "1/123")
50	case 2:
51		UITopBarTitle = extras.FitWithinCharacterLimits(phnparser.MaxLengthOfLine-2, strconv.Itoa(UISelectionIndex)+"/"+strconv.Itoa(int(math.Ceil(float64(len(phnparser.LoadedBookLines))/float64(UIMaxBookLines))))+"-"+phnparser.LoadedBookTitle)
52		DrawTopBar(UIMargin, UITopBarHeight)
53		// Book text
54		for i := 0; i < UIMaxBookLines; i++ {
55			if UISelectionIndex*UIMaxBookLines+i < len(phnparser.LoadedBookLines) {
56				renderer.AddTextAllDisplays(UIMargin, UIMargin*(2+i)+UITopBarHeight+i*UILineHeight-1, 1, phnparser.LoadedBookLines[UISelectionIndex*UIMaxBookLines+i])
57			}
58		}
59	default:
60		fmt.Printf("Error! Menu does not exist!")
61	}
62	fmt.Printf("Frame render took %fms.\n", float64(renderer.PerformanceTrackerEnd())/float64(1000000))
63	fmt.Printf("%d\n", UISelectionIndex)
64	screendriver.UpdateDisplays()
65	//renderer.UpdateFrameBuffersToFiles(renderer.FrameBuffers[0], 0, UIFrameCount)
66	//renderer.UpdateFrameBuffersToFiles(renderer.FrameBuffers[1], 1, UIFrameCount)
67	UIFrameCount++
68}
69
70func DrawTopBar(UIMargin int, UITopBarHeight int) {
71	// Top bar
72	renderer.DrawHorizontalLine(0, UITopBarHeight, 256, 1)
73	renderer.AddTextAllDisplays(UIMargin, UIMargin, 1, UITopBarTitle)
74	renderer.DrawLightningIcon()
75	if UIConnectedToInternet {
76		renderer.DrawWifiEnabledIcon()
77	} else {
78		renderer.DrawWifiDisabledIcon()
79	}
80}