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