1 | // Copyright (C) 2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of NV Libraries.
|
---|
5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
6 |
|
---|
7 | #include "nv/gui/gui_ascii_renderer.hh"
|
---|
8 |
|
---|
9 | using namespace nv;
|
---|
10 | using namespace nv::gui;
|
---|
11 |
|
---|
12 |
|
---|
13 | struct ascii_render_data : public render_data
|
---|
14 | {
|
---|
15 | ascii_render_data() {}
|
---|
16 | bool border;
|
---|
17 | char border_chars[8];
|
---|
18 | uint32 border_color;
|
---|
19 | uint32 text_color;
|
---|
20 | };
|
---|
21 |
|
---|
22 |
|
---|
23 | ascii_renderer::ascii_renderer( terminal* t )
|
---|
24 | : m_terminal(t)
|
---|
25 | {
|
---|
26 |
|
---|
27 | }
|
---|
28 |
|
---|
29 | void ascii_renderer::redraw( element* e, uint32 )
|
---|
30 | {
|
---|
31 | ascii_render_data* er = nullptr;
|
---|
32 | if ( e->m_render_data == nullptr )
|
---|
33 | {
|
---|
34 | er = new ascii_render_data;
|
---|
35 | er->border = false;
|
---|
36 | er->text_color = 0;
|
---|
37 | e->m_render_data = er;
|
---|
38 | }
|
---|
39 | else
|
---|
40 | er = (ascii_render_data*)( e->m_render_data );
|
---|
41 |
|
---|
42 | rectangle abs = e->m_absolute;
|
---|
43 | if ( abs != get_area() )
|
---|
44 | {
|
---|
45 | int color = 0;
|
---|
46 | std::string path;
|
---|
47 | std::string text;
|
---|
48 | const char* stext[] = { nullptr, "selected", "hover" };
|
---|
49 | const char* selector = stext[0];
|
---|
50 | if ( e->m_flags[HOVER] ) selector = stext[2];
|
---|
51 | if ( e->m_flags[SELECTED] ) selector = stext[1];
|
---|
52 |
|
---|
53 |
|
---|
54 | m_style.get( e, "ascii_color", selector, color );
|
---|
55 | er->text_color = uint32( color );
|
---|
56 | er->border = false;
|
---|
57 | if ( m_style.get( e, "ascii_border", selector, path ) )
|
---|
58 | {
|
---|
59 | er->border = true;
|
---|
60 | er->border_color = er->text_color;
|
---|
61 | int border_color = 0;
|
---|
62 | if ( m_style.get( e, "ascii_border_color", selector, border_color ) )
|
---|
63 | er->border_color = uint32( border_color );
|
---|
64 | for ( uint32 i = 0; i < 8 && i < path.length(); )
|
---|
65 | er->border_chars[i] = path[i];
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | void ascii_renderer::draw( element* e )
|
---|
71 | {
|
---|
72 | ascii_render_data* er = (ascii_render_data*)( e->m_render_data );
|
---|
73 | rectangle abs = e->m_absolute;
|
---|
74 | m_terminal->clear( abs );
|
---|
75 | if ( er->border )
|
---|
76 | {
|
---|
77 | for ( int x = 0; x < abs.get_width(); ++x )
|
---|
78 | {
|
---|
79 | m_terminal->print( position( abs.ul.y, abs.ul.x + x ), er->border_color, er->border_chars[0] );
|
---|
80 | m_terminal->print( position( abs.lr.y, abs.ul.x + x ), er->border_color, er->border_chars[1] );
|
---|
81 | }
|
---|
82 |
|
---|
83 | for ( int y = 0; y < abs.get_height(); ++y )
|
---|
84 | {
|
---|
85 | m_terminal->print( position( abs.ul.y + y, abs.ul.x ), er->border_color, er->border_chars[2] );
|
---|
86 | m_terminal->print( position( abs.ul.y + y, abs.lr.x ), er->border_color, er->border_chars[3] );
|
---|
87 | }
|
---|
88 |
|
---|
89 | m_terminal->print( abs.ul, er->border_color, er->border_chars[4] );
|
---|
90 | m_terminal->print( abs.ur(), er->border_color, er->border_chars[5] );
|
---|
91 | m_terminal->print( abs.ll(), er->border_color, er->border_chars[6] );
|
---|
92 | m_terminal->print( abs.lr, er->border_color, er->border_chars[7] );
|
---|
93 | }
|
---|
94 | if ( !e->m_text.empty() )
|
---|
95 | {
|
---|
96 | position p = abs.ul;
|
---|
97 | for ( char c : e->m_text )
|
---|
98 | {
|
---|
99 | m_terminal->print( p, er->text_color, c );
|
---|
100 | ++p.x;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | void ascii_renderer::draw()
|
---|
106 | {
|
---|
107 | m_terminal->update();
|
---|
108 | }
|
---|
109 |
|
---|
110 | void nv::gui::ascii_renderer::on_hover_change( element* e, bool hover )
|
---|
111 | {
|
---|
112 | // TODO: FIX
|
---|
113 | NV_LOG( nv::LOG_DEBUG, "on_hover_change" );
|
---|
114 | e->m_flags[DIRTY] = true;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void nv::gui::ascii_renderer::on_select_change( element* e, bool select )
|
---|
118 | {
|
---|
119 | // TODO: FIX
|
---|
120 | NV_LOG( nv::LOG_DEBUG, "on_select_change" );
|
---|
121 | e->m_flags[DIRTY] = true;
|
---|
122 | }
|
---|
123 |
|
---|
124 | rectangle ascii_renderer::get_area() const
|
---|
125 | {
|
---|
126 | return rectangle().dim( m_terminal->get_size() );
|
---|
127 | }
|
---|
128 |
|
---|
129 | ascii_renderer::~ascii_renderer()
|
---|
130 | {
|
---|
131 |
|
---|
132 | }
|
---|