lotrointerface.com
Search Downloads

LoTROInterface SVN KragenBars

[/] [branches/] [3.21/] [UI/] [ComboBox.lua] - Blame information for rev 72

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 72 Kragenwar-3044
import "Turbine.UI";
2 Kragenwar-3044
 
3 Kragenwar-3044
ComboBox = class(Turbine.UI.Control);
4 Kragenwar-3044
 
5 Kragenwar-3044
-- colors
6 Kragenwar-3044
ComboBox.HighlightColor = Turbine.UI.Color(232/255, 175/255, 72/255);
7 Kragenwar-3044
ComboBox.SelectionColor = Turbine.UI.Color(203/255, 195/255, 52/255);
8 Kragenwar-3044
ComboBox.ItemColor = Turbine.UI.Color(245/255, 222/255, 147/255);
9 Kragenwar-3044
ComboBox.DisabledColor = Turbine.UI.Color(162/255, 162/255, 162/255);
10 Kragenwar-3044
ComboBox.BlackColor = Turbine.UI.Color(1, 0, 0, 0);
11 Kragenwar-3044
 
12 Kragenwar-3044
-- create a var to store the open dropdown in
13 Kragenwar-3044
ComboBox.open = nil;
14 Kragenwar-3044
 
15 Kragenwar-3044
-- close any open dropdowns
16 Kragenwar-3044
ComboBox.Cleanup = function()
17 Kragenwar-3044
    if (ComboBox.open ~= nil) then
18 Kragenwar-3044
        ComboBox.open:CloseDropDown();
19 Kragenwar-3044
        ComboBox.open = nil;
20 Kragenwar-3044
    end
21 Kragenwar-3044
end
22 Kragenwar-3044
 
23 Kragenwar-3044
function ComboBox:Constructor()
24 Kragenwar-3044
    Turbine.UI.Control.Constructor(self);
25 Kragenwar-3044
 
26 Kragenwar-3044
    self:SetBackColor(ComboBox.DisabledColor);
27 Kragenwar-3044
    --self:SetZOrder(5);
28 Kragenwar-3044
 
29 Kragenwar-3044
    -- state
30 Kragenwar-3044
    self.dropped = false;
31 Kragenwar-3044
    self.selection = -1;
32 Kragenwar-3044
 
33 Kragenwar-3044
    -- text label
34 Kragenwar-3044
    self.label = Turbine.UI.Label();
35 Kragenwar-3044
    self.label:SetParent(self);
36 Kragenwar-3044
    self.label:SetFont(Turbine.UI.Lotro.Font.TrajanPro14);
37 Kragenwar-3044
    self.label:SetForeColor(ComboBox.ItemColor);
38 Kragenwar-3044
    self.label:SetBackColor(ComboBox.BlackColor);
39 Kragenwar-3044
    self.label:SetOutlineColor(ComboBox.HighlightColor);
40 Kragenwar-3044
    self.label:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
41 Kragenwar-3044
    self.label:SetMouseVisible(false);
42 Kragenwar-3044
 
43 Kragenwar-3044
    -- arrow
44 Kragenwar-3044
    self.arrow = Turbine.UI.Control();
45 Kragenwar-3044
    self.arrow:SetParent(self);
46 Kragenwar-3044
    self.arrow:SetSize(16, 16);
47 Kragenwar-3044
    self.arrow:SetZOrder(20);
48 Kragenwar-3044
    self.arrow:SetBlendMode(Turbine.UI.BlendMode.AlphaBlend);
49 Kragenwar-3044
    self.arrow:SetBackground("KragenPlugs/UI/Resources/dropdown_arrow_closed.tga");
50 Kragenwar-3044
    self.arrow:SetMouseVisible(false);
51 Kragenwar-3044
 
52 Kragenwar-3044
    -- drop down window
53 Kragenwar-3044
    self.dropDownWindow = Turbine.UI.Window();
54 Kragenwar-3044
    self.dropDownWindow:SetBackColor(ComboBox.DisabledColor);
