Skip to content

Commit

Permalink
Bug fix, added a lot of layers, added a edit sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
SioGabx committed May 1, 2023
1 parent 82dbf66 commit 17cdf70
Show file tree
Hide file tree
Showing 60 changed files with 1,770 additions and 985 deletions.
176 changes: 106 additions & 70 deletions MapsInMyFolder.Common/Collectif.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using System;
using Jint;
using MapsInMyFolder.Commun;
using NetVips;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Text.RegularExpressions;
using System.Windows.Media.Animation;
using System.Windows.Input;
using MapsInMyFolder.Commun;
using Newtonsoft.Json;
using NetVips;
using Jint;
using System.Windows.Media.Imaging;

namespace MapsInMyFolder.Commun
{
Expand Down Expand Up @@ -57,27 +57,14 @@ public static class Collectif
public static class GetUrl
{
public enum InvokeFunction { getTile, getPreview, getPreviewFallback }
public static string FromTileXYZ(string urlbase, int Tilex, int Tiley, int z, int LayerID, InvokeFunction InvokeFunction)

public static (Dictionary<string, object> DefaultCallValue, Dictionary<string, string> ResultCallValue) CallFunctionAndGetResult(string urlbase, string TileComputationScript, int Tilex, int Tiley, int z, int LayerID, InvokeFunction InvokeFunction)
{
if (LayerID == -1)
{
return urlbase;
return (null, null);
}

Layers calque = Layers.GetLayerById(LayerID);
if (calque is null)
{
return string.Empty;
}
string finalurl;
if (string.IsNullOrEmpty(urlbase))
{
finalurl = calque.class_tile_url;
}
else
{
finalurl = urlbase;
}
var location_topleft = TileToCoordonnees(Tilex, Tiley, z);
var location_bottomright = TileToCoordonnees(Tilex + 1, Tiley + 1, z);
var location = GetCenterBetweenTwoPoints(location_topleft, location_bottomright);
Expand All @@ -99,16 +86,14 @@ public static string FromTileXYZ(string urlbase, int Tilex, int Tiley, int z, in
{ "layerid", LayerID.ToString() },
{ "url", urlbase },
};
//Jint.Native.JsValue JavascriptMainResult = Commun.Javascript.ExecuteScript("function main() { var js = new TheType(); log(js.TestDoubleReturn(0,0)); return args; }", argument);

string TileComputationScript = calque.class_tilecomputationscript;

if (!string.IsNullOrEmpty(TileComputationScript))
{
Jint.Native.JsValue JavascriptMainResult = null;
try
{
JavascriptMainResult = Javascript.ExecuteScript(TileComputationScript, argument, LayerID, InvokeFunction);
JavascriptMainResult = Javascript.ExecuteScript(TileComputationScript, new Dictionary<string, object>(argument), LayerID, InvokeFunction);
}
catch (Exception ex)
{
Expand All @@ -119,40 +104,73 @@ public static string FromTileXYZ(string urlbase, int Tilex, int Tiley, int z, in
object JavascriptMainResultObject = JavascriptMainResult.ToObject();
var JavascriptMainResultJson = JsonConvert.SerializeObject(JavascriptMainResultObject);
var JavascriptMainResultDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(JavascriptMainResultJson);
return (argument, JavascriptMainResultDictionary);
}
}
return (null, null);

}


if (JavascriptMainResultDictionary.TryGetValue("url", out string urlResult))
public static string FromTileXYZ(string urlbase, int Tilex, int Tiley, int z, int LayerID, InvokeFunction InvokeFunction)
{
if (LayerID == -1)
{
return urlbase;
}

Layers calque = Layers.GetLayerById(LayerID);
if (calque is null)
{
return string.Empty;
}
string TileComputationScript = calque.class_tilecomputationscript;
var ValuesDictionnary = CallFunctionAndGetResult(urlbase, TileComputationScript, Tilex, Tiley, z, LayerID, InvokeFunction);
if (ValuesDictionnary.ResultCallValue is null)
{
return string.Empty;
}

string finalurl;
if (string.IsNullOrEmpty(urlbase))
{
finalurl = calque.class_tile_url;
}
else
{
finalurl = urlbase;
}
if (ValuesDictionnary.ResultCallValue.TryGetValue("url", out string urlResult))
{
if (!string.IsNullOrEmpty(urlResult))
{
finalurl = urlResult;
}
}

foreach (var JavascriptReplacementVar in ValuesDictionnary.ResultCallValue)
{
string replacementValue = string.Empty;
try
{
if (JavascriptReplacementVar.Value is null)
{
if (!string.IsNullOrEmpty(urlResult))
{
finalurl = urlResult;
}
replacementValue = "null";
}

foreach (var JavascriptReplacementVar in JavascriptMainResultDictionary)
else
{
string replacementValue = string.Empty;
try
{
if (JavascriptReplacementVar.Value is null)
{
replacementValue = "null";
}
else
{
replacementValue = JavascriptReplacementVar.Value;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
replacementValue = "null";
}
finally
{
finalurl = finalurl.Replace("{" + JavascriptReplacementVar.Key + "}", replacementValue);
}
replacementValue = JavascriptReplacementVar.Value;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
replacementValue = "null";
}
finally
{
finalurl = finalurl.Replace("{" + JavascriptReplacementVar.Key + "}", replacementValue);
}
}
return finalurl;
}
Expand Down Expand Up @@ -234,13 +252,15 @@ public static void GetAllManifestResourceNames()

public static string ReadResourceString(string pathWithSlash)
{
Stream stream = ReadResourceStream(pathWithSlash);
string return_rsx = String.Empty;
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true))
using (Stream stream = ReadResourceStream(pathWithSlash))
{
return_rsx = reader.ReadToEnd();
string return_rsx = String.Empty;
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true))
{
return_rsx = reader.ReadToEnd();
}
return return_rsx;
}
return return_rsx;
}

public static Stream ReadResourceStream(string pathWithSlash)
Expand Down Expand Up @@ -458,7 +478,7 @@ public static byte[] GetEmptyImageBufferFromText(string BitmapErrorsMessage)
}
lock (Locker)
{
using (NetVips.Image text = NetVips.Image.Text(WordWrap(BitmapErrorsMessage, 20), null, null, null, NetVips.Enums.Align.Centre, null, 100, true, 5, null))
using (NetVips.Image text = NetVips.Image.Text(WordWrap(BitmapErrorsMessage, 20), null, null, null, NetVips.Enums.Align.Centre, null, 100, 5, null))
{
int offsetX = (int)Math.Floor((double)(border_tile_size - text.Width) / 2);
int offsetY = (int)Math.Floor((double)(border_tile_size - text.Height) / 2);
Expand Down Expand Up @@ -790,19 +810,18 @@ public static void LockPreviousUndo(TextBox uIElement)

public static void InsertTextAtCaretPosition(ICSharpCode.AvalonEdit.TextEditor TextBox, string text)
{
int CaretIndex = TextBox.CaretOffset;
if (TextBox.SelectionLength == 0)
{
int CaretIndex = TextBox.CaretOffset;
TextBox.TextArea.Document.Insert(CaretIndex, text);

TextBox.CaretOffset = CaretIndex + text.Length;
}
else
{
TextBox.SelectedText = text;
TextBox.CaretOffset += TextBox.SelectedText.Length;
TextBox.SelectionLength = 0;
}

TextBox.CaretOffset = Math.Min(TextBox.Text.Length, CaretIndex + text.Length);
}

public static void TextEditorCursorPositionChanged(ICSharpCode.AvalonEdit.TextEditor textEditor, Grid grid, ScrollViewer scrollViewer, int MarginTop = 25)
Expand Down Expand Up @@ -898,7 +917,7 @@ public static List<UIElement> FindVisualChildren(UIElement obj, List<System.Type
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found,
/// a null parent is being returned.</returns>
public static T FindChild<T>(DependencyObject parent, string childName)
public static T FindChildByName<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
Expand All @@ -915,7 +934,7 @@ public static T FindChild<T>(DependencyObject parent, string childName)
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
foundChild = FindChildByName<T>(child, childName);

// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
Expand All @@ -942,6 +961,23 @@ public static T FindChild<T>(DependencyObject parent, string childName)
return foundChild;
}

public static T FindChild<T>(DependencyObject parent) where T : DependencyObject
{
if (parent == null)
{
return null;
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
var result = (child as T) ?? FindChild<T>(child);
if (result != null)
{
return result;
}
}
return null;
}

public static int CheckIfInputValueHaveChange(UIElement SourcePanel)
{
Expand Down Expand Up @@ -1010,7 +1046,7 @@ public static int CheckIfInputValueHaveChange(UIElement SourcePanel)
BlackPearl.Controls.CoreLibrary.MultiSelectCombobox MultiSelectCombobox = (BlackPearl.Controls.CoreLibrary.MultiSelectCombobox)element;
if (MultiSelectCombobox.SelectedItems != null && MultiSelectCombobox.SelectedItems.Count > 0)
{

hachCode = string.Join(";", MultiSelectCombobox.SelectedValues("EnglishName")).GetHashCode();
}
else
Expand Down
3 changes: 0 additions & 3 deletions MapsInMyFolder.Common/Country.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MapsInMyFolder.Commun
{
Expand Down
10 changes: 6 additions & 4 deletions MapsInMyFolder.Common/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public static async Task DB_AskDownload(bool force_download = false)
}

}
private static void CreateEmptyDatabase(string database_pathname)
private static async void CreateEmptyDatabase(string database_pathname)
{
SQLiteConnection.CreateFile(database_pathname);
DB_CreateTables(database_pathname);
RefreshPanels.Invoke(null, EventArgs.Empty);
Database.CheckIfNewerVersionAvailable();
await Database.CheckIfNewerVersionAvailable();
}

private static async Task<bool> DB_DownloadFile(string database_url, string database_pathname)
Expand Down Expand Up @@ -333,7 +333,7 @@ public static SQLiteConnection DB_CreateTables(string datasource)
return null;
}
SQLiteCommand sqlite_cmd = sqlite_conn.CreateCommand();
const string commande_arg = "'ID' INTEGER UNIQUE, 'NOM' TEXT DEFAULT '', 'DESCRIPTION' TEXT DEFAULT '', 'CATEGORIE' TEXT DEFAULT '','PAYS' TEXT DEFAULT '', 'IDENTIFIANT' TEXT DEFAULT '', 'TILE_URL' TEXT DEFAULT '','TILE_FALLBACK_URL' TEXT DEFAULT '', 'MIN_ZOOM' INTEGER DEFAULT 0, 'MAX_ZOOM' INTEGER DEFAULT 0, 'FORMAT' TEXT DEFAULT 'jpeg', 'SITE' TEXT DEFAULT '', 'SITE_URL' TEXT DEFAULT '', 'TILE_SIZE' INTEGER DEFAULT 256, 'FAVORITE' INTEGER DEFAULT 0, 'TILECOMPUTATIONSCRIPT' TEXT DEFAULT '','VISIBILITY' TEXT DEFAULT 'Visible' ,'SPECIALSOPTIONS' TEXT DEFAULT '','RECTANGLES' TEXT DEFAULT '', 'VERSION' INTEGER DEFAULT 1, 'HAS_SCALE' INTEGER DEFAULT 0";
const string commande_arg = "'ID' INTEGER UNIQUE, 'NOM' TEXT DEFAULT '', 'DESCRIPTION' TEXT DEFAULT '', 'CATEGORIE' TEXT DEFAULT '','PAYS' TEXT DEFAULT '', 'IDENTIFIANT' TEXT DEFAULT '', 'TILE_URL' TEXT DEFAULT '','TILE_FALLBACK_URL' TEXT DEFAULT '', 'MIN_ZOOM' INTEGER DEFAULT '', 'MAX_ZOOM' INTEGER DEFAULT '', 'FORMAT' TEXT DEFAULT '', 'SITE' TEXT DEFAULT '', 'SITE_URL' TEXT DEFAULT '', 'TILE_SIZE' INTEGER DEFAULT '', 'FAVORITE' INTEGER DEFAULT 0, 'TILECOMPUTATIONSCRIPT' TEXT DEFAULT '','VISIBILITY' TEXT DEFAULT '' ,'SPECIALSOPTIONS' TEXT DEFAULT '','RECTANGLES' TEXT DEFAULT '', 'VERSION' INTEGER DEFAULT 1, 'HAS_SCALE' INTEGER DEFAULT 1";
sqlite_cmd.CommandText = $@"
CREATE TABLE IF NOT EXISTS 'CUSTOMSLAYERS' ({commande_arg});
CREATE TABLE IF NOT EXISTS 'LAYERS' ({commande_arg},PRIMARY KEY('ID' AUTOINCREMENT));
Expand Down Expand Up @@ -418,13 +418,15 @@ public static void DB_Download_Delete(int bdid)
}
}

