-
Notifications
You must be signed in to change notification settings - Fork 17
/
chatbox.lua
129 lines (102 loc) · 2.73 KB
/
chatbox.lua
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
if not chat then return end
chat.panel = chat.panel or NULL
function chat.IsVisible()
return chat.panel:IsValid() and chat.panel:IsVisible()
end
function chat.SetInputText(str)
if not chat.panel:IsValid() then return end
chat.panel:SetText(str)
end
function chat.GetInputText()
if not chat.panel:IsValid() then return "" end
return chat.panel:GetText()
end
function chat.GetInputPosition()
if not chat.panel:IsValid() then return 0, 0 end
return chat.panel:GetPosition()
end
function chat.CreateEditPanel(history_path, autocomplete_list)
return edit
end
function chat.GetPanel()
if chat.panel:IsValid() then return chat.panel end
local frame = gui.CreatePanel("frame")
chat.panel = frame
frame:CallOnRemove(chat.Close)
frame:SetSize(Vec2(400, 250))
frame:SetPosition(Vec2(50, window.GetSize().y - frame:GetHeight() - 50))
frame:SetTitle("chat")
frame:SetIcon("textures/silkicons/user_comment.png")
do -- edit line
local edit = frame:CreatePanel("text_input")
edit:SetPadding(Rect() + 3)
edit:SetHeight(20)
edit:SetAutocomplete("chatsounds")
edit:SetHistoryPath("data/chat_history.txt")
function edit:OnEscape()
chat.Close()
end
function edit:OnFinish(str)
chat.Say(str)
self:SetText("")
self:Unfocus()
chat.Close()
return false
end
function edit:OnHeightChanged()
self:SetupLayout("bottom", "fill_x")
end
function edit:OnTextChanged(str)
event.Call("ChatTextChanged", str)
end
edit:SetupLayout("bottom", "fill_x")
frame.edit = edit
end
do -- chat history
local scroll = frame:CreatePanel("scroll")
scroll:SetXScrollBar(false)
scroll:SetupLayout("center_simple", "fill")
local text = scroll:SetPanel(gui.CreatePanel("text"))
text.markup:SetLineWrap(true)
text:AddEvent("ChatAddText", true)
frame.text = text
local old = text.OnStyleChanged -- API ME
function text:OnStyleChanged(skin)
text:SetMargin(Rect() + skin:GetScale() * 2)
old(self, skin)
end
function text:OnTextChanged()
scroll:Layout()
scroll:ScrollToFraction(Vec2(0, 1))
end
function text:OnChatAddText(args)
self.markup:AddFont(self:GetSkin().default_font)
self.markup:AddTable(args, true)
self.markup:AddTagStopper()
self.markup:AddString("\n")
end
end
return frame
end
local old_mouse_trap
function chat.Open()
if event.Call("ChatOpen") == false then return end
local panel = chat.GetPanel()
panel:SetVisible(true)
panel.edit:RequestFocus()
window.SetMouseTrapped(false)
end
function chat.Close()
local panel = chat.GetPanel()
panel:SetVisible(false)
window.SetMouseTrapped(true)
end
input.Bind("y", "show_chat", function()
if not menu.IsVisible() then chat.Open() end
end)
if RELOAD then
--chat.Close()
gui.RemovePanel(chat.panel)
chat.panel = NULL
chat.Open()
end