How can I create a sprite with relative path? I mean I have several folder in Resources directory such as:
sd
hd
....
One of this directories is set as the place where resources should be looked up :
std::vector<std::string> resDirOrders;
if (device is hd)
resDirOrders.push_back("hd")
FileUtils::getInstance()->setSearchResolutionsOrder(resDirOrders);
Now in each of above mentioned directories I have lots of other directories such as:
intro_popup
outro_popup
main_menu
top_bar
ingame
....
In these directories I place the images. And the names of images can collide, as far as I can have coin.png in main_menu, in top_bar in ingame which are different images. Theretofore, I want to be able to create a sprite like this:
Sprite::create("ingame/coin.png");
Sprite::create("top_bar/coin.png");
But it does not work. It just does not find the file coin.png.
How I should solve this issue? I am on Windows with cocos2d-x 3.0, but it should be handled on iOS and Android too.
The folder structure must be like this :
Res/
----hd/
----sd/
----ingame/
----hd/
----sd/
----top_bar/
----hd/
----sd/
You should add the Res folder to the project, and make sure that "Create folder references for any added folders" is checked.
Set the search path and search resolution order:
Size screenSize = Director::getInstance()->getOpenGLView()->getFrameSize();
std::vector<std::string> resDirOrders;
std::vector<std::string> searchPaths = FileUtils::getInstance()->getSearchPaths();
searchPaths.insert(searchPaths.begin(), "Res");
FileUtils::getInstance()->setSearchPaths(searchPaths);
if (screenSize.width > 1024) {
resDirOrders.push_back("hd");
}
else{
resDirOrders.push_back("sd");
}
FileUtils::getInstance()->setSearchResolutionsOrder(resDirOrders);
Then you can create the sprite with Sprite::create("ingame/coin.png");
Related
In my QT app I allowed users to load .qss stylesheets as a custom theme.
This however is problematic since they could not load images in the styles initially.
After a bit of digging it happens that relative paths in the stylesheet are resolved towards the current working dir, and not towards the stylesheet path (which QT cannot know since we load it as a string).
So the only "hack" that I could come up with is to change the current working path to the folder where the stylesheet is located, like so:
if (KreatorSettings::instance().customTheme().isNull() == false) {
QString qsFileName(KreatorSettings::instance().customTheme());
QFile qfFile(qsFileName);
if (qfFile.exists()) {
if (qfFile.open(QFile::ReadOnly | QFile::Text)) {
QDir::setCurrent(QFileInfo(qsFileName).absolutePath()); // needed
qApp->setStyleSheet(QLatin1String(qfFile.readAll()));
loadedCustom = true;
}
}
This is an example of how the images are defined in the stylesheet:
QCheckBox::indicator:checked
{
image:url(icon_checkbox_checked.png);
}
This is obviously not the right way, since I need to keep that current working path for the "skin" to work.
Is there a way in qss to use path relative to the stylesheet or some other method to use paths that do not require either embedding resources or changing the current working dir? Maybe we can tell QT what is the stylesheet root folder so it resolves from there? I have no clue, so need your help.
I would hope there is something like in CSS, or else the QT team made some major mistake in the design making the *.qss files unfit for user customization.
I am a beginner with cocos2d-x and try to use it with Tiled to create maps.
I created a TileMap, and here is my code, in LevelOne::init() in level_one.cpp :
if (!CCLayer::init())
{
return false;
}
_tileMap = new CCTMXTiledMap();
_tileMap->initWithTMXFile("levelone.tmx");
this->addChild(_tileMap);
return true;
The debugger allows me to see that the variable _tileMap contains well (at least a part) of the information in my levelone.tmx file.
But when i run it, got a black screen.
Here is the project on github : https://github.com/LeopoldBriand-bot/Platformer
what would I have misunderstood?
Thanks.
I figure it out: Tiles folder should be on th same disk than .tmx file to have relative path and both should be in Ressource folder.
I am populating a tree control with directories and subdirectories from a ftp server. I already have the code working but just cant get the full path.
How do I get the full path all the way to root of a selected sub directory when I double click the tree item. Am I going to have to manually do this?
ex..
ROOT
FOLDER A 1
FOLDER A 2
FOLDER B 1
So if I click on FOLDER A 2, how can I get the path /ROOT/FOLDER A 1/FOLDER A 2
A CTreeCtrl provides basic control functionality. Implementing the business logic is on you. To get a path to the root item, you will have to concatenate the individual path parts. You can do this at different times:
When populating the control
A tree item allows you to store custom data alongside the display text. When populating the tree control you can store a pointer to your data (e.g. a string with the full path) by setting the lParam parameter in a call to CTreeCtrl::InsertItem. You can later retrieve it calling CTreeCtrl::GetItemData.
On demand
You can alternatively construct the full path on demand by traversing the tree structure all the way to the root element, concatenating each path part along the way. The following code serves as a starting point:
CStringW PathFromItem(CTreeCtrl const& tree, HTREEITEM hItem)
{
CStringW path{ tree.GetItemText(hItem) };
while (hItem = tree.GetParentItem(hItem))
{
path = tree.GetItemText(hItem) + L"\\" + path;
}
path = tree.GetItemText(tree.GetRootItem()) + L"\\" + path;
return path;
}
Alternative
As an alternative you should evaluate, whether CMFCShellTreeCtrl works for you. It displays a hierarchy of Shell items, and can be confined to including file system items only.
I'm using the code below to build a qstringlist of filenames:
QStringList filenames = QFileDialog::getOpenFileNames(this,"",QDir::currentPath() );
How can I change this so I can select directories as well?
I looked at:
dialog.setFileMode(QFileDialog::AnyFile);
but I don't get how to use it with my code.
This code snippet linked in the comment above solves my issue.
QFileDialog* _f_dlg = new QFileDialog(this);
_f_dlg->setFileMode(QFileDialog::Directory);
_f_dlg->setOption(QFileDialog::DontUseNativeDialog, true);
// Try to select multiple files and directories at the same time in QFileDialog
QListView *l = _f_dlg->findChild<QListView*>("listView");
if (l) {
l->setSelectionMode(QAbstractItemView::MultiSelection);
}
QTreeView *t = _f_dlg->findChild<QTreeView*>();
if (t) {
t->setSelectionMode(QAbstractItemView::MultiSelection);
}
int nMode = _f_dlg->exec();
QStringList _fnames = _f_dlg->selectedFiles();
I tried this, but the result in my case is a bit strange: I can select a combination of folders and files, as long as the first selected item is a folder.
So when I select a folder, then a file and again a folder, I can proceed clicking the button and retrieving the results: see screenshot in link below.
First folder selected, then file: OK
However, when the first item is a file (followed by a folder, or just a file), the button to proceed is not available... So just selecting one or multiple files is not available to me in this implementation it seems, as can be seen in the other screenshot:
First file selected, then folder: not able to proceed
Is there any way using the same code using QFileDialog that allows you to
select one or more files without selecting folders
selecting one or more folders without selecting files
selecting a combination of files and folders regardless of selection order
I wrote this on official forums of Qt, but it seems dead, so I am going to copy-paste it here.
I am writing small program for copying files. I use QTreeView and I have inherited from QFileSystemModel, so I was able to add checkboxes to every row in the QTreeView. I also use setNameFilters method connected with QLineEdit, so user can specify what file extensions he wants to display in the QTreeView. I have spotted the following behavior:
1) When I run the program and enter extensions to filter (without touching any node from the QTreeView) everything works fine and files with extensions I have provided are only displayed (and folders of course). When I change the extensions and the view is refreshed, on my "C:/" drive everything is updated and only new set of extensions is displayed. When I expand some other drive that I didn’t touch before, it also shows files correctly.
2) When I run the program and expand let say my "C:/" and "D:/" drives I see all directories and files (expected behavior). Then I write some extensions and the view is refreshed. I expand "C:/" drive and everything works fine, only files with extensions I have provided are displayed. Then I go to "D:/" drive and here is the problem. It displays all files. It ignores the filters I have provided. When I open the "E:/" drive that I have not opened before, the files are filtered correctly as in "C:/" drive.
I have concluded, that this behavior has something to do with setRootPath method, because for my QTreeView only in "C:/" drive the filters are working correctly. All other drives that were expanded before change of filters don’t work. Those not expanded work just fine.
The question is: How to get this working, so after user changes the filters and reset() method is fired, the whole QTreeView is refreshed and not only root path and not-expanded elements? Maybe there exists some root path that have all the drives as children and it will work as expected? Or maybe I should make some virtual folder in the QTreeView called "MyComputer" and set it to be a parent for all the drives? But how to get list of all the available drives?
I hope that what I wrote is clear for you and you can help me to get this working.
Edit:
Adding some code that is relevant. If you need more just ask.
//setting up the model and view
QString rPath = "C:/";
rTree_model = new TreeModel(this); //TreeModel inherits from QFileSystemModel
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model); //applies the model for the qtreeview (ui->rTree)
//(...)
//action when extensions were provided by user
QString extensions = QString(ui->extensionBox->text()); //gets extensions provided by user
QStringList filters;
if(extensions.length() > 0) {
filters = extensions.split(";", QString::SkipEmptyParts); //splits extensions provided with ';' as separator
rTree_model->setNameFilters(filters); //applies filters
ui->rTree->reset(); //resets the view
}
Try changing your root path to My Computer instead of C:/. It seems to work with QFileSystemModel in Windows 7 x64 and Qt 4.8.2, but I can't guarantee anything for other platforms.
rTree_model = new TreeModel(this);
QString rPath = model->myComputer().toString(); //causes the QFileSystemWatcher to watch every drive?
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model);