1 #include "live_view.h"
2
3 #include <algorithm> // min & max
4 #include <functional>
5 #include <iosfwd>
6 #include <memory>
7 #include <string>
8
9 #include "color.h"
10 #include "cursesdef.h"
11 #include "game.h"
12 #include "map.h"
13 #include "options.h"
14 #include "output.h"
15 #include "panels.h"
16 #include "translations.h"
17 #include "ui_manager.h"
18
19 namespace
20 {
21
22 constexpr int START_LINE = 1;
23 constexpr int MIN_BOX_HEIGHT = 3;
24
25 } //namespace
26
27 live_view::live_view() = default;
28 live_view::~live_view() = default;
29
init()30 void live_view::init()
31 {
32 hide();
33 }
34
hide()35 void live_view::hide()
36 {
37 ui = nullptr;
38 }
39
show(const tripoint & p)40 void live_view::show( const tripoint &p )
41 {
42 mouse_position = p;
43 if( !ui ) {
44 ui = std::make_unique<ui_adaptor>();
45 ui->on_screen_resize( [this]( ui_adaptor & ui ) {
46 auto &mgr = panel_manager::get_manager();
47 const bool sidebar_right = get_option<std::string>( "SIDEBAR_POSITION" ) == "right";
48 const int width = sidebar_right ? mgr.get_width_right() : mgr.get_width_left();
49
50 const int max_height = TERMY / 2;
51 const int line_limit = max_height - 2;
52 const visibility_variables &cache = get_map().get_visibility_variables_cache();
53 int line_out = START_LINE;
54 // HACK: using dummy window to get the window height without refreshing.
55 win = catacurses::newwin( 1, width, point_zero );
56 g->pre_print_all_tile_info( mouse_position, win, line_out, line_limit, cache );
57 const int live_view_box_height = std::min( max_height, std::max( line_out + 2, MIN_BOX_HEIGHT ) );
58
59 win = catacurses::newwin( live_view_box_height, width,
60 point( sidebar_right ? TERMX - width : 0, 0 ) );
61 ui.position_from_window( win );
62 } );
63 ui->on_redraw( [this]( const ui_adaptor & ) {
64 werase( win );
65 const visibility_variables &cache = get_map().get_visibility_variables_cache();
66 int line_out = START_LINE;
67 g->pre_print_all_tile_info( mouse_position, win, line_out, getmaxy( win ) - 2, cache );
68 draw_border( win );
69 center_print( win, 0, c_white, _( "< <color_green>Mouse View</color> >" ) );
70 wnoutrefresh( win );
71 } );
72 }
73 // Always mark ui for resize as the required box height may have changed.
74 ui->mark_resize();
75 }
76
is_enabled()77 bool live_view::is_enabled()
78 {
79 return ui != nullptr;
80 }
81