Handle a ressource path

Started by roket, 04 January 2014, 21:51:55

roket

I made this for me, not to have to put any link in my txt gui descriptions files, I made this feature that allows you to handle a ressourcePath. It could be improved by make it work not only with loaded files.
It's an attribute in more to the Container class, very similar to "m_GlobalFont", I just made setters, no getters, and initialized it to "" in Container constructor.
The only really changed part is in Container.cpp in bool loadWidgetsFromFile(const std::string& filename)
at the end it becomes :

//Using the provided Ressource Path
std::string filepath = line.substr(equalSignPosition + 1);

if(line.substr(0, equalSignPosition) == "configfile" || line.substr(0, equalSignPosition) == "filename")
   filepath = m_RessourcePath + line.substr(equalSignPosition + 1);
                   
if(!widgetPtr.back()->setProperty(line.substr(0, equalSignPosition), filepath))
   failed = true;

Just adding the ressource path if the property is about Filename or ConfigFile

Modified files : Gui.cpp Gui.hpp Container.cpp Container.hpp
https://dl.dropboxusercontent.com/u/89706975/miniChanges.zip

Exemple, this is useful when you use sfml on a mac because apps have very complicated ressource directories.

...
gui.setRessourcePath(resourcePath());
gui.loadWidgetsFromFile(resourcePath()+"form.txt");
...

form.txt becomes :


Window:
{
Picture: ""
{
Filename = "xubuntu_bg_aluminium.jpg"
Left = 0
Top = 0
Width = 800
Height = 600
Visible = true
Enabled = true
Transparency = 255
CallbackId = 0
Smooth = false
}

Label: ""
{
ConfigFile = ""
Left = 200
Top = 100
Width = 161
Height = 30
Visible = true
Enabled = true
Transparency = 255
CallbackId = 0
Text = "Username:"
TextColor = (255,255,255,255)
TextSize = 30
BackgroundColor = (0,0,0,0)
AutoSize = true
}

Label: ""
{
ConfigFile = ""
Left = 200
Top = 250
Width = 150
Height = 30
Visible = true
Enabled = true
Transparency = 255
CallbackId = 0
Text = "Password:"
TextColor = (255,255,255,255)
TextSize = 30
BackgroundColor = (0,0,0,0)
AutoSize = true
}

EditBox: "Username"
{
ConfigFile = "confs/Black.conf"
Left = 200
Top = 140
Width = 400
Height = 40
Visible = true
Enabled = true
Transparency = 255
CallbackId = 0
Text = ""
TextSize = 25
MaximumCharacters = 0
Borders = (6,4,6,4)
TextColor = (200,200,200,255)
SelectedTextColor = (255,255,255,255)
SelectedTextBackgroundColor = (10,110,255,255)
SelectionPointColor = (110,110,255,255)
LimitTextWidth = false
SelectionPointWidth = 2
NumbersOnly = false
}

EditBox: "Password"
{
ConfigFile = "confs/Black.conf"
Left = 200
Top = 290
Width = 400
Height = 40
Visible = true
Enabled = true
Transparency = 255
CallbackId = 0
Text = ""
TextSize = 25
PasswordCharacter = *
MaximumCharacters = 0
Borders = (6,4,6,4)
TextColor = (200,200,200,255)
SelectedTextColor = (255,255,255,255)
SelectedTextBackgroundColor = (10,110,255,255)
SelectionPointColor = (110,110,255,255)
LimitTextWidth = false
SelectionPointWidth = 2
NumbersOnly = false
}

Button: ""
{
ConfigFile = "confs/Black.conf"
Left = 270
Top = 440
Width = 260
Height = 60
Visible = true
Enabled = true
Transparency = 255
Callback = LeftMouseClicked
CallbackId = 1
Text = "Login"
TextColor = (200,200,200,255)
TextSize = 40
}

}


texus

Thanks.

On mac I tend to not use the resourcePath() like the sfml examples but to just put this at the beginning of the file:
Code (c++) Select
#ifdef __APPLE__
    #include "CoreFoundation/CoreFoundation.h"

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
        return 1;

    CFRelease(resourcesURL);

    chdir(path);
#endif


But using resource paths will probably make it easier either way, and could also be used for other things. So I'll definately include this in the v0.6.0 release.

roket

Thanks for the code tip!
It's been a long tile that I didn't code on a mac and with the earlier systems it became quite awfull managing links....

texus

#3
The change didn't make it into the v0.6.0 release after all, but it will be included in v0.6.1.
It is already available in the latest version on github.

I had already forgotten that you had implemented part of it so I implemented it myself. But its good that you provided the changed code because I had implemented everything except loading from a widgets file :).

Thanks for suggestion this feature.

Blog post on resource paths

roket

My pleasure, pleased that it's useful