55 Kragenwar-3044
    self.dropDownWindow:SetZOrder(30);
56 Kragenwar-3044
    self.dropDownWindow:SetVisible(false);
57 Kragenwar-3044
 
58 Kragenwar-3044
    -- list scroll bar
59 Kragenwar-3044
    self.scrollBar = Turbine.UI.Lotro.ScrollBar();
60 Kragenwar-3044
    self.scrollBar:SetOrientation(Turbine.UI.Orientation.Vertical);
61 Kragenwar-3044
    self.scrollBar:SetParent(self.dropDownWindow);
62 Kragenwar-3044
    self.scrollBar:SetBackColor(ComboBox.BlackColor);
63 Kragenwar-3044
 
64 Kragenwar-3044
    -- list to contain the drop down items
65 Kragenwar-3044
    self.listBox = Turbine.UI.ListBox();
66 Kragenwar-3044
    self.listBox:SetParent(self.dropDownWindow);
67 Kragenwar-3044
    self.listBox:SetOrientation(Turbine.UI.Orientation.Horizontal);
68 Kragenwar-3044
    self.listBox:SetVerticalScrollBar(self.scrollBar);
69 Kragenwar-3044
    self.listBox:SetMaxItemsPerLine(1);
70 Kragenwar-3044
    self.listBox:SetMouseVisible(false);
71 Kragenwar-3044
    self.listBox:SetPosition(2, 2);
72 Kragenwar-3044
    self.listBox:SetBackColor(ComboBox.BlackColor);
73 Kragenwar-3044
end
74 Kragenwar-3044
 
75 Kragenwar-3044
function ComboBox:MouseEnter(args)
76 Kragenwar-3044
    if (not self:IsEnabled()) then
77 Kragenwar-3044
        return;
78 Kragenwar-3044
    end
79 Kragenwar-3044
 
80 Kragenwar-3044
    self.label:SetFontStyle(Turbine.UI.FontStyle.Outline);
81 Kragenwar-3044
    self.label:SetForeColor(ComboBox.ItemColor);
82 Kragenwar-3044
    self.label:SetText(self.label:GetText());
83 Kragenwar-3044
 
84 Kragenwar-3044
    self.arrow:SetBackground("KragenPlugs/UI/Resources/dropdown_arrow_"..(self.dropped and "open_rollover" or "closed_rollover")..".tga");
85 Kragenwar-3044
end
86 Kragenwar-3044
 
87 Kragenwar-3044
function ComboBox:MouseLeave(args)
88 Kragenwar-3044
    if (not self:IsEnabled()) then
89 Kragenwar-3044
        return;
90 Kragenwar-3044
    end
91 Kragenwar-3044
 
92 Kragenwar-3044
    self.label:SetFontStyle(Turbine.UI.FontStyle.None);
93 Kragenwar-3044
    if (self.dropped) then
94 Kragenwar-3044
        self.label:SetForeColor(ComboBox.SelectionColor);
95 Kragenwar-3044
    end
96 Kragenwar-3044
    self.label:SetText(self.label:GetText());
97 Kragenwar-3044
 
98 Kragenwar-3044
    self.arrow:SetBackground("KragenPlugs/UI/Resources/dropdown_arrow_"..(self.dropped and "open" or "closed")..".tga");
99 Kragenwar-3044
end
100 Kragenwar-3044
 
101 Kragenwar-3044
function ComboBox:MouseClick(args)
102 Kragenwar-3044
    if (not self:IsEnabled()) then
103 Kragenwar-3044
        return;
104 Kragenwar-3044
    end
105 Kragenwar-3044
 
106 Kragenwar-3044
    if (args.Button == Turbine.UI.MouseButton.Left) then
107 Kragenwar-3044
        if (self.dropped) then
108 Kragenwar-3044
            self:CloseDropDown();
109 Kragenwar-3044
        else
110 Kragenwar-3044
            self:ShowDropDown();
111 Kragenwar-3044
        end
112 Kragenwar-3044
    end
113 Kragenwar-3044
end
114 Kragenwar-3044
 
