nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
talk_window.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <cfloat>
6#include <optional>
7#include <string>
8#include <utility>
9
10#include "client/theme.h"
11#include "client/translations.h"
12
13namespace nanommo {
14
15void TalkWindow::Open(uint32_t npc, std::string name) {
16 npc_ = npc;
17 name_ = std::move(name);
18 focus_ = true;
19}
20
21std::optional<protocol::Talk> TalkWindow::Build() {
22 if (!npc_) {
23 return std::nullopt;
24 }
25 const ImVec2 display = ImGui::GetIO().DisplaySize;
26 ImGui::SetNextWindowPos({display.x / 2, display.y * 0.75f},
27 ImGuiCond_Appearing, {0.5f, 0.5f});
28 ImGui::SetNextWindowSize({Ui(380.0f), 0.0f});
29 bool open = true;
30 BeginPanel(name_.c_str(), &open,
31 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
32 ImGui::TextWrapped("%s",
33 Tr("Give me a few words, and I'll tell you a story."));
34 // So that the player can type straight away.
35 if (focus_) {
36 ImGui::SetKeyboardFocusHere();
37 focus_ = false;
38 }
39 ImGui::SetNextItemWidth(-FLT_MIN);
40 const bool entered = ImGui::InputTextWithHint(
41 "##words", Tr("dragon, castle, gold"), words_.data(), words_.size(),
42 ImGuiInputTextFlags_EnterReturnsTrue);
43 const bool asked = GameButton("Tell me a story", {-1.0f, 0.0f}, true);
44 std::optional<protocol::Talk> talk;
45 if ((entered || asked) && words_[0] != '\0') {
46 talk.emplace();
47 talk->set_npc(*npc_);
48 talk->set_words(words_.data());
49 words_.fill('\0');
50 open = false;
51 }
52 EndPanel();
53 if (!open) {
54 npc_.reset();
55 }
56 return talk;
57}
58
59} // namespace nanommo
bool open() const
Definition talk_window.h:20
void Open(uint32_t npc, std::string name)
std::optional< protocol::Talk > Build()
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
float Ui(float points)
Definition theme.h:67
void EndPanel()
Definition theme.cc:379
const char * Tr(const char *text)
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381