rework QuickLinkOpener
This commit is contained in:
parent
b7ecc1e6b5
commit
40ce9dcb81
@ -1,26 +1,80 @@
|
||||
bool menu_visibility = false;
|
||||
string quickURL;
|
||||
[Setting name="Number of links saved in cache"]
|
||||
uint S_NbOfLinksInCache = 5;
|
||||
|
||||
void Main() {}
|
||||
const string C_LinksCache = "LinksCache.txt";
|
||||
|
||||
void Render() {
|
||||
if (!menu_visibility) {
|
||||
return;
|
||||
string G_QuickURL = "";
|
||||
bool G_PressEnter = false;
|
||||
bool G_WasEditing = false;
|
||||
|
||||
array<string> G_LinksCache;
|
||||
|
||||
void Main() {
|
||||
trace("Loading links cache");
|
||||
string filepath = IO::FromStorageFolder(C_LinksCache);
|
||||
IO::File file(filepath);
|
||||
|
||||
file.Open(IO::FileMode::Read);
|
||||
while(!file.EOF()) {
|
||||
G_LinksCache.InsertLast(file.ReadLine());
|
||||
}
|
||||
UI::Begin("\\$cf9" + Icons::ExternalLinkAlt + "\\$z Quick Link Opener###Quick Link Opener", menu_visibility, UI::WindowFlags::NoResize | UI::WindowFlags::AlwaysAutoResize | UI::WindowFlags::NoCollapse);
|
||||
quickURL = UI::InputText("", quickURL);
|
||||
UI::SameLine();
|
||||
if (UI::Button(Icons::ExternalLinkAlt + " Go !###QuickURL")) {
|
||||
string parsedURL = Regex::Replace(quickURL,'uplay:\\/\\/launch\\/5595\\/0\\/','maniaplanet://');
|
||||
}
|
||||
|
||||
void RenderMenuMain() {
|
||||
if (!G_PressEnter && UI::BeginMenu("\\$cf9" + Icons::ExternalLinkAlt + "\\$z Quick Link Opener##QuickLinkOpenerMenu")) {
|
||||
G_QuickURL = UI::InputText("###quickURL", G_QuickURL, G_PressEnter, UI::InputTextFlags::EnterReturnsTrue + UI::InputTextFlags::CallbackAlways, UI::InputTextCallback(ITCB));
|
||||
|
||||
if (G_LinksCache.Length > 0) {
|
||||
UI::Separator();
|
||||
|
||||
for(uint i = 0; i < G_LinksCache.Length; i++ ) {
|
||||
if (UI::MenuItem(G_LinksCache[i] + "###" + i)) {
|
||||
LoadLink(G_LinksCache[i], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UI::EndMenu();
|
||||
} else if (G_PressEnter || G_WasEditing) {
|
||||
G_PressEnter = false;
|
||||
G_WasEditing = false;
|
||||
LoadLink(G_QuickURL, true);
|
||||
G_QuickURL = "";
|
||||
}
|
||||
}
|
||||
|
||||
void ITCB(UI::InputTextCallbackData@ d) {
|
||||
G_WasEditing = true;
|
||||
}
|
||||
|
||||
void LoadLink(string _Url, bool _NewUrl) {
|
||||
if (_Url == "") return;
|
||||
|
||||
string parsedURL = "";
|
||||
if (_NewUrl) {
|
||||
parsedURL = Regex::Replace(_Url,'uplay:\\/\\/launch\\/5595\\/0\\/','maniaplanet://');
|
||||
|
||||
G_LinksCache.InsertAt(0, parsedURL);
|
||||
|
||||
// Clear cache if too long
|
||||
if (G_LinksCache.Length > S_NbOfLinksInCache) {
|
||||
G_LinksCache.RemoveRange(S_NbOfLinksInCache, G_LinksCache.Length - S_NbOfLinksInCache);
|
||||
}
|
||||
|
||||
// Compute text for the cache file
|
||||
string content;
|
||||
for(uint i = 0; i < G_LinksCache.Length; i++ ) {
|
||||
content = content + G_LinksCache[i] + "\n";
|
||||
}
|
||||
|
||||
trace("Writing links cache");
|
||||
string filepath = IO::FromStorageFolder(C_LinksCache);
|
||||
IO::File file(filepath, IO::FileMode::Write);
|
||||
file.Write(content);
|
||||
} else {
|
||||
parsedURL = _Url;
|
||||
}
|
||||
|
||||
CTrackMania@ app = cast<CTrackMania>(GetApp());
|
||||
app.ManiaPlanetScriptAPI.OpenLink(parsedURL, CGameManiaPlanetScriptAPI::ELinkType::ManialinkBrowser);
|
||||
menu_visibility = false;
|
||||
}
|
||||
UI::End();
|
||||
}
|
||||
|
||||
void RenderMenu() {
|
||||
if(UI::MenuItem("\\$cf9" + Icons::ExternalLinkAlt + "\\$z Quick Link Opener", "", menu_visibility)) {
|
||||
menu_visibility = !menu_visibility;
|
||||
}
|
||||
}
|
||||
|
@ -4,11 +4,6 @@ author = "Beu"
|
||||
category = "Utilities"
|
||||
|
||||
siteid = 101
|
||||
version = "1.1"
|
||||
|
||||
perms = "paid"
|
||||
version = "2.0"
|
||||
|
||||
blocks = [ "Plugin_QuickLinkOpener" ]
|
||||
|
||||
[script]
|
||||
imports = [ "Icons.as" ]
|
Loading…
Reference in New Issue
Block a user