115 Kragenwar-3044
function ComboBox:FireEvent()
116 Kragenwar-3044
    if (type(self.SelectedIndexChanged) == "function") then
117 Kragenwar-3044
        self:SelectedIndexChanged({selection=self:GetSelection()});
118 Kragenwar-3044
    end
119 Kragenwar-3044
end
120 Kragenwar-3044
 
121 Kragenwar-3044
function ComboBox:ItemSelected(index)
122 Kragenwar-3044
    if (self.selection ~= -1) then
123 Kragenwar-3044
        local old = self.listBox:GetItem(self.selection);
124 Kragenwar-3044
        old:SetForeColor(ComboBox.ItemColor);
125 Kragenwar-3044
    end
126 Kragenwar-3044
 
127 Kragenwar-3044
    local item = self.listBox:GetItem(index);
128 Kragenwar-3044
    self.selection = index;
129 Kragenwar-3044
    item:SetForeColor(ComboBox.SelectionColor);
130 Kragenwar-3044
    self.label:SetText(item:GetText());
131 Kragenwar-3044
 
132 Kragenwar-3044
    self:CloseDropDown();
133 Kragenwar-3044
end
134 Kragenwar-3044
 
135 Kragenwar-3044
function ComboBox:AddItem(text, value)
136 Kragenwar-3044
    local width, height = self.listBox:GetSize();
137 Kragenwar-3044
 
138 Kragenwar-3044
    local listItem = Turbine.UI.Label();
139 Kragenwar-3044
    listItem:SetSize(width, 20);
140 Kragenwar-3044
    listItem:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
141 Kragenwar-3044
    listItem:SetForeColor(ComboBox.ItemColor);
142 Kragenwar-3044
    listItem:SetFont(Turbine.UI.Lotro.Font.TrajanPro14);
143 Kragenwar-3044
    listItem:SetOutlineColor(ComboBox.HighlightColor);
144 Kragenwar-3044
    listItem:SetText(text);
145 Kragenwar-3044
 
146 Kragenwar-3044
    listItem.MouseEnter = function(sender, args)
147 Kragenwar-3044
        sender:SetFontStyle(Turbine.UI.FontStyle.Outline);
148 Kragenwar-3044
        sender:SetForeColor(ComboBox.ItemColor);
149 Kragenwar-3044
        sender:SetText(sender:GetText());
150 Kragenwar-3044
    end
151 Kragenwar-3044
    listItem.MouseLeave = function(sender, args)
152 Kragenwar-3044
        sender:SetFontStyle(Turbine.UI.FontStyle.None);
153 Kragenwar-3044
        if (self.listBox:IndexOfItem(sender) == self.selection) then
154 Kragenwar-3044
            sender:SetForeColor(ComboBox.SelectionColor);
155 Kragenwar-3044
        end
156 Kragenwar-3044
        sender:SetText(sender:GetText());
157 Kragenwar-3044
    end
158 Kragenwar-3044
    listItem.MouseClick = function(sender, args)
159 Kragenwar-3044
        if (args.Button == Turbine.UI.MouseButton.Left) then
160 Kragenwar-3044
            self:ItemSelected(self.listBox:GetSelectedIndex());
161 Kragenwar-3044
            self:FireEvent();
162 Kragenwar-3044
        end
163 Kragenwar-3044
    end
164 Kragenwar-3044
    listItem.value = value;
165 Kragenwar-3044
    self.listBox:AddItem(listItem);
166 Kragenwar-3044
end
167 Kragenwar-3044
 
168 Kragenwar-3044
function ComboBox:RemoveItem(value)
169 Kragenwar-3044
    for i = 1, self.listBox:GetItemCount() do
170 Kragenwar-3044
        local item = self.listBox:GetItem(i);
171 Kragenwar-3044
        if (item.value == value) then
172 Kragenwar-3044
            item:SetVisible(false);
173 Kragenwar-3044
            self.listBox:RemoveItemAt(i);
174 Kragenwar-3044
 
175 Kragenwar-3044
            -- if the removed item is the selection update it
