fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosMore class changes27686d7

settings-overhaul
9.4 KiB371 linesraw
1local PANEL = class.create("TextEntryContext", "Panel")
2
3PANEL:ACCESSOR("TextEntry", "m_pTextEntry")
4
5function PANEL:TextEntryContext()
6	self:super() -- Initialize baseclass
7	self:DisableScissor()
8	self:SetVisible(false)
9
10	self.COPY = self:Add("Button")
11	self.COPY:Dock(DOCK_TOP)
12	self.COPY:SetText("Copy")
13
14	self.COPY.OnClick = function(this)
15		self.m_pTextEntry:Copy()
16	end
17
18	self.PASTE = self:Add("Button")
19	self.PASTE:Dock(DOCK_TOP)
20	self.PASTE:SetText("Paste")
21
22	self.PASTE.OnClick = function(this)
23		self.m_pTextEntry:Paste()
24	end
25end
26
27function PANEL:PerformLayout()
28	self:SizeToChildren()
29end
30
31function PANEL:OnFocusChanged(focus)
32	if not focus then
33		-- Hide when clicking an option/clicking off
34		self:SetVisible(false)
35	end
36end
37
38local PANEL = class.create("TextEntry", "Panel")
39
40PANEL:ACCESSOR("HoveredInput", "m_bHoveredInput", false)
41
42local utf8 = require("extensions.utf8")
43
44function PANEL:TextEntry()
45	self:super() -- Initialize our baseclass
46
47	self.m_pContext = gui.create("TextEntryContext")
48	self.m_pContext:SetTextEntry(self)
49	
50	self.m_sText = ""
51	self.m_iCaretPos = 0
52	self.m_bSelectMode = false
53	self.m_iSelectLeft = 0
54	self.m_iSelectRight = 0
55	self.m_bInsert = false
56	self.m_cTextColor = color(0, 0, 0)
57	self.m_cHighlightTextColor = color(0, 150, 255, 100)
58	self.m_pFont = graphics.newFont() --love.graphics.newFont("resource/fonts/VeraMono.ttf", 12)
59	self.m_pMouseBeam = love.mouse.getSystemCursor("ibeam")
60	self.m_tCharacterSizes = {}
61	self.m_tUndoBuffer = {}
62	self.m_tRedoBuffer = {}
63end
64
65function PANEL:ShowContext()
66	self.m_pContext:SetVisible(true)
67	self.m_pContext:GiveFocus()
68	self.m_pContext:BringToFront()
69	self.m_pContext:SetPos(gui.getMousePosition())
70end
71
72function PANEL:Think(dt)
73	if self:IsHovered() and self:IsEnabled() then
74		love.mouse.setCursor(self.m_pMouseBeam)
75	else
76		love.mouse.setCursor()
77	end
78end
79
80function PANEL:GetText()
81	return self.m_sText
82end
83
84function PANEL:Paint(w, h)
85	gui.skinHook("Paint", "TextEntry", self, w, h)
86
87	love.graphics.setColor(self.m_cTextColor)
88	love.graphics.setFont(self.m_pFont)
89
90	local height = self.m_pFont:getHeight()
91	local tw = self.m_pFont:getWidth(self.m_sText)
92	local pw = self:GetWidth()
93
94	if tw > pw then
95		love.graphics.print(self.m_sText, pw-tw-6, h/2 - (height/2))
96	else
97		love.graphics.print(self.m_sText, 6, h/2 - (height/2))
98	end
99
100	if self.m_iSelectLeft < self.m_iSelectRight then
101		local pre = self:GetPreSelection()
102		local sel = self:GetSelection()
103		local px = self.m_pFont:getWidth(pre)
104		love.graphics.setColor(self.m_cHighlightTextColor)
105		if tw > pw then
106			love.graphics.rectangle("fill", px - tw + pw - 6, h/2 - (height/2), self.m_pFont:getWidth(sel), height)
107		else
108			love.graphics.rectangle("fill", px + 6, h/2 - (height/2), self.m_pFont:getWidth(sel), height)
109		end
110	end
111
112	if not self:HasFocus() or not self:IsEnabled() then return end
113	if math.floor(love.timer.getTime()*2) % 2 == 0 then return end -- Blink cursor on and off
114
115	local pre = utf8.sub(self.m_sText, 1, self.m_iCaretPos)
116	local caretPos = self.m_pFont:getWidth(pre)
117	local caretChar = utf8.sub(self.m_sText, self.m_iCaretPos, self.m_iCaretPos)
118
119	if tw > pw then
120		caretPos = caretPos - tw + pw - 6
121	else
122		caretPos = caretPos + 6
123	end
124
125	if caretChar == "" then caretChar = " " end
126
127	if self.m_bInsert then
128		love.graphics.rectangle("fill", caretPos, h/2 + (height/2), self.m_pFont:getWidth(caretChar), 1)
129	else
130		love.graphics.rectangle("fill", caretPos, h/2 - (height/2), 1, height)
131	end
132end
133
134function PANEL:PaintOverlay(w, h)
135	gui.skinHook("PaintOverlay", "FocusPanel", self, w, h)
136end
137
138function PANEL:SetTextColor(c)
139	self.m_cTextColor = c
140end
141
142function PANEL:OnMousePressed(x, y, but)
143	if but == 2 then
144		self:ShowContext()
145	end
146end
147
148function PANEL:OnMouseReleased(x, y, but)
149
150end
151
152function PANEL:CaretGoToEnd()
153	self.m_iCaretPos = utf8.len(self.m_sText)
154end
155
156function PANEL:CaretGoToStart()
157	self.m_iCaretPos = 0
158end
159
160function PANEL:GetPreCaret(offset)
161	return utf8.sub(self.m_sText, 1, self.m_iCaretPos + (offset or 0))
162end
163
164function PANEL:GetPostCaret(offset)
165	return utf8.sub(self.m_sText, self.m_iCaretPos + 1 + (offset or 0))
166end
167
168function PANEL:GetPreSelection()
169	return utf8.sub(self.m_sText, 1, self.m_iSelectLeft)
170end
171
172function PANEL:GetSelection()
173	return utf8.sub(self.m_sText, self.m_iSelectLeft + 1, self.m_iSelectRight) or ""
174end
175
176function PANEL:GetPostSelection()
177	return utf8.sub(self.m_sText, self.m_iSelectRight + 1) or ""
178end
179
180function PANEL:ResetSelection()
181	if not self.m_bSelectMode then return end
182	self.m_iSelectLeft = 0
183	self.m_iSelectRight	= 0
184	self.m_bSelectMode = false
185end
186
187function PANEL:EnterSelectMode()
188	if self.m_bSelectMode then return end
189	self.m_iSelectLeft = self.m_iCaretPos
190	self.m_iSelectRight	= self.m_iCaretPos
191	self.m_bSelectMode = true
192end
193
194function PANEL:SelectAll()
195	self:EnterSelectMode()
196	self.m_iSelectLeft = 0
197	self.m_iSelectRight = utf8.len(self.m_sText)
198	self.m_iCaretPos = self.m_iSelectRight
199end
200
201function PANEL:SetText(str)
202	self.m_sText = str
203	self:ResetSelection()
204	self:CaretGoToEnd()
205end
206
207function PANEL:OnHoveredTextInput(text)
208	if self.m_bHoveredInput then
209		self:OnTextInput(text)
210	end
211end
212
213function PANEL:OnTextInput(text)
214	if not self:IsEnabled() then return end
215	
216	for c in text:gfind("([%z\1-\127\194-\244][\128-\191]*)") do
217		if not self.m_tCharacterSizes[c] then
218			self.m_tCharacterSizes[c] = self.m_pFont:getWidth(c)
219		end
220	end
221
222	if self.m_bSelectMode then
223		self.m_sText = self:GetPreSelection() .. text .. self:GetPostSelection()
224		self.m_iCaretPos = utf8.len(self:GetPreSelection() .. text)
225	else
226		self.m_sText = self:GetPreCaret() .. text .. self:GetPostCaret(self.m_bInsert and 1 or 0)
227		self.m_iCaretPos = self.m_iCaretPos + utf8.len(text)
228	end
229	self:UpdateUndoBuffer()
230	self:ResetSelection()
231
232	self:OnTextChanged(self.m_sText, text)
233end
234
235function PANEL:UpdateUndoBuffer()
236	table.insert(self.m_tUndoBuffer, self.m_sText)
237end
238
239function PANEL:Copy()
240	love.system.setClipboardText(self:GetSelection())
241end
242
243function PANEL:Paste()
244	self:OnTextInput(love.system.getClipboardText())
245end
246
247function PANEL:OnHoveredKeyPressed(key, isrepeat)
248	if self.m_bHoveredInput then
249		self:OnKeyPressed(key, isrepeat)
250	end
251end
252
253function PANEL:OnKeyPressed(key, isrepeat)
254	if not self:IsEnabled() then return end
255
256	local shift = love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift")
257	local control = love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl")
258
259	if key == "home" then
260		self.m_iCaretPos = 0
261	elseif key == "end" then
262		self.m_iCaretPos = utf8.len(self.m_sText)
263	elseif key == "backspace" then
264		if self.m_iSelectLeft < self.m_iSelectRight then
265			self.m_sText = self:GetPreSelection() .. self:GetPostSelection()
266			self.m_iCaretPos = math.min(self.m_iCaretPos, utf8.len(self.m_sText))
267			self:ResetSelection()
268		else
269			self.m_iCaretPos = math.max(0, self.m_iCaretPos - 1)
270			self.m_sText = self:GetPreCaret() .. self:GetPostCaret(1)
271		end
272		self:UpdateUndoBuffer()
273	elseif key == "delete" then
274		if self.m_iSelectLeft < self.m_iSelectRight then
275			self.m_sText = self:GetPreSelection() .. self:GetPostSelection()
276			self.m_iCaretPos = math.min(self.m_iCaretPos, utf8.len(self.m_sText))
277			self:ResetSelection()
278		else
279			self.m_sText = self:GetPreCaret() .. self:GetPostCaret(1)
280		end
281		self:UpdateUndoBuffer()
282	elseif key == "left" then
283		local pos = math.max(0, self.m_iCaretPos - 1)
284		if shift then
285			self:EnterSelectMode()
286			if self.m_iCaretPos > 0 then
287				if pos < self.m_iSelectLeft then
288					self.m_iSelectLeft = pos -- move selection left
289				else
290					self.m_iSelectRight = pos -- deselect from the right of the selection
291				end
292			end
293			self.m_iCaretPos = pos
294		else
295			if self.m_bSelectMode then
296				self.m_iCaretPos = utf8.len(self:GetPreSelection())
297			else
298				self.m_iCaretPos = pos
299			end
300			self:ResetSelection()
301		end
302	elseif key == "right" then
303		local pos = math.min(utf8.len(self.m_sText), self.m_iCaretPos + 1)
304		if shift then
305			self:EnterSelectMode()
306			if self.m_iCaretPos < utf8.len(self.m_sText) then
307				if pos > self.m_iSelectRight then
308					self.m_iSelectRight = pos -- move selection right
309				else
310					self.m_iSelectLeft = pos -- deselect from the left of the selection
311				end
312			end
313			self.m_iCaretPos = pos
314		else
315			if self.m_bSelectMode then
316				self.m_iCaretPos = utf8.len(self:GetPreSelection() .. self:GetSelection())
317			else
318				self.m_iCaretPos = pos
319			end
320			self:ResetSelection()
321		end
322		self.m_iCaretPos = pos
323	elseif key == "insert" then
324		self.m_bInsert = not self.m_bInsert
325	elseif key == "up" then
326	elseif key == "down" then
327	elseif control then
328		if key == "a" then
329			self:SelectAll()
330		elseif key == "x" then
331			self:Copy()
332			self.m_sText = self:GetPreSelection() .. self:GetPostSelection()
333			self:UpdateUndoBuffer()
334			self.m_iCaretPos = math.min(self.m_iCaretPos, utf8.len(self.m_sText))
335			self:ResetSelection()
336		elseif key == "c" then
337			self:Copy()
338		elseif key == "v" then
339			self:Paste()
340		elseif key == "y" then
341			local pop = table.remove(self.m_tRedoBuffer) or ""
342			if pop ~= "" then
343				self.m_sText = pop
344				self:CaretGoToEnd()
345				self:ResetSelection()
346				table.insert(self.m_tUndoBuffer, pop)
347			end
348		elseif key == "z" then
349			local pop = table.remove(self.m_tUndoBuffer) or ""
350			self.m_sText = self.m_tUndoBuffer[#self.m_tUndoBuffer] or ""
351			self:CaretGoToEnd()
352			self:ResetSelection()
353			if pop ~= "" then
354				table.insert(self.m_tRedoBuffer, pop)
355			end
356		end
357	end
358end
359
360function PANEL:OnHoveredOnKeyReleased(key)
361	if self.m_bHoveredInput then
362		self:OnKeyReleased(key)
363	end
364end
365
366function PANEL:OnKeyReleased(key)
367end
368
369function PANEL:OnTextChanged(text, add)
370
371end