comparison NameDialog.lua @ 11:d2cbfe498c4d

first beta
author Jay Bird <a4blank@yahoo.com>
date Sat, 04 Dec 2010 05:53:52 +0300
parents
children
comparison
equal deleted inserted replaced
10:f93b554bb7cf 11:d2cbfe498c4d
1
2 NameDialog = class()
3 local L = LibStub("AceLocale-3.0"):GetLocale("RaidTargetTactics")
4
5 function NameDialog:init(names)
6 self.invalidNames = names
7 self.callback = function() end
8 end
9
10 function NameDialog:InitDialog()
11 local this = self
12
13 self.nameBox = CreateFrame("Frame", nil, UIParent)
14 self.nameBox:SetWidth(240)
15 self.nameBox:SetHeight(50)
16 local backdrop = {
17 -- path to the background texture
18 bgFile = "Interface\\FrameGeneral\\UI-Background-Marble",
19 -- path to the border texture
20 edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
21 -- true to repeat the background texture to fill the frame, false to scale it
22 tile = true,
23 -- size (width or height) of the square repeating background tiles (in pixels)
24 tileSize = 256,
25 -- thickness of edge segments and square size of edge corners (in pixels)
26 edgeSize = 32,
27 -- distance from the edges of the frame to those of the background texture (in pixels)
28 insets = {
29 left = 11,
30 right = 12,
31 top = 12,
32 bottom = 11
33 }
34 }
35 self.nameBox:SetBackdrop(backdrop)
36 self.nameBox:SetFrameStrata("DIALOG")
37 self.nameBox:SetPoint("CENTER")
38
39 self.nameLabel = self.nameBox:CreateFontString()
40 self.nameLabel:SetFontObject(GameFontHighlight)
41 self.nameLabel:SetPoint("LEFT", 20, 0)
42 self.nameLabel:SetText(L.Name .. ":")
43 self.nameLabel:SetTextColor(YELLOW_FONT_COLOR.r, YELLOW_FONT_COLOR.g, YELLOW_FONT_COLOR.b)
44 self.nameLabel:Show()
45
46 self.nameField = CreateFrame("EditBox", "__RTT_ASKNAME", self.nameBox, "InputBoxTemplate")
47 self.nameField:SetAutoFocus(false)
48 self.nameField:SetFontObject(ChatFontNormal)
49 self.nameField:SetWidth(100)
50 self.nameField:SetHeight(20)
51 self.nameField:SetPoint("LEFT", 60, 0)
52 self.nameField:SetTextInsets(0, 0, 3, 3)
53 self.nameField:SetScript("OnEscapePressed", function() this:Hide() end)
54 self.nameField:SetScript("OnTextChanged", function() this:OnChange() end)
55 self.nameField:SetScript("OnEnterPressed", function() this:OnEnter() end)
56
57 -- add button
58 self.okButton = CreateFrame("Button", nil, self.nameBox, "OptionsButtonTemplate")
59 self.okButton:SetText("OK")
60 self.okButton:Disable()
61 self.okButton:SetWidth(50)
62 self.okButton:SetPoint("left", 170, 0)
63 self.okButton:Show()
64 self.okButton:SetScript("OnClick", function() this:OnEnter() end)
65
66 self.nameBox:Hide()
67 end
68
69 function NameDialog:SetInvalidNames(names)
70 self.invalidNames = names
71 end
72
73 function NameDialog:Show()
74 self.nameBox:Show()
75 self.nameField:SetText("")
76 self.nameField:SetFocus()
77 end
78
79 function NameDialog:Hide()
80 self.nameBox:Hide()
81 end
82
83 function NameDialog:GetName()
84 return strtrim(self.nameField:GetText())
85 end
86
87 function NameDialog:IsValidName()
88 local name = self:GetName()
89 if #name > 0 then
90 for _, taken in ipairs(self.invalidNames) do
91 if name == taken then
92 return false
93 end
94 end
95 return true
96 end
97 return false
98 end
99
100 function NameDialog:OnChange()
101 if self:IsValidName() then
102 self.okButton:Enable()
103 else
104 self.okButton:Disable()
105 end
106 end
107
108 function NameDialog:OnEnter()
109 if not self:IsValidName() then
110 return
111 end
112 local name = self:GetName()
113 self:Hide()
114 self.callback(name)
115 end
116
117 function NameDialog:OnNameChosen(func)
118 self.callback = func
119 end
120