176 Kragenwar-3044
            if (self.selection == i) then
177 Kragenwar-3044
                self.selection = -1;
178 Kragenwar-3044
                if (self.listBox:GetItemCount() > 0) then
179 Kragenwar-3044
                    self:ItemSelected(1);
180 Kragenwar-3044
                    self:FireEvent();
181 Kragenwar-3044
                else
182 Kragenwar-3044
                    self.label:SetText("");
183 Kragenwar-3044
                end
184 Kragenwar-3044
            end
185 Kragenwar-3044
            break;
186 Kragenwar-3044
        end
187 Kragenwar-3044
    end
188 Kragenwar-3044
end
189 Kragenwar-3044
 
190 Kragenwar-3044
function ComboBox:SetSelection(value)
191 Kragenwar-3044
    for i = 1, self.listBox:GetItemCount() do
192 Kragenwar-3044
        local item = self.listBox:GetItem(i);
193 Kragenwar-3044
        if (item.value == value) then
194 Kragenwar-3044
            self:ItemSelected(i);
195 Kragenwar-3044
            self:FireEvent();
196 Kragenwar-3044
            break;
197 Kragenwar-3044
        end
198 Kragenwar-3044
    end
199 Kragenwar-3044
end
200 Kragenwar-3044
 
201 Kragenwar-3044
function ComboBox:GetSelection()
202 Kragenwar-3044
    if (self.selection == -1) then
203 Kragenwar-3044
        return nil;
204 Kragenwar-3044
    else
205 Kragenwar-3044
        local item = self.listBox:GetItem(self.selection);
206 Kragenwar-3044
        return item.value;
207 Kragenwar-3044
    end
208 Kragenwar-3044
end
209 Kragenwar-3044
 
210 Kragenwar-3044
function ComboBox:UpdateSelectionText(text)
211 Kragenwar-3044
    if (self.selection ~= -1) then
212 Kragenwar-3044
        local item = self.listBox:GetItem(self.selection);
213 Kragenwar-3044
        item:SetText(text);
214 Kragenwar-3044
        self.label:SetText(text);
215 Kragenwar-3044
    end
216 Kragenwar-3044
end
217 Kragenwar-3044
 
218 Kragenwar-3044
function ComboBox:GetItemCount()
219 Kragenwar-3044
    return self.listBox:GetItemCount();
220 Kragenwar-3044
end
221 Kragenwar-3044
 
222 Kragenwar-3044
function ComboBox:GetItem(index)
223 Kragenwar-3044
    local item = self.listBox:GetItem(index);
224 Kragenwar-3044
    return item.value;
225 Kragenwar-3044
end
226 Kragenwar-3044
 
227 Kragenwar-3044
function ComboBox:SetSize(width, height)
228 Kragenwar-3044
    Turbine.UI.Control.SetSize(self, width, height);
229 Kragenwar-3044
    self:Layout();
230 Kragenwar-3044
end
231 Kragenwar-3044
 
232 Kragenwar-3044
function ComboBox:SetEnabled(enabled)
233 Kragenwar-3044
    Turbine.UI.Control.SetEnabled(self, enabled);
234 Kragenwar-3044
    if (enabled) then
235 Kragenwar-3044
        self.label:SetForeColor(ComboBox.ItemColor);
236 Kragenwar-3044
        self.arrow:SetBackground("KragenPlugs/UI/Resources/dropdown_arrow_closed.tga");
237 Kragenwar-3044
    else
238 Kragenwar-3044
        self:CloseDropDown();
239 Kragenwar-3044
        self.label:SetForeColor(ComboBox.DisabledColor);
240 Kragenwar-3044
        self.arrow:SetBackground("KragenPlugs/UI/Resources/dropdown_arrow_closed_ghosted.tga");
241 Kragenwar-3044
    end
242 Kragenwar-3044
end
243 Kragenwar-3044
 
244 Kragenwar-3044
function ComboBox:Layout()
245 Kragenwar-3044
    local width, height = self:GetSize();
