Comments (12)
Hey!
You can retrieve the Text
-object by going into your object layer where the text is, and get it using its name.
If your text object has an unique name, you can use the firstObj("<yourname>")
function of the object layer, then get the "color" property as a tson::Colori
. Here is an example using the very first example map ultimate_test.json
:
tson::Colori color = map->getLayer("Object Layer")->firstObj("text")->get<tson::Colori>("color");
From here you can use the color.r/color.g/color.r/color.b/color.r/color.a
as you like. You can also use the color.asFloat()
function, if you need to convert it to something representing a value from 0 to 1, commonly used with OpenGL
get<T>("<property name>")
can be used for any property, but you need to know what the property represents.
Hope this helps
from tileson.
Hey, thanks for the assistance!
So I still couldn't get it to work, but I discovered something while trying to mess around. Using ->getText()-> you have access to "text" and "wrap" and why not color? What I think is happening is I'm trying to access color, but I have to access the text list first?
Under the JSON file for the text object it says:
"text": {"color":"#00ff00", "text":"Test Map", "wrap":true" },
So I'm thinking that could be the problem?
My text is on a GameObjects layer, I think you can only put them on GameObject layers, to begin with.
Name is "TestText"
Font Text is "Test Map"
Code
`auto gameObjects = m_map->getLayer("GameObjects");
if (gameObjects != nullptr) {
auto c = gameObjects->firstObj("TestText")->get<tson::Colori>("color"); // also tried Color to make sure about case sensitive.
std::cout << gameObjects->firstObj("TestText")->getText().text << "\n"; // Correct
auto color = sf::Color{ c.r,c.g,c.b };
std::cout << color.r << ", " << color.g << ", " << color.b << "\n"; // " , , "
m_text.setFillColor(color);
}`
Results: No font is shown.
from tileson.
I looked into the details, and you are right. There is something missing!
The reason why is that it is missing in the Tiled
json specification (which has been the base for Tileson
):
https://doc.mapeditor.org/en/stable/reference/json-map-format/#text
When I look at the first example map, I see that there is an additional property for color
which I must have missed:
I'll look into this asap, and get back to you. Tileson
is as good as done for its 1.2.0
release, but I'll make sure this makes it into version1.2.0
as well.
Thank you very much for reporting! I'll get back to you
from tileson.
Great!
I thought I was going crazy!
from tileson.
Nope, you were right all along!
This is why reporting issues is important
Anyways, I just pushed a fix for this into master. A little copy/paste from my integration test:
tson::Colori color = map->getLayer("Object Layer")->firstObj("text")->getText().color;
REQUIRE(color.r == 254);
REQUIRE(color.g == 254);
REQUIRE(color.b == 254);
REQUIRE(color.a == 255);
Can you verify that this fixes your problem?
from tileson.
Yay! It works! Thanks!
Few more questions if you don't mind. My computer doesn't like the u8strings used in Tileson::Parse for the error message and ProjectFolder::loadData(). So I had to go and change that and I just changed them to string. It says cannot cast from std::basic_string<char8_t> to std::basic_string via functional cast.
Secondly,
While I was letting you work that out I was experimenting and seeing if I could put a custom property named ColorTest in the JSON file under the TextObject and it wasn't finding anything. I turned it into just an int, and nothing appeared in my cout statement. So I decided to get the vector of properties from the layers and cout them and there was nothing to show. I'm wondering if custom types aren't allowed and there are just predefined properties? I then used the add function from Property to create a Color attribute and it worked fine then. Also, there was a way to retrieve a map version and a vector version. The map version did actually show the name if I added it manually.
Now, I am expanding basically on the example you provide. When you are parsing the file, do I have to do something more to get all the properties in the vector? I'd like to just be about to cout everything it's reading if that makes sense? It was a kind of version of debugging the problem making sure it was even reading the property "Color" at the time. I call it the cout trial and error solution!
I appreciate the prompt fix and I am really liking this library and I appreciate all the work. Thanks and have a great night or day depending on where you are.
from tileson.
Thanks for your kind feedback
For the ProjectFolder::loadData()
-problem, my guess this is a platform implementation issue, which is the very last thing I go through before an actual release, so I wouldn't worry too much about it. I do my development using Linux, but I always make sure the examples are compiling on Windows and OS X before the actual release. Usually I have to fix some minor platform differences like that before a release.
When it comes to adding properties, I have to look on the details, but the library is a parser, so I would guess all property related stuff are defined as read-only by default. With that said: I have no problem allowing the user to add custom properties, if that is a feature that is wanted. It's a very low-risk change that I have no problems making. I'll look into it this weekend.
You should be able to get PropertyCollection
of an object or tile using the getProperties()
. A PropertyCollection
also has a function called getProperties()
, but the one inside the collection returns the actual property map, where the key is the name of the property, and the value is a tson::Property
. Alternatively you can also call the get()
function inside PropertyCollection
to get a list of tson::Property
-pointers to all existing properties
from tileson.
@LedLoaf : Sorry, I think I read your message too quickly and misread some important details
You can actually add whatever custom property you want, but as it stands there is a requirement that you have an existing default/parameterless constructor if you use an own class.
Example:
class CustomProperty
{
public:
CustomProperty() = default;
explicit CustomProperty(std::string str) : m_str{std::move(str)}
{
};
[[nodiscard]] const std::string &getStr() const
{
return m_str;
}
private:
std::string m_str{};
};
TEST_CASE( "Add custom property - expect working", "[property][custom]" ) {
tson::PropertyCollection props;
props.add("james", CustomProperty("CustomValue"), tson::Type::Undefined);
CustomProperty custom = props.getValue<CustomProperty>("james");
REQUIRE( custom.getStr() == "CustomValue" );
}
Does this answer your question?
from tileson.
As for std::cout
-ing everything: It will not work with tson::Color
-objects because it doesn't override the stream output operator (also known as <<
). This could be implemented, though, but it'll be for the next release
from tileson.
Hey SSBMTonberry,
Sorry, been a bit busy. I am a bit confused though. So it should already parse any custom keys from the JSON file correct? For instance, in Tiled where the properties are you can add for example, "num" and assign it as 9. Irrelevant variable, but I'm sure there could be valuable variables.
At the time, I had added "ColorTest" (something like that) as a property, which I actually just assigned an int value to see if it was even reading it. After getting the map of properties, I was unable to find it, or anything for that matter after std::cout'ing
the names and values. It was for sure not even registering it when I was trying to retrieve it.
{
"height":19,
"id":27,
"name":"TestText",
"rotation":0,
"num":9,
"text":
{
"color":"#00ff00",
"text":"Test Map",
"wrap":true
},
"type":"",
"visible":true,
"width":83,
"x":100.625,
"y":60
}
As you can see "num":9,
I added just because I was messing around and saw there could be used for adding values. And the reason I think I can't find any of the properties and std::cout
them is the size is showing up 0. Perhaps it's clearing the vector at some point? I must be doing something wrong.
What I am expecting is all those properties showing up, name, and value. I can't access the vector or map because it's size is 0.
auto prop = m_map->getLayer("GameObjects");
std::cout << "Size: " << prop->getProperties().get().size() << "\n"; // Output: 0
std::cout << "Num: " << prop->getObj(27)->getProperties().getProperty("num") << "\n"; // Output: 0
for (auto& [name, value] : prop->getProperties().getProperties()) { // Size Output: 0
if (value.getType() == tson::Type::String)
std::cout << name << " " << std::any_cast<std::string>(value) << "\n";
else if (value.getType() == tson::Type::Float)
std::cout << name << " " << std::any_cast<float>(value) << "\n";
else if (value.getType() == tson::Type::Int)
std::cout << name << " " << std::any_cast<int>(value) << "\n";
else if (value.getType() == tson::Type::Boolean)
std::cout << name << " " << std::any_cast<bool>(value) << "\n";
else
std::cout << name << " " << std::any_cast<int>(value) << "\n";
}
from tileson.
No problem!
I misunderstood what you tried to achieve, but now I think I get it.
When editing a Tiled json manually, you must remember that Tileson
can only understand what Tiled
would understand.
Neither Tiled
nor Tileson
would have any idea what num
is in your json, because it's not added as a property, but rather as a variable in a tson::Object
. With that said, what you are trying to do is achievable
If you'd want to add num
as a property, you could do something like I've done here:
{
"height":19,
"id":12,
"name":"text",
"properties":[
{
"name":"num",
"type":"int",
"value":9
}],
"rotation":0,
"text":
{
"color":"#fefefe",
"text":"Tileson - Demo Map",
"wrap":true
},
"type":"",
"visible":true,
"width":170,
"x":144,
"y":0
}
Then I just get it like this:
int num = map->getLayer("Object Layer")->firstObj("text")->get<int>("num");
It would otherwise show up in your for-loop like any other property would
The for-loop you have to print the values looks like it will fail as well, if your value
in fact is a tson::Property
.
Remember that you can get a value from a tson::Property
in a safe way like this:
int num = value.getValue<int>();
To be clear: The only property you will get from this example is 1, which is num
. The other stuff are simply not properties
from tileson.
Appreciate it, great! Merry Christmas.
from tileson.
Related Issues (20)
- CMake Issue when building tests on Windows with MinGW/G++
- Tile layer data compression HOT 2
- Tileset::GetImage() returns a space before extension
- Multiple definition of `std::type_info::operator==(std::type_info const&) const` HOT 6
- Add support for Tiled 1.9
- [SUGGESTION] Add a conan package for library HOT 2
- Update README and documentation for v1.4.0
- Access violation when getting the drawing rect of a tile by id HOT 4
- 'tson::World': no appropriate default constructor available HOT 6
- Text object is missing several important fields. HOT 3
- Add an option to be able to read Tiled Projects and its maps through memory.
- Add an option to be able to read Tiled Worlds and its maps through memory.
- Make sure tson::Class objects with modified values are updated.
- tests depend on amalgamated header, but amalgamate tool requires Linux or Windows; no source provided HOT 2
- tests failing using apple clang 13.0.0 HOT 4
- compiler warnings with Apple Clang 13.0.0
- Fix broken CI for Linux (GCC and Clang)
- Add stricter compile flags when building tests HOT 3
- Make sure TiledEnum values can be properly read from Objects and Tiles
- Fix compression errors occuring on Clang 11 and newer HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tileson.