123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- --[[
- MIT License
- Copyright (c) 2019 Mitchell Davis <coding.jackalope@gmail.com>
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- --]]
- local Slab = require(SLAB_PATH .. '.Slab')
- local DrawCommands = require(SLAB_PATH .. '.Internal.Core.DrawCommands')
- local Input = require(SLAB_PATH .. '.Internal.UI.Input')
- local Keyboard = require(SLAB_PATH .. '.Internal.Input.Keyboard')
- local Mouse = require(SLAB_PATH .. '.Internal.Input.Mouse')
- local Region = require(SLAB_PATH .. '.Internal.UI.Region')
- local Stats = require(SLAB_PATH .. '.Internal.Core.Stats')
- local Style = require(SLAB_PATH .. '.Style')
- local Tooltip = require(SLAB_PATH .. '.Internal.UI.Tooltip')
- local Window = require(SLAB_PATH .. '.Internal.UI.Window')
- local SlabDebug = {}
- local SlabDebug_About = 'SlabDebug_About'
- local SlabDebug_Mouse = false
- local SlabDebug_Keyboard = false
- local SlabDebug_Windows = false
- local SlabDebug_Regions = false
- local SlabDebug_Tooltip = false
- local SlabDebug_DrawCommands = false
- local SlabDebug_Performance = false
- local SlabDebug_StyleEditor = false
- local SlabDebug_Input = false
- local SlabDebug_MultiLine = false
- local SlabDebug_MultiLine_FileDialog = false
- local SlabDebug_MultiLine_FileName = ""
- local SlabDebug_MultiLine_Contents = ""
- local SlabDebug_Windows_Categories = {"Inspector", "Stack"}
- local SlabDebug_Windows_Category = "Inspector"
- local SlabDebug_Regions_Selected = ""
- local Selected_Window = ""
- local Style_EditingColor = nil
- local Style_ColorStore = nil
- local Style_FileDialog = nil
- local function Window_Inspector()
- local Ids = Window.GetInstanceIds()
- if Slab.BeginComboBox('SlabDebug_Windows_Inspector', {Selected = Selected_Window}) then
- for I, V in ipairs(Ids) do
- if Slab.TextSelectable(V) then
- Selected_Window = V
- end
- end
- Slab.EndComboBox()
- end
- local Info = Window.GetInstanceInfo(Selected_Window)
- for I, V in ipairs(Info) do
- Slab.Text(V)
- end
- end
- local function Window_Stack()
- local Stack = Window.GetStackDebug()
- Slab.Text("Stack: " .. #Stack)
- for I, V in ipairs(Stack) do
- Slab.Text(V)
- end
- end
- local function DrawCommands_Item(Root, Label)
- if type(Root) == "table" then
- if Slab.BeginTree(Label) then
- for K, V in pairs(Root) do
- DrawCommands_Item(V, K)
- end
- Slab.EndTree()
- end
- else
- Slab.BeginTree(Label .. " " .. tostring(Root), {IsLeaf = true})
- end
- end
- local DrawPerformance_Category = nil
- local DrawPerformance_WinX = 50.0
- local DrawPerformance_WinY = 50.0
- local DrawPerformance_ResetPosition = false
- local DrawPerformance_Init = false
- local DrawPerformance_W = 200.0
- local function DrawPerformance()
- if not DrawPerformance_Init then
- Slab.EnableStats(true)
- DrawPerformance_Init = true
- end
- Slab.BeginWindow('SlabDebug_Performance', {
- Title = "Performance",
- X = DrawPerformance_WinX,
- Y = DrawPerformance_WinY,
- ResetPosition = DrawPerformance_ResetPosition
- })
- DrawPerformance_ResetPosition = false
- local Categories = Stats.GetCategories()
- if DrawPerformance_Category == nil then
- DrawPerformance_Category = Categories[1]
- end
- if Slab.BeginComboBox('DrawPerformance_Categories', {Selected = DrawPerformance_Category, W = DrawPerformance_W}) then
- for I, V in ipairs(Categories) do
- if Slab.TextSelectable(V) then
- DrawPerformance_Category = V
- end
- end
- Slab.EndComboBox()
- end
- if Slab.CheckBox(Slab.IsStatsEnabled(), "Enabled") then
- Slab.EnableStats(not Slab.IsStatsEnabled())
- end
- Slab.SameLine()
- if Slab.Button("Flush") then
- Slab.FlushStats()
- end
- Slab.Separator()
- if DrawPerformance_Category ~= nil then
- local Items = Stats.GetItems(DrawPerformance_Category)
- local Pad = 50.0
- local MaxW = 0.0
- for I, V in ipairs(Items) do
- MaxW = math.max(MaxW, Slab.GetTextWidth(V))
- end
- local CursorX, CursorY = Slab.GetCursorPos()
- Slab.SetCursorPos(MaxW * 0.5 - Slab.GetTextWidth("Stat") * 0.5)
- Slab.Text("Stat")
- local TimeX = MaxW + Pad
- local TimeW = Slab.GetTextWidth("Time")
- local TimeItemW = Slab.GetTextWidth(string.format("%.4f", 0.0))
- Slab.SetCursorPos(TimeX, CursorY)
- Slab.Text("Time")
- local MaxTimeX = TimeX + TimeW + Pad
- local MaxTimeW = Slab.GetTextWidth("Max Time")
- Slab.SetCursorPos(MaxTimeX, CursorY)
- Slab.Text("Max Time")
- local CallCountX = MaxTimeX + MaxTimeW + Pad
- local CallCountW = Slab.GetTextWidth("Call Count")
- Slab.SetCursorPos(CallCountX, CursorY)
- Slab.Text("Call Count")
- DrawPerformance_W = CallCountX + CallCountW
- Slab.Separator()
- for I, V in ipairs(Items) do
- local Time = Stats.GetTime(V, DrawPerformance_Category)
- local MaxTime = Stats.GetMaxTime(V, DrawPerformance_Category)
- local CallCount = Stats.GetCallCount(V, DrawPerformance_Category)
- CursorX, CursorY = Slab.GetCursorPos()
- Slab.SetCursorPos(MaxW * 0.5 - Slab.GetTextWidth(V) * 0.5)
- Slab.Text(V)
- Slab.SetCursorPos(TimeX + TimeW * 0.5 - TimeItemW * 0.5, CursorY)
- Slab.Text(string.format("%.4f", Time))
- Slab.SetCursorPos(MaxTimeX + MaxTimeW * 0.5 - TimeItemW * 0.5, CursorY)
- Slab.Text(string.format("%.4f", MaxTime))
- Slab.SetCursorPos(CallCountX + CallCountW * 0.5 - Slab.GetTextWidth(CallCount) * 0.5, CursorY)
- Slab.Text(CallCount)
- end
- end
- Slab.EndWindow()
- end
- local function EditColor(Color)
- Style_EditingColor = Color
- Style_ColorStore = {Color[1], Color[2], Color[3], Color[4]}
- end
- local function DrawStyleEditor()
- Slab.BeginWindow('SlabDebug_StyleEditor', {Title = "Style Editor", AutoSizeWindow = false, AllowResize = true, W = 700.0, H = 500.0})
- local Style = Slab.GetStyle()
- local Names = Style.API.GetStyleNames()
- local CurrentStyle = Style.API.GetCurrentStyleName()
- Slab.BeginLayout('SlabDebug_StyleEditor_Styles_Layout', {ExpandW = true})
- if Slab.BeginComboBox('SlabDebug_StyleEditor_Styles', {Selected = CurrentStyle}) then
- for I, V in ipairs(Names) do
- if Slab.TextSelectable(V) then
- Style.API.SetStyle(V)
- end
- end
- Slab.EndComboBox()
- end
- if Slab.Button("New") then
- Style_FileDialog = 'new'
- end
- Slab.SameLine()
- if Slab.Button("Load") then
- Style_FileDialog = 'load'
- end
- Slab.SameLine()
- local SaveDisabled = Style.API.IsDefaultStyle(CurrentStyle)
- if Slab.Button("Save", {Disabled = SaveDisabled}) then
- Style.API.SaveCurrentStyle()
- end
- Slab.EndLayout()
- Slab.Separator()
- Slab.BeginLayout('SlabDebug_StyleEditor_Content_Layout', {Columns = 2, ExpandW = true})
- for K, V in pairs(Style) do
- if type(V) == "table" and K ~= "Font" and K ~= "API" then
- Slab.SetLayoutColumn(1)
- Slab.Text(K)
- Slab.SetLayoutColumn(2)
- local W, H = Slab.GetLayoutSize()
- H = Slab.GetTextHeight()
- Slab.Rectangle({W = W, H = H, Color = V, Outline = true})
- if Slab.IsControlClicked() then
- EditColor(V)
- end
- end
- end
- for K, V in pairs(Style) do
- if type(V) == "number" and K ~= "FontSize" then
- Slab.SetLayoutColumn(1)
- Slab.Text(K)
- Slab.SetLayoutColumn(2)
- if Slab.Input('SlabDebug_Style_' .. K, {Text = tostring(V), ReturnOnText = false, NumbersOnly = true}) then
- Style[Label] = Slab.GetInputNumber()
- end
- end
- end
- Slab.EndLayout()
- Slab.EndWindow()
- if Style_EditingColor ~= nil then
- local Result = Slab.ColorPicker({Color = Style_ColorStore})
- Style_EditingColor[1] = Result.Color[1]
- Style_EditingColor[2] = Result.Color[2]
- Style_EditingColor[3] = Result.Color[3]
- Style_EditingColor[4] = Result.Color[4]
- if Result.Button ~= "" then
- if Result.Button == "OK" then
- Style.API.StoreCurrentStyle()
- end
- if Result.Button == "Cancel" then
- Style_EditingColor[1] = Style_ColorStore[1]
- Style_EditingColor[2] = Style_ColorStore[2]
- Style_EditingColor[3] = Style_ColorStore[3]
- Style_EditingColor[4] = Style_ColorStore[4]
- end
- Style_EditingColor = nil
- end
- end
- if Style_FileDialog ~= nil then
- local Type = Style_FileDialog == 'new' and 'savefile' or Style_FileDialog == 'load' and 'openfile' or nil
- if Type ~= nil then
- local Path = love.filesystem.getRealDirectory(SLAB_PATH) .. "/" .. SLAB_PATH .. "Internal/Resources/Styles"
- local Result = Slab.FileDialog({AllowMultiSelect = false, Directory = Path, Type = Type, Filters = {{"*.style", "Styles"}}})
- if Result.Button ~= "" then
- if Result.Button == "OK" then
- if Style_FileDialog == 'new' then
- Style.API.CopyCurrentStyle(Result.Files[1])
- else
- Style.API.LoadStyle(Result.Files[1], true)
- end
- end
- Style_FileDialog = nil
- end
- else
- Style_FileDialog = nil
- end
- end
- end
- function SlabDebug.About()
- if Slab.BeginDialog(SlabDebug_About, {Title = "About"}) then
- Slab.Text("Slab Version: " .. Slab.GetVersion())
- Slab.Text("Love Version: " .. Slab.GetLoveVersion())
- Slab.NewLine()
- Slab.BeginLayout(SlabDebug_About .. '.Buttons_Layout', {AlignX = 'center'})
- if Slab.Button("OK") then
- Slab.CloseDialog()
- end
- Slab.EndLayout()
- Slab.EndDialog()
- end
- end
- function SlabDebug.OpenAbout()
- Slab.OpenDialog(SlabDebug_About)
- end
- function SlabDebug.Mouse()
- Slab.BeginWindow('SlabDebug_Mouse', {Title = "Mouse"})
- local X, Y = Mouse.Position()
- Slab.Text("X: " .. X)
- Slab.Text("Y: " .. Y)
- local DeltaX, DeltaY = Mouse.GetDelta()
- Slab.Text("Delta X: " .. DeltaX)
- Slab.Text("Delta Y: " .. DeltaY)
- for I = 1, 3, 1 do
- Slab.Text("Button " .. I .. ": " .. (Mouse.IsPressed(I) and "Pressed" or "Released"))
- end
- Slab.Text("Hot Region: " .. Region.GetHotInstanceId())
- Slab.EndWindow()
- end
- function SlabDebug.Keyboard()
- Slab.BeginWindow('SlabDebug_Keyboard', {Title = "Keyboard"})
- Slab.BeginLayout('SlabDebug_Keyboard', {Columns = 2})
- local Keys = Keyboard.Keys()
- for I, V in ipairs(Keys) do
- Slab.SetLayoutColumn(1)
- Slab.Text(V)
- Slab.SetLayoutColumn(2)
- Slab.Text(tostring(Keyboard.IsDown(V)))
- end
- Slab.EndLayout()
- Slab.EndWindow()
- end
- function SlabDebug.Windows()
- Slab.BeginWindow('SlabDebug_Windows', {Title = "Windows"})
- if Slab.BeginComboBox('SlabDebug_Windows_Categories', {Selected = SlabDebug_Windows_Category}) then
- for I, V in ipairs(SlabDebug_Windows_Categories) do
- if Slab.TextSelectable(V) then
- SlabDebug_Windows_Category = V
- end
- end
- Slab.EndComboBox()
- end
- if SlabDebug_Windows_Category == "Inspector" then
- Window_Inspector()
- elseif SlabDebug_Windows_Category == "Stack" then
- Window_Stack()
- end
- Slab.EndWindow()
- end
- function SlabDebug.Regions()
- Slab.BeginWindow('SlabDebug_Regions', {Title = "Regions"})
- local Ids = Region.GetInstanceIds()
- if Slab.BeginComboBox('SlabDebug_Regions_Ids', {Selected = SlabDebug_Regions_Selected}) then
- for I, V in ipairs(Ids) do
- if Slab.TextSelectable(V) then
- SlabDebug_Regions_Selected = V
- end
- end
- Slab.EndComboBox()
- end
- local Info = Region.GetDebugInfo(SlabDebug_Regions_Selected)
- for I, V in ipairs(Info) do
- Slab.Text(V)
- end
- Slab.EndWindow()
- end
- function SlabDebug.Tooltip()
- Slab.BeginWindow('SlabDebug_Tooltip', {Title = "Tooltip"})
- local Info = Tooltip.GetDebugInfo()
- for I, V in ipairs(Info) do
- Slab.Text(V)
- end
- Slab.EndWindow()
- end
- function SlabDebug.DrawCommands()
- Slab.BeginWindow('SlabDebug_DrawCommands', {Title = "Draw Commands"})
-
- local Info = DrawCommands.GetDebugInfo()
- for K, V in pairs(Info) do
- DrawCommands_Item(V, K)
- end
- Slab.EndWindow()
- end
- function SlabDebug.Performance()
- DrawPerformance()
- end
- function SlabDebug.Performance_SetPosition(X, Y)
- DrawPerformance_WinX = X ~= nil and X or 50.0
- DrawPerformance_WinY = Y ~= nil and Y or 50.0
- DrawPerformance_ResetPosition = true
- end
- function SlabDebug.StyleEditor()
- DrawStyleEditor()
- end
- function SlabDebug.Input()
- Slab.BeginWindow('SlabDebug_Input', {Title = "Input"})
- local Info = Input.GetDebugInfo()
- Slab.Text("Focused: " .. Info['Focused'])
- Slab.Text("Width: " .. Info['Width'])
- Slab.Text("Height: " .. Info['Height'])
- Slab.Text("Cursor X: " .. Info['CursorX'])
- Slab.Text("Cursor Y: " .. Info['CursorY'])
- Slab.Text("Cursor Position: " .. Info['CursorPos'])
- Slab.Text("Character: " .. Info['Character'])
- Slab.Text("Line Position: " .. Info['LineCursorPos'])
- Slab.Text("Line Position Max: " .. Info['LineCursorPosMax'])
- Slab.Text("Line Number: " .. Info['LineNumber'])
- Slab.Text("Line Length: " .. Info['LineLength'])
- local Lines = Info['Lines']
- if Lines ~= nil then
- Slab.Text("Lines: " .. #Lines)
- end
- Slab.EndWindow()
- end
- local SlabDebug_MultiLine_Highlight = {
- ['function'] = {1, 0, 0, 1},
- ['end'] = {1, 0, 0, 1},
- ['if'] = {1, 0, 0, 1},
- ['then'] = {1, 0, 0, 1},
- ['local'] = {1, 0, 0, 1},
- ['for'] = {1, 0, 0, 1},
- ['do'] = {1, 0, 0, 1},
- ['not'] = {1, 0, 0, 1},
- ['while'] = {1, 0, 0, 1},
- ['repeat'] = {1, 0, 0, 1},
- ['until'] = {1, 0, 0, 1},
- ['break'] = {1, 0, 0, 1},
- ['else'] = {1, 0, 0, 1},
- ['elseif'] = {1, 0, 0, 1},
- ['in'] = {1, 0, 0, 1},
- ['and'] = {1, 0, 0, 1},
- ['or'] = {1, 0, 0, 1},
- ['true'] = {1, 0, 0, 1},
- ['false'] = {1, 0, 0, 1},
- ['nil'] = {1, 0, 0, 1},
- ['return'] = {1, 0, 0, 1}
- }
- local SlabDebug_MultiLine_ShouldHighlight = true
- function SlabDebug.MultiLine()
- Slab.BeginWindow('SlabDebug_MultiLine', {Title = "Multi-Line Input"})
- if Slab.Button("Load") then
- SlabDebug_MultiLine_FileDialog = true
- end
- Slab.SameLine()
- if Slab.Button("Save", {Disabled = SlabDebug_MultiLine_FileName == ""}) then
- local Handle, Error = io.open(SlabDebug_MultiLine_FileName, "w")
- if Handle ~= nil then
- Handle:write(SlabDebug_MultiLine_Contents)
- Handle:close()
- end
- end
- local ItemW, ItemH = Slab.GetControlSize()
- Slab.SameLine()
- if Slab.CheckBox(SlabDebug_MultiLine_ShouldHighlight, "Use Lua Highlight", {Size = ItemH}) then
- SlabDebug_MultiLine_ShouldHighlight = not SlabDebug_MultiLine_ShouldHighlight
- end
- Slab.Separator()
- Slab.Text("File: " .. SlabDebug_MultiLine_FileName)
- if Slab.Input('SlabDebug_MultiLine', {
- MultiLine = true,
- Text = SlabDebug_MultiLine_Contents,
- W = 500.0,
- H = 500.0,
- Highlight = SlabDebug_MultiLine_ShouldHighlight and SlabDebug_MultiLine_Highlight or nil
- }) then
- SlabDebug_MultiLine_Contents = Slab.GetInputText()
- end
- Slab.EndWindow()
- if SlabDebug_MultiLine_FileDialog then
- local Result = Slab.FileDialog({AllowMultiSelect = false, Type = 'openfile'})
- if Result.Button ~= "" then
- SlabDebug_MultiLine_FileDialog = false
- if Result.Button == "OK" then
- SlabDebug_MultiLine_FileName = Result.Files[1]
- local Handle, Error = io.open(SlabDebug_MultiLine_FileName, "r")
- if Handle ~= nil then
- SlabDebug_MultiLine_Contents = Handle:read("*a")
- Handle:close()
- end
- end
- end
- end
- end
- function SlabDebug.Menu()
- if Slab.BeginMenu("Debug") then
- if Slab.MenuItem("About") then
- SlabDebug.OpenAbout()
- end
- if Slab.MenuItemChecked("Mouse", SlabDebug_Mouse) then
- SlabDebug_Mouse = not SlabDebug_Mouse
- end
- if Slab.MenuItemChecked("Keyboard", SlabDebug_Keyboard) then
- SlabDebug_Keyboard = not SlabDebug_Keyboard
- end
- if Slab.MenuItemChecked("Windows", SlabDebug_Windows) then
- SlabDebug_Windows = not SlabDebug_Windows
- end
- if Slab.MenuItemChecked("Regions", SlabDebug_Regions) then
- SlabDebug_Regions = not SlabDebug_Regions
- end
- if Slab.MenuItemChecked("Tooltip", SlabDebug_Tooltip) then
- SlabDebug_Tooltip = not SlabDebug_Tooltip
- end
- if Slab.MenuItemChecked("Draw Commands", SlabDebug_DrawCommands) then
- SlabDebug_DrawCommands = not SlabDebug_DrawCommands
- end
- if Slab.MenuItemChecked("Performance", SlabDebug_Performance) then
- SlabDebug_Performance = not SlabDebug_Performance
- Stats.SetEnabled(SlabDebug_Performance)
- end
- if Slab.MenuItemChecked("Style Editor", SlabDebug_StyleEditor) then
- SlabDebug_StyleEditor = not SlabDebug_StyleEditor
- end
- if Slab.MenuItemChecked("Input", SlabDebug_Input) then
- SlabDebug_Input = not SlabDebug_Input
- end
- if Slab.MenuItemChecked("Multi-Line", SlabDebug_MultiLine) then
- SlabDebug_MultiLine = not SlabDebug_MultiLine
- end
- Slab.EndMenu()
- end
- end
- function SlabDebug.Begin()
- SlabDebug.About()
- if SlabDebug_Mouse then
- SlabDebug.Mouse()
- end
- if SlabDebug_Keyboard then
- SlabDebug.Keyboard()
- end
- if SlabDebug_Windows then
- SlabDebug.Windows()
- end
- if SlabDebug_Regions then
- SlabDebug.Regions()
- end
- if SlabDebug_Tooltip then
- SlabDebug.Tooltip()
- end
- if SlabDebug_DrawCommands then
- SlabDebug.DrawCommands()
- end
- if SlabDebug_Performance then
- SlabDebug.Performance()
- end
- if SlabDebug_StyleEditor then
- SlabDebug.StyleEditor()
- end
- if SlabDebug_Input then
- SlabDebug.Input()
- end
- if SlabDebug_MultiLine then
- SlabDebug.MultiLine()
- end
- end
- return SlabDebug
|