Text.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. --[[
  2. MIT License
  3. Copyright (c) 2019 Mitchell Davis <coding.jackalope@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. --]]
  20. local Cursor = require(SLAB_PATH .. '.Internal.Core.Cursor')
  21. local DrawCommands = require(SLAB_PATH .. '.Internal.Core.DrawCommands')
  22. local Layout = require(SLAB_PATH .. '.Internal.UI.Layout')
  23. local Mouse = require(SLAB_PATH .. '.Internal.Input.Mouse')
  24. local Stats = require(SLAB_PATH .. '.Internal.Core.Stats')
  25. local Style = require(SLAB_PATH .. '.Style')
  26. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  27. local Text = {}
  28. function Text.Begin(Label, Options)
  29. Stats.Begin('Text')
  30. Options = Options == nil and {} or Options
  31. Options.Color = Options.Color == nil and Style.TextColor or Options.Color
  32. Options.Pad = Options.Pad == nil and 0.0 or Options.Pad
  33. Options.AddItem = Options.AddItem == nil and true or Options.AddItem
  34. Options.HoverColor = Options.HoverColor == nil and Style.TextHoverBgColor or Options.HoverColor
  35. Options.CenterX = Options.CenterX == nil and false or Options.CenterX
  36. local X, Y = Cursor.GetPosition()
  37. local W = Text.GetWidth(Label)
  38. local H = Style.Font:getHeight()
  39. local Color = Options.Color
  40. local Result = false
  41. local PadX = Options.Pad
  42. local WinId = Window.GetItemId(Label)
  43. local MouseX, MouseY = Window.GetMousePosition()
  44. if Options.CenterX then
  45. X = Layout.CenterX(W)
  46. end
  47. local IsObstructed = Window.IsObstructedAtMouse()
  48. if not IsObstructed and X <= MouseX and MouseX <= X + W and Y <= MouseY and MouseY <= Y + H then
  49. Window.SetHotItem(WinId)
  50. end
  51. if Options.IsSelectable or Options.IsSelected then
  52. local WinX, WinY, WinW, WinH = Window.GetBounds()
  53. local CheckX = Options.IsSelectableTextOnly and X or WinX
  54. local CheckW = Options.IsSelectableTextOnly and W or WinW
  55. local Hovered = not IsObstructed and CheckX <= MouseX and MouseX <= CheckX + CheckW + PadX and Y <= MouseY and MouseY <= Y + H
  56. if Hovered or Options.IsSelected then
  57. DrawCommands.Rectangle('fill', CheckX, Y, CheckW + PadX, H, Options.HoverColor)
  58. end
  59. if Hovered then
  60. if Options.SelectOnHover then
  61. Result = true
  62. else
  63. if Mouse.IsClicked(1) then
  64. Result = true
  65. end
  66. end
  67. end
  68. end
  69. DrawCommands.Print(Label, math.floor(X + PadX * 0.5), math.floor(Y), Color, Style.Font)
  70. Cursor.SetItemBounds(X, Y, W + PadX, H)
  71. Cursor.AdvanceY(H)
  72. if Options.AddItem then
  73. Window.AddItem(X, Y, W + PadX, H, WinId)
  74. end
  75. Stats.End('Text')
  76. return Result
  77. end
  78. function Text.BeginFormatted(Label, Options)
  79. Stats.Begin('Textf')
  80. local WinW, WinH = Window.GetBorderlessSize()
  81. Options = Options == nil and {} or Options
  82. Options.Color = Options.Color == nil and Style.TextColor or Options.Color
  83. Options.W = Options.W == nil and WinW or Options.W
  84. Options.Align = Options.Align == nil and 'left' or Options.Align
  85. local X, Y = Cursor.GetPosition()
  86. DrawCommands.Printf(Label, math.floor(X), math.floor(Y), Options.W, Options.Align, Options.Color, Style.Font)
  87. local Width, Wrapped = Style.Font:getWrap(Label, Options.W)
  88. local H = #Wrapped * Style.Font:getHeight()
  89. Cursor.SetItemBounds(math.floor(X), math.floor(Y), Width, H)
  90. Cursor.AdvanceY(H)
  91. Window.ResetContentSize()
  92. Window.AddItem(math.floor(X), math.floor(Y), Width, H)
  93. Stats.End('Textf')
  94. end
  95. function Text.BeginObject(Object, Options)
  96. local WinW, WinH = Window.GetBorderlessSize()
  97. Options = Options == nil and {} or Options
  98. Options.Color = Options.Color == nil and Style.TextColor or Options.Color
  99. local X, Y = Cursor.GetPosition()
  100. local W, H = Object:getDimensions()
  101. DrawCommands.Text(Object, math.floor(X), math.floor(Y), Options.Color)
  102. Cursor.SetItemBounds(math.floor(X), math.floor(Y), W, H)
  103. Cursor.AdvanceY(Y)
  104. Window.ResetContentSize()
  105. Window.AddItem(math.floor(X), math.floor(Y), W, H)
  106. end
  107. function Text.GetWidth(Label)
  108. return Style.Font:getWidth(Label)
  109. end
  110. function Text.GetHeight()
  111. return Style.Font:getHeight()
  112. end
  113. function Text.GetSize(Label)
  114. return Style.Font:getWidth(Label), Style.Font:getHeight()
  115. end
  116. function Text.GetSizeWrap(Label, Width)
  117. local W, Lines = Style.Font:getWrap(Label, Width)
  118. return W, #Lines * Text.GetHeight()
  119. end
  120. function Text.GetLines(Label, Width)
  121. local W, Lines = Style.Font:getWrap(Label, Width)
  122. local Start = 0
  123. for I, V in ipairs(Lines) do
  124. if #V == 0 then
  125. Lines[I] = "\n"
  126. else
  127. local Offset = Start + #V + 1
  128. local Ch = string.sub(Label, Offset, Offset)
  129. if Ch == '\n' then
  130. Lines[I] = Lines[I] .. "\n"
  131. end
  132. end
  133. Start = Start + #Lines[I]
  134. end
  135. if string.sub(Label, #Label, #Label) == '\n' then
  136. table.insert(Lines, "")
  137. end
  138. if #Lines == 0 then
  139. table.insert(Lines, "")
  140. end
  141. return Lines
  142. end
  143. function Text.CreateObject()
  144. return love.graphics.newText(Style.Font)
  145. end
  146. return Text