-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement address-search and save/load
- Loading branch information
1 parent
8bfa34f
commit 228cf20
Showing
9 changed files
with
536 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System.Xml; | ||
using FancyMapSnapper.Mapping; | ||
using FancyMapSnapper.Ui; | ||
using FancyMapSnapper.Ui.Widgets; | ||
using SkiaSharp; | ||
|
||
namespace FancyMapSnapper.AppMode; | ||
|
||
public class AddPlaceToListByAddress : ApplicationMode { | ||
private readonly List<string> _places; | ||
|
||
public AddPlaceToListByAddress(List<string> places) { | ||
_places = places; | ||
} | ||
|
||
private readonly UiInputField _houseNum = new() { Size = new SKRect(400, 386, 584, 434) }; | ||
private readonly UiInputField _streetName = new() { Size = new SKRect(616, 386, 1200, 434) }; | ||
private readonly UiInputField _city = new() { Size = new SKRect(400, 466, 784, 514) }; | ||
private readonly UiInputField _state = new() { Size = new SKRect(816, 466, 984, 514) }; | ||
private readonly UiInputField _postCode = new() { Size = new SKRect(1016, 466, 1200, 514) }; | ||
|
||
private readonly UiButton _add = new() { Size = new SKRect(500, 600, 784, 648), Text = "Add" }; | ||
private readonly UiButton _cancel = new() { Size = new SKRect(816, 600, 1100, 648), Text = "Cancel" }; | ||
|
||
private readonly UiRoot _ui = new(); | ||
|
||
private static async Task<List<string>?> PerformAddressQuery(string houseNum, string streetName, string city, string state, string postCode) { | ||
var taskResult = await XmlHelper.ConstructOsmQuery( | ||
(scriptDoc, osmScript) => scriptDoc.AddressQuery(osmScript, houseNum, streetName, city, state, postCode) | ||
).GetPlaceXml(); | ||
|
||
var osmRoot = taskResult["osm"]; | ||
if (osmRoot == null) { | ||
Console.WriteLine($"Got error document: {taskResult.ToXmlString()}"); | ||
return null; | ||
} | ||
|
||
// Clear out extra stuff that we don't need | ||
var noteElement = osmRoot["note"]; | ||
if (noteElement != null) osmRoot.RemoveChild(noteElement); | ||
|
||
var metaElement = osmRoot["meta"]; | ||
if (metaElement != null) osmRoot.RemoveChild(metaElement); | ||
|
||
List<string> places = new(); | ||
foreach (var wayObj in osmRoot.GetElementsByTagName("way")) { | ||
var wayElement = (XmlElement)wayObj; | ||
|
||
var id = wayElement.GetAttribute("id"); | ||
string? name = null; | ||
foreach (var tagObj in wayElement.GetElementsByTagName("tag")) { | ||
var tagElement = (XmlElement)tagObj; | ||
if (tagElement.GetAttribute("k") == "name") | ||
name = tagElement.GetAttribute("v"); | ||
} | ||
|
||
if (name == null) | ||
places.Add("id:" + id); | ||
else | ||
places.Add(name); | ||
} | ||
|
||
return places; | ||
} | ||
|
||
private void AddFromAddress() { | ||
_add.IsEnabled = false; | ||
_cancel.IsEnabled = false; | ||
|
||
PerformAddressQuery( | ||
_houseNum.Builder.ToString(), | ||
_streetName.Builder.ToString(), | ||
_city.Builder.ToString(), | ||
_state.Builder.ToString(), | ||
_postCode.Builder.ToString() | ||
).ContinueWith(task => { | ||
var taskResult = task.Result; | ||
FmsApp.Instance.PostAction(() => { | ||
var places = _places.ToList(); | ||
if (taskResult != null) | ||
places.AddRange(taskResult); | ||
_next = new InitialPlaceListBuilding(places); | ||
}); | ||
}); | ||
} | ||
|
||
private void Cancel() { | ||
_next = new InitialPlaceListBuilding(_places); | ||
} | ||
|
||
public override void Initialize() { | ||
_add.OnClick += AddFromAddress; | ||
_cancel.OnClick += Cancel; | ||
|
||
_ui.AddChild(_houseNum); | ||
_ui.AddChild(_streetName); | ||
_ui.AddChild(_city); | ||
_ui.AddChild(_state); | ||
_ui.AddChild(_postCode); | ||
_ui.AddChild(_add); | ||
_ui.AddChild(_cancel); | ||
} | ||
|
||
public override void HandleInput(in InputEvent input) { | ||
_ui.HandleInput(in input); | ||
} | ||
|
||
public override void Render(SKCanvas canvas) { | ||
_ui.Tick(); | ||
_ui.Render(canvas); | ||
} | ||
|
||
private ApplicationMode? _next; | ||
public override ApplicationMode NextMode => _next ?? this; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System.Xml; | ||
using FancyMapSnapper.Mapping; | ||
using FancyMapSnapper.Ui; | ||
using FancyMapSnapper.Ui.Widgets; | ||
using SkiaSharp; | ||
|
||
namespace FancyMapSnapper.AppMode; | ||
|
||
public class LoadingMap : ApplicationMode { | ||
private readonly List<string> _places; | ||
private readonly List<string> _saveFiles = new(); | ||
|
||
public LoadingMap(List<string> places) { | ||
_places = places; | ||
|
||
var saves = Directory.CreateDirectory("saves"); | ||
foreach (var fileInfo in saves.GetFiles()) { | ||
if (!fileInfo.Name.EndsWith(".xml")) continue; | ||
|
||
_saveFiles.Add(fileInfo.Name[..^".xml".Length]); | ||
} | ||
} | ||
|
||
private readonly UiRoot _ui = new(); | ||
|
||
private void Load(string name) { | ||
var saves = Directory.CreateDirectory("saves"); | ||
var loadPath = Path.Combine(saves.FullName, name + ".xml"); | ||
var xmlDoc = new XmlDocument(); | ||
xmlDoc.LoadXml(File.ReadAllText(loadPath)); | ||
var (places, result) = OsmQueryResult.FromXml(xmlDoc); | ||
|
||
_next = new CustomizingMap(places, result); | ||
} | ||
|
||
private void Cancel() { | ||
_next = new InitialPlaceListBuilding(_places); | ||
} | ||
|
||
private const float InputButtonWidth = 784; | ||
private const float InputButtonHeight = 48; | ||
private const float InputButtonSeparation = 72; | ||
|
||
public override void Initialize() { | ||
var scrollPane = new UiScrollPane { OuterSize = new SKRect(400, 200, 1200, 640) }; | ||
|
||
float y = 0; | ||
foreach (var saveFile in _saveFiles) { | ||
var loadButton = new UiButton { | ||
Text = saveFile, | ||
IsEnabled = true, | ||
Size = new SKRect(0, y + (InputButtonSeparation - InputButtonHeight) / 2, InputButtonWidth, y + (InputButtonSeparation + InputButtonHeight) / 2) | ||
}; | ||
|
||
loadButton.OnClick += () => Load(saveFile); | ||
scrollPane.AddChild(loadButton); | ||
|
||
y += InputButtonSeparation; | ||
} | ||
|
||
_ui.AddChild(scrollPane); | ||
|
||
var cancelButton = new UiButton { | ||
Text = "Cancel Load", | ||
IsEnabled = true, | ||
Size = new SKRect(400, 640 + (InputButtonSeparation - InputButtonHeight) / 2, 1200, 640 + (InputButtonSeparation + InputButtonHeight) / 2) | ||
}; | ||
|
||
cancelButton.OnClick += Cancel; | ||
|
||
_ui.AddChild(cancelButton); | ||
} | ||
|
||
public override void HandleInput(in InputEvent input) { | ||
_ui.HandleInput(in input); | ||
} | ||
|
||
public override void Render(SKCanvas canvas) { | ||
_ui.Tick(); | ||
_ui.Render(canvas); | ||
} | ||
|
||
private ApplicationMode? _next; | ||
public override ApplicationMode NextMode => _next ?? this; | ||
} |
Oops, something went wrong.