document
document
is the main API interface for the library, it contains most of the methods provided in the JavaScript document object
flowchart LR; index.html-->open master.css-->open gui-->index.html; gui-->master.css; gui-->new; open-->new; new-->window; window-->document; document-->gui.display; script-->document;
# Open?(go)
The Open
methed is used to load a file from disk and display the file.
1package document
2
3import (
4 "gui/cstyle"
5 "gui/element"
6 "gui/events"
7 "gui/window"
8
9 "gui/cstyle/plugins/block"
10 "gui/cstyle/plugins/flex"
11 "gui/cstyle/plugins/inline"
12
13 rl "github.com/gen2brain/raylib-go/raylib"
14 "golang.org/x/net/html"
15)
16
17type Window struct {
18 StyleSheets []string
19 StyleTags []string
20 DOM *html.Node
21 Title string
22}
23
24type Document struct {
25 CSS cstyle.CSS
26}
27
28func (doc Document) Open(index string, script func(*element.Node)) {
29 d := parse(index)
30
31 wm := window.NewWindowManager()
32 wm.FPS = true
33
34 // Initialization
35 var screenWidth int32 = 800
36 var screenHeight int32 = 450
37
38 // Open the window
39 wm.OpenWindow(screenWidth, screenHeight)
40 defer wm.CloseWindow()
41
42 doc.CSS = cstyle.CSS{
43 Width: 800,
44 Height: 450,
45 }
46 doc.CSS.StyleSheet("./master.css")
47 // css.AddPlugin(position.Init())
48 doc.CSS.AddPlugin(inline.Init())
49 doc.CSS.AddPlugin(block.Init())
50 doc.CSS.AddPlugin(flex.Init())
51
52 for _, v := range d.StyleSheets {
53 doc.CSS.StyleSheet(v)
54 }
55
56 for _, v := range d.StyleTags {
57 doc.CSS.StyleTag(v)
58 }
59
60 nodes := doc.CSS.CreateDocument(d.DOM)
61 root := &nodes
62
63 script(root)
64
65 // fmt.Println(nodes.Style)
66
67 evts := map[string]element.EventList{}
68
69 eventStore := &evts
70
71 // Main game loop
72 for !wm.WindowShouldClose() {
73 rl.BeginDrawing()
74
75 // Check if the window size has changed
76 newWidth := int32(rl.GetScreenWidth())
77 newHeight := int32(rl.GetScreenHeight())
78
79 if newWidth != screenWidth || newHeight != screenHeight {
80 rl.ClearBackground(rl.RayWhite)
81 // Window has been resized, handle the event
82 screenWidth = newWidth
83 screenHeight = newHeight
84
85 doc.CSS.Width = float32(screenWidth)
86 doc.CSS.Height = float32(screenHeight)
87
88 nodes = doc.CSS.CreateDocument(d.DOM)
89 root = &nodes
90 script(root)
91 }
92
93 eventStore = events.GetEvents(root, eventStore)
94 doc.CSS.ComputeNodeStyle(root)
95 rd := doc.CSS.Render(*root)
96 wm.LoadTextures(rd)
97 wm.Draw(rd)
98
99 events.RunEvents(eventStore)
100
101 rl.EndDrawing()
102 }
103}