No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

textmessage.lua 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. TextMessage = {}
  2. -- require styles
  3. importStyle 'textmessage.otui'
  4. -- private variables
  5. local MessageTypes = {
  6. consoleRed = { color = '#F55E5E', consoleTab = 'Default' },
  7. consoleOrange = { color = '#FE6500', consoleTab = 'Default' },
  8. consoleBlue = { color = '#9F9DFD', consoleTab = 'Default' },
  9. warning = { color = '#F55E5E', consoleTab = 'Server Log', labelId = 'centerWarning', wrap = true },
  10. infoDescription = { color = '#00EB00', consoleTab = 'Server Log', labelId = 'centerInfo', consoleOption = 'showInfoMessagesInConsole', wrap = true },
  11. eventAdvance = { color = '#FFFFFF', consoleTab = 'Server Log', labelId = 'centerAdvance', consoleOption = 'showEventMessagesInConsole', wrap = true },
  12. eventDefault = { color = '#FFFFFF', consoleTab = 'Server Log', labelId = 'bottomStatus', consoleOption = 'showEventMessagesInConsole' },
  13. statusDefault = { color = '#FFFFFF', consoleTab = 'Server Log', labelId = 'bottomStatus', consoleOption = 'showStatusMessagesInConsole' },
  14. statusSmall = { color = '#FFFFFF', labelId = 'bottomStatus' },
  15. }
  16. local centerTextMessagePanel
  17. local centerLabel
  18. -- private functions
  19. local function displayMessage(msgtype, msg, time)
  20. if not Game.isOnline() then return end
  21. if msgtype.consoleTab ~= nil then
  22. if msgtype.consoleOption == nil or Options[msgtype.consoleOption] then
  23. Console.addText(msg, msgtype, msgtype.consoleTab)
  24. end
  25. end
  26. if msgtype.labelId then
  27. local label = Game.gameMapPanel:recursiveGetChildById(msgtype.labelId)
  28. label:setVisible(true)
  29. label:setText(msg)
  30. label:setColor(msgtype.color)
  31. label:resizeToText()
  32. if msgtype.wrap then
  33. label:setWidth(label:getParent():getWidth())
  34. label:wrapText()
  35. label:setHeight(label:getTextSize().height)
  36. end
  37. if not time then
  38. time = math.max(#msg * 100, 4000)
  39. else
  40. time = time * 1000
  41. end
  42. removeEvent(label.hideEvent)
  43. label.hideEvent = scheduleEvent(function() label:setVisible(false) end, time)
  44. end
  45. end
  46. local function createTextMessageLabel(id, parent)
  47. local label = createWidget('UILabel', parent)
  48. label:setFont('verdana-11px-rounded')
  49. label:setTextAlign(AlignCenter)
  50. label:setId(id)
  51. label:setMarginBottom(2)
  52. label:setVisible(false)
  53. return label
  54. end
  55. -- public functions
  56. function TextMessage.create()
  57. centerTextMessagePanel = createWidget('Panel', Game.gameMapPanel)
  58. centerTextMessagePanel:setId('centerTextMessagePanel')
  59. local layout = UIVerticalLayout.create(centerTextMessagePanel)
  60. layout:setFitChildren(true)
  61. centerTextMessagePanel:setLayout(layout)
  62. centerTextMessagePanel:setWidth(360)
  63. centerTextMessagePanel:centerIn('parent')
  64. createTextMessageLabel('centerWarning', centerTextMessagePanel)
  65. createTextMessageLabel('centerAdvance', centerTextMessagePanel)
  66. createTextMessageLabel('centerInfo', centerTextMessagePanel)
  67. bottomStatusLabel = createTextMessageLabel('bottomStatus', Game.gameMapPanel)
  68. bottomStatusLabel:setHeight(16)
  69. bottomStatusLabel:addAnchor(AnchorBottom, 'parent', AnchorBottom)
  70. bottomStatusLabel:addAnchor(AnchorLeft, 'parent', AnchorLeft)
  71. bottomStatusLabel:addAnchor(AnchorRight, 'parent', AnchorRight)
  72. end
  73. function TextMessage.displayStatus(msg, time)
  74. displayMessage(MessageTypes.warning, msg)
  75. end
  76. function TextMessage.displayEventAdvance(msg, time)
  77. displayMessage(MessageTypes.eventAdvance, msg, time)
  78. end
  79. function TextMessage.display(msgtypedesc, msg)
  80. local msgtype = MessageTypes[msgtypedesc]
  81. if msgtype then
  82. displayMessage(msgtype, msg)
  83. end
  84. end
  85. -- hooked events
  86. local function onGameDeath()
  87. local advanceLabel = Game.gameMapPanel:recursiveGetChildById('centerAdvance')
  88. if advanceLabel:isVisible() then return end
  89. TextMessage.displayEventAdvance('You are dead.')
  90. end
  91. local function onGameTextMessage(msgtypedesc, msg)
  92. TextMessage.display(msgtypedesc, msg)
  93. end
  94. connect(Game, { onGameStart = TextMessage.create,
  95. onGameEnd = TextMessage.destroy,
  96. onDeath = onGameDeath,
  97. onTextMessage = onGameTextMessage })