flamendless / Slab

An immediate mode GUI for the Love2D framework.
MIT License
294 stars 25 forks source link

Bug: Slab fails to load styles when it's not in the top level of a love2d project #30

Closed idbrii closed 4 years ago

idbrii commented 4 years ago

I modify my path in conf.lua to allow me to split my code into two folders: src/ and src/lib/

love.filesystem.setRequirePath("src/?.lua;src/?/init.lua;src/lib/?.lua;src/lib/?/init.lua")

This allows me to import modules from either folder without any prefixes:

local Slab = require 'Slab'

This doesn't work with Slab. Even excluding lib from require path (so it must be specified in includes) doesn't work with Slab.

Investigation

Slab sets its path using the require path:

SLAB_PATH = ...

But styles use this as if it were a filesystem path (that ignores lua's require path):

  local StylePath = "/Internal/Resources/Styles/"
  local Path = SLAB_PATH .. StylePath
  Path = string.gsub(Path, "%.", "/")
  -- Use love's filesystem functions to support both packaged and unpackaged builds
  local Items = love.filesystem.getDirectoryItems(Path)

Styles fail to load and every execution outputs this error:

  Style 'Dark' is not loaded.

So far, nothing looks wrong, but it does mean I can't use styles.

Two solutions I can see is for styles to search for the style folder using love.filesystem.getRequirePath or be lua files that can use require(). My preference is the latter (removes style parser implementation, familiar data format), but I'd assume it's more work (style editor).

Repro example in my repro-style-load-path branch. I moved Slab's files around to replicate a real project. See how I moved things around in idbrii/love-slab@cc89b7e575eecf3d0d004029298f0e00aee01cdd.

sdleffler commented 4 years ago

A similar issue happens when using Slab.FileDialog. Slab crashes because it can't open the folder icon image file. The cause seems to be here, lines 116-122 of Dialog.lua in my local repository:

local function FileDialogItem(Id, Label, IsDirectory, Index)
    ListBox.BeginItem(Id, {Selected = Utility.HasValue(ActiveInstance.Selected, Index)})

    if IsDirectory then
        Image.Begin('FileDialog_Folder', {Path = SLAB_PATH .. "/Internal/Resources/Textures/Folder.png"})
        Cursor.SameLine({CenterY = true})
    end

Changing SLAB_PATH to SLAB_PATH:gsub("%.", "/") fixes the issue. I think it may be a good idea to add a SLAB_FILEPATH = SLAP_PATH:gsub("%.", "/") global to the init.lua or wherever else SLAB_PATH is set, and possibly rename SLAB_PATH to SLAB_MODULE_PATH to avoid confusion.

sdleffler commented 4 years ago

Ah, there's already a PR to deal with this, somewhat: #21

idbrii commented 3 years ago

I can confirm this works for me on an example project with my setRequirePath line above and slab in ./src/lib/slab and in my main project.

~Unfortunately, it doesn't work in my current project, but I guess something else is interfering. I'll have to investigate more later.~ Was missing the Resources folder.

Thanks for fixing!