Inline Plugin
# Init?(go)
1package inline
2
3import (
4 "gui/cstyle"
5 "gui/element"
6 "math"
7)
8
9func Init() cstyle.Plugin {
10 return cstyle.Plugin{
11 Selector: func(n *element.Node) bool {
12 styles := map[string]string{
13 "display": "inline",
14 }
15 matches := true
16 for name, value := range styles {
17 if n.Style[name] != value && !(value == "*") && n.Style[name] != "" {
18 matches = false
19 }
20 }
21 return matches
22 },
23 Level: 1,
24 Handler: func(n *element.Node, state *map[string]element.State) {
25 s := *state
26 self := s[n.Properties.Id]
27 parent := s[n.Parent.Properties.Id]
28 copyOfX := self.X
29 copyOfY := self.Y
30 if copyOfX < parent.X+parent.Padding.Left {
31 copyOfX = parent.X + parent.Padding.Left
32 }
33
34 // xCollect := float32(0)
35 for i, v := range n.Parent.Children {
36 // vState := s[v.Properties.Id]
37 if i > 0 {
38 if v.Style["position"] != "absolute" {
39 if v.Properties.Id == n.Properties.Id {
40 sib := n.Parent.Children[i-1]
41 sibling := s[sib.Properties.Id]
42 if sibling.X+sibling.Width+self.Width > (parent.Width+parent.X+parent.Border.Left.Width)-parent.Padding.Left {
43 // Break Node.Id
44 self.Y = sibling.Y + sibling.Height
45 self.X = copyOfX
46 } else {
47 // Node did not break
48 if sib.Style["display"] != "inline" {
49 self.Y = sibling.Y + sibling.Height
50 } else {
51 self.Y = sibling.Y
52 self.X = sibling.X + sibling.Width
53 }
54 if n.InnerText != "" {
55 baseY := sibling.Y
56 var max float32
57 for a := i; a >= 0; a-- {
58 b := n.Parent.Children[a]
59 bStyle := s[b.Properties.Id]
60 if bStyle.Y == baseY {
61 if bStyle.EM > max {
62 max = bStyle.EM
63 }
64 }
65 }
66
67 for a := i; a >= 0; a-- {
68 b := n.Parent.Children[a]
69 bStyle := s[b.Properties.Id]
70 if bStyle.Y == baseY {
71 bStyle.Y += (float32(math.Ceil(float64((max - (max * 0.3))))) - float32(math.Ceil(float64(bStyle.EM-(bStyle.EM*0.3)))))
72 (*state)[b.Properties.Id] = bStyle
73 }
74 }
75 if self.Y == baseY {
76 self.Y += (float32(math.Ceil(float64((max - (max * 0.3))))) - float32(math.Ceil(float64(self.EM-(self.EM*0.3)))))
77 }
78 }
79
80 }
81 break
82 }
83 }
84 }
85
86 }
87 propagateOffsets(n, copyOfX, copyOfY, self, state)
88 (*state)[n.Properties.Id] = self
89 },
90 }
91}
92
93func propagateOffsets(n *element.Node, copyOfX, copyOfY float32, self element.State, state *map[string]element.State) {
94 s := *state
95 for _, v := range n.Children {
96 vState := s[v.Properties.Id]
97 vState.X += self.X - copyOfX
98 vState.Y += self.Y - copyOfY
99 if len(v.Children) > 0 {
100 propagateOffsets(v, copyOfX, copyOfY, self, state)
101 }
102 (*state)[v.Properties.Id] = vState
103 }
104}
105
106// func colliderDetection(s1, s2 element.State) bool {
107// s1Min := s1.Y
108// s1Max := s1.Y + s1.Height
109// s2Min := s2.Y
110// s2Max := s2.Y + s2.Height
111// return s1Min > s2Min && s1Min < s2Max || s1Max > s2Min && s1Min < s2Max || s2Min > s1Min && s2Min < s1Max || s2Max > s1Min && s2Min < s1Max
112// }