The Battle for Wesnoth  1.15.9+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  /**
40  * Return the ID that is cross-referenced at the (screen)
41  * coordinates x, y. If no cross-reference is there, return the
42  * empty string.
43  */
44  std::string ref_at(const int x, const int y);
45 
46 protected:
47  virtual void scroll(unsigned int pos);
48  virtual void set_inner_location(const SDL_Rect& rect);
49 
50 private:
52  /** Convert a string to an alignment. Throw parse_error if unsuccessful. */
53  ALIGNMENT str_to_align(const std::string &s);
54 
55  /**
56  * An item that is displayed in the text area. Contains the surface
57  * that should be blitted along with some other information.
58  */
59  struct item {
60 
61  item(surface surface, int x, int y, const std::string& text="",
62  const std::string& reference_to="", bool floating=false,
63  bool box=false, ALIGNMENT alignment=HERE);
64 
65  item(surface surface, int x, int y,
66  bool floating, bool box=false, ALIGNMENT=HERE);
67 
68  /** Relative coordinates of this item. */
69  SDL_Rect rect;
70 
71  surface surf;
72 
73  // If this item contains text, this will contain that text.
74  std::string text;
75 
76  // If this item contains a cross-reference, this is the id
77  // of the referenced topic.
78  std::string ref_to;
79 
80  // If this item is floating, that is, if things should be filled
81  // around it.
82  bool floating;
83  bool box;
85  };
86 
87  /** Function object to find an item at the specified coordinates. */
88  class item_at {
89  public:
90  item_at(const int x, const int y) : x_(x), y_(y) {}
91  bool operator()(const item&) const;
92  private:
93  const int x_, y_;
94  };
95 
96  /**
97  * Update the vector with the items of the shown topic, creating
98  * surfaces for everything and putting things where they belong.
99  */
100  void set_items();
101 
102  // Create appropriate items from configs. Items will be added to the
103  // internal vector. These methods check that the necessary
104  // attributes are specified.
105  void handle_ref_cfg(const config &cfg);
106  void handle_img_cfg(const config &cfg);
107  void handle_bold_cfg(const config &cfg);
108  void handle_italic_cfg(const config &cfg);
109  void handle_header_cfg(const config &cfg);
110  void handle_jump_cfg(const config &cfg);
111  void handle_format_cfg(const config &cfg);
112 
113  void draw_contents();
114 
115  /**
116  * Add an item with text. If ref_dst is something else than the
117  * empty string, the text item will be underlined to show that it
118  * is a cross-reference. The item will also remember what the
119  * reference points to. If font_size is below zero, the default
120  * will be used.
121  */
122  void add_text_item(const std::string& text, const std::string& ref_dst="",
123  bool broken_link = false,
124  int font_size=-1, bool bold=false, bool italic=false,
126 
127  /** Add an image item with the specified attributes. */
128  void add_img_item(const std::string& path, const std::string& alignment, const bool floating,
129  const bool box);
130 
131  /** Move the current input point to the next line. */
132  void down_one_line();
133 
134  /** Adjust the heights of the items in the last row to make it look good. */
135  void adjust_last_row();
136 
137  /** Return the width that remain on the line the current input point is at. */
138  int get_remaining_width();
139 
140  /**
141  * Return the least x coordinate at which something of the
142  * specified height can be drawn at the specified y coordinate
143  * without interfering with floating images.
144  */
145  int get_min_x(const int y, const int height=0);
146 
147  /** Analogous with get_min_x but return the maximum X. */
148  int get_max_x(const int y, const int height=0);
149 
150  /**
151  * Find the lowest y coordinate where a floating img of the
152  * specified width and at the specified x coordinate can be
153  * placed. Start looking at desired_y and continue downwards. Only
154  * check against other floating things, since text and inline
155  * images only can be above this place if called correctly.
156  */
157  int get_y_for_floating_img(const int width, const int x, const int desired_y);
158 
159  /** Add an item to the internal list, update the locations and row height. */
160  void add_item(const item& itm);
161 
162  std::list<item> items_;
163  std::list<item *> last_row_;
166  const int title_spacing_;
167  /** The current input location when creating items. */
168  std::pair<int, int> curr_loc_;
169  const unsigned min_row_height_;
171  /** The height of all items in total. */
173 };
174 
175 } // 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:143
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:399
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:38
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_
The current input location when creating items.
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:109
ALIGNMENT str_to_align(const std::string &s)
Convert a string to an alignment.
Definition: help.cpp:55
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:59
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.