• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

OTClient Clickable word npc in OTClient

Hundanger

New Member
Joined
Jul 15, 2010
Messages
40
Reaction score
4
Hello there,


I would like to install in otclient the same functionality that exists in global tibia in which the player "clicks" on the highlighted word of the npcs, example:

1609987353215.png

When the play click on the word "records" the creature automaticaly says "records" in this channel, facilitating interaction.


I've been looking and in the console.lua class of the game_console module of otclient has this code:
Lua:
  if speaktype.npcChat and (g_game.getCharacterName() ~= creatureName or g_game.getCharacterName() == 'Account Manager') then
    local highlightData = getHighlightedText(text)
    if #highlightData > 0 then
      local labelHighlight = g_ui.createWidget('ConsolePhantomLabel', label)
      labelHighlight:fill('parent')
      labelHighlight:setId('consoleLabelHighlight' .. consoleBuffer:getChildCount())
      labelHighlight:setColor("#1f9ffe")
      -- 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, 1)
        -- 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 drawText = label:getDrawText()
      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
          local tmpChar = string.byte(drawText:sub(letter, letter))
          local fillChar = (tmpChar == 10 or tmpChar == 32) and string.char(tmpChar) or string.char(127)
          tmpText = tmpText .. string.rep(fillChar, letterWidth[tmpChar])
        end
        tmpText = tmpText .. dataBlock.words
      end
      -- Fill the highlight label to the same size as default label
      local finalBlockEnd = (highlightData[(#highlightData/3-1)*3+2] or 1)
      for letter = finalBlockEnd, drawText:len() do
          local tmpChar = string.byte(drawText:sub(letter, letter))
          local fillChar = (tmpChar == 10 or tmpChar == 32) and string.char(tmpChar) or string.char(127)
          tmpText = tmpText .. string.rep(fillChar, letterWidth[tmpChar])
      end
      labelHighlight:setText(tmpText)
    end
  end


Here it transforms all words that start with {and end with} into highlighted words in color #1f9ffe
further down it has this function:

Lua:
label.onMousePress = function(self, mousePos, button)
    if button == MouseLeftButton then clearSelection(consoleBuffer) end
end


The only possible logic I thought was:
calculate the position of the screen where each highlighted word starts and ends and do an if in onMousePress with parameter mousePos.


Do you know any easier way to do this? I looked like hell and didn't find it.

Thank you guys,
 

Attachments

OP
OP
H

Hundanger

New Member
Joined
Jul 15, 2010
Messages
40
Reaction score
4
Hello guys,


I opened a issue request in the official otclient github and the user Ayato96 responded with a solution that was very good:

1610228264072.png

When you click on the phrase, it will bring up a menu with options to choose from.

To more information go to:

Thank you,
 
Top