246 Kragenwar-3044
    self.label:SetSize(width - 4, height - 4);
247 Kragenwar-3044
    self.label:SetPosition(2, 2);
248 Kragenwar-3044
    self.arrow:SetPosition(width - 2 - 16, 2 + ((height - 4 - 16) / 2));
249 Kragenwar-3044
end
250 Kragenwar-3044
 
251 Kragenwar-3044
function ComboBox:ShowDropDown()
252 Kragenwar-3044
    -- close the existing drop down if one is open
253 Kragenwar-3044
    ComboBox.Cleanup();
254 Kragenwar-3044
 
255 Kragenwar-3044
    local itemCount = self.listBox:GetItemCount();
256 Kragenwar-3044
    if ((itemCount > 0) and not (self.dropped)) then
257 Kragenwar-3044
        self.dropped = true;
258 Kragenwar-3044
        self.label:SetForeColor(ComboBox.SelectionColor);
259 Kragenwar-3044
        self.arrow:SetBackground("KragenPlugs/UI/Resources/dropdown_arrow_open_rollover.tga");
260 Kragenwar-3044
        local width, height = self:GetSize();
261 Kragenwar-3044
        --width = width + 10;
262 Kragenwar-3044
 
263 Kragenwar-3044
        -- max size
264 Kragenwar-3044
        local maxItems = itemCount;
265 Kragenwar-3044
        local scrollSize = 0;
266 Kragenwar-3044
        if (maxItems > 10) then
267 Kragenwar-3044
            maxItems = 10;
268 Kragenwar-3044
            scrollSize = 10;
269 Kragenwar-3044
        end
270 Kragenwar-3044
 
271 Kragenwar-3044
        -- list item sizes
272 Kragenwar-3044
        local listHeight = 0;
273 Kragenwar-3044
        for i = 1, self.listBox:GetItemCount() do
274 Kragenwar-3044
            local item = self.listBox:GetItem(i);
275 Kragenwar-3044
            item:SetWidth(width - 14);
276 Kragenwar-3044
            if (i <= maxItems) then
277 Kragenwar-3044
                listHeight = listHeight + item:GetHeight();
278 Kragenwar-3044
            end
279 Kragenwar-3044
        end
280 Kragenwar-3044
 
281 Kragenwar-3044
        -- window size
282 Kragenwar-3044
        self.listBox:SetSize(width - 4 - scrollSize, listHeight);
283 Kragenwar-3044
        self.dropDownWindow:SetSize(width, listHeight + 4);
284 Kragenwar-3044
 
285 Kragenwar-3044
        -- scrollbar
286 Kragenwar-3044
        self.scrollBar:SetSize(10, listHeight);
287 Kragenwar-3044
        self.scrollBar:SetPosition(width - 12, 2);
288 Kragenwar-3044
 
289 Kragenwar-3044
        -- position
290 Kragenwar-3044
        local x, y = self:GetParent():PointToScreen(self:GetPosition());
291 Kragenwar-3044
        self.dropDownWindow:SetPosition(x, y + height + 3);
292 Kragenwar-3044
 
293 Kragenwar-3044
        self.dropDownWindow:SetVisible(true);
294 Kragenwar-3044
 
295 Kragenwar-3044
        -- store the open drop down
296 Kragenwar-3044
        ComboBox.open = self;
297 Kragenwar-3044
    end
298 Kragenwar-3044
end
299 Kragenwar-3044
 
300 Kragenwar-3044
function ComboBox:CloseDropDown()
301 Kragenwar-3044
    if (self.dropped) then
302 Kragenwar-3044
        self.dropped = false;
303 Kragenwar-3044
        self.dropDownWindow:SetVisible(false);
304 Kragenwar-3044
        self.label:SetForeColor(ComboBox.ItemColor);
305 Kragenwar-3044
        self.arrow:SetBackground("KragenPlugs/UI/Resources/dropdown_arrow_closed_rollover.tga");
306 Kragenwar-3044
    end
307 Kragenwar-3044
end

All times are GMT -5. The time now is 03:34 AM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui