about summary refs log tree commit diff stats
path: root/src/connection_dialog.cpp
blob: 64fee98ade2b2087afc63c3eded1d78633601955 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "connection_dialog.h"

#include "tracker_config.h"

ConnectionDialog::ConnectionDialog()
    : wxDialog(nullptr, wxID_ANY, "Connect to Archipelago") {
  server_box_ =
      new wxTextCtrl(this, -1, GetTrackerConfig().connection_details.ap_server,
                     wxDefaultPosition, {300, -1});
  player_box_ =
      new wxTextCtrl(this, -1, GetTrackerConfig().connection_details.ap_player,
                     wxDefaultPosition, {300, -1});
  password_box_ = new wxTextCtrl(
      this, -1, GetTrackerConfig().connection_details.ap_password,
      wxDefaultPosition, {300, -1});

  wxFlexGridSizer* form_sizer = new wxFlexGridSizer(2, 10, 10);

  form_sizer->Add(
      new wxStaticText(this, -1, "Server:"),
      wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
  form_sizer->Add(server_box_, wxSizerFlags().Expand());
  form_sizer->Add(
      new wxStaticText(this, -1, "Player:"),
      wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
  form_sizer->Add(player_box_, wxSizerFlags().Expand());
  form_sizer->Add(
      new wxStaticText(this, -1, "Password:"),
      wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
  form_sizer->Add(password_box_, wxSizerFlags().Expand());

  history_list_ = new wxListBox(this, -1);
  for (const ConnectionDetails& details : GetTrackerConfig().connection_history) {
    wxString display_text;
    display_text << details.ap_player;
    display_text << " (";
    display_text << details.ap_server;
    display_text << ")";

    history_list_->Append(display_text);
  }

  history_list_->Bind(wxEVT_LISTBOX, &ConnectionDialog::OnOldConnectionChosen, this);

  wxBoxSizer* mid_sizer = new wxBoxSizer(wxHORIZONTAL);
  mid_sizer->Add(form_sizer, wxSizerFlags().Proportion(3).Expand());
  mid_sizer->AddSpacer(10);
  mid_sizer->Add(history_list_, wxSizerFlags().Proportion(2).Expand());

  wxBoxSizer* top_sizer = new wxBoxSizer(wxVERTICAL);
  top_sizer->Add(new wxStaticText(
                     this, -1, "Enter the details to connect to Archipelago."),
                 wxSizerFlags().Align(wxALIGN_LEFT).DoubleBorder());
  top_sizer->Add(mid_sizer, wxSizerFlags().DoubleBorder().Expand());
  top_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Border().Center());

  SetSizerAndFit(top_sizer);

  Center();
  server_box_->SetFocus();
}

void ConnectionDialog::OnOldConnectionChosen(wxCommandEvent& e) {
  if (e.IsSelection()) {
    const ConnectionDetails& details = GetTrackerConfig().connection_history.at(e.GetSelection());
    server_box_->SetValue(details.ap_server);
    player_box_->SetValue(details.ap_player);
    password_box_->SetValue(details.ap_password);
  }
}
/span>, &TrackerFrame::OnAbout, this, wxID_ABOUT); Bind(wxEVT_MENU, &TrackerFrame::OnExit, this, wxID_EXIT); Bind(wxEVT_MENU, &TrackerFrame::OnConnect, this, ID_CONNECT); Bind(wxEVT_MENU, &TrackerFrame::OnSettings, this, ID_SETTINGS); Bind(wxEVT_MENU, &TrackerFrame::OnCheckForUpdates, this, ID_CHECK_FOR_UPDATES); Bind(wxEVT_MENU, &TrackerFrame::OnZoomIn, this, ID_ZOOM_IN); Bind(wxEVT_MENU, &TrackerFrame::OnZoomOut, this, ID_ZOOM_OUT); Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, &TrackerFrame::OnChangePage, this); Bind(STATE_RESET, &TrackerFrame::OnStateReset, this); Bind(STATE_CHANGED, &TrackerFrame::OnStateChanged, this); Bind(STATUS_CHANGED, &TrackerFrame::OnStatusChanged, this); wxChoicebook *choicebook = new wxChoicebook(this, wxID_ANY); achievements_pane_ = new AchievementsPane(choicebook); choicebook->AddPage(achievements_pane_, "Achievements"); notebook_ = new wxNotebook(this, wxID_ANY); tracker_panel_ = new TrackerPanel(notebook_); subway_map_ = new SubwayMap(notebook_); notebook_->AddPage(tracker_panel_, "Map"); notebook_->AddPage(subway_map_, "Subway"); wxBoxSizer *top_sizer = new wxBoxSizer(wxHORIZONTAL); top_sizer->Add(choicebook, wxSizerFlags().Expand().Proportion(1)); top_sizer->Add(notebook_, wxSizerFlags().Expand().Proportion(3)); SetSizerAndFit(top_sizer); SetSize(1280, 728); if (!GetTrackerConfig().asked_to_check_for_updates) { GetTrackerConfig().asked_to_check_for_updates = true; if (wxMessageBox( "Check for updates automatically when the tracker is opened?", "Lingo AP Tracker", wxYES_NO) == wxYES) { GetTrackerConfig().should_check_for_updates = true; } else { GetTrackerConfig().should_check_for_updates = false; } GetTrackerConfig().Save(); } if (GetTrackerConfig().should_check_for_updates) { CheckForUpdates(/*manual=*/false); } } void TrackerFrame::SetStatusMessage(std::string message) { wxCommandEvent *event = new wxCommandEvent(STATUS_CHANGED); event->SetString(message.c_str()); QueueEvent(event); } void TrackerFrame::ResetIndicators() { QueueEvent(new wxCommandEvent(STATE_RESET)); } void TrackerFrame::UpdateIndicators() { QueueEvent(new wxCommandEvent(STATE_CHANGED)); } void TrackerFrame::OnAbout(wxCommandEvent &event) { wxAboutDialogInfo about_info; about_info.SetName("Lingo Archipelago Tracker"); about_info.SetVersion(kTrackerVersion.ToString()); about_info.AddDeveloper("hatkirby"); about_info.AddArtist("Brenton Wildes"); about_info.AddArtist("kinrah"); wxAboutBox(about_info); } void TrackerFrame::OnExit(wxCommandEvent &event) { Close(true); } void TrackerFrame::OnConnect(wxCommandEvent &event) { ConnectionDialog dlg; if (dlg.ShowModal() == wxID_OK) { GetTrackerConfig().connection_details.ap_server = dlg.GetServerValue(); GetTrackerConfig().connection_details.ap_player = dlg.GetPlayerValue(); GetTrackerConfig().connection_details.ap_password = dlg.GetPasswordValue(); std::deque<ConnectionDetails> new_history; new_history.push_back(GetTrackerConfig().connection_details); for (const ConnectionDetails &details : GetTrackerConfig().connection_history) { if (details != GetTrackerConfig().connection_details) { new_history.push_back(details); } } while (new_history.size() > 10) { new_history.pop_back(); } GetTrackerConfig().connection_history = std::move(new_history); GetTrackerConfig().Save(); AP_Connect(dlg.GetServerValue(), dlg.GetPlayerValue(), dlg.GetPasswordValue()); } } void TrackerFrame::OnSettings(wxCommandEvent &event) { SettingsDialog dlg; if (dlg.ShowModal() == wxID_OK) { GetTrackerConfig().should_check_for_updates = dlg.GetShouldCheckForUpdates(); GetTrackerConfig().hybrid_areas = dlg.GetHybridAreas(); GetTrackerConfig().show_hunt_panels = dlg.GetShowHuntPanels(); GetTrackerConfig().Save(); UpdateIndicators(); } } void TrackerFrame::OnCheckForUpdates(wxCommandEvent &event) { CheckForUpdates(/*manual=*/true); } void TrackerFrame::OnZoomIn(wxCommandEvent &event) { if (notebook_->GetSelection() == 1) { subway_map_->Zoom(true); } } void TrackerFrame::OnZoomOut(wxCommandEvent& event) { if (notebook_->GetSelection() == 1) { subway_map_->Zoom(false); } } void TrackerFrame::OnChangePage(wxBookCtrlEvent &event) { zoom_in_menu_item_->Enable(event.GetSelection() == 1); zoom_out_menu_item_->Enable(event.GetSelection() == 1); } void TrackerFrame::OnStateReset(wxCommandEvent& event) { tracker_panel_->UpdateIndicators(); achievements_pane_->UpdateIndicators(); subway_map_->OnConnect(); Refresh(); } void TrackerFrame::OnStateChanged(wxCommandEvent &event) { tracker_panel_->UpdateIndicators(); achievements_pane_->UpdateIndicators(); subway_map_->UpdateIndicators(); Refresh(); } void TrackerFrame::OnStatusChanged(wxCommandEvent &event) { SetStatusText(event.GetString()); } void TrackerFrame::CheckForUpdates(bool manual) { wxWebRequest request = wxWebSession::GetDefault().CreateRequest( this, "https://code.fourisland.com/lingo-ap-tracker/plain/VERSION"); if (!request.IsOk()) { if (manual) { wxMessageBox("Could not check for updates.", "Error", wxOK | wxICON_ERROR); } else { SetStatusText("Could not check for updates."); } return; } Bind(wxEVT_WEBREQUEST_STATE, [this, manual](wxWebRequestEvent &evt) { if (evt.GetState() == wxWebRequest::State_Completed) { std::string response = evt.GetResponse().AsString().ToStdString(); Version latest_version(response); if (kTrackerVersion < latest_version) { std::ostringstream message_text; message_text << "There is a newer version of Lingo AP Tracker " "available. You have " << kTrackerVersion.ToString() << ", and the latest version is " << latest_version.ToString() << ". Would you like to update?"; if (wxMessageBox(message_text.str(), "Update available", wxYES_NO) == wxYES) { wxLaunchDefaultBrowser( "https://code.fourisland.com/lingo-ap-tracker/about/" "CHANGELOG.md"); } } else if (manual) { wxMessageBox("Lingo AP Tracker is up to date!", "Lingo AP Tracker", wxOK); } } else if (evt.GetState() == wxWebRequest::State_Failed) { if (manual) { wxMessageBox("Could not check for updates.", "Error", wxOK | wxICON_ERROR); } else { SetStatusText("Could not check for updates."); } } }); request.Start(); }