Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/frontend/StarConfirmationDialog.cpp
blob: 038ebef223c0a8640df013395cfcf421148858a6 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "StarConfirmationDialog.hpp"
#include "StarGuiReader.hpp"
#include "StarRoot.hpp"
#include "StarLabelWidget.hpp"
#include "StarButtonWidget.hpp"
#include "StarImageWidget.hpp"
#include "StarRandom.hpp"
#include "StarAssets.hpp"

namespace Star {

ConfirmationDialog::ConfirmationDialog() {}

void ConfirmationDialog::displayConfirmation(Json const& dialogConfig, RpcPromiseKeeper<Json> resultPromise) {
  m_resultPromise = resultPromise;
  displayConfirmation(dialogConfig, [this] (Widget*) { m_resultPromise->fulfill(true); }, [this] (Widget*) { m_resultPromise->fulfill(false); } );
}

void ConfirmationDialog::displayConfirmation(Json const& dialogConfig, WidgetCallbackFunc okCallback, WidgetCallbackFunc cancelCallback) {
  Json config;
  if (dialogConfig.isType(Json::Type::String))
    config = Root::singleton().assets()->json(dialogConfig.toString());
  else
    config = dialogConfig;

  auto assets = Root::singleton().assets();

  removeAllChildren();

  GuiReader reader;

  m_okCallback = std::move(okCallback);
  m_cancelCallback = std::move(cancelCallback);

  reader.registerCallback("close", bind(&ConfirmationDialog::dismiss, this));
  reader.registerCallback("cancel", bind(&ConfirmationDialog::dismiss, this));
  reader.registerCallback("ok", bind(&ConfirmationDialog::ok, this));

  m_confirmed = false;

  String paneLayoutPath =
      config.optString("paneLayout").value("/interface/windowconfig/confirmation.config:paneLayout");
  reader.construct(assets->json(paneLayoutPath), this);

  ImageWidgetPtr titleIcon = {};
  if (config.contains("icon"))
    titleIcon = make_shared<ImageWidget>(config.getString("icon"));

  setTitle(titleIcon, config.getString("title", ""), config.getString("subtitle", ""));
  fetchChild<LabelWidget>("message")->setText(config.getString("message"));

  if (config.contains("okCaption"))
    fetchChild<ButtonWidget>("ok")->setText(config.getString("okCaption"));
  if (config.contains("cancelCaption"))
    fetchChild<ButtonWidget>("cancel")->setText(config.getString("cancelCaption"));

  m_sourceEntityId = config.optInt("sourceEntityId");

  for (auto image : config.optObject("images").value({})) {
    auto widget = fetchChild<ImageWidget>(image.first);
    if (image.second.isType(Json::Type::String))
      widget->setImage(image.second.toString());
    else
      widget->setDrawables(image.second.toArray().transformed(construct<Drawable>()));
  }

  for (auto label : config.optObject("labels").value({})) {
    auto widget = fetchChild<LabelWidget>(label.first);
    widget->setText(label.second.toString());
  }

  show();
  auto sound = Random::randValueFrom(Root::singleton().assets()->json("/interface/windowconfig/confirmation.config:onShowSound").toArray(), "").toString();

  if (!sound.empty())
    context()->playAudio(sound);
}

Maybe<EntityId> ConfirmationDialog::sourceEntityId() {
  return m_sourceEntityId;
}

void ConfirmationDialog::dismissed() {
  if (!m_confirmed)
    m_cancelCallback(this);

  Pane::dismissed();
}

void ConfirmationDialog::ok() {
  m_okCallback(this);
  m_confirmed = true;
  dismiss();
}

}