Author Topic: apply a texture to a group of provinces  (Read 96 times)

Dotecin

  • Newbie
  • *
  • Posts: 6
    • View Profile
apply a texture to a group of provinces
« on: February 08, 2021, 02:21:29 PM »
I apologise if the topic has been already discussed, I could not find it with the search.

Globe Edition.
I would like to apply at runtime a texture (in my case a flag) to a group of provinces, like they would be one area. Then clean it later by calling map.HideProvinceSurfaces() for example, as I usually do for single provinces.
Is there a fairly easy way to achieve this?

I was also thinking to do it by merging the provinces I need, but I could not find a method to restore the original provinces once the user starts a new game.

Thanks in advance
« Last Edit: February 10, 2021, 12:01:41 AM by Kronnect »

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: apply a texture to a group of provinces
« Reply #1 on: February 08, 2021, 04:11:53 PM »
Hi,

To color a province, use any of the ToggleProvinceSurface methods. You can use this method to color or hide a province. Call it any number of times on any provinces, like using the same color to group a few provinces by color (although they will retain their borders).

You can merge a few provinces, and the color or texture the merged province but then there's no way back unless you save the geodata and load it (which you can also do using the API although perhaps this results a bit slow as everything is saved/loaded).

Regards


Dotecin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: apply a texture to a group of provinces
« Reply #2 on: February 08, 2021, 05:18:38 PM »
jep, using colors it's not a problem, but with texture I will not get the effect I'm aiming for, as every province will have the texture mapped its own way (see attachment where I try to highlight the Grand Est region of France). I was thinking to get the effect of the texture over the entire group of provinces, like it would be one area only.

But it's ok then, I can live with using colors.

thanks for the fast response!


Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: apply a texture to a group of provinces
« Reply #3 on: February 10, 2021, 12:03:07 AM »
It's certainly doable. The API currently maps the texture to the enclosing rectangle of the province or country. We could add a custom method overload that takes an array or list of provinces and apply the texture to the boundary of all provinces.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: apply a texture to a group of provinces
« Reply #4 on: February 12, 2021, 10:44:33 AM »
After further investigation it looks like assigning a texture to a group of provinces is not optimal since:
1) Provinces could include several regions.
2) They would still count as individual provinces so borders and highlight still work on them individually.

We opted to add new APIs to latest beta:
- ProvincesMerge which does the job of merging a group of provinces.
- SetProvinceGeoData method that allows you to set the province data previously captured with GetProvinceGeoData in case you want to unmerge them.

For example:

Step 1) Show Spain provinces:
Code
map.DrawProvince(map.GetCountryIndex("Spain"), false, false);

Step 2) Assume you want to merge 3 provinces of Spain (Madrid, Ciudad Real and Toledo). Because we want to unmerge them later, we store the province geodata in a temporary variable:
Code
string data = map.GetProvinceGeoData();

Step 3) Now we merge the 3 provinces:
Code
List<Province> provinces = new List<Province>();
provinces.Add(map.GetProvince("Spain", "Madrid"));
provinces.Add(map.GetProvince("Spain", "Toledo"));
provinces.Add(map.GetProvince("Spain", "Ciudad Real"));
map.ProvincesMerge(provinces, true); // <-- true means issue a redraw after the merge

The new province could be colorized or textured as always and highlight or borders now reflect the combined surfaces of the 3 former provinces.
Note: ProvincesMerge will make the first province in the list to absorb the rest of provinces. In this case, Madrid province will remain while Toledo and Ciudad Real will be removed. Also note that because two provinces are removed, the integer index of the province (provinceIndex) may have changed.

Step 4) Unmerge provinces. We load the previously stored geodata info and issue a redraw of the country provinces:
Code
map.SetProvinceGeoData(data);
map.DrawProvince(map.GetCountryIndex("Spain"), false, true);

I think this gives you more flexibility now since you can backup province state and restore it when you want, sort of undo/redo functionality.
« Last Edit: February 12, 2021, 10:46:51 AM by Kronnect »

Dotecin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: apply a texture to a group of provinces
« Reply #5 on: February 13, 2021, 04:23:40 PM »
thank you!!

I have already imported the latest Beta, I'll use the new functionality and give feedbacks hopefully within couple of days.

Best Regards

