forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.lua
More file actions
240 lines (218 loc) · 9.51 KB
/
console.lua
File metadata and controls
240 lines (218 loc) · 9.51 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
--*****************************************************************************
--* File: lua/modules/ui/dialogs/console.lua
--* Author: Chris Blackwell
--* Summary: command console
--*
--* Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
--*****************************************************************************
local UIUtil = import('/lua/ui/uiutil.lua')
local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
local Group = import('/lua/maui/group.lua').Group
local Bitmap = import('/lua/maui/bitmap.lua').Bitmap
local ItemList = import('/lua/maui/itemlist.lua').ItemList
local Scrollbar = import('/lua/maui/scrollbar.lua').Scrollbar
local Border = import('/lua/maui/border.lua').Border
local Edit = import('/lua/maui/edit.lua').Edit
local Control = import('/lua/maui/control.lua').Control
local Window = import('/lua/maui/window.lua').Window
local IntegerSlider = import('/lua/maui/slider.lua').IntegerSlider
local Prefs = import('/lua/user/prefs.lua')
local commandDeque = {}
local maxCommandDequeSize = 10
local currentCommandDequeIndex = 0
local currentText = ""
local consoleFontName = UIUtil.fixedFont
local consoleFontSize = 12
local window = false
local parent = false
local function InsertCommand(text)
table.insert(commandDeque, text)
if table.getn(commandDeque) > maxCommandDequeSize then
table.remove(commandDeque, 1)
end
currentCommandDequeIndex = table.getn(commandDeque)
end
function ConfigWindow(parent)
if window then return end
window = Window(GetFrame(0), '<LOC console_0000>Console Config', nil, nil, nil, true)
window.Left:Set(function() return parent.Left() + 10 end)
window.Top:Set(function() return parent.Top() + 30 end)
window.Right:Set(function() return parent.Left() + 230 end)
window.Bottom:Set(function() return parent.Top() + 120 end)
window.Depth:Set(GetFrame(0):GetTopmostDepth() + 1)
local client = window:GetClientGroup()
local defValue = Prefs.GetFromCurrentProfile('console_alpha') or 1
defValue = defValue * 100
local label = UIUtil.CreateText(client, LOCF("<LOC console_alpha>Alpha: %d%%", defValue), 14)
LayoutHelpers.AtLeftTopIn(label, client, 5, 5)
local slider = IntegerSlider(client, false,
20, 100, 1, UIUtil.SkinnableFile('/slider02/slider_btn_up.dds'),
UIUtil.SkinnableFile('/slider02/slider_btn_over.dds'), UIUtil.SkinnableFile('/slider02/slider_btn_down.dds'),
UIUtil.SkinnableFile('/dialogs/options-02/slider-back_bmp.dds'))
LayoutHelpers.Below(slider, label)
slider.OnValueSet = function(self, newValue)
end
slider.OnValueChanged = function(self, newValue)
parent:SetAlpha(newValue / 100, true)
label:SetText(LOCF("<LOC console_alpha>Alpha: %d%%", newValue))
end
slider:SetValue(defValue)
window.OnClose = function(self)
Prefs.SetToCurrentProfile('console_alpha', slider:GetValue()/100)
window:Destroy()
window = false
end
end
function CreateDialog()
local mainFrame = GetFrame(0)
local location = {Top = 5, Left = 5, Bottom = 500, Right = 300}
parent = Window(mainFrame, '<LOC _Console>Console', nil, nil, true, false, false, 'console_window', location)
parent.Depth:Set(UIUtil.consoleDepth)
parent:SetMinimumResize(200, 170)
local edit = Edit(parent:GetClientGroup())
edit.Left:Set(function() return parent:GetClientGroup().Left() + 10 end)
edit.Right:Set(function() return parent:GetClientGroup().Right() - 38 end)
edit.Bottom:Set(function() return parent:GetClientGroup().Bottom() - 10 end)
edit:SetForegroundColor(UIUtil.consoleFGColor())
edit:SetBackgroundColor('ff333333')
edit:SetHighlightForegroundColor("black")
edit:SetHighlightBackgroundColor(UIUtil.consoleTextBGColor())
edit:SetFont(consoleFontName, consoleFontSize)
edit.Height:Set(function() return math.floor(edit:GetFontHeight()) end)
local consoleOutput = ItemList(parent:GetClientGroup())
LayoutHelpers.Above(consoleOutput, edit, 10)
consoleOutput.Top:Set(function() return parent:GetClientGroup().Top() + 5 end)
consoleOutput.Right:Set(edit.Right)
consoleOutput:SetColors(UIUtil.consoleFGColor(), UIUtil.consoleTextBGColor(), UIUtil.consoleFGColor(), UIUtil.consoleTextBGColor()) -- we don't really want selection here so don't differentiate colors
consoleOutput:SetFont(consoleFontName, consoleFontSize)
UIUtil.CreateVertScrollbarFor(consoleOutput)
parent.OnConfigClick = function(self)
ConfigWindow(parent)
end
-- set up output control to recieve console spew
local outputHandler = AddConsoleOutputReciever(function(text)
consoleOutput:AddItem(text)
local itemCount = consoleOutput:GetItemCount()
if itemCount > 300 then -- 300 seems like a good buffer size
consoleOutput:DeleteItem(0)
end
consoleOutput:SetSelection(itemCount - 1)
consoleOutput:ScrollToBottom()
end)
-- if this gets destroyed we need to unregister and destroy the output handler
parent.OnDestroy = function(self)
RemoveConsoleOutputReciever(outputHandler)
end
-- handle edit control
local conFuncsList = false
local raiseConFuncList = true
edit.OnTextChanged = function(self, newText, oldText)
if raiseConFuncList then
if conFuncsList then
conFuncsList:Destroy()
conFuncsList = false
end
local matches = ConTextMatches(newText)
local numMatches = table.getn(matches)
if (numMatches > 0) then
conFuncsList = ItemList(consoleOutput)
conFuncsList:SetFont(consoleFontName, consoleFontSize)
LayoutHelpers.Above(conFuncsList, edit)
conFuncsList.Right:Set(function() return edit.Right() - 32 end)
conFuncsList.Height:Set(function()
return math.min(consoleOutput.Height(), conFuncsList:GetRowHeight() * numMatches)
end)
for i,v in matches do
conFuncsList:AddItem(v)
end
if conFuncsList:NeedsScrollBar() then
UIUtil.CreateVertScrollbarFor(conFuncsList)
end
conFuncsList:SetSelection(conFuncsList:GetItemCount() - 1)
conFuncsList:ScrollToBottom()
conFuncsList.OnDoubleClick = function(self, row)
edit:SetText(conFuncsList:GetItem(row))
edit:AcquireFocus()
end
end
end
end
edit.OnEnterPressed = function(self, text)
if string.lower(text) == "ren_showdirtyterrain" then
WARN("The command \'" .. text .. "\' is harmful and will crash the game. It is not executed.")
return
end
ConExecuteSave(text)
InsertCommand(text)
end
edit.OnNonTextKeyPressed = function(self, keycode)
local function ChangeConFuncSelection(direction)
local selection = math.max(math.min(conFuncsList:GetSelection() + direction, conFuncsList:GetItemCount() - 1), 0)
conFuncsList:SetSelection(selection)
raiseConFuncList = false
edit:SetText(conFuncsList:GetItem(selection))
raiseConFuncList = true
end
if keycode == UIUtil.VK_UP then
if conFuncsList then
ChangeConFuncSelection(-1)
else
-- store off the current text if this is the first press
local commandDequeSize = table.getn(commandDeque)
if commandDequeSize > 0 then
if currentCommandDequeIndex == commandDequeSize then currentText = edit:GetText() end
edit:SetText(commandDeque[currentCommandDequeIndex])
currentCommandDequeIndex = currentCommandDequeIndex - 1
if currentCommandDequeIndex <= 0 then currentCommandDequeIndex = 1 end
end
end
elseif keycode == UIUtil.VK_DOWN then
if conFuncsList then
ChangeConFuncSelection(1)
else
currentCommandDequeIndex = currentCommandDequeIndex + 1
if currentCommandDequeIndex > table.getn(commandDeque) then
currentCommandDequeIndex = table.getn(commandDeque)
edit:SetText(currentText)
else
edit:SetText(commandDeque[currentCommandDequeIndex])
end
end
end
end
edit.OnCharPressed = function(self, charcode)
-- close console on tilde key (apostrophe, or ascii 96)
if charcode == 96 then
edit:AbandonKeyboardFocus()
parent:Hide()
return true
elseif charcode == UIUtil.VK_TAB then
if conFuncsList then
edit:SetText(conFuncsList:GetItem(conFuncsList:GetSelection()))
return true
else
return false
end
else
return false
end
end
-- override Show behavior to acquire keyboard focus
parent.Show = function(self)
edit:AcquireFocus()
Control.Show(self)
end
parent.Hide = function(self)
if window then
window:Destroy()
window = false
end
Control.Hide(self)
end
parent.OnClose = function(self)
self:Hide()
end
local tempalpha = Prefs.GetFromCurrentProfile('console_alpha') or 1
parent:SetAlpha(tempalpha, true)
return parent
end