public static async void CheckIfNewerVersionAvailable()
public static async Task<bool> CheckIfNewerVersionAvailable()
{
(bool IsNewVersionAvailable, int NewVersionNumber) NewVersion = await CompareVersion();
if (NewVersion.IsNewVersionAvailable)
{
NewUpdateFoundEvent(null, NewVersion.NewVersionNumber);
return true;
}
return false;
}

private static async Task<(bool IsNewVersionAvailable, int NewVersionNumber)> CompareVersion()
Expand Down
4 changes: 2 additions & 2 deletions MapsInMyFolder.Common/DiffMatchPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public override bool Equals(Object obj)

// If parameter cannot be cast to Diff return false.
Diff p = obj as Diff;
if ((System.Object)p == null)
if (p == null)
{
return false;
}
Expand Down Expand Up @@ -718,7 +718,7 @@ private string diff_linesToCharsMunge(string text, List<string> lineArray,

if (lineHash.ContainsKey(line))
{
chars.Append(((char)(int)lineHash[line]));
chars.Append(((char)lineHash[line]));
}
else
{
Expand Down
20 changes: 19 additions & 1 deletion MapsInMyFolder.Common/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -150,6 +149,25 @@ public static bool ContainsOneOrMore(this IEnumerable<string> BaseArray, IEnumer
return false;
}

public static string Replace(this string Texte, IEnumerable<char> oldCharArray, string newString)
{
foreach (char SearchStr in oldCharArray)
{
Texte = Texte.Replace(SearchStr.ToString(), newString);
}
return Texte;
}

public static string ReplaceLoop(this string Texte, string oldString, string newString)
{
while (Texte.Contains(oldString))
{
Texte = Texte.Replace(oldString, newString);
}
return Texte;
}



}
}
Loading

0 comments on commit 17cdf70

Please sign in to comment.