Text.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 LayoutManager = require(SLAB_PATH .. '.Internal.UI.LayoutManager')
  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. local StatHandle = Stats.Begin('Text', 'Slab')
  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. local W = Text.GetWidth(Label)
  36. local H = Style.Font:getHeight()
  37. local PadX = Options.Pad
  38. LayoutManager.AddControl(W + PadX, H)
  39. local Color = Options.Color
  40. local Result = false
  41. local WinId = Window.GetItemId(Label)
  42. local X, Y = Cursor.GetPosition()
  43. local MouseX, MouseY = Window.GetMousePosition()
  44. local IsObstructed = Window.IsObstructedAtMouse()
  45. if not IsObstructed and X <= MouseX and MouseX <= X + W and Y <= MouseY and MouseY <= Y + H then
  46. Window.SetHotItem(WinId)
  47. end
  48. if Options.IsSelectable or Options.IsSelected then
  49. local WinX, WinY, WinW, WinH = Window.GetBounds()
  50. local CheckX = Options.IsSelectableTextOnly and X or WinX
  51. local CheckW = Options.IsSelectableTextOnly and W or WinW
  52. local Hovered = not IsObstructed and CheckX <= MouseX and MouseX <= CheckX + CheckW + PadX and Y <= MouseY and MouseY <= Y + H
  53. if Hovered or Options.IsSelected then
  54. DrawCommands.Rectangle('fill', CheckX, Y, CheckW + PadX, H, Options.HoverColor)
  55. end
  56. if Hovered then
  57. if Options.SelectOnHover then
  58. Result = true
  59. else
  60. if Mouse.IsClicked(1) then
  61. Result = true
  62. end
  63. end
  64. end
  65. end
  66. DrawCommands.Print(Label, math.floor(X + PadX * 0.5), math.floor(Y), Color, Style.Font)
  67. Cursor.SetItemBounds(X, Y, W + PadX, H)
  68. Cursor.AdvanceY(H)
  69. if Options.AddItem then
  70. Window.AddItem(X, Y, W + PadX, H, WinId)
  71. end
  72. Stats.End(StatHandle)
  73. return Result
  74. end
  75. function Text.BeginFormatted(Label, Options)
  76. local StatHandle = Stats.Begin('Textf', 'Slab')
  77. local WinW, WinH = Window.GetBorderlessSize()
  78. Options = Options == nil and {} or Options
  79. Options.Color = Options.Color == nil and Style.TextColor or Options.Color
  80. Options.W = Options.W == nil and WinW or Options.W
  81. Options.Align = Options.Align == nil and 'left' or Options.Align
  82. local Width, Wrapped = Style.Font:getWrap(Label, Options.W)
  83. local H = #Wrapped * Style.Font:getHeight()
  84. LayoutManager.AddControl(Width, H)
  85. local X, Y = Cursor.GetPosition()
  86. DrawCommands.Printf(Label, math.floor(X), math.floor(Y), Width, Options.Align, Options.Color, Style.Font)
  87. Cursor.SetItemBounds(math.floor(X), math.floor(Y), Width, H)
  88. Cursor.AdvanceY(H)
  89. Window.ResetContentSize()
  90. Window.AddItem(math.floor(X), math.floor(Y), Width, H)
  91. Stats.End(StatHandle)
  92. end
  93. function Text.BeginObject(Object, Options)
  94. local StatHandle = Stats.Begin('TextObject', 'Slab')
  95. local WinW, WinH = Window.GetBorderlessSize()
  96. Options = Options == nil and {} or Options
  97. Options.Color = Options.Color == nil and Style.TextColor or Options.Color
  98. local W, H = Object:getDimensions()
  99. LayoutManager.AddControl(W, H)
  100. local X, Y = Cursor.GetPosition()
  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. Stats.End(StatHandle)
  107. end
  108. function Text.GetWidth(Label)
  109. return Style.Font:getWidth(Label)
  110. end
  111. function Text.GetHeight()
  112. return Style.Font:getHeight()
  113. end
  114. function Text.GetSize(Label)
  115. return Style.Font:getWidth(Label), Style.Font:getHeight()
  116. end
  117. function Text.GetSizeWrap(Label, Width)
  118. local W, Lines = Style.Font:getWrap(Label, Width)
  119. return W, #Lines * Text.GetHeight()
  120. end
  121. function Text.GetLines(Label, Width)
  122. local W, Lines = Style.Font:getWrap(Label, Width)
  123. local Start = 0
  124. for I, V in ipairs(Lines) do
  125. if #V == 0 then
  126. Lines[I] = "\n"
  127. else
  128. local Offset = Start + #V + 1
  129. local Ch = string.sub(Label, Offset, Offset)
  130. if Ch == '\n' then
  131. Lines[I] = Lines[I] .. "\n"
  132. end
  133. end
  134. Start = Start + #Lines[I]
  135. end
  136. if string.sub(Label, #Label, #Label) == '\n' then
  137. table.insert(Lines, "")
  138. end
  139. if #Lines == 0 then
  140. table.insert(Lines, "")
  141. end
  142. return Lines
  143. end
  144. function Text.CreateObject()
  145. return love.graphics.newText(Style.Font)
  146. end
  147. return Text