The Battle for Wesnoth  1.17.0-dev
floating_textbox.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2021
3  by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #include "floating_textbox.hpp"
18 
19 #include "display_chat_manager.hpp"
20 #include "floating_label.hpp"
21 #include "font/standard_colors.hpp"
22 #include "game_display.hpp"
23 #include "preferences/game.hpp"
24 #include "log.hpp"
25 
26 #include <ctime>
27 
28 static lg::log_domain log_display("display");
29 #define ERR_DP LOG_STREAM(err, log_display)
30 
31 namespace gui{
33  box_(nullptr),
34  check_(nullptr),
35  mode_(TEXTBOX_NONE),
36  label_string_(),
37  label_(0),
38  command_history_()
39  {}
40 
42  {
43  if(!active()) {
44  return;
45  }
46  if(check_ != nullptr) {
47  if(mode_ == TEXTBOX_MESSAGE) {
49  }
50  }
51  box_.reset(nullptr);
52  check_.reset(nullptr);
55  gui.invalidate_all();
56  }
57 
59  {
60  if (box_ == nullptr)
61  return;
62 
63  const SDL_Rect& area = gui.map_outside_area();
64 
65  const int border_size = 10;
66 
67  const int ypos = area.y+area.h-30 - (check_ != nullptr ? check_->height() + border_size : 0);
68 
69  if (label_ != 0)
71 
74  flabel.set_position(area.x + border_size, ypos);
76  flabel.set_clip_rect(area);
77 
79 
80  if (label_ == 0)
81  return;
82 
83  const SDL_Rect& label_area = font::get_floating_label_rect(label_);
84  const int textbox_width = area.w - label_area.w - border_size*3;
85 
86  if(textbox_width <= 0) {
88  return;
89  }
90 
91  if(box_ != nullptr) {
92  box_->set_volatile(true);
93  const SDL_Rect rect {
94  area.x + label_area.w + border_size * 2
95  , ypos
96  , textbox_width
97  , box_->height()
98  };
99 
100  box_->set_location(rect);
101  }
102 
103  if(check_ != nullptr) {
104  check_->set_volatile(true);
105  check_->set_location(box_->location().x,box_->location().y + box_->location().h + border_size);
106  }
107  }
108 
110  const std::string& check_label, bool checked, game_display& gui)
111  {
112  close(gui);
113 
115  mode_ = mode;
116 
117  if(!check_label.empty()) {
118  check_.reset(new gui::button(gui.video(),check_label,gui::button::TYPE_CHECK));
119  check_->set_check(checked);
120  }
121 
122 
123  box_.reset(new gui::textbox(gui.video(),100,"",true,256,font::SIZE_NORMAL,0.8,0.6));
124 
125  update_location(gui);
126  }
127 
128  void floating_textbox::tab(const std::set<std::string>& dictionary)
129  {
130  if(active() == false) {
131  return;
132  }
133 
134  std::string text = box_->text();
135  std::vector<std::string> matches(dictionary.begin(), dictionary.end());
136  const bool line_start = utils::word_completion(text, matches);
137 
138  if (matches.empty()) return;
139  if (matches.size() == 1 && mode_ == gui::TEXTBOX_MESSAGE) {
140  text.append(line_start ? ": " : " ");
141  } else if (matches.size() > 1) {
142  std::string completion_list = utils::join(matches, " ");
143  game_display::get_singleton()->get_chat_manager().add_chat_message(time(nullptr), "", 0, completion_list,
145  }
146  box_->set_text(text);
147  }
148 
149  void floating_textbox::memorize_command(const std::string& command)
150  {
151  if(command.empty()) {
152  return;
153  }
154 
155  auto prev = std::find(command_history_.begin(), command_history_.end(), command);
156 
157  if(prev != command_history_.end()) {
158  command_history_.erase(prev);
159  }
160  command_history_.emplace_back(command);
161  }
162 }
void remove_floating_label(int handle, int fadeout)
removes the floating label given by &#39;handle&#39; from the screen
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
void set_clip_rect(const SDL_Rect &r)
General purpose widgets.
void memorize_command(const std::string &command)
void add_chat_message(const std::time_t &time, const std::string &speaker, int side, const std::string &msg, events::chat_handler::MESSAGE_TYPE type, bool bell)
const int SIZE_NORMAL
Definition: constants.cpp:20
void set_alignment(ALIGN align)
std::string label
What to show in the filter&#39;s drop-down list.
Definition: manager.cpp:217
void update_location(game_display &gui)
TEXTBOX_MODE mode() const
void set_position(double xpos, double ypos)
const color_t YELLOW_COLOR
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:3019
void set_message_private(bool value)
Definition: game.cpp:852
void set_color(const color_t &color)
std::unique_ptr< gui::button > check_
A button is a control that can be pushed to start an action or close a dialog.
Definition: button.hpp:50
std::unique_ptr< gui::textbox > box_
const SDL_Rect & map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.hpp:242
void tab(const std::set< std::string > &dictionary)
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
SDL_Rect get_floating_label_rect(int handle)
void show(gui::TEXTBOX_MODE mode, const std::string &label, const std::string &check_label, bool checked, game_display &gui)
static lg::log_domain log_display("display")
display_chat_manager & get_chat_manager()
map_location prev
Definition: astarsearch.cpp:66
std::vector< std::string > command_history_
Standard logging facilities (interface).
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:201
void close(game_display &gui)
bool word_completion(std::string &text, std::vector< std::string > &wordlist)
Try to complete the last word of &#39;text&#39; with the &#39;wordlist&#39;.
static game_display * get_singleton()