Added support for curly braces in NPC chat

-Added overlay to the default consoleBuffer for highlighting
-Char 127 now is used as spacer (Width 1)
-Supports default font "verdana-11px-antialised"

http://i.imgur.com/8drWH.png
This commit is contained in:
Samuel 2012-10-05 17:50:54 +02:00
parent 737001264d
commit 6c2539bbd4
4 changed files with 107 additions and 0 deletions

View File

@ -71,6 +71,7 @@ function UIScrollArea:setVerticalScrollBar(scrollbar)
local virtualOffset = self:getVirtualOffset() local virtualOffset = self:getVirtualOffset()
virtualOffset.y = value virtualOffset.y = value
self:setVirtualOffset(virtualOffset) self:setVirtualOffset(virtualOffset)
self:onScrollbarChange(value) -- Maybe there is a better way to do this?
end end
self:updateScrollBars() self:updateScrollBars()
end end
@ -81,6 +82,7 @@ function UIScrollArea:setHorizontalScrollBar(scrollbar)
local virtualOffset = self:getVirtualOffset() local virtualOffset = self:getVirtualOffset()
virtualOffset.x = value virtualOffset.x = value
self:setVirtualOffset(virtualOffset) self:setVirtualOffset(virtualOffset)
self:onScrollbarChange(value) -- Maybe there is a better way to do this?
end end
self:updateScrollBars() self:updateScrollBars()
end end

View File

@ -271,6 +271,38 @@ function addText(text, speaktype, tabName, creatureName)
end end
end end
-- Contains letter width for font "verdana-11px-antialised" as console is based on it
local letterWidth = {
[32] = 4, [33] = 3, [34] = 6, [35] = 8, [36] = 7, [37] = 13, [38] = 9, [39] = 3, [40] = 5, [41] = 5, [42] = 6, [43] = 8, [44] = 4, [45] = 5, [46] = 3, [47] = 8,
[48] = 7, [49] = 6, [50] = 7, [51] = 7, [52] = 7, [53] = 7, [54] = 7, [55] = 7, [56] = 7, [57] = 7, [58] = 3, [59] = 4, [60] = 8, [61] = 8, [62] = 8, [63] = 6,
[64] = 10, [65] = 9, [66] = 7, [67] = 7, [68] = 8, [69] = 7, [70] = 7, [71] = 8, [72] = 8, [73] = 5, [74] = 5, [75] = 7, [76] = 7, [77] = 9, [78] = 8, [79] = 8,
[80] = 7, [81] = 8, [82] = 8, [83] = 7, [84] = 8, [85] = 8, [86] = 8, [87] = 12, [88] = 8, [89] = 8, [90] = 7, [91] = 5, [92] = 8, [93] = 5, [94] = 9, [95] = 8,
[96] = 5, [97] = 7, [98] = 7, [99] = 6, [100] = 7, [101] = 7, [102] = 5, [103] = 7, [104] = 7, [105] = 3, [106] = 4, [107] = 7, [108] = 3, [109] = 11, [110] = 7,
[111] = 7, [112] = 7, [113] = 7, [114] = 6, [115] = 6, [116] = 5, [117] = 7, [118] = 8, [119] = 10, [120] = 8, [121] = 8, [122] = 6, [123] = 7, [124] = 4, [125] = 7, [126] = 8,
[127] = 1, [128] = 7, [129] = 6, [130] = 3, [131] = 7, [132] = 6, [133] = 11, [134] = 7, [135] = 7, [136] = 7, [137] = 13, [138] = 7, [139] = 4, [140] = 11, [141] = 6, [142] = 6,
[143] = 6, [144] = 6, [145] = 4, [146] = 3, [147] = 7, [148] = 6, [149] = 6, [150] = 7, [151] = 10, [152] = 7, [153] = 10, [154] = 6, [155] = 5, [156] = 11, [157] = 6, [158] = 6,
[159] = 8, [160] = 4, [161] = 3, [162] = 7, [163] = 7, [164] = 7, [165] = 8, [166] = 4, [167] = 7, [168] = 6, [169] = 10, [170] = 6, [171] = 8, [172] = 8, [173] = 16, [174] = 10,
[175] = 8, [176] = 5, [177] = 8, [178] = 5, [179] = 5, [180] = 6, [181] = 7, [182] = 7, [183] = 3, [184] = 5, [185] = 6, [186] = 6, [187] = 8, [188] = 12, [189] = 12, [190] = 12,
[191] = 6, [192] = 9, [193] = 9, [194] = 9, [195] = 9, [196] = 9, [197] = 9, [198] = 11, [199] = 7, [200] = 7, [201] = 7, [202] = 7, [203] = 7, [204] = 5, [205] = 5, [206] = 6,
[207] = 5, [208] = 8, [209] = 8, [210] = 8, [211] = 8, [212] = 8, [213] = 8, [214] = 8, [215] = 8, [216] = 8, [217] = 8, [218] = 8, [219] = 8, [220] = 8, [221] = 8, [222] = 7,
[223] = 7, [224] = 7, [225] = 7, [226] = 7, [227] = 7, [228] = 7, [229] = 7, [230] = 11, [231] = 6, [232] = 7, [233] = 7, [234] = 7, [235] = 7, [236] = 3, [237] = 4, [238] = 4,
[239] = 4, [240] = 7, [241] = 7, [242] = 7, [243] = 7, [244] = 7, [245] = 7, [246] = 7, [247] = 9, [248] = 7, [249] = 7, [250] = 7, [251] = 7, [252] = 7, [253] = 8, [254] = 7, [255] = 8
}
-- Return information about start, end in the string and the highlighted words
function getHighlightedText(text)
local tmpData = {}
repeat
local tmp = {string.find(text, "{([^}]+)}", tmpData[#tmpData-1])}
for _, v in pairs(tmp) do
table.insert(tmpData, v)
end
until not(string.find(text, "{([^}]+)}", tmpData[#tmpData-1]))
return tmpData
end
function addTabText(text, speaktype, tab, creatureName) function addTabText(text, speaktype, tab, creatureName)
if Options.getOption('showTimestampsInConsole') then if Options.getOption('showTimestampsInConsole') then
text = os.date('%H:%M') .. ' ' .. text text = os.date('%H:%M') .. ' ' .. text
@ -284,11 +316,56 @@ function addTabText(text, speaktype, tab, creatureName)
label:setColor(speaktype.color) label:setColor(speaktype.color)
consoleTabBar:blinkTab(tab) consoleTabBar:blinkTab(tab)
-- Overlay for consoleBuffer which shows highlighted words only
local consoleBufferHighlight = panel:getChildById('consoleBufferHighlight')
local labelHighlight = g_ui.createWidget('ConsoleLabel', consoleBufferHighlight)
labelHighlight:setId('consoleLabel' .. panel:getChildCount())
labelHighlight:setColor("#1f9ffe")
local player = g_game.getLocalPlayer()
if speaktype.npcChat and player:getName() ~= creatureName then -- Check if it is the npc who is talking
local highlightData = getHighlightedText(text)
if #highlightData == 0 then
labelHighlight:setText("")
else
-- Remove the curly braces
for i = 1, #highlightData / 3 do
local dataBlock = { _start = highlightData[(i-1)*3+1], _end = highlightData[(i-1)*3+2], words = highlightData[(i-1)*3+3] }
text = text:gsub("{"..dataBlock.words.."}", dataBlock.words)
-- Recalculate positions as braces are removed
highlightData[(i-1)*3+1] = dataBlock._start - ((i-1) * 2)
highlightData[(i-1)*3+2] = dataBlock._end - (1 + (i-1) * 2)
end
label:setText(text)
-- Calculate the positions of the highlighted text and fill with string.char(127) [Width: 1]
local tmpText = ""
for i = 1, #highlightData / 3 do
local dataBlock = { _start = highlightData[(i-1)*3+1], _end = highlightData[(i-1)*3+2], words = highlightData[(i-1)*3+3] }
local lastBlockEnd = (highlightData[(i-2)*3+2] or 1)
for letter = lastBlockEnd, dataBlock._start-1 do
tmpText = tmpText .. string.rep(string.char(127), letterWidth[string.byte(text:sub(letter, letter))])
end
tmpText = tmpText .. dataBlock.words
end
labelHighlight:setText(tmpText)
end
else
labelHighlight:setText("")
end
label.onMouseRelease = function (self, mousePos, mouseButton) popupMenu(mousePos, mouseButton, creatureName, text) end label.onMouseRelease = function (self, mousePos, mouseButton) popupMenu(mousePos, mouseButton, creatureName, text) end
if consoleBuffer:getChildCount() > MAX_LINES then if consoleBuffer:getChildCount() > MAX_LINES then
consoleBuffer:getFirstChild():destroy() consoleBuffer:getFirstChild():destroy()
end end
if consoleBufferHighlight:getChildCount() > MAX_LINES then
consoleBufferHighlight:getFirstChild():destroy()
end
end end
function popupMenu(mousePos, mouseButton, creatureName, text) function popupMenu(mousePos, mouseButton, creatureName, text)

View File

@ -24,6 +24,31 @@ ConsoleTabBarPanel < TabBarRoundedPanel
inverted-scroll: true inverted-scroll: true
padding: 1 padding: 1
ScrollablePanel
id: consoleBufferHighlight
anchors.fill: parent
margin-right: 12
vertical-scrollbar: consoleScrollBarHighlight
layout:
type: verticalBox
align-bottom: true
border-width: 1
border-color: #202327
inverted-scroll: true
padding: 1
@onScrollbarChange: |
local consoleScrollBar = self:getParent():getChildById('consoleScrollBar')
local consoleScrollBarHighlight = self:getParent():getChildById('consoleScrollBarHighlight')
consoleScrollBar:setValue(consoleScrollBarHighlight:getValue())
VerticalScrollBar
id: consoleScrollBarHighlight
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.right: parent.right
step: 14
pixels-scroll: true
VerticalScrollBar VerticalScrollBar
id: consoleScrollBar id: consoleScrollBar
anchors.top: parent.top anchors.top: parent.top

View File

@ -52,6 +52,9 @@ void BitmapFont::load(const OTMLNodePtr& fontNode)
m_glyphsSize[32].setWidth(spaceWidth); m_glyphsSize[32].setWidth(spaceWidth);
m_glyphsSize[160].setWidth(spaceWidth); m_glyphsSize[160].setWidth(spaceWidth);
// use 127 as spacer [Width: 1]
m_glyphsSize[127].setWidth(1);
// new line actually has a size that will be useful in multiline algorithm // new line actually has a size that will be useful in multiline algorithm
m_glyphsSize[(uchar)'\n'] = Size(1, m_glyphHeight); m_glyphsSize[(uchar)'\n'] = Size(1, m_glyphHeight);