-
Notifications
You must be signed in to change notification settings - Fork 26
/
chat.go
226 lines (210 loc) · 5.69 KB
/
chat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package steven
import (
"math"
"github.com/go-gl/glfw/v3.1/glfw"
"github.com/thinkofdeath/steven/format"
"github.com/thinkofdeath/steven/protocol"
"github.com/thinkofdeath/steven/render"
"github.com/thinkofdeath/steven/ui"
)
const (
chatHistoryLines = 10
maxLineWidth = 640
)
type ChatUI struct {
Lines [chatHistoryLines]format.AnyComponent
container *ui.Container
parts []*chatLine
input *ui.Text
inputBackground *ui.Image
enteringText bool
wasEnteringText bool
inputLine []rune
cursorTick float64
}
type chatLine struct {
fade float64
text *ui.Formatted
background *ui.Image
}
func (c *ChatUI) init() {
c.container = ui.NewContainer(0, 44, maxLineWidth, chatHistoryLines*18+2)
c.container.Attach(ui.Bottom, ui.Left)
c.input = ui.NewText("", 5, 1, 255, 255, 255).Attach(ui.Bottom, ui.Left)
c.input.SetDraw(false)
c.input.AttachTo(c.container)
c.inputBackground = ui.NewImage(render.GetTexture("solid"), 0, 0, maxLineWidth, 20, 0, 0, 1, 1, 0, 0, 0).Attach(ui.Bottom, ui.Left)
c.inputBackground.SetA(77)
c.inputBackground.AttachTo(c.container)
c.inputBackground.SetDraw(false)
Client.scene.AddDrawable(c.inputBackground)
Client.scene.AddDrawable(c.input)
}
func (c *ChatUI) Draw(delta float64) {
if c.wasEnteringText != c.enteringText {
if c.wasEnteringText {
c.input.SetDraw(false)
c.inputBackground.SetDraw(false)
c.input.Update(string(c.inputLine))
for _, p := range c.parts {
p.text.SetY(p.text.Y() + 18)
p.background.SetY(p.background.Y() + 18)
}
} else {
for _, p := range c.parts {
p.text.SetY(p.text.Y() - 18)
p.background.SetY(p.background.Y() - 18)
}
}
c.wasEnteringText = c.enteringText
}
if c.enteringText {
c.input.SetDraw(true)
c.inputBackground.SetDraw(true)
c.cursorTick += delta
// Add on our cursor
if int(c.cursorTick/30)%2 == 0 {
c.input.Update(string(c.inputLine) + "|")
} else {
c.input.Update(string(c.inputLine))
}
// Lazy way of preventing rounding errors buiding up over time
if c.cursorTick > 0xFFFFFF {
c.cursorTick = 0
}
}
limit := 0.0
if c.enteringText {
limit = -18
}
for i := 0; i < len(c.parts); i++ {
p := c.parts[i]
if p.background.Y() < limit {
c.parts = append(c.parts[:i], c.parts[i+1:]...)
i--
p.text.Remove()
p.background.Remove()
} else {
p.fade -= 0.005 * delta
if p.fade < 0 {
p.fade = 0
}
for _, t := range p.text.Text {
if c.enteringText {
t.SetA(255)
} else {
t.SetA(int(255 * p.fade))
}
}
ba := 0.3
if !c.enteringText {
ba -= (1.0 - p.fade) / 2.0
ba = math.Min(ba, 0.3)
}
p.background.SetA(int(255 * ba))
if p.background.A() < 0 {
p.background.SetA(0)
}
}
}
}
func (c *ChatUI) handleKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if (key == glfw.KeyEscape || key == glfw.KeyEnter) && action == glfw.Release {
if key == glfw.KeyEnter && len(c.inputLine) != 0 {
Client.network.Write(&protocol.ChatMessage{string(c.inputLine)})
}
// Return control back to the default
c.enteringText = false
c.inputLine = c.inputLine[:0]
lockMouse = true
w.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
w.SetCharCallback(nil)
return
}
if key == glfw.KeyBackspace && action != glfw.Release {
if len(c.inputLine) > 0 {
c.inputLine = c.inputLine[:len(c.inputLine)-1]
}
}
}
func (c *ChatUI) handleChar(w *glfw.Window, char rune) {
if len(c.inputLine) < 100 {
c.inputLine = append(c.inputLine, char)
}
}
func chatColorRGB(c format.Color) (r, g, b int) {
switch c {
case format.Black:
return 0, 0, 0
case format.DarkBlue:
return 0, 0, 170
case format.DarkGreen:
return 0, 170, 0
case format.DarkAqua:
return 0, 170, 170
case format.DarkRed:
return 170, 0, 0
case format.DarkPurple:
return 170, 0, 170
case format.Gold:
return 255, 170, 0
case format.Gray:
return 170, 170, 170
case format.DarkGray:
return 85, 85, 85
case format.Blue:
return 85, 85, 255
case format.Green:
return 85, 255, 85
case format.Aqua:
return 85, 255, 255
case format.Red:
return 255, 85, 85
case format.LightPurple:
return 255, 85, 255
case format.Yellow:
return 255, 255, 85
case format.White:
return 255, 255, 255
}
return 255, 255, 255
}
func (c *ChatUI) Add(msg format.AnyComponent) {
format.ConvertLegacy(msg)
copy(c.Lines[0:chatHistoryLines-1], c.Lines[1:])
c.Lines[chatHistoryLines-1] = msg
f := ui.NewFormattedWidth(msg, 5, chatHistoryLines*18+1, maxLineWidth-10).Attach(ui.Top, ui.Left)
f.AttachTo(c.container)
line := &chatLine{
text: f,
fade: 3.0,
background: ui.NewImage(render.GetTexture("solid"), 0, chatHistoryLines*18, maxLineWidth, f.Height, 0, 0, 1, 1, 0, 0, 0),
}
line.background.AttachTo(c.container)
line.background.SetA(77)
c.parts = append(c.parts, line)
Client.scene.AddDrawable(line.background)
Client.scene.AddDrawable(f)
ff := f
for _, f := range c.parts {
f.text.SetY(f.text.Y() - 18*float64(ff.Lines))
f.background.SetY(f.background.Y() - 18*float64(ff.Lines))
}
if c.enteringText {
ff.SetY(ff.Y() - 18)
line.background.SetY(line.background.Y() - 18)
}
}