You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1184 lines
39 KiB

4 years ago
3 years ago
local DF = _G["DetailsFramework"]
4 years ago
if (not DF or not DetailsFrameworkCanLoad) then
3 years ago
return
4 years ago
end
local _
3 years ago
local loadstring = loadstring --lua local
4 years ago
local APITextEntryFunctions = false
do
local metaPrototype = {
WidgetType = "textentry",
dversion = DF.dversion,
}
--check if there's a metaPrototype already existing
if (_G[DF.GlobalWidgetControlNames["textentry"]]) then
--get the already existing metaPrototype
3 years ago
local oldMetaPrototype = _G[DF.GlobalWidgetControlNames["textentry"]]
4 years ago
--check if is older
if ( (not oldMetaPrototype.dversion) or (oldMetaPrototype.dversion < DF.dversion) ) then
--the version is older them the currently loading one
--copy the new values into the old metatable
for funcName, _ in pairs(metaPrototype) do
oldMetaPrototype[funcName] = metaPrototype[funcName]
end
end
else
--first time loading the framework
3 years ago
_G[DF.GlobalWidgetControlNames["textentry"]] = metaPrototype
4 years ago
end
end
3 years ago
local TextEntryMetaFunctions = _G[DF.GlobalWidgetControlNames["textentry"]]
DF:Mixin(TextEntryMetaFunctions, DF.SetPointMixin)
DF:Mixin(TextEntryMetaFunctions, DF.FrameMixin)
DF:Mixin(TextEntryMetaFunctions, DF.TooltipHandlerMixin)
DF:Mixin(TextEntryMetaFunctions, DF.ScriptHookMixin)
4 years ago
DF.TextEntryCounter = DF.TextEntryCounter or 1
------------------------------------------------------------------------------------------------------------
3 years ago
--metatables
4 years ago
3 years ago
TextEntryMetaFunctions.__call = function(object, value)
4 years ago
end
3 years ago
4 years ago
------------------------------------------------------------------------------------------------------------
3 years ago
--members
--tooltip
local gmember_tooltip = function(object)
return object:GetTooltip()
end
4 years ago
3 years ago
--shown
local gmember_shown = function(object)
return object:IsShown()
4 years ago
end
3 years ago
--frame width
local gmember_width = function(object)
return object.editbox:GetWidth()
4 years ago
end
3 years ago
--frame height
local gmember_height = function(object)
return object.editbox:GetHeight()
4 years ago
end
3 years ago
--get text
local gmember_text = function(object)
return object.editbox:GetText()
4 years ago
end
3 years ago
--return if the text entry has focus
local gmember_hasfocus = function(object)
return object:HasFocus()
4 years ago
end
TextEntryMetaFunctions.GetMembers = TextEntryMetaFunctions.GetMembers or {}
3 years ago
TextEntryMetaFunctions.GetMembers["tooltip"] = gmember_tooltip
TextEntryMetaFunctions.GetMembers["shown"] = gmember_shown
TextEntryMetaFunctions.GetMembers["width"] = gmember_width
TextEntryMetaFunctions.GetMembers["height"] = gmember_height
TextEntryMetaFunctions.GetMembers["text"] = gmember_text
TextEntryMetaFunctions.GetMembers["hasfocus"] = gmember_hasfocus
TextEntryMetaFunctions.__index = function(object, key)
local func = TextEntryMetaFunctions.GetMembers[key]
4 years ago
if (func) then
3 years ago
return func(object, key)
4 years ago
end
3 years ago
local fromMe = rawget(object, key)
4 years ago
if (fromMe) then
return fromMe
end
3 years ago
return TextEntryMetaFunctions[key]
4 years ago
end
3 years ago
4 years ago
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 years ago
--tooltip
local smember_tooltip = function(object, value)
return object:SetTooltip(value)
end
--show
local smember_show = function(object, value)
if (value) then
return object:Show()
4 years ago
else
3 years ago
return object:Hide()
4 years ago
end
end
3 years ago
--hide
local smember_hide = function(object, value)
if (not value) then
return object:Show()
4 years ago
else
3 years ago
return object:Hide()
4 years ago
end
3 years ago
end
--frame width
local smember_width = function(object, value)
return object.editbox:SetWidth(value)
end
--frame height
local smember_height = function(object, value)
return object.editbox:SetHeight(value)
end
--set text
local smember_text = function(object, value)
return object.editbox:SetText(value)
end
--set multiline
local smember_multiline = function(object, value)
if (value) then
return object.editbox:SetMultiLine(true)
4 years ago
else
3 years ago
return object.editbox:SetMultiLine(false)
4 years ago
end
end
3 years ago
--text horizontal pos
local smember_horizontalpos = function(object, value)
return object.editbox:SetJustifyH(string.lower(value))
4 years ago
end
3 years ago
4 years ago
TextEntryMetaFunctions.SetMembers = TextEntryMetaFunctions.SetMembers or {}
3 years ago
TextEntryMetaFunctions.SetMembers["tooltip"] = smember_tooltip
TextEntryMetaFunctions.SetMembers["show"] = smember_show
TextEntryMetaFunctions.SetMembers["hide"] = smember_hide
TextEntryMetaFunctions.SetMembers["width"] = smember_width
TextEntryMetaFunctions.SetMembers["height"] = smember_height
TextEntryMetaFunctions.SetMembers["text"] = smember_text
TextEntryMetaFunctions.SetMembers["multiline"] = smember_multiline
TextEntryMetaFunctions.SetMembers["align"] = smember_horizontalpos
TextEntryMetaFunctions.__newindex = function(object, key, value)
local func = TextEntryMetaFunctions.SetMembers[key]
4 years ago
if (func) then
3 years ago
return func(object, value)
4 years ago
else
3 years ago
return rawset(object, key, value)
4 years ago
end
end
------------------------------------------------------------------------------------------------------------
3 years ago
--methods
4 years ago
local cleanfunction = function()end
3 years ago
function TextEntryMetaFunctions:SetEnterFunction(func, param1, param2)
4 years ago
if (func) then
3 years ago
rawset(self, "func", func)
4 years ago
else
3 years ago
rawset(self, "func", cleanfunction)
4 years ago
end
3 years ago
4 years ago
if (param1 ~= nil) then
3 years ago
rawset(self, "param1", param1)
4 years ago
end
if (param2 ~= nil) then
3 years ago
rawset(self, "param2", param2)
4 years ago
end
end
3 years ago
function TextEntryMetaFunctions:SetText(text)
self.editbox:SetText(text)
4 years ago
end
3 years ago
4 years ago
function TextEntryMetaFunctions:GetText()
return self.editbox:GetText()
end
3 years ago
--select all text
function TextEntryMetaFunctions:SelectAll()
self.editbox:HighlightText()
4 years ago
end
3 years ago
function TextEntryMetaFunctions:SetAutoSelectTextOnFocus(value)
self.autoSelectAllText = value
4 years ago
end
3 years ago
--set label description
function TextEntryMetaFunctions:SetLabelText(text)
self.label:SetText(text)
4 years ago
end
3 years ago
--set tab order
function TextEntryMetaFunctions:SetNext(nextbox)
4 years ago
self.next = nextbox
end
3 years ago
--blink
4 years ago
function TextEntryMetaFunctions:Blink()
3 years ago
self.label:SetTextColor(1, .2, .2, 1)
4 years ago
end
3 years ago
--hooks
4 years ago
function TextEntryMetaFunctions:Enable()
if (not self.editbox:IsEnabled()) then
self.editbox:Enable()
3 years ago
self.editbox:SetBackdropBorderColor(unpack(self.enabled_border_color))
self.editbox:SetBackdropColor(unpack(self.enabled_backdrop_color))
self.editbox:SetTextColor(unpack(self.enabled_text_color))
4 years ago
if (self.editbox.borderframe) then
3 years ago
local r, g, b, a = DF:ParseColors(unpack(self.editbox.borderframe.onleave_backdrop))
self.editbox.borderframe:SetBackdropColor(r, g, b, a)
4 years ago
end
end
end
3 years ago
4 years ago
function TextEntryMetaFunctions:Disable()
if (self.editbox:IsEnabled()) then
self.enabled_border_color = {self.editbox:GetBackdropBorderColor()}
self.enabled_backdrop_color = {self.editbox:GetBackdropColor()}
self.enabled_text_color = {self.editbox:GetTextColor()}
self.editbox:Disable()
3 years ago
self.editbox:SetBackdropBorderColor(.5, .5, .5, .5)
self.editbox:SetBackdropColor(.5, .5, .5, .5)
self.editbox:SetTextColor(.5, .5, .5, .5)
4 years ago
if (self.editbox.borderframe) then
3 years ago
self.editbox.borderframe:SetBackdropColor(.5, .5, .5, .5)
4 years ago
end
end
end
function TextEntryMetaFunctions:SetCommitFunction(func)
if (type(func) == "function") then
self.func = func
end
end
3 years ago
function TextEntryMetaFunctions:IgnoreNextCallback()
self.ignoreNextCallback = true
end
4 years ago
------------------------------------------------------------------------------------------------------------
3 years ago
--scripts and hooks
4 years ago
3 years ago
local OnEnter = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnEnter", textentry, object)
4 years ago
if (kill) then
return
end
3 years ago
object:ShowTooltip()
textentry.mouse_over = true
4 years ago
3 years ago
if (textentry:IsEnabled()) then
4 years ago
textentry.current_bordercolor = textentry.current_bordercolor or {textentry:GetBackdropBorderColor()}
3 years ago
textentry:SetBackdropBorderColor(1, 1, 1, 1)
4 years ago
end
end
3 years ago
local OnLeave = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnLeave", textentry, object)
4 years ago
if (kill) then
return
end
3 years ago
object:HideTooltip()
textentry.mouse_over = false
if (textentry:IsEnabled()) then
textentry:SetBackdropBorderColor(unpack(textentry.current_bordercolor))
4 years ago
end
end
3 years ago
local OnHide = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnHide", textentry, object)
4 years ago
if (kill) then
return
end
end
3 years ago
local OnShow = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnShow", textentry, object)
4 years ago
if (kill) then
return
end
end
3 years ago
local OnEnterPressed = function(textentry, byScript)
local object = textentry.MyObject
if (object.ignoreNextCallback) then
DF.Schedules.RunNextTick(function() object.ignoreNextCallback = nil end)
return
end
local kill = object:RunHooksForWidget("OnEnterPressed", textentry, object, object.text)
4 years ago
if (kill) then
return
end
3 years ago
local text = DF:Trim(textentry:GetText())
if (string.len(text) > 0) then
textentry.text = text
if (textentry.MyObject.func) then
--need to have a dispatch here
textentry.MyObject.func(textentry.MyObject.param1, textentry.MyObject.param2, text, textentry, byScript or textentry)
4 years ago
end
else
3 years ago
textentry:SetText("")
4 years ago
textentry.MyObject.currenttext = ""
end
3 years ago
if (not object.NoClearFocusOnEnterPressed) then
textentry.focuslost = true --quando estiver editando e clicar em outra caixa
4 years ago
textentry:ClearFocus()
if (textentry.MyObject.tab_on_enter and textentry.MyObject.next) then
textentry.MyObject.next:SetFocus()
end
end
end
3 years ago
local OnEscapePressed = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnEscapePressed", textentry, object, object.text)
4 years ago
if (kill) then
return
3 years ago
end
4 years ago
textentry.focuslost = true
3 years ago
textentry:ClearFocus()
4 years ago
end
3 years ago
local OnSpacePressed = function(textEntry)
local object = textEntry.MyObject
local kill = object:RunHooksForWidget("OnSpacePressed", textEntry, object)
4 years ago
if (kill) then
return
end
end
3 years ago
local OnEditFocusLost = function(textEntry)
local object = textEntry.MyObject
if (object.ignoreNextCallback) then
DF.Schedules.RunNextTick(function() object.ignoreNextCallback = nil end)
return
end
if (textEntry:IsShown()) then
local kill = object:RunHooksForWidget("OnEditFocusLost", textEntry, object, object.text)
4 years ago
if (kill) then
return
end
3 years ago
if (not textEntry.focuslost) then
local text = DF:Trim(textEntry:GetText())
if (string.len(text) > 0) then
textEntry.MyObject.currenttext = text
if (textEntry.MyObject.func) then
textEntry.MyObject.func(textEntry.MyObject.param1, textEntry.MyObject.param2, text, textEntry, nil)
4 years ago
end
3 years ago
else
textEntry:SetText("")
textEntry.MyObject.currenttext = ""
end
4 years ago
else
3 years ago
textEntry.focuslost = false
4 years ago
end
3 years ago
textEntry.MyObject.label:SetTextColor(.8, .8, .8, 1)
4 years ago
end
end
3 years ago
local OnEditFocusGained = function(textentry)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnEditFocusGained", textentry, object)
4 years ago
if (kill) then
return
end
3 years ago
textentry.MyObject.label:SetTextColor(1, 1, 1, 1)
4 years ago
3 years ago
if (object.autoSelectAllText) then
object:SelectAll()
end
4 years ago
end
3 years ago
local OnChar = function(textentry, char)
local object = textentry.MyObject
local kill = object:RunHooksForWidget("OnChar", textentry, char, object)
4 years ago
if (kill) then
return
end
end
3 years ago
local OnTextChanged = function(textentry, byUser)
4 years ago
local capsule = textentry.MyObject
3 years ago
local kill = capsule:RunHooksForWidget("OnTextChanged", textentry, byUser, capsule)
4 years ago
if (kill) then
return
end
end
3 years ago
local OnTabPressed = function(textentry)
4 years ago
local capsule = textentry.MyObject
3 years ago
local kill = capsule:RunHooksForWidget("OnTabPressed", textentry, byUser, capsule)
4 years ago
if (kill) then
return
end
3 years ago
if (textentry.MyObject.next) then
OnEnterPressed(textentry, false)
4 years ago
textentry.MyObject.next:SetFocus()
end
end
3 years ago
function TextEntryMetaFunctions:PressEnter(byScript)
OnEnterPressed(self.editbox, byScript)
4 years ago
end
3 years ago
4 years ago
------------------------------------------------------------------------------------------------------------
3 years ago
function TextEntryMetaFunctions:SetTemplate(template)
4 years ago
if (template.width) then
3 years ago
self.editbox:SetWidth(template.width)
4 years ago
end
if (template.height) then
3 years ago
self.editbox:SetHeight(template.height)
4 years ago
end
3 years ago
4 years ago
if (template.backdrop) then
3 years ago
self.editbox:SetBackdrop(template.backdrop)
4 years ago
end
if (template.backdropcolor) then
3 years ago
local r, g, b, a = DF:ParseColors(template.backdropcolor)
self.editbox:SetBackdropColor(r, g, b, a)
4 years ago
self.onleave_backdrop = {r, g, b, a}
end
if (template.backdropbordercolor) then
3 years ago
local r, g, b, a = DF:ParseColors(template.backdropbordercolor)
self.editbox:SetBackdropBorderColor(r, g, b, a)
4 years ago
self.editbox.current_bordercolor[1] = r
self.editbox.current_bordercolor[2] = g
self.editbox.current_bordercolor[3] = b
self.editbox.current_bordercolor[4] = a
self.onleave_backdrop_border_color = {r, g, b, a}
end
end
------------------------------------------------------------------------------------------------------------
3 years ago
--object constructor
4 years ago
3 years ago
function DF:CreateTextEntry(parent, func, w, h, member, name, with_label, entry_template, label_template)
return DF:NewTextEntry(parent, parent, name, member, w, h, func, nil, nil, nil, with_label, entry_template, label_template)
4 years ago
end
3 years ago
function DF:NewTextEntry(parent, container, name, member, width, height, func, param1, param2, space, withLabel, entryTemplate, labelTemplate)
4 years ago
if (not name) then
name = "DetailsFrameworkTextEntryNumber" .. DF.TextEntryCounter
DF.TextEntryCounter = DF.TextEntryCounter + 1
3 years ago
4 years ago
elseif (not parent) then
3 years ago
return error("Details! FrameWork: parent not found.", 2)
4 years ago
end
3 years ago
4 years ago
if (not container) then
container = parent
end
3 years ago
if (name:find("$parent")) then
local parentName = DF.GetParentName(parent)
name = name:gsub("$parent", parentName)
4 years ago
end
3 years ago
local newTextEntryObject = {type = "textentry", dframework = true}
4 years ago
if (member) then
3 years ago
parent[member] = newTextEntryObject
4 years ago
end
if (parent.dframework) then
parent = parent.widget
end
3 years ago
4 years ago
if (container.dframework) then
container = container.widget
end
3 years ago
--misc
newTextEntryObject.container = container
if (not width and space) then
width = space
end
--editbox
newTextEntryObject.editbox = CreateFrame("EditBox", name, parent,"BackdropTemplate")
newTextEntryObject.editbox:SetSize(232, 20)
newTextEntryObject.editbox:SetBackdrop({bgFile = [["Interface\DialogFrame\UI-DialogBox-Background"]], tileSize = 64, tile = true, edgeFile = [[Interface\DialogFrame\UI-DialogBox-Border]], edgeSize = 10, insets = {left = 1, right = 1, top = 0, bottom = 0}})
newTextEntryObject.editbox:SetTextInsets(3, 0, 0, -3)
newTextEntryObject.editbox:SetWidth(width)
newTextEntryObject.editbox:SetHeight(height)
newTextEntryObject.editbox:SetJustifyH("center")
newTextEntryObject.editbox:EnableMouse(true)
newTextEntryObject.editbox:SetText("")
newTextEntryObject.editbox:SetAutoFocus(false)
newTextEntryObject.editbox:SetFontObject("GameFontHighlightSmall")
--editbox label
newTextEntryObject.editbox.label = newTextEntryObject.editbox:CreateFontString("$parent_Desc", "OVERLAY", "GameFontHighlightSmall")
newTextEntryObject.editbox.label:SetJustifyH("left")
newTextEntryObject.editbox.label:SetPoint("RIGHT", newTextEntryObject.editbox, "LEFT", -2, 0)
newTextEntryObject.label = newTextEntryObject.editbox.label
newTextEntryObject.widget = newTextEntryObject.editbox
newTextEntryObject.editbox.MyObject = newTextEntryObject
4 years ago
if (not APITextEntryFunctions) then
APITextEntryFunctions = true
3 years ago
local idx = getmetatable(newTextEntryObject.editbox).__index
for funcName, funcAddress in pairs(idx) do
if (not TextEntryMetaFunctions[funcName]) then
TextEntryMetaFunctions[funcName] = function(object, ...)
local x = loadstring( "return _G['"..object.editbox:GetName().."']:"..funcName.."(...)")
return x(...)
4 years ago
end
end
end
end
3 years ago
newTextEntryObject.editbox.current_bordercolor = {1, 1, 1, 0.7}
newTextEntryObject.enabled_border_color = {newTextEntryObject.editbox:GetBackdropBorderColor()}
newTextEntryObject.enabled_backdrop_color = {newTextEntryObject.editbox:GetBackdropColor()}
newTextEntryObject.enabled_text_color = {newTextEntryObject.editbox:GetTextColor()}
newTextEntryObject.onleave_backdrop = {newTextEntryObject.editbox:GetBackdropColor()}
newTextEntryObject.onleave_backdrop_border_color = {newTextEntryObject.editbox:GetBackdropBorderColor()}
newTextEntryObject.func = func
newTextEntryObject.param1 = param1
newTextEntryObject.param2 = param2
newTextEntryObject.next = nil
newTextEntryObject.space = space
newTextEntryObject.tab_on_enter = false
newTextEntryObject.editbox:SetBackdrop({bgFile = [[Interface\Tooltips\UI-Tooltip-Background]], tileSize = 64, edgeFile = [[Interface\Buttons\WHITE8X8]], edgeSize = 1, insets = {left = 1, right = 1, top = 1, bottom = 1}})
newTextEntryObject.editbox:SetBackdropColor(.2, .2, .2, 1)
newTextEntryObject.editbox:SetBackdropBorderColor(1, 1, 1, 0.7)
--hooks
newTextEntryObject.HookList = {
OnEnter = {},
OnLeave = {},
OnHide = {},
OnShow = {},
OnEnterPressed = {},
OnEscapePressed = {},
OnSpacePressed = {},
OnEditFocusLost = {},
OnEditFocusGained = {},
OnChar = {},
OnTextChanged = {},
OnTabPressed = {},
}
newTextEntryObject.editbox:SetScript("OnEnter", OnEnter)
newTextEntryObject.editbox:SetScript("OnLeave", OnLeave)
newTextEntryObject.editbox:SetScript("OnHide", OnHide)
newTextEntryObject.editbox:SetScript("OnShow", OnShow)
newTextEntryObject.editbox:SetScript("OnEnterPressed", OnEnterPressed)
newTextEntryObject.editbox:SetScript("OnEscapePressed", OnEscapePressed)
newTextEntryObject.editbox:SetScript("OnSpacePressed", OnSpacePressed)
newTextEntryObject.editbox:SetScript("OnEditFocusLost", OnEditFocusLost)
newTextEntryObject.editbox:SetScript("OnEditFocusGained", OnEditFocusGained)
newTextEntryObject.editbox:SetScript("OnChar", OnChar)
newTextEntryObject.editbox:SetScript("OnTextChanged", OnTextChanged)
newTextEntryObject.editbox:SetScript("OnTabPressed", OnTabPressed)
setmetatable(newTextEntryObject, TextEntryMetaFunctions)
if (withLabel) then
local label = DF:CreateLabel(newTextEntryObject.editbox, withLabel, nil, nil, nil, "label", nil, "overlay")
label.text = withLabel
newTextEntryObject.editbox:SetPoint("left", label.widget, "right", 2, 0)
if (labelTemplate) then
label:SetTemplate(labelTemplate)
4 years ago
end
3 years ago
withLabel = label
4 years ago
end
3 years ago
if (entryTemplate) then
newTextEntryObject:SetTemplate(entryTemplate)
end
return newTextEntryObject, withLabel
4 years ago
end
3 years ago
function DF:NewSpellEntry(parent, func, w, h, param1, param2, member, name)
local editbox = DF:NewTextEntry(parent, parent, name, member, w, h, func, param1, param2)
return editbox
4 years ago
end
3 years ago
local function_gettext = function(self)
4 years ago
return self.editbox:GetText()
end
3 years ago
local function_settext = function(self, text)
return self.editbox:SetText(text)
4 years ago
end
3 years ago
local function_clearfocus = function(self)
4 years ago
return self.editbox:ClearFocus()
end
3 years ago
local function_setfocus = function(self)
return self.editbox:SetFocus(true)
end
4 years ago
------------------------------------------------------------------------------------
--auto complete
-- block -------------------
--code author Saiket from http://www.wowinterface.com/forums/showpost.php?p=245759&postcount=6
--- @return StartPos, EndPos of highlight in this editbox.
local function GetTextHighlight ( self )
local Text, Cursor = self:GetText(), self:GetCursorPosition();
self:Insert( "" ); -- Delete selected text
local TextNew, CursorNew = self:GetText(), self:GetCursorPosition();
-- Restore previous text
self:SetText( Text );
self:SetCursorPosition( Cursor );
local Start, End = CursorNew, #Text - ( #TextNew - CursorNew );
self:HighlightText( Start, End );
return Start, End;
end
local StripColors;
do
local CursorPosition, CursorDelta;
--- Callback for gsub to remove unescaped codes.
local function StripCodeGsub ( Escapes, Code, End )
if ( #Escapes % 2 == 0 ) then -- Doesn't escape Code
if ( CursorPosition and CursorPosition >= End - 1 ) then
CursorDelta = CursorDelta - #Code;
end
return Escapes;
end
end
--- Removes a single escape sequence.
local function StripCode ( Pattern, Text, OldCursor )
CursorPosition, CursorDelta = OldCursor, 0;
return Text:gsub( Pattern, StripCodeGsub ), OldCursor and CursorPosition + CursorDelta;
end
--- Strips Text of all color escape sequences.
-- @param Cursor Optional cursor position to keep track of.
-- @return Stripped text, and the updated cursor position if Cursor was given.
function StripColors ( Text, Cursor )
Text, Cursor = StripCode( "(|*)(|c%x%x%x%x%x%x%x%x)()", Text, Cursor );
return StripCode( "(|*)(|r)()", Text, Cursor );
end
end
local COLOR_END = "|r";
--- Wraps this editbox's selected text with the given color.
local function ColorSelection ( self, ColorCode )
local Start, End = GetTextHighlight( self );
local Text, Cursor = self:GetText(), self:GetCursorPosition();
if ( Start == End ) then -- Nothing selected
--Start, End = Cursor, Cursor; -- Wrap around cursor
return; -- Wrapping the cursor in a color code and hitting backspace crashes the client!
end
-- Find active color code at the end of the selection
local ActiveColor;
if ( End < #Text ) then -- There is text to color after the selection
local ActiveEnd;
local CodeEnd, _, Escapes, Color = 0;
while ( true ) do
_, CodeEnd, Escapes, Color = Text:find( "(|*)(|c%x%x%x%x%x%x%x%x)", CodeEnd + 1 );
if ( not CodeEnd or CodeEnd > End ) then
break;
end
if ( #Escapes % 2 == 0 ) then -- Doesn't escape Code
ActiveColor, ActiveEnd = Color, CodeEnd;
end
end
if ( ActiveColor ) then
-- Check if color gets terminated before selection ends
CodeEnd = 0;
while ( true ) do
_, CodeEnd, Escapes = Text:find( "(|*)|r", CodeEnd + 1 );
if ( not CodeEnd or CodeEnd > End ) then
break;
end
if ( CodeEnd > ActiveEnd and #Escapes % 2 == 0 ) then -- Terminates ActiveColor
ActiveColor = nil;
break;
end
end
end
end
local Selection = Text:sub( Start + 1, End );
-- Remove color codes from the selection
local Replacement, CursorReplacement = StripColors( Selection, Cursor - Start );
self:SetText( ( "" ):join(
Text:sub( 1, Start ),
ColorCode, Replacement, COLOR_END,
ActiveColor or "", Text:sub( End + 1 )
) );
-- Restore cursor and highlight, adjusting for wrapper text
Cursor = Start + CursorReplacement;
if ( CursorReplacement > 0 ) then -- Cursor beyond start of color code
Cursor = Cursor + #ColorCode;
end
if ( CursorReplacement >= #Replacement ) then -- Cursor beyond end of color
Cursor = Cursor + #COLOR_END;
end
3 years ago
4 years ago
self:SetCursorPosition( Cursor );
-- Highlight selection and wrapper
self:HighlightText( Start, #ColorCode + ( #Replacement - #Selection ) + #COLOR_END + End );
end
-- end of the block ---------------------
3 years ago
local get_last_word = function(self)
4 years ago
self.lastword = ""
local cursor_pos = self.editbox:GetCursorPosition()
local text = self.editbox:GetText()
for i = cursor_pos, 1, -1 do
local character = text:sub (i, i)
if (character:match ("%a")) then
self.lastword = character .. self.lastword
3 years ago
--print(self.lastword)
4 years ago
else
break
end
end
end
--On Text Changed
3 years ago
local AutoComplete_OnTextChanged = function(editboxWidget, byUser, capsule)
4 years ago
capsule = capsule or editboxWidget.MyObject or editboxWidget
3 years ago
4 years ago
local chars_now = editboxWidget:GetText():len()
if (not editboxWidget.ignore_textchange) then
3 years ago
--backspace
4 years ago
if (chars_now == capsule.characters_count -1) then
capsule.lastword = capsule.lastword:sub (1, capsule.lastword:len()-1)
3 years ago
--delete lots of text
4 years ago
elseif (chars_now < capsule.characters_count) then
--o auto complete selecionou outra palavra bem menor e caiu nesse filtro
editboxWidget.end_selection = nil
capsule:GetLastWord()
end
else
editboxWidget.ignore_textchange = nil
end
capsule.characters_count = chars_now
end
3 years ago
local AutoComplete_OnSpacePressed = function(editboxWidget, capsule)
4 years ago
capsule = capsule or editboxWidget.MyObject or editboxWidget
-- if (not gotMatch) then
--editboxWidget.end_selection = nil
-- end
end
3 years ago
local AutoComplete_OnEscapePressed = function(editboxWidget)
4 years ago
editboxWidget.end_selection = nil
end
3 years ago
local AutoComplete_OnEnterPressed = function(editboxWidget)
4 years ago
local capsule = editboxWidget.MyObject or editboxWidget
if (editboxWidget.end_selection) then
editboxWidget:SetCursorPosition (editboxWidget.end_selection)
editboxWidget:HighlightText (0, 0)
editboxWidget.end_selection = nil
--editboxWidget:Insert (" ") --estava causando a adi��o de uma palavra a mais quando o pr�ximo catactere for um espa�o
else
if (editboxWidget:IsMultiLine()) then
editboxWidget:Insert ("\n")
--reseta a palavra se acabou de ganhar focus e apertou enter
if (editboxWidget.focusGained) then
capsule.lastword = ""
editboxWidget.focusGained = nil
end
else
editboxWidget:Insert ("")
editboxWidget.focuslost = true
editboxWidget:ClearFocus()
end
end
capsule.lastword = ""
end
3 years ago
local AutoComplete_OnEditFocusGained = function(editboxWidget)
4 years ago
local capsule = editboxWidget.MyObject or editboxWidget
capsule:GetLastWord()
3 years ago
--print("last word:", editboxWidget.lastword)
4 years ago
editboxWidget.end_selection = nil
editboxWidget.focusGained = true
3 years ago
capsule.characters_count = editboxWidget:GetText():len()
4 years ago
end
3 years ago
local OptimizeAutoCompleteTable = function(self, wordList)
4 years ago
local optimizedTable = {}
3 years ago
4 years ago
local lower = string.lower
local sub = string.sub
local len = string.len
3 years ago
4 years ago
local subTables = 0
3 years ago
4 years ago
for i = 1, #wordList do
local thisWord = wordList [i]
if (len (thisWord) > 0) then
thisWord = lower (thisWord)
3 years ago
4 years ago
local firstCharacter = sub (thisWord, 1, 1)
3 years ago
4 years ago
local charTable = optimizedTable [firstCharacter]
if (not charTable) then
charTable = {}
optimizedTable [firstCharacter] = charTable
3 years ago
4 years ago
subTables = subTables + 1
end
3 years ago
4 years ago
charTable [#charTable+1] = thisWord
end
end
3 years ago
4 years ago
wordList.Optimized = optimizedTable
end
3 years ago
local AutoComplete_OnChar = function(editboxWidget, char, capsule)
4 years ago
if (char == "") then
return
end
3 years ago
4 years ago
capsule = capsule or editboxWidget.MyObject or editboxWidget
editboxWidget.end_selection = nil
3 years ago
4 years ago
if (editboxWidget.ignore_input) then
return
end
3 years ago
4 years ago
--reseta a palavra se acabou de ganhar focus e apertou espa�o
if (editboxWidget.focusGained and char == " ") then
capsule.lastword = ""
editboxWidget.focusGained = nil
else
editboxWidget.focusGained = nil
end
3 years ago
4 years ago
if (char:match ("%a") or (char == " " and capsule.lastword ~= "")) then
capsule.lastword = capsule.lastword .. char
else
capsule.lastword = ""
end
3 years ago
4 years ago
editboxWidget.ignore_input = true
3 years ago
4 years ago
if (capsule.lastword:len() >= 2) then
3 years ago
4 years ago
local wordList = capsule [capsule.poolName]
if (not wordList) then
3 years ago
error("Details! Framework: TextEntry has AutoComplete but no word list table.")
4 years ago
return
end
3 years ago
4 years ago
if (capsule.ShouldOptimizeAutoComplete) then
if (not wordList.Optimized) then
OptimizeAutoCompleteTable (capsule, wordList)
end
3 years ago
local firstCharacter = string.lower(string.sub (capsule.lastword, 1, 1))
4 years ago
wordList = wordList.Optimized [firstCharacter]
3 years ago
4 years ago
if (wordList) then
for i = 1, #wordList do
local thisWord = wordList [i]
3 years ago
if (thisWord and (thisWord:find("^" .. capsule.lastword) or thisWord:lower():find("^" .. capsule.lastword))) then
local rest = thisWord:gsub(capsule.lastword, "")
rest = rest:lower():gsub(capsule.lastword, "")
4 years ago
local cursor_pos = editboxWidget:GetCursorPosition()
editboxWidget:Insert (rest)
editboxWidget:HighlightText (cursor_pos, cursor_pos + rest:len())
editboxWidget:SetCursorPosition (cursor_pos)
editboxWidget.end_selection = cursor_pos + rest:len()
editboxWidget.ignore_textchange = true
break
end
end
end
3 years ago
4 years ago
editboxWidget.ignore_input = false
return
end
3 years ago
4 years ago
for i = 1, #wordList do
local thisWord = wordList [i]
3 years ago
if (thisWord and (thisWord:find("^" .. capsule.lastword) or thisWord:lower():find("^" .. capsule.lastword))) then
local rest = thisWord:gsub(capsule.lastword, "")
rest = rest:lower():gsub(capsule.lastword, "")
4 years ago
local cursor_pos = editboxWidget:GetCursorPosition()
editboxWidget:Insert (rest)
editboxWidget:HighlightText (cursor_pos, cursor_pos + rest:len())
editboxWidget:SetCursorPosition (cursor_pos)
editboxWidget.end_selection = cursor_pos + rest:len()
editboxWidget.ignore_textchange = true
break
end
end
end
3 years ago
4 years ago
editboxWidget.ignore_input = false
end
function TextEntryMetaFunctions:SetAsAutoComplete (poolName, poolTable, shouldOptimize)
3 years ago
4 years ago
if (not self.SetHook) then
--self is borderframe
self = self.editbox
self.editbox = self --compatible with fw functions
3 years ago
4 years ago
self.lastword = ""
self.characters_count = 0
self.poolName = poolName
self.GetLastWord = get_last_word --editbox:GetLastWord()
self.NoClearFocusOnEnterPressed = true --avoid auto clear focus
self.ShouldOptimizeAutoComplete = shouldOptimize
3 years ago
4 years ago
if (poolTable) then
self [poolName] = poolTable
end
3 years ago
4 years ago
self:HookScript ("OnEditFocusGained", AutoComplete_OnEditFocusGained)
self:HookScript ("OnEnterPressed", AutoComplete_OnEnterPressed)
self:HookScript ("OnEscapePressed", AutoComplete_OnEscapePressed)
self:HookScript ("OnTextChanged", AutoComplete_OnTextChanged)
self:HookScript ("OnChar", AutoComplete_OnChar)
self:HookScript ("OnSpacePressed", AutoComplete_OnSpacePressed)
else
--fw textfield
self.lastword = ""
self.characters_count = 0
self.poolName = poolName
self.GetLastWord = get_last_word --editbox:GetLastWord()
self.NoClearFocusOnEnterPressed = true --avoid auto clear focus
self.ShouldOptimizeAutoComplete = shouldOptimize
3 years ago
self:SetHook("OnEditFocusGained", AutoComplete_OnEditFocusGained)
self:SetHook("OnEnterPressed", AutoComplete_OnEnterPressed)
4 years ago
self.editbox:HookScript ("OnEscapePressed", AutoComplete_OnEscapePressed)
3 years ago
self.editbox:SetScript("OnTextChanged", AutoComplete_OnTextChanged)
self.editbox:SetScript("OnChar", AutoComplete_OnChar)
self.editbox:SetScript("OnSpacePressed", AutoComplete_OnSpacePressed)
4 years ago
end
end
local set_speciallua_editor_font_size = function(borderFrame, newSize)
local file, size, flags = borderFrame.editbox:GetFont()
borderFrame.editbox:SetFont (file, newSize, flags)
borderFrame.editboxlines:SetFont (file, newSize, flags)
end
function DF:NewSpecialLuaEditorEntry (parent, w, h, member, name, nointent, showLineNumbers)
3 years ago
if (name:find("$parent")) then
local parentName = DF.GetParentName(parent)
name = name:gsub("$parent", parentName)
4 years ago
end
3 years ago
local borderframe = CreateFrame("Frame", name, parent,"BackdropTemplate")
borderframe:SetSize(w, h)
4 years ago
if (member) then
parent [member] = borderframe
end
3 years ago
local scrollframe = CreateFrame("ScrollFrame", name, borderframe, "UIPanelScrollFrameTemplate, BackdropTemplate")
local scrollframeNumberLines = CreateFrame("ScrollFrame", name .. "NumberLines", borderframe, "UIPanelScrollFrameTemplate, BackdropTemplate")
scrollframe.editbox = CreateFrame("editbox", "$parentEditBox", scrollframe,"BackdropTemplate")
4 years ago
scrollframe.editbox:SetMultiLine (true)
3 years ago
scrollframe.editbox:SetAutoFocus(false)
scrollframe.editbox:SetScript("OnCursorChanged", _G.ScrollingEdit_OnCursorChanged)
scrollframe.editbox:SetScript("OnEscapePressed", _G.EditBox_ClearFocus)
scrollframe.editbox:SetFontObject("GameFontHighlightSmall")
4 years ago
scrollframe:SetScrollChild (scrollframe.editbox)
--line number
if (showLineNumbers) then
3 years ago
scrollframeNumberLines.editbox = CreateFrame("editbox", "$parentLineNumbers", scrollframeNumberLines, "BackdropTemplate")
4 years ago
scrollframeNumberLines.editbox:SetMultiLine (true)
3 years ago
scrollframeNumberLines.editbox:SetAutoFocus(false)
4 years ago
scrollframeNumberLines.editbox:SetEnabled (false)
3 years ago
scrollframeNumberLines.editbox:SetFontObject("GameFontHighlightSmall")
scrollframeNumberLines.editbox:SetJustifyH("left")
4 years ago
scrollframeNumberLines.editbox:SetJustifyV ("top")
scrollframeNumberLines.editbox:SetTextColor(.3, .3, .3, .5)
3 years ago
scrollframeNumberLines.editbox:SetPoint("topleft", borderframe, "topleft", 10, -10)
scrollframeNumberLines.editbox:SetPoint("bottomright", borderframe, "bottomright", -30, 10)
4 years ago
scrollframeNumberLines:SetScrollChild (scrollframeNumberLines.editbox)
3 years ago
scrollframeNumberLines:EnableMouseWheel(false)
4 years ago
for i = 1, 1000 do
scrollframeNumberLines.editbox:Insert (i .. "\n")
end
--place the lua code field 20 pixels to the right to make run to the lines scroll
3 years ago
scrollframe:SetPoint("topleft", borderframe, "topleft", 30, -10)
scrollframe:SetPoint("bottomright", borderframe, "bottomright", -10, 10)
4 years ago
--when the lua code field scrolls, make the lua field scroll too
3 years ago
scrollframe:SetScript("OnVerticalScroll", function(self, offset)
4 years ago
scrollframeNumberLines:SetVerticalScroll(scrollframe:GetVerticalScroll())
scrollframeNumberLines.ScrollBar:Hide()
end)
--place the number lines scroll in the begining of the editing code space
3 years ago
scrollframeNumberLines:SetPoint("topleft", borderframe, "topleft", 10, -10)
scrollframeNumberLines:SetPoint("bottomright", borderframe, "bottomright", -10, 10)
4 years ago
3 years ago
scrollframeNumberLines.editbox:SetJustifyH("left")
4 years ago
scrollframeNumberLines.editbox:SetJustifyV ("top")
3 years ago
scrollframeNumberLines:SetScript("OnSizeChanged", function(self)
scrollframeNumberLines.editbox:SetSize(self:GetSize())
4 years ago
scrollframeNumberLines.ScrollBar:Hide()
end)
scrollframeNumberLines.ScrollBar:HookScript("OnShow", function(self)
self:Hide()
end)
borderframe.scrollnumberlines = scrollframeNumberLines
borderframe.editboxlines = scrollframeNumberLines.editbox
borderframe.editboxlines.borderframe = borderframe
scrollframeNumberLines.ScrollBar:Hide()
scrollframeNumberLines:SetBackdrop(nil)
scrollframeNumberLines.editbox:SetBackdrop(nil)
else
3 years ago
scrollframe:SetPoint("topleft", borderframe, "topleft", 10, -10)
scrollframe:SetPoint("bottomright", borderframe, "bottomright", -10, 10)
scrollframeNumberLines:SetPoint("topleft", borderframe, "topleft", 10, -10)
scrollframeNumberLines:SetPoint("bottomright", borderframe, "bottomright", -10, 10)
4 years ago
scrollframeNumberLines:Hide()
end
borderframe.SetAsAutoComplete = TextEntryMetaFunctions.SetAsAutoComplete
3 years ago
scrollframe:SetScript("OnSizeChanged", function(self)
scrollframe.editbox:SetSize(self:GetSize())
4 years ago
end)
3 years ago
scrollframe.editbox:SetJustifyH("left")
4 years ago
scrollframe.editbox:SetJustifyV ("top")
scrollframe.editbox:SetMaxBytes (1024000)
scrollframe.editbox:SetMaxLetters (128000)
3 years ago
4 years ago
borderframe.GetText = function_gettext
borderframe.SetText = function_settext
borderframe.ClearFocus = function_clearfocus
borderframe.SetFocus = function_setfocus
borderframe.SetTextSize = set_speciallua_editor_font_size
3 years ago
4 years ago
borderframe.Enable = TextEntryMetaFunctions.Enable
borderframe.Disable = TextEntryMetaFunctions.Disable
3 years ago
4 years ago
borderframe.SetTemplate = TextEntryMetaFunctions.SetTemplate
3 years ago
4 years ago
if (not nointent) then
IndentationLib.enable (scrollframe.editbox, nil, 4)
end
3 years ago
borderframe:SetBackdrop({bgFile = [[Interface\Tooltips\UI-Tooltip-Background]], edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]],
4 years ago
tile = 1, tileSize = 16, edgeSize = 16, insets = {left = 5, right = 5, top = 5, bottom = 5}})
3 years ago
4 years ago
scrollframe.editbox.current_bordercolor = {1, 1, 1, 0.7}
3 years ago
borderframe:SetBackdropBorderColor(1, 1, 1, 0.7)
borderframe:SetBackdropColor(0.090195, 0.090195, 0.188234, 1)
4 years ago
borderframe.enabled_border_color = {borderframe:GetBackdropBorderColor()}
borderframe.enabled_backdrop_color = {borderframe:GetBackdropColor()}
borderframe.enabled_text_color = {scrollframe.editbox:GetTextColor()}
borderframe.onleave_backdrop = {scrollframe.editbox:GetBackdropColor()}
borderframe.onleave_backdrop_border_color = {scrollframe.editbox:GetBackdropBorderColor()}
3 years ago
4 years ago
borderframe.scroll = scrollframe
borderframe.editbox = scrollframe.editbox
borderframe.editbox.borderframe = borderframe
3 years ago
4 years ago
return borderframe
end
-- encryption table
local base64chars = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
-- decryption table
local base64bytes = {['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63,['=']=nil}
-- shift left
local function lsh (value,shift)
return (value*(2^shift)) % 256
end
-- shift right
local function rsh (value,shift)
return math.floor(value/2^shift) % 256
end
-- return single bit (for OR)
local function bit (x,b)
return (x % 2^b - x % 2^(b-1) > 0)
end
local function lor (x,y)
local result = 0
for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end
return result
end
function DF.EncodeString (data)
local bytes = {}
local result = ""
for spos=0,string.len(data)-1,3 do
for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
result = string.format('%s%s%s%s%s',result,base64chars[rsh(bytes[1],2)],base64chars[lor(lsh((bytes[1] % 4),4), rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[lor(lsh(bytes[2] % 16,2), rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
end
return result
end
function DF.DecodeString (data)
local chars = {}
local result=""
for dpos=0,string.len(data)-1,4 do
for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
result = string.format('%s%s%s%s',result,string.char(lor(lsh(chars[1],2), rsh(chars[2],4))),(chars[3] ~= nil) and string.char(lor(lsh(chars[2],4), rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(lor(lsh(chars[3],6) % 192, (chars[4]))) or "")
end
return result
end