The Battle for Wesnoth  1.15.2+dev
help_text_area.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <list> // for list
18 #include <string> // for string
19 #include <utility> // for pair
20 #include "font/standard_colors.hpp" // for NORMAL_COLOR
21 #include "sdl/surface.hpp" // for surface
22 #include "widgets/scrollarea.hpp" // for scrollarea
23 class CVideo;
24 class config;
25 namespace help { struct section; }
26 namespace help { struct topic; }
27 
28 namespace help {
29 
30 
31 /// The area where the content is shown in the help browser.
33 {
34 public:
35  help_text_area(CVideo &video, const section &toplevel);
36  /// Display the topic.
37  void show_topic(const topic &t);
38 
39  /// Return the ID that is cross-referenced at the (screen)
40  /// coordinates x, y. If no cross-reference is there, return the
41  /// empty string.
42  std::string ref_at(const int x, const int y);
43 
44 protected:
45  virtual void scroll(unsigned int pos);
46  virtual void set_inner_location(const SDL_Rect& rect);
47 
48 private:
50  /// Convert a string to an alignment. Throw parse_error if
51  /// unsuccessful.
52  ALIGNMENT str_to_align(const std::string &s);
53 
54  /// An item that is displayed in the text area. Contains the surface
55  /// that should be blitted along with some other information.
56  struct item {
57 
58  item(surface surface, int x, int y, const std::string& text="",
59  const std::string& reference_to="", bool floating=false,
60  bool box=false, ALIGNMENT alignment=HERE);
61 
62  item(surface surface, int x, int y,
63  bool floating, bool box=false, ALIGNMENT=HERE);
64 
65  /// Relative coordinates of this item.
66  SDL_Rect rect;
67 
68  surface surf;
69 
70  // If this item contains text, this will contain that text.
71  std::string text;
72 
73  // If this item contains a cross-reference, this is the id
74  // of the referenced topic.
75  std::string ref_to;
76 
77  // If this item is floating, that is, if things should be filled
78  // around it.
79  bool floating;
80  bool box;
82  };
83 
84  /// Function object to find an item at the specified coordinates.
85  class item_at {
86  public:
87  item_at(const int x, const int y) : x_(x), y_(y) {}
88  bool operator()(const item&) const;
89  private:
90  const int x_, y_;
91  };
92 
93  /// Update the vector with the items of the shown topic, creating
94  /// surfaces for everything and putting things where they belong.
95  void set_items();
96 
97  // Create appropriate items from configs. Items will be added to the
98  // internal vector. These methods check that the necessary
99  // attributes are specified.
100  void handle_ref_cfg(const config &cfg);
101  void handle_img_cfg(const config &cfg);
102  void handle_bold_cfg(const config &cfg);
103  void handle_italic_cfg(const config &cfg);
104  void handle_header_cfg(const config &cfg);
105  void handle_jump_cfg(const config &cfg);
106  void handle_format_cfg(const config &cfg);
107 
108  void draw_contents();
109 
110  /// Add an item with text. If ref_dst is something else than the
111  /// empty string, the text item will be underlined to show that it
112  /// is a cross-reference. The item will also remember what the
113  /// reference points to. If font_size is below zero, the default
114  /// will be used.
115  void add_text_item(const std::string& text, const std::string& ref_dst="",
116  bool broken_link = false,
117  int font_size=-1, bool bold=false, bool italic=false,
119 
120  /// Add an image item with the specified attributes.
121  void add_img_item(const std::string& path, const std::string& alignment, const bool floating,
122  const bool box);
123 
124  /// Move the current input point to the next line.
125  void down_one_line();
126 
127  /// Adjust the heights of the items in the last row to make it look
128  /// good .
129  void adjust_last_row();
130 
131  /// Return the width that remain on the line the current input point is at.
132  int get_remaining_width();
133 
134  /// Return the least x coordinate at which something of the
135  /// specified height can be drawn at the specified y coordinate
136  /// without interfering with floating images.
137  int get_min_x(const int y, const int height=0);
138 
139  /// Analogous with get_min_x but return the maximum X.
140  int get_max_x(const int y, const int height=0);
141 
142  /// Find the lowest y coordinate where a floating img of the
143  /// specified width and at the specified x coordinate can be
144  /// placed. Start looking at desired_y and continue downwards. Only
145  /// check against other floating things, since text and inline
146  /// images only can be above this place if called correctly.
147  int get_y_for_floating_img(const int width, const int x, const int desired_y);
148 
149  /// Add an item to the internal list, update the locations and row
150  /// height.
151  void add_item(const item& itm);
152 
153  std::list<item> items_;
154  std::list<item *> last_row_;
157  const int title_spacing_;
158  // The current input location when creating items.
159  std::pair<int, int> curr_loc_;
160  const unsigned min_row_height_;
162  /// The height of all items in total.
164 };
165 
166 } // end namespace help
int get_max_x(const int y, const int height=0)
Analogous with get_min_x but return the maximum X.
help_text_area(CVideo &video, const section &toplevel)
virtual void set_inner_location(const SDL_Rect &rect)
void add_img_item(const std::string &path, const std::string &alignment, const bool floating, const bool box)
Add an image item with the specified attributes.
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:141
int get_remaining_width()
Return the width that remain on the line the current input point is at.
int get_y_for_floating_img(const int width, const int x, const int desired_y)
Find the lowest y coordinate where a floating img of the specified width and at the specified x coord...
Definition: video.hpp:31
std::list< item * > last_row_
void set_items()
Update the vector with the items of the shown topic, creating surfaces for everything and putting thi...
std::string bold(const std::string &s)
Definition: help_impl.hpp:363
topic const * shown_topic_
void add_item(const item &itm)
Add an item to the internal list, update the locations and row height.
std::string ref_at(const int x, const int y)
Return the ID that is cross-referenced at the (screen) coordinates x, y.
The area where the content is shown in the help browser.
virtual void scroll(unsigned int pos)
std::list< item > items_
const section & toplevel_
std::string path
Definition: game_config.cpp:39
void handle_ref_cfg(const config &cfg)
const unsigned min_row_height_
void handle_img_cfg(const config &cfg)
const color_t NORMAL_COLOR
void handle_header_cfg(const config &cfg)
An item that is displayed in the text area.
void handle_italic_cfg(const config &cfg)
void handle_bold_cfg(const config &cfg)
void show_topic(const topic &t)
Display the topic.
void adjust_last_row()
Adjust the heights of the items in the last row to make it look good .
void add_text_item(const std::string &text, const std::string &ref_dst="", bool broken_link=false, int font_size=-1, bool bold=false, bool italic=false, color_t color=font::NORMAL_COLOR)
Add an item with text.
item_at(const int x, const int y)
static map_location::DIRECTION s
std::pair< int, int > curr_loc_
item(surface surface, int x, int y, const std::string &text="", const std::string &reference_to="", bool floating=false, bool box=false, ALIGNMENT alignment=HERE)
int width() const
Definition: widget.cpp:124
int height() const
Definition: widget.cpp:129
int get_min_x(const int y, const int height=0)
Return the least x coordinate at which something of the specified height can be drawn at the specifie...
CVideo & video() const
Definition: widget.hpp:83
void down_one_line()
Move the current input point to the next line.
void handle_format_cfg(const config &cfg)
double t
Definition: astarsearch.cpp:64
A topic contains a title, an id and some text.
Definition: help_impl.hpp:107
ALIGNMENT str_to_align(const std::string &s)
Convert a string to an alignment.
Definition: help.cpp:56
SDL_Rect rect
Relative coordinates of this item.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
const int font_size
Definition: button.cpp:40
int contents_height_
The height of all items in total.
void handle_jump_cfg(const config &cfg)
Function object to find an item at the specified coordinates.