From a93fc5d8ec18aa9b4f67d633140359d74ba7a3c6 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Tue, 8 Nov 2016 02:46:31 +0100 Subject: [PATCH] color and module refactoring --- color.lua | 83 +++++++++++++++-------------------------- core/history.lua | 8 ++-- gui/auction_listing.lua | 20 +++++----- libs/module.lua | 9 +++-- tabs/post/core.lua | 20 +++++----- util.lua | 49 +----------------------- util/filter.lua | 2 +- 7 files changed, 65 insertions(+), 126 deletions(-) diff --git a/color.lua b/color.lua index ade57bc..668b124 100644 --- a/color.lua +++ b/color.lua @@ -1,61 +1,40 @@ module 'aux' -local COLORS = { - text = {enabled = {255, 254, 250, 1}, disabled = {147, 151, 139, 1}}, - label = {enabled = {216, 225, 211, 1}, disabled = {150, 148, 140, 1}}, - link = {153, 255, 255, 1}, - window = {background = {24, 24, 24, .93}, border = {30, 30, 30, 1}}, - panel = {background = {24, 24, 24, 1}, border = {255, 255, 255, .03}}, - content = {background = {42, 42, 42, 1}, border = {0, 0, 0, 0}}, - state = {enabled = {70, 140, 70, 1}, disabled = {140, 70, 70, 1}}, - - blue = {41, 146, 255, 1}, - green = {22, 255, 22, 1}, - yellow = {255, 255, 0, 1}, - orange = {255, 146, 24, 1}, - red = {255, 0, 0, 1}, - gray = {187, 187, 187, 1}, - - blizzard = {0, 180, 255, 1}, - aux = {255, 255, 154, 1}, -} - -do - local function index_handler(self, key) - self.private.table = self.private.table[key] - if getn(self.private.table) == 0 then - return self.public +function C(r, g, b, a) + local mt = O('__metatable', false, '__newindex', nop, 'color', A(r, g, b, a)) + function mt:__call(text) + local r, g, b, a = unpack(mt.color) + if text then + return format('|c%02X%02X%02X%02X', a, r, g, b) .. text .. FONT_COLOR_CODE_CLOSE else - local color = copy(self.private.table) - self.private.table = COLORS - return self.private.callback(color) + return r/255, g/255, b/255, a end end - function color_accessor(callback) - return function() - return index_function({callback=callback, table=COLORS}, index_handler) - end + function mt:__concat(text) + local r, g, b, a = unpack(mt.color) + return format('|c%02X%02X%02X%02X', a, r, g, b) .. text end + return setmetatable(T, mt) end -do - local mt = { - __call = function(self, text) - local r, g, b, a = unpack(self) - if text then - return format('|c%02X%02X%02X%02X', a, r*255, g*255, b*255) .. text .. FONT_COLOR_CODE_CLOSE - else - return r, g, b, a - end - end - } - M.get_color = color_accessor(function(color) - local r, g, b, a = unpack(color) - return setmetatable(A(r/255, g/255, b/255, a), mt) - end) -end +W = wrapper -M.get_inline_color = color_accessor(function(color) - local r, g, b, a = unpack(color) - return format('|c%02X%02X%02X%02X', a, r, g, b) -end) +M.color = W-{ + text = W-{enabled = C(255, 254, 250, 1), disabled = C(147, 151, 139, 1)}, + label = W-{enabled = C(216, 225, 211, 1), disabled = C(150, 148, 140, 1)}, + link = C(153, 255, 255, 1), + window = W-{background = C(24, 24, 24, .93), border = C(30, 30, 30, 1)}, + panel = W-{background = C(24, 24, 24, 1), border = C(255, 255, 255, .03)}, + content = W-{background = C(42, 42, 42, 1), border = C(0, 0, 0, 0)}, + state = W-{enabled = C(70, 140, 70, 1), disabled = C(140, 70, 70, 1)}, + + blue = C(41, 146, 255, 1), + green = C(22, 255, 22, 1), + yellow = C(255, 255, 0, 1), + orange = C(255, 146, 24, 1), + red = C(255, 0, 0, 1), + gray = C(187, 187, 187, 1), + + blizzard = C(0, 180, 255, 1), + aux = C(255, 255, 154, 1), +} diff --git a/core/history.lua b/core/history.lua index 382b437..8035488 100644 --- a/core/history.lua +++ b/core/history.lua @@ -90,7 +90,7 @@ function M.value(item_key) end value = weighted_median(weighted_values) else - value = calculate_market_value(item_record) + value = market_value(item_record) end value_cache[item_key] = O('value', value, 'next_push', item_record.next_push) end @@ -98,10 +98,10 @@ function M.value(item_key) end function M.market_value(item_key) - return calculate_market_value(read_record(item_key)) + return market_value(read_record(item_key)) end -function calculate_market_value(item_record) +function market_value(item_record) return item_record.daily_min_buyout and min(ceil(item_record.daily_min_buyout * 1.15), item_record.daily_max_price) end @@ -117,7 +117,7 @@ function weighted_median(list) end function push_record(item_record) - for market_value in present(calculate_market_value(item_record)) do + for market_value in present(market_value(item_record)) do tinsert(item_record.data_points, 1, weak-O('market_value', market_value, 'time', item_record.next_push)) while getn(item_record.data_points) > 11 do release(item_record.data_points[getn(item_record.data_points)]) diff --git a/gui/auction_listing.lua b/gui/auction_listing.lua index 0a5b807..7169948 100644 --- a/gui/auction_listing.lua +++ b/gui/auction_listing.lua @@ -17,11 +17,11 @@ local HEAD_HEIGHT = 27 local HEAD_SPACE = 2 local AUCTION_PCT_COLORS = { - {color=inline_color.blue, value=50}, - {color=inline_color.green, value=80}, - {color=inline_color.yellow, value=110}, - {color=inline_color.orange, value=135}, - {color=inline_color.red, value=huge}, + {color=color.blue, value=50}, + {color=color.green, value=80}, + {color=color.yellow, value=110}, + {color=color.orange, value=135}, + {color=color.red, value=huge}, } local TIME_LEFT_STRINGS = { @@ -168,9 +168,9 @@ M.search_config = { fill = function(cell, record) local color if record.high_bidder then - color = inline_color.green + color = color.green elseif record.high_bid ~= 0 then - color = inline_color.orange + color = color.orange end local price if record.high_bidder then @@ -270,7 +270,7 @@ M.auctions_config = { width = .06, align = 'CENTER', fill = function(cell, record, count, own, expandable) - local numAuctionsText = expandable and inline_color.link .. count .. FONT_COLOR_CODE_CLOSE or count + local numAuctionsText = expandable and color.link(count) or count cell:SetText(numAuctionsText) end, cmp = function(record_a, record_b, desc) @@ -404,7 +404,7 @@ M.bids_config = { width = .06, align = 'CENTER', fill = function(cell, record, count, own, expandable) - local numAuctionsText = expandable and inline_color.link .. count .. FONT_COLOR_CODE_CLOSE or count + local numAuctionsText = expandable and color.link(count) or count cell:SetText(numAuctionsText) end, cmp = function(record_a, record_b, desc) @@ -549,7 +549,7 @@ function percentage_color(pct) end function M.percentage_historical(pct, bid) - return (bid and inline_color.gray or percentage_color(pct)) .. (pct > 10000 and '>10000' or pct) .. '%' .. FONT_COLOR_CODE_CLOSE + return (bid and color.gray or percentage_color(pct)) .. (pct > 10000 and '>10000' or pct) .. '%' .. FONT_COLOR_CODE_CLOSE end function M.time_left(code) diff --git a/libs/module.lua b/libs/module.lua index d5eaac4..cfef5a4 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -11,9 +11,12 @@ function define(self, k, v, private) if type(k) ~= 'string' or not strfind(k, '^[_%a][_%w]*') then error('Invalid identifier "%s".', k) end local _, _, prefix, suffix = strfind(k, '^(.?.?.?.?)([_%a].*)') local module = loaded[self] - module.defined[k] = module.defined[k] and error('Duplicate identifier "%s".', k) or true - module.fields[k] = v - if prefix == 'get_' then module.accessors[suffix] = v elseif prefix == 'set_' then module.mutators[suffix] = v end + local signature = (private and '-' or '+') .. k + module.defined[signature] = module.defined[signature] and error('Duplicate identifier "%s".', signature) or true + if private or not module.defined['-' .. k] then + module.fields[k] = v + if prefix == 'get_' then module.accessors[suffix] = v elseif prefix == 'set_' then module.mutators[suffix] = v end + end if not private then module.public_fields[k] = v if prefix == 'get_' then module.public_accessors[suffix] = v elseif prefix == 'set_' then module.public_mutators[suffix] = v end diff --git a/tabs/post/core.lua b/tabs/post/core.lua index 3b6f3d8..140ce49 100644 --- a/tabs/post/core.lua +++ b/tabs/post/core.lua @@ -130,20 +130,20 @@ function update_auction_listing() local bid_color if blizzard_bid_undercut < unit_start_price and stack_blizzard_bid_undercut < unit_start_price then - bid_color = inline_color.red + bid_color = color.red elseif blizzard_bid_undercut < unit_start_price then - bid_color = inline_color.orange + bid_color = color.orange elseif stack_blizzard_bid_undercut < unit_start_price then - bid_color = inline_color.yellow + bid_color = color.yellow end local buyout_color if buyout_price_undercut < unit_buyout_price and stack_buyout_price_undercut < unit_buyout_price then - buyout_color = inline_color.red + buyout_color = color.red elseif buyout_price_undercut < unit_buyout_price then - buyout_color = inline_color.orange + buyout_color = color.orange elseif stack_buyout_price_undercut < unit_buyout_price then - buyout_color = inline_color.yellow + buyout_color = color.yellow end tinsert(auction_rows, O( @@ -301,8 +301,10 @@ function update_item_configuration() item.texture:SetTexture(selected_item.texture) item.name:SetText('[' .. selected_item.name .. ']') - local color = ITEM_QUALITY_COLORS[selected_item.quality] - item.name:SetTextColor(color.r, color.g, color.b) + do + local color = ITEM_QUALITY_COLORS[selected_item.quality] + item.name:SetTextColor(color.r, color.g, color.b) + end if selected_item.aux_quantity > 1 then item.count:SetText(selected_item.aux_quantity) else @@ -316,7 +318,7 @@ function update_item_configuration() local deposit_factor = neutral_faction() and .25 or .05 local stack_size, stack_count = stack_size_slider:GetValue(), stack_count_slider:GetValue() local amount = floor(selected_item.unit_vendor_price * deposit_factor * (selected_item.max_charges and 1 or stack_size)) * stack_count * UIDropDownMenu_GetSelectedValue(duration_dropdown) / 120 - deposit:SetText('Deposit: ' .. money.to_string(amount, nil, nil, nil, inline_color.text.enabled)) + deposit:SetText('Deposit: ' .. money.to_string(amount, nil, nil, nil, color.text.enabled)) end refresh_button:Enable() diff --git a/util.lua b/util.lua index 6ee5a8b..ebbe081 100644 --- a/util.lua +++ b/util.lua @@ -1,58 +1,13 @@ module 'aux' do - local classes, constructors, interfaces, objects = {}, {}, {}, setmetatable({}, {__mode='k'}) - local private_mt = {__metatable=false} - function private_mt:__newindex(k, v) - classes[self][k] = v - end - local public_mt = {__metatable=false} - function public_mt:__newindex(k, v) - classes[self][k] = v - interfaces[self][k] = function(self, ...) - return classes[self][k](objects[self], unpack(arg)) - end - end - local proxy_mt = {__metatable=false, __newindex=nop} - function proxy_mt:__call() - local object = setmetatable({}, {__metatable=false, __newindex=nop, __index=interfaces[self]}) - classes[object] = classes[self] - objects[object] = setmetatable({}, {__index=classes[self]}) - constructors[self](objects[object]) - return object - end - function M.class() - local class, interface = {}, {} - local private, public, proxy = setmetatable({}, private_mt), setmetatable({}, public_mt), setmetatable({}, proxy_mt) - classes[private], classes[public], classes[proxy] = class, class, class - interfaces[public], interfaces[proxy] = interface, interface - return private, public, function(constructor) - constructors[proxy] = constructor or nop - return proxy - end - end -end - -do - local _state = setmetatable({}, {__mode='kv'}) - local __index = function(self, k) - return _state[self].handler({public=self, private=_state[self].state}, k) - end - function M.index_function(state, handler) -- TODO rename table-accessor, use predicate to stop - local state, self = {handler=handler, state=state}, T - _state[self] = state - return setmetatable(self, {__metatable=false, __index=__index, state=state}) - end -end - -do - local mt = {__metatable=false, __newindex=nop} + local mt = {__metatable=false, __newindex=nop, __mode='k'} function mt:__sub(table) local proxy = setmetatable(T, O('__metatable', false, '__newindex', nop, '__index', table)) self[proxy] = table return proxy end - function M.wrapper() -- TODO replace index function + function M.get_wrapper() return setmetatable(T, mt) end end diff --git a/util/filter.lua b/util/filter.lua index d52bf31..bb9e8b0 100644 --- a/util/filter.lua +++ b/util/filter.lua @@ -473,7 +473,7 @@ function prettified_filter_string(filter) prettified.append(info.display_name(cache.item_id(parameter)) or color.label.enabled('[' .. parameter .. ']')) else if filters[component[2]].input_type == 'money' then - prettified.append(money.to_string(money.from_string(parameter), nil, true, nil, inline_color.label.enabled)) + prettified.append(money.to_string(money.from_string(parameter), nil, true, nil, color.label.enabled)) else prettified.append(color.label.enabled(parameter)) end