fork/m-overlay

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

BkacjiosBugfixes + Dark Mode skina8034e9

v2.1.6
12.3 KiB487 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	self:DockPadding(2,2,2,2)
10
11	self.COPY = self:Add("Button")
12	self.COPY:Dock(DOCK_TOP)
13	self.COPY:SetText("Copy")
14	self.COPY:DockMargin(0, 0, 0, 2)
15
16	self.COPY.OnClick = function(this)
17		self.m_pTextEntry:Copy()
18	end
19
20	self.PASTE = self:Add("Button")
21	self.PASTE:Dock(DOCK_TOP)
22	self.PASTE:SetText("Paste")
23	self.PASTE:DockMargin(0, 0, 0, 2)
24
25	self.PASTE.OnClick = function(this)
26		self.m_pTextEntry:Paste()
27	end
28end
29
30function PANEL:PerformLayout()
31	self:SizeToChildren()
32end
33
34function PANEL:OnFocusChanged(focus)
35	if not focus then
36		-- Hide when clicking an option/clicking off
37		self:SetVisible(false)
38	end
39end
40
41local PANEL = class.create("TextEntry", "Panel")
42local utf8 = require("extensions.utf8")
43
44PANEL:ACCESSOR("HoveredInput", "m_bHoveredInput", false)
45PANEL:ACCESSOR("TextHint", "m_sTextHint", "")
46
47function PANEL:TextEntry()
48	self:super() -- Initialize our baseclass
49
50	self.m_pContext = gui.create("TextEntryContext")
51	self.m_pContext:SetTextEntry(self)
52
53	self.m_sText = ""
54	self.m_iCaretPos = 0
55	self.m_bSelectMode = false
56	self.m_iSelectLeft = 0
57	self.m_iSelectRight = 0
58	self.m_bInsert = false
59	self.m_cTextColor = color(0, 0, 0)
60	self.m_cTextHintColor = color_grey
61	self.m_cHighlightTextColor = color(0, 150, 255, 100)
62	self.m_pFont = graphics.newFont() --love.graphics.newFont("resource/fonts/VeraMono.ttf", 12)
63	self.m_pMouseBeam = love.mouse.getSystemCursor("ibeam")
64	self.m_tCharacterSizes = {}
65	self.m_tUndoBuffer = {}
66	self.m_tRedoBuffer = {}
67end
68
69function PANEL:ShowContext()
70	self.m_pContext:SetVisible(true)
71	self.m_pContext:GiveFocus()
72	self.m_pContext:BringToFront()
73	self.m_pContext:SetPos(gui.getMousePosition())
74end
75
76function PANEL:Think(dt)
77	if self:IsHovered() and self:IsEnabled() then
78		love.mouse.setCursor(self.m_pMouseBeam)
79	else
80		love.mouse.setCursor()
81	end
82end
83
84function PANEL:GetText()
85	return self.m_sText
86end
87
88function PANEL:Paint(w, h)
89	gui.skinHook("Paint", "TextEntry", self, w, h)
90
91	love.graphics.setColor(self.m_cTextColor)
92	love.graphics.setFont(self.m_pFont)
93
94	local drawText = self.m_sText
95
96	if not self:HasFocus() and self.m_sText == "" then
97		drawText = self.m_sTextHint
98		love.graphics.setColor(self.m_cTextHintColor)
99	end
100
101	local height = self.m_pFont:getHeight()
102	local tw = self.m_pFont:getWidth(drawText)
103	local pw = self:GetWidth()
104
105	if tw > pw then
106		love.graphics.print(drawText, pw-tw-6, h/2 - (height/2))
107	else
108		love.graphics.print(drawText, 6, h/2 - (height/2))
109	end
110
111	if self.m_iSelectLeft < self.m_iSelectRight then
112		local pre = self:GetPreSelection()
113		local sel = self:GetSelection()
114		local px = self.m_pFont:getWidth(pre)
115		love.graphics.setColor(self.m_cHighlightTextColor)
116		if tw > pw then
117			love.graphics.rectangle("fill", px - tw + pw - 6, h/2 - (height/2), self.m_pFont:getWidth(sel), height)
118		else
119			love.graphics.rectangle("fill", px + 6, h/2 - (height/2), self.m_pFont:getWidth(sel), height)
120		end
121	end
122
123	if not self:HasFocus() or not self:IsEnabled() then return end
124	if math.floor(love.timer.getTime()*2) % 2 == 0 then return end -- Blink cursor on and off
125
126	local pre = utf8.sub(drawText, 1, self.m_iCaretPos)
127	local caretPos = self.m_pFont:getWidth(pre)
128	local caretChar = utf8.sub(drawText, self.m_iCaretPos, self.m_iCaretPos)
129
130	if tw > pw then
131		caretPos = caretPos - tw + pw - 6
132	else
133		caretPos = caretPos + 6
134	end
135
136	if caretChar == "" then caretChar = " " end
137
138	if self.m_bInsert then
139		love.graphics.rectangle("fill", caretPos, h/2 + (height/2), self.m_pFont:getWidth(caretChar), 1)
140	else
141		love.graphics.rectangle("fill", caretPos, h/2 - (height/2), 1, height)
142	end
143end
144
145function PANEL:PaintOverlay(w, h)
146	gui.skinHook("PaintOverlay", "FocusPanel", self, w, h)
147end
148
149function PANEL:SetTextColor(c)
150	self.m_cTextColor = c
151end
152
153function PANEL:OnMousePressed(x, y, but)
154	if but == 1 then
155		self.m_iCaretPos = self:GetCaretPosFromMouse(x, y)
156
157		if self.m_fDoubleClickTime and love.timer.getTime() <= self.m_fDoubleClickTime then
158			-- Select the closest word chunk to the caret
159			for left, right in utf8.gmatch(self.m_sText, '()%w+%s*()') do
160				left = left - 1
161				right = right - 1
162				if self.m_iCaretPos >= left and self.m_iCaretPos <= right then
163					self:SelectCharacters(left, right)
164					self.m_iCaretPos = right
165					return true
166				end
167			end
168
169			-- Select the closest punctuation chunk to the caret
170			for left, right in utf8.gmatch(self.m_sText, '()%p+%s*()') do
171				left = left - 1
172				right = right - 1
173				if self.m_iCaretPos >= left and self.m_iCaretPos <= right then
174					self:SelectCharacters(left, right)
175					self.m_iCaretPos = right
176					return true
177				end
178			end
179		else
180			self.m_fDoubleClickTime = love.timer.getTime() + 0.5
181			self.m_bGrabbed = true
182			self:ExitSelection()
183			self:EnterSelectMode()
184			return true
185		end
186	elseif but == 2 then
187		self:ShowContext()
188		return true
189	end
190end
191
192function PANEL:OnMouseReleased(x, y, but)
193	if but == 1 then
194		if self.m_bGrabbed then
195			self.m_bGrabbed = false
196			self.m_iCaretPos = self:GetCaretPosFromMouse(x, y)
197		end
198		return true
199	end
200end
201
202function PANEL:OnMouseMoved(x, y, dx, dy, istouch)
203	if self.m_bGrabbed then
204		local pos = self:GetCaretPosFromMouse(x, y)
205
206		if self.m_iCaretPos ~= pos then
207			self.m_iCaretPos = pos
208
209			self:EnterSelectMode()
210
211			if dx < 0 then
212				if self.m_iCaretPos >= 0 then
213					if pos < self.m_iSelectLeft then
214						self.m_iSelectLeft = pos -- move selection left
215					else
216						self.m_iSelectRight = pos -- deselect from the right of the selection
217					end
218				end
219			elseif dx > 0 then
220				if self.m_iCaretPos <= utf8.len(self.m_sText) then
221					if pos > self.m_iSelectRight then
222						self.m_iSelectRight = pos -- move selection right
223					else
224						self.m_iSelectLeft = pos -- deselect from the left of the selection
225					end
226				end
227			end
228		end
229		return true
230	end
231end
232
233function PANEL:CaretGoToEnd()
234	self.m_iCaretPos = utf8.len(self.m_sText)
235end
236
237function PANEL:CaretGoToStart()
238	self.m_iCaretPos = 0
239end
240
241function PANEL:GetPreCaret(offset)
242	return utf8.sub(self.m_sText, 1, self.m_iCaretPos + (offset or 0))
243end
244
245function PANEL:GetPostCaret(offset)
246	return utf8.sub(self.m_sText, self.m_iCaretPos + 1 + (offset or 0))
247end
248
249function PANEL:GetPreSelection()
250	return utf8.sub(self.m_sText, 1, self.m_iSelectLeft)
251end
252
253function PANEL:GetSelection()
254	return utf8.sub(self.m_sText, self.m_iSelectLeft + 1, self.m_iSelectRight) or ""
255end
256
257function PANEL:GetPostSelection()
258	return utf8.sub(self.m_sText, self.m_iSelectRight + 1) or ""
259end
260
261function PANEL:GetCaretPosFromMouse(mx, my)
262	local posx = 3
263	local caret_pos = 0
264	local smallest_pos = 0
265	local smallest_diff = math.huge
266
267	-- TODO: Support selection in Y axis..
268
269	local diff = math.abs(mx - posx)
270	if diff < smallest_diff then
271		smallest_diff = diff
272		smallest_pos = caret_pos
273		caret_pos = caret_pos + 1
274	end
275
276	-- "([%z\1-\127\194-\244][\128-\191]*)"
277	for c in self.m_sText:gfind(utf8.charpattern) do
278		if not self.m_tCharacterSizes[c] then
279			self.m_tCharacterSizes[c] = self.m_pFont:getWidth(c)
280		end
281
282		local width = self.m_tCharacterSizes[c]
283		posx = posx + width
284
285		local diff = math.abs(mx - posx)
286		if diff < smallest_diff then
287			smallest_diff = diff
288			smallest_pos = caret_pos
289			caret_pos = caret_pos + 1
290		end
291	end
292
293	return smallest_pos
294end
295
296function PANEL:ExitSelection()
297	if not self.m_bSelectMode then return end
298	self.m_bSelectMode = false
299	self:ResetSelection()
300end
301
302function PANEL:ResetSelection()
303	self.m_iSelectLeft = 0
304	self.m_iSelectRight	= 0
305end
306
307function PANEL:EnterSelectMode()
308	if self.m_bSelectMode then return end
309	self.m_iSelectLeft = self.m_iCaretPos
310	self.m_iSelectRight	= self.m_iCaretPos
311	self.m_bSelectMode = true
312end
313
314function PANEL:SelectCharacters(left, right)
315	self.m_bSelectMode = true
316	self.m_iSelectLeft = left
317	self.m_iSelectRight	= right
318end
319
320function PANEL:SelectAll()
321	self:EnterSelectMode()
322	self.m_iSelectLeft = 0
323	self.m_iSelectRight = utf8.len(self.m_sText)
324	self.m_iCaretPos = self.m_iSelectRight
325end
326
327function PANEL:SetText(str)
328	self.m_sText = str
329	self:ExitSelection()
330	self:CaretGoToEnd()
331end
332
333function PANEL:ChangeText(str)
334	self.m_sText = str
335	self:OnTextChanged(str)
336end
337
338function PANEL:OnTextInput(text)
339	if not self:IsEnabled() then return end
340
341	for c in text:gfind("([%z\1-\127\194-\244][\128-\191]*)") do
342		if not self.m_tCharacterSizes[c] then
343			self.m_tCharacterSizes[c] = self.m_pFont:getWidth(c)
344		end
345	end
346
347	if self.m_bSelectMode then
348		self:ChangeText(self:GetPreSelection() .. text .. self:GetPostSelection())
349		self.m_iCaretPos = utf8.len(self:GetPreSelection() .. text)
350	else
351		self:ChangeText(self:GetPreCaret() .. text .. self:GetPostCaret(self.m_bInsert and 1 or 0))
352		self.m_iCaretPos = self.m_iCaretPos + utf8.len(text)
353	end
354	self:UpdateUndoBuffer()
355	self:ExitSelection()
356	return true
357end
358
359function PANEL:UpdateUndoBuffer()
360	table.insert(self.m_tUndoBuffer, self.m_sText)
361end
362
363function PANEL:Copy()
364	love.system.setClipboardText(self:GetSelection())
365end
366
367function PANEL:Paste()
368	self:OnTextInput(love.system.getClipboardText())
369end
370
371function PANEL:OnKeyPressed(key, isrepeat)
372	if not self:IsEnabled() then return end
373
374	local shift = love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift")
375	local control = love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl")
376
377	if key == "home" then
378		self.m_iCaretPos = 0
379	elseif key == "end" then
380		self.m_iCaretPos = utf8.len(self.m_sText)
381	elseif key == "backspace" then
382		if self.m_iSelectLeft < self.m_iSelectRight then
383			local preSelect = self:GetPreSelection()
384			self:ChangeText(preSelect .. self:GetPostSelection())
385			self.m_iCaretPos = math.min(self.m_iCaretPos, utf8.len(preSelect))
386			self:ExitSelection()
387		else
388			self.m_iCaretPos = math.max(0, self.m_iCaretPos - 1)
389			self:ChangeText(self:GetPreCaret() .. self:GetPostCaret(1))
390		end
391		self:UpdateUndoBuffer()
392	elseif key == "delete" then
393		if self.m_iSelectLeft < self.m_iSelectRight then
394			self:ChangeText(self:GetPreSelection() .. self:GetPostSelection())
395			self.m_iCaretPos = math.min(self.m_iCaretPos, utf8.len(self.m_sText))
396			self:ExitSelection()
397		else
398			self:ChangeText(self:GetPreCaret() .. self:GetPostCaret(1))
399		end
400		self:UpdateUndoBuffer()
401	elseif key == "left" then
402		local pos = math.max(0, self.m_iCaretPos - 1)
403		if shift then
404			self:EnterSelectMode()
405			if self.m_iCaretPos > 0 then
406				if pos < self.m_iSelectLeft then
407					self.m_iSelectLeft = pos -- move selection left
408				else
409					self.m_iSelectRight = pos -- deselect from the right of the selection
410				end
411			end
412			self.m_iCaretPos = pos
413		else
414			if self.m_bSelectMode then
415				self.m_iCaretPos = utf8.len(self:GetPreSelection())
416			else
417				self.m_iCaretPos = pos
418			end
419			self:ExitSelection()
420		end
421	elseif key == "right" then
422		local pos = math.min(utf8.len(self.m_sText), self.m_iCaretPos + 1)
423		if shift then
424			self:EnterSelectMode()
425			if self.m_iCaretPos < utf8.len(self.m_sText) then
426				if pos > self.m_iSelectRight then
427					self.m_iSelectRight = pos -- move selection right
428				else
429					self.m_iSelectLeft = pos -- deselect from the left of the selection
430				end
431			end
432			self.m_iCaretPos = pos
433		else
434			if self.m_bSelectMode then
435				self.m_iCaretPos = utf8.len(self:GetPreSelection() .. self:GetSelection())
436			else
437				self.m_iCaretPos = pos
438			end
439			self:ExitSelection()
440		end
441		self.m_iCaretPos = pos
442	elseif key == "insert" then
443		self.m_bInsert = not self.m_bInsert
444	elseif key == "up" then
445	elseif key == "down" then
446	elseif control then
447		if key == "a" then
448			self:SelectAll()
449		elseif key == "x" then
450			self:Copy()
451			self:ChangeText(self:GetPreSelection() .. self:GetPostSelection())
452			self:UpdateUndoBuffer()
453			self.m_iCaretPos = math.min(self.m_iCaretPos, utf8.len(self.m_sText))
454			self:ExitSelection()
455		elseif key == "c" then
456			self:Copy()
457		elseif key == "v" then
458			self:Paste()
459		elseif key == "y" then
460			local pop = table.remove(self.m_tRedoBuffer) or ""
461			if pop ~= "" then
462				self:ChangeText(pop)
463				self:CaretGoToEnd()
464				self:ExitSelection()
465				table.insert(self.m_tUndoBuffer, pop)
466			end
467		elseif key == "z" then
468			local pop = table.remove(self.m_tUndoBuffer) or ""
469			self:ChangeText(self.m_tUndoBuffer[#self.m_tUndoBuffer] or "")
470			self:CaretGoToEnd()
471			self:ExitSelection()
472			if pop ~= "" then
473				table.insert(self.m_tRedoBuffer, pop)
474			end
475		end
476	end
477	return true
478end
479
480function PANEL:OnKeyReleased(key)
481	if not self:IsEnabled() then return end
482	return true
483end
484
485function PANEL:OnTextChanged(text, add)
486
487end