Dotecin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: apply a texture to a group of provinces
« Reply #6 on: February 13, 2021, 07:50:47 PM »
did some tests, seems working ok most of the times, but in some cases I get something strange.
For example this code is supposed to merge two provinces of Italy:
Code
            List<Province> provinces = new List<Province>();
            provinces.Add(map.GetProvince("Italy", "Matera"));
            provinces.Add(map.GetProvince("Italy", "Potenza"));
            map.ProvincesMerge(provinces, true); // <-- true means issue a redraw after the merge

but the end result after showing the provinces is two provinces called "Matera". While all the other provinces of Italy are correctly merged by county, according the the list I have passed (attached file "Italy_issue.png").

Also for France I get something strange: all the counties are built correctly according to the list of provinces I pass, except one. When I try to merge the provinces
 1073,1112,1074,1082,1159,1160 (original id-s before any merge)
then for some reason 1082 (Bouches-du-Rhône) is left out. I have attached a screenshot called province_not_merged_france.png that shows the county of France after the merge and the faulty province.

Another thing, let's suppose the user is playing game#1, then he completes, or he goes back to the main menu, I call
Code
map.SetProvinceGeoData(data); 
map.DrawProvince((int)country, false, true);
in order to restore the original situation. All provinces seems fine.
Then let's suppose the user starts a new game#2 on the same Country, the merge is triggered again.
But if I then use this code:
Code
int provinceIndex = map.GetProvinceIndex(p);
map.ToggleProvinceSurface(provinceIndex, true, color);
on many of the newly merged provinces, the province is colorized correctly, but  then I need to access p.mainRegion.surfaceGameObject , which is now null, while it was not happening after the merge in game#1. Not sure if I'm doing something wrong, I'll try to dig more.

Best Regards

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: apply a texture to a group of provinces
« Reply #7 on: February 17, 2021, 11:25:53 AM »
Please get latest beta. It includes a fix for ProvincesMerge method when a region of a province is surrounded by neighbours.

Dotecin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: apply a texture to a group of provinces
« Reply #8 on: February 22, 2021, 08:13:10 PM »
thank you!
I did some tests, the issues I described are not reproducible anymore, except the one with France:
...
Also for France I get something strange: all the counties are built correctly according to the list of provinces I pass, except one. When I try to merge the provinces
 1073,1112,1074,1082,1159,1160 (original id-s before any merge)
then for some reason 1082 (Bouches-du-Rhône) is left out. I have attached a screenshot called province_not_merged_france.png that shows the county of France after the merge and the faulty province.


After the merge, the province 1082 is lost, the territory belongs to France but not to any province. Not sure if it's something related to this province only or if the same might happen with some other province as well.

Best Regards

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 6630
    • View Profile
Re: apply a texture to a group of provinces
« Reply #9 on: February 22, 2021, 08:21:05 PM »
Are you sure 1082 is a province of France? Last time I tested, it belonged to UK.

Dotecin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: apply a texture to a group of provinces
« Reply #10 on: February 23, 2021, 10:52:17 AM »
for example this piece of code says that 1082 belong to France (before doing any merge):
Code
Country country = map.GetCountry("France");
                string log = "Provinces of France:\r\n";

                foreach (Province province in country.provinces)
                {
                    log += "{ province_id: " + map.GetProvinceIndex(province) + ", province_name: \"" + province.name + "\"}\r\n";
                }

output:
Code
Provinces of France:
{ province_id: 1065, province_name: "Martinique"}
{ province_id: 1066, province_name: "Guyane française"}
{ province_id: 1067, province_name: "La Réunion"}
{ province_id: 1068, province_name: "Mayotte"}
{ province_id: 1069, province_name: "Guadeloupe"}
{ province_id: 1070, province_name: "Ain"}
{ province_id: 1071, province_name: "Aisne"}
{ province_id: 1072, province_name: "Allier"}
{ province_id: 1073, province_name: "Alpes-de-Haute-Provence"}
{ province_id: 1074, province_name: "Alpes-Maritimes"}
{ province_id: 1075, province_name: "Ardèche"}
{ province_id: 1076, province_name: "Ardennes"}
{ province_id: 1077, province_name: "Ariège"}
{ province_id: 1078, province_name: "Aube"}
{ province_id: 1079, province_name: "Aude"}
{ province_id: 1080, province_name: "Aveyron"}
{ province_id: 1081, province_name: "Bas-Rhin"}
{ province_id: 1082, province_name: "Bouches-du-Rhône"}
{ province_id: 1083, province_name: "Calvados"}
....
....