diff NameDialog.lua @ 11:d2cbfe498c4d

first beta
author Jay Bird <a4blank@yahoo.com>
date Sat, 04 Dec 2010 05:53:52 +0300
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NameDialog.lua	Sat Dec 04 05:53:52 2010 +0300
@@ -0,0 +1,120 @@
+
+NameDialog = class()
+local L = LibStub("AceLocale-3.0"):GetLocale("RaidTargetTactics")
+
+function NameDialog:init(names)
+    self.invalidNames = names
+    self.callback = function() end
+end
+
+function NameDialog:InitDialog()
+    local this = self
+    
+    self.nameBox = CreateFrame("Frame", nil, UIParent)
+    self.nameBox:SetWidth(240)
+    self.nameBox:SetHeight(50)
+    local backdrop = {
+      -- path to the background texture
+      bgFile = "Interface\\FrameGeneral\\UI-Background-Marble",
+      -- path to the border texture
+      edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
+      -- true to repeat the background texture to fill the frame, false to scale it
+      tile = true,
+      -- size (width or height) of the square repeating background tiles (in pixels)
+      tileSize = 256,
+      -- thickness of edge segments and square size of edge corners (in pixels)
+      edgeSize = 32,
+      -- distance from the edges of the frame to those of the background texture (in pixels)
+      insets = {
+        left = 11,
+        right = 12,
+        top = 12,
+        bottom = 11
+      }
+    }
+    self.nameBox:SetBackdrop(backdrop)
+    self.nameBox:SetFrameStrata("DIALOG")
+    self.nameBox:SetPoint("CENTER")
+
+    self.nameLabel = self.nameBox:CreateFontString()
+    self.nameLabel:SetFontObject(GameFontHighlight)
+    self.nameLabel:SetPoint("LEFT", 20, 0)
+    self.nameLabel:SetText(L.Name .. ":")
+    self.nameLabel:SetTextColor(YELLOW_FONT_COLOR.r, YELLOW_FONT_COLOR.g, YELLOW_FONT_COLOR.b)
+    self.nameLabel:Show()
+
+    self.nameField = CreateFrame("EditBox", "__RTT_ASKNAME", self.nameBox, "InputBoxTemplate")
+    self.nameField:SetAutoFocus(false)
+    self.nameField:SetFontObject(ChatFontNormal)
+    self.nameField:SetWidth(100)
+    self.nameField:SetHeight(20)
+    self.nameField:SetPoint("LEFT", 60, 0)
+    self.nameField:SetTextInsets(0, 0, 3, 3)
+    self.nameField:SetScript("OnEscapePressed", function() this:Hide() end)
+    self.nameField:SetScript("OnTextChanged", function() this:OnChange() end)
+    self.nameField:SetScript("OnEnterPressed", function() this:OnEnter() end)
+
+    -- add button
+    self.okButton = CreateFrame("Button", nil, self.nameBox, "OptionsButtonTemplate")
+    self.okButton:SetText("OK")
+    self.okButton:Disable()
+    self.okButton:SetWidth(50)
+    self.okButton:SetPoint("left", 170, 0)
+    self.okButton:Show()
+    self.okButton:SetScript("OnClick", function() this:OnEnter() end)
+
+    self.nameBox:Hide()
+end
+
+function NameDialog:SetInvalidNames(names)
+    self.invalidNames = names
+end
+
+function NameDialog:Show()
+    self.nameBox:Show()
+    self.nameField:SetText("")
+    self.nameField:SetFocus()
+end
+
+function NameDialog:Hide()
+    self.nameBox:Hide()
+end
+
+function NameDialog:GetName()
+    return strtrim(self.nameField:GetText())
+end
+
+function NameDialog:IsValidName()
+    local name = self:GetName()
+    if #name > 0 then
+        for _, taken in ipairs(self.invalidNames) do
+            if name == taken then
+                return false
+            end
+        end
+        return true
+    end
+    return false
+end
+
+function NameDialog:OnChange()
+    if self:IsValidName() then
+        self.okButton:Enable()
+    else
+        self.okButton:Disable()
+    end
+end
+
+function NameDialog:OnEnter()
+    if not self:IsValidName() then
+        return
+    end
+    local name = self:GetName()
+    self:Hide()
+    self.callback(name)
+end
+
+function NameDialog:OnNameChosen(func)
+    self.callback = func
+end
+