From 6c2539bbd4bf880dfb251100c10e3b51cf80376c Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 5 Oct 2012 17:50:54 +0200 Subject: [PATCH] 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 --- modules/corelib/ui/uiscrollarea.lua | 2 + modules/game_console/console.lua | 77 +++++++++++++++++++++++++++ modules/game_console/console.otui | 25 +++++++++ src/framework/graphics/bitmapfont.cpp | 3 ++ 4 files changed, 107 insertions(+) diff --git a/modules/corelib/ui/uiscrollarea.lua b/modules/corelib/ui/uiscrollarea.lua index 782e438d..69cdac3d 100644 --- a/modules/corelib/ui/uiscrollarea.lua +++ b/modules/corelib/ui/uiscrollarea.lua @@ -71,6 +71,7 @@ function UIScrollArea:setVerticalScrollBar(scrollbar) local virtualOffset = self:getVirtualOffset() virtualOffset.y = value self:setVirtualOffset(virtualOffset) + self:onScrollbarChange(value) -- Maybe there is a better way to do this? end self:updateScrollBars() end @@ -81,6 +82,7 @@ function UIScrollArea:setHorizontalScrollBar(scrollbar) local virtualOffset = self:getVirtualOffset() virtualOffset.x = value self:setVirtualOffset(virtualOffset) + self:onScrollbarChange(value) -- Maybe there is a better way to do this? end self:updateScrollBars() end diff --git a/modules/game_console/console.lua b/modules/game_console/console.lua index c4bf4189..59083a8e 100644 --- a/modules/game_console/console.lua +++ b/modules/game_console/console.lua @@ -271,6 +271,38 @@ function addText(text, speaktype, tabName, creatureName) 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) if Options.getOption('showTimestampsInConsole') then text = os.date('%H:%M') .. ' ' .. text @@ -283,12 +315,57 @@ function addTabText(text, speaktype, tab, creatureName) label:setText(text) label:setColor(speaktype.color) 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 if consoleBuffer:getChildCount() > MAX_LINES then consoleBuffer:getFirstChild():destroy() end + + if consoleBufferHighlight:getChildCount() > MAX_LINES then + consoleBufferHighlight:getFirstChild():destroy() + end end function popupMenu(mousePos, mouseButton, creatureName, text) diff --git a/modules/game_console/console.otui b/modules/game_console/console.otui index 023ad221..9643b837 100644 --- a/modules/game_console/console.otui +++ b/modules/game_console/console.otui @@ -24,6 +24,31 @@ ConsoleTabBarPanel < TabBarRoundedPanel inverted-scroll: true 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 id: consoleScrollBar anchors.top: parent.top diff --git a/src/framework/graphics/bitmapfont.cpp b/src/framework/graphics/bitmapfont.cpp index bed56130..741653cd 100644 --- a/src/framework/graphics/bitmapfont.cpp +++ b/src/framework/graphics/bitmapfont.cpp @@ -52,6 +52,9 @@ void BitmapFont::load(const OTMLNodePtr& fontNode) m_glyphsSize[32].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 m_glyphsSize[(uchar)'\n'] = Size(1, m_glyphHeight);