/trunk/TipTop/libs/AceGUI-3.0/widgets
local AceGUI = LibStub("AceGUI-3.0") |
------------- |
-- Widgets -- |
------------- |
--[[ |
Widgets must provide the following functions |
Acquire() - Called when the object is aquired, should set everything to a default hidden state |
Release() - Called when the object is Released, should remove any anchors and hide the Widget |
And the following members |
frame - the frame or derivitive object that will be treated as the widget for size and anchoring purposes |
type - the type of the object, same as the name given to :RegisterWidget() |
Widgets contain a table called userdata, this is a safe place to store data associated with the wigdet |
It will be cleared automatically when a widget is released |
Placing values directly into a widget object should be avoided |
If the Widget can act as a container for other Widgets the following |
content - frame or derivitive that children will be anchored to |
The Widget can supply the following Optional Members |
]] |
-------------------------- |
-- Scroll Frame -- |
-------------------------- |
do |
local Type = "ScrollFrame" |
local Version = 3 |
local function OnAcquire(self) |
end |
local function OnRelease(self) |
self.frame:ClearAllPoints() |
self.frame:Hide() |
self.status = nil |
for k in pairs(self.localstatus) do |
self.localstatus[k] = nil |
end |
end |
local function SetScroll(self, value) |
local status = self.status or self.localstatus |
local frame, child = self.scrollframe, self.content |
local viewheight = frame:GetHeight() |
local height = child:GetHeight() |
local offset |
if viewheight > height then |
offset = 0 |
else |
offset = floor((height - viewheight) / 1000.0 * value) |
end |
child:ClearAllPoints() |
child:SetPoint("TOPLEFT",frame,"TOPLEFT",0,offset) |
child:SetPoint("TOPRIGHT",frame,"TOPRIGHT",0,offset) |
status.offset = offset |
status.scrollvalue = value |
end |
local function MoveScroll(self, value) |
local status = self.status or self.localstatus |
local frame, child = self.scrollframe, self.content |
local height, viewheight = frame:GetHeight(), child:GetHeight() |
if height > viewheight then |
self.scrollbar:Hide() |
else |
self.scrollbar:Show() |
local diff = height - viewheight |
local delta = 1 |
if value < 0 then |
delta = -1 |
end |
self.scrollbar:SetValue(math.min(math.max(status.scrollvalue + delta*(1000/(diff/45)),0), 1000)) |
end |
end |
local function FixScroll(self) |
local status = self.status or self.localstatus |
local frame, child = self.scrollframe, self.content |
local height, viewheight = frame:GetHeight(), child:GetHeight() |
local offset = status.offset |
if not offset then |
offset = 0 |
end |
local curvalue = self.scrollbar:GetValue() |
if viewheight < height then |
self.scrollbar:Hide() |
self.scrollbar:SetValue(0) |
--self.scrollframe:SetPoint("BOTTOMRIGHT",self.frame,"BOTTOMRIGHT",0,0) |
else |
self.scrollbar:Show() |
--self.scrollframe:SetPoint("BOTTOMRIGHT",self.frame,"BOTTOMRIGHT",-16,0) |
local value = (offset / (viewheight - height) * 1000) |
if value > 1000 then value = 1000 end |
self.scrollbar:SetValue(value) |
self:SetScroll(value) |
if value < 1000 then |
child:ClearAllPoints() |
child:SetPoint("TOPLEFT",frame,"TOPLEFT",0,offset) |
child:SetPoint("TOPRIGHT",frame,"TOPRIGHT",0,offset) |
status.offset = offset |
end |
end |
end |
local function OnMouseWheel(this,value) |
this.obj:MoveScroll(value) |
end |
local function OnScrollValueChanged(this, value) |
this.obj:SetScroll(value) |
end |
local function FixScrollOnUpdate(this) |
this:SetScript("OnUpdate", nil) |
this.obj:FixScroll() |
end |
local function OnSizeChanged(this) |
--this:SetScript("OnUpdate", FixScrollOnUpdate) |
this.obj:FixScroll() |
end |
local function LayoutFinished(self,width,height) |
self.content:SetHeight(height or 0 + 20) |
self:FixScroll() |
end |
-- called to set an external table to store status in |
local function SetStatusTable(self, status) |
assert(type(status) == "table") |
self.status = status |
if not status.scrollvalue then |
status.scrollvalue = 0 |
end |
end |
local createdcount = 0 |
local function OnWidthSet(self, width) |
local content = self.content |
content.width = width |
end |
local function OnHeightSet(self, height) |
local content = self.content |
content.height = height |
end |
local function Constructor() |
local frame = CreateFrame("Frame",nil,UIParent) |
local self = {} |
self.type = Type |
self.OnRelease = OnRelease |
self.OnAcquire = OnAcquire |
self.MoveScroll = MoveScroll |
self.FixScroll = FixScroll |
self.SetScroll = SetScroll |
self.LayoutFinished = LayoutFinished |
self.SetStatusTable = SetStatusTable |
self.OnWidthSet = OnWidthSet |
self.OnHeightSet = OnHeightSet |
self.localstatus = {} |
self.frame = frame |
frame.obj = self |
--Container Support |
local scrollframe = CreateFrame("ScrollFrame",nil,frame) |
local content = CreateFrame("Frame",nil,scrollframe) |
createdcount = createdcount + 1 |
local scrollbar = CreateFrame("Slider",("AceConfigDialogScrollFrame%dScrollBar"):format(createdcount),scrollframe,"UIPanelScrollBarTemplate") |
local scrollbg = scrollbar:CreateTexture(nil,"BACKGROUND") |
scrollbg:SetAllPoints(scrollbar) |
scrollbg:SetTexture(0,0,0,0.4) |
self.scrollframe = scrollframe |
self.content = content |
self.scrollbar = scrollbar |
scrollbar.obj = self |
scrollframe.obj = self |
content.obj = self |
scrollframe:SetScrollChild(content) |
scrollframe:SetPoint("TOPLEFT",frame,"TOPLEFT",0,0) |
scrollframe:SetPoint("BOTTOMRIGHT",self.frame,"BOTTOMRIGHT",-16,0) |
scrollframe:EnableMouseWheel(true) |
scrollframe:SetScript("OnMouseWheel", OnMouseWheel) |
scrollframe:SetScript("OnSizeChanged", OnSizeChanged) |
content:SetPoint("TOPLEFT",scrollframe,"TOPLEFT",0,0) |
content:SetPoint("TOPRIGHT",scrollframe,"TOPRIGHT",0,0) |
content:SetHeight(400) |
scrollbar:SetPoint("TOPLEFT",scrollframe,"TOPRIGHT",0,-16) |
scrollbar:SetPoint("BOTTOMLEFT",scrollframe,"BOTTOMRIGHT",0,16) |
scrollbar:SetScript("OnValueChanged", OnScrollValueChanged) |
scrollbar:SetMinMaxValues(0,1000) |
scrollbar:SetValueStep(1) |
scrollbar:SetValue(0) |
scrollbar:SetWidth(16) |
self.localstatus.scrollvalue = 0 |
self:FixScroll() |
AceGUI:RegisterAsContainer(self) |
--AceGUI:RegisterAsWidget(self) |
return self |
end |
AceGUI:RegisterWidgetType(Type,Constructor,Version) |
end |
do |
local Type = "TreeGroup" |
local Version = 16 |
local Version = 17 |
local DEFAULT_TREE_WIDTH = 175 |
local DEFAULT_TREE_SIZABLE = true |
}, |
} |
]] |
local function SetTree(self, tree) |
local function SetTree(self, tree, filter) |
self.filter = filter |
if tree then |
assert(type(tree) == "table") |
end |
self:RefreshTree() |
end |
local function ShouldDisplayLevel(tree) |
local result = false |
for k, v in ipairs(tree) do |
if v.children == nil and v.visible ~= false then |
result = true |
elseif v.children then |
result = result or ShouldDisplayLevel(v.children) |
end |
if result then return result end |
end |
return false |
end |
local function addLine(self, v, tree, level, parent) |
local line = new() |
line.value = v.value |
line.text = v.text |
line.disabled = v.disabled |
line.tree = tree |
line.level = level |
line.parent = parent |
line.visible = v.visible |
line.uniquevalue = GetButtonUniqueValue(line) |
if v.children then |
line.hasChildren = true |
else |
line.hasChildren = nil |
end |
self.lines[#self.lines+1] = line |
return line |
end |
local function BuildLevel(self, tree, level, parent) |
local lines = self.lines |
local status = (self.status or self.localstatus) |
local groups = status.groups |
local groups = (self.status or self.localstatus).groups |
local hasChildren = self.hasChildren |
for i, v in ipairs(tree) do |
local line = new() |
lines[#lines+1] = line |
line.value = v.value |
line.text = v.text |
line.disabled = v.disabled |
line.tree = tree |
line.level = level |
line.parent = parent |
line.uniquevalue = GetButtonUniqueValue(line) |
if v.children then |
line.hasChildren = true |
else |
line.hasChildren = nil |
end |
if v.children then |
if groups[line.uniquevalue] then |
self:BuildLevel(v.children, level+1, line) |
if not self.filter or ShouldDisplayLevel(v.children) then |
local line = addLine(self, v, tree, level, parent) |
if groups[line.uniquevalue] then |
self:BuildLevel(v.children, level+1, line) |
end |
end |
elseif v.visible ~= false or not self.filter then |
addLine(self, v, tree, level, parent) |
end |
end |
end |
end |
local function RefreshTree(self) |
local buttons = self.buttons |
local buttons = self.buttons |
local lines = self.lines |
for i, v in ipairs(buttons) do |
end |
local function Select(self, uniquevalue, ...) |
self.filter = false |
local status = self.status or self.localstatus |
local groups = status.groups |
for i = 1, select('#', ...) do |
self.hasChildren = {} |
self.localstatus = {} |
self.localstatus.groups = {} |
self.filter = false |
local treeframe = CreateFrame("Frame",nil,frame) |
treeframe.obj = self |
self.OnWidthSet = OnWidthSet |
self.OnHeightSet = OnHeightSet |
self.EnableButtonTooltips = EnableButtonTooltips |
self.Filter = Filter |
self.frame = frame |
frame.obj = self |
]] |
do |
local Type = "EditBox" |
local Version = 8 |
local Version = 9 |
local function OnAcquire(self) |
self:SetDisabled(false) |
self.label:Show() |
self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,-18) |
self.frame:SetHeight(44) |
self.alignoffset = 30 |
else |
self.label:SetText("") |
self.label:Hide() |
self.editbox:SetPoint("TOPLEFT",self.frame,"TOPLEFT",7,0) |
self.frame:SetHeight(26) |
self.alignoffset = 12 |
end |
end |
local AceGUI = LibStub("AceGUI-3.0") |
-------------------------- |
-- Label -- |
-------------------------- |
do |
local Type = "Label" |
local Version = 8 |
local function OnAcquire(self) |
self:SetText("") |
self:SetImage(nil) |
self:SetColor() |
end |
local function OnRelease(self) |
self.frame:ClearAllPoints() |
self.frame:Hide() |
end |
local function UpdateImageAnchor(self) |
local width = self.frame.width or self.frame:GetWidth() or 0 |
local image = self.image |
local label = self.label |
local frame = self.frame |
local height |
label:ClearAllPoints() |
image:ClearAllPoints() |
if self.imageshown then |
local imagewidth = image:GetWidth() |
if (width - imagewidth) < 200 or (label:GetText() or "") == "" then |
--image goes on top centered when less than 200 width for the text, or if there is no text |
image:SetPoint("TOP",frame,"TOP",0,0) |
label:SetPoint("TOP",image,"BOTTOM",0,0) |
label:SetPoint("LEFT",frame,"LEFT",0,0) |
label:SetWidth(width) |
height = image:GetHeight() + label:GetHeight() |
else |
--image on the left |
image:SetPoint("TOPLEFT",frame,"TOPLEFT",0,0) |
label:SetPoint("TOPLEFT",image,"TOPRIGHT",0,0) |
label:SetWidth(width - imagewidth) |
height = math.max(image:GetHeight(), label:GetHeight()) |
end |
else |
--no image shown |
label:SetPoint("TOPLEFT",frame,"TOPLEFT",0,0) |
label:SetWidth(width) |
height = self.label:GetHeight() |
end |
self.resizing = true |
self.frame:SetHeight(height) |
self.frame.height = height |
self.resizing = nil |
end |
local function SetText(self, text) |
self.label:SetText(text or "") |
UpdateImageAnchor(self) |
end |
local function SetColor(self, r, g, b) |
if not (r and g and b) then |
r, g, b = 1, 1, 1 |
end |
self.label:SetVertexColor(r, g, b) |
end |
local function OnWidthSet(self, width) |
if self.resizing then return end |
UpdateImageAnchor(self) |
end |
local function SetImage(self, path, ...) |
local image = self.image |
image:SetTexture(path) |
if image:GetTexture() then |
self.imageshown = true |
local n = select('#', ...) |
if n == 4 or n == 8 then |
image:SetTexCoord(...) |
end |
else |
self.imageshown = nil |
end |
UpdateImageAnchor(self) |
end |
local function SetImageSize(self, width, height) |
self.image:SetWidth(width) |
self.image:SetHeight(height) |
UpdateImageAnchor(self) |
end |
local function Constructor() |
local frame = CreateFrame("Frame",nil,UIParent) |
local self = {} |
self.type = Type |
self.OnRelease = OnRelease |
self.OnAcquire = OnAcquire |
self.SetText = SetText |
self.SetColor = SetColor |
self.frame = frame |
self.OnWidthSet = OnWidthSet |
self.SetImage = SetImage |
self.SetImageSize = SetImageSize |
frame.obj = self |
frame:SetHeight(18) |
frame:SetWidth(200) |
local label = frame:CreateFontString(nil,"BACKGROUND","GameFontHighlightSmall") |
label:SetPoint("TOPLEFT",frame,"TOPLEFT",0,0) |
label:SetWidth(200) |
label:SetJustifyH("LEFT") |
label:SetJustifyV("TOP") |
self.label = label |
local image = frame:CreateTexture(nil,"BACKGROUND") |
self.image = image |
AceGUI:RegisterAsWidget(self) |
return self |
end |
AceGUI:RegisterWidgetType(Type,Constructor,Version) |
end |