[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 | {
|
---|
| 17 | ascii_render_data() {}
|
---|
[356] | 18 | bool clear;
|
---|
[355] | 19 | bool border;
|
---|
[487] | 20 | char border_chars[8];
|
---|
[355] | 21 | uint32 border_color;
|
---|
| 22 | uint32 text_color;
|
---|
| 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 | er->text_color = 0;
|
---|
| 41 | e->m_render_data = er;
|
---|
| 42 | }
|
---|
| 43 | else
|
---|
[406] | 44 | er = static_cast< ascii_render_data* >( e->m_render_data );
|
---|
[355] | 45 |
|
---|
| 46 | rectangle abs = e->m_absolute;
|
---|
| 47 | if ( abs != get_area() )
|
---|
| 48 | {
|
---|
[356] | 49 | er->clear = true;
|
---|
[355] | 50 | int color = 0;
|
---|
[440] | 51 | string128 path;
|
---|
[355] | 52 | const char* stext[] = { nullptr, "selected", "hover" };
|
---|
| 53 | const char* selector = stext[0];
|
---|
| 54 | if ( e->m_flags[HOVER] ) selector = stext[2];
|
---|
| 55 | if ( e->m_flags[SELECTED] ) selector = stext[1];
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | m_style.get( e, "ascii_color", selector, color );
|
---|
| 59 | er->text_color = uint32( color );
|
---|
| 60 | er->border = false;
|
---|
| 61 | if ( m_style.get( e, "ascii_border", selector, path ) )
|
---|
| 62 | {
|
---|
| 63 | er->border = true;
|
---|
| 64 | er->border_color = er->text_color;
|
---|
| 65 | int border_color = 0;
|
---|
| 66 | if ( m_style.get( e, "ascii_border_color", selector, border_color ) )
|
---|
| 67 | er->border_color = uint32( border_color );
|
---|
[444] | 68 | for ( uint32 i = 0; i < 8 && i < path.length(); i++ )
|
---|
[487] | 69 | er->border_chars[i] = path[i];
|
---|
[355] | 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void ascii_renderer::draw( element* e )
|
---|
| 75 | {
|
---|
[406] | 76 | ascii_render_data* er = static_cast< ascii_render_data* >( e->m_render_data );
|
---|
[355] | 77 | rectangle abs = e->m_absolute;
|
---|
[356] | 78 | if ( er->clear ) m_terminal->clear( abs );
|
---|
[355] | 79 | if ( er->border )
|
---|
| 80 | {
|
---|
| 81 | for ( int x = 0; x < abs.get_width(); ++x )
|
---|
| 82 | {
|
---|
[444] | 83 | m_terminal->print( position( abs.ul.x + x, abs.ul.y ), er->border_color, er->border_chars[0] );
|
---|
| 84 | m_terminal->print( position( abs.ul.x + x, abs.lr.y ), er->border_color, er->border_chars[1] );
|
---|
[355] | 85 | }
|
---|
| 86 |
|
---|
| 87 | for ( int y = 0; y < abs.get_height(); ++y )
|
---|
| 88 | {
|
---|
[444] | 89 | m_terminal->print( position( abs.ul.x, abs.ul.y + y ), er->border_color, er->border_chars[2] );
|
---|
| 90 | m_terminal->print( position( abs.lr.x, abs.ul.y + y ), er->border_color, er->border_chars[3] );
|
---|
[355] | 91 | }
|
---|
| 92 |
|
---|
[406] | 93 | m_terminal->print( abs.ul, er->border_color, er->border_chars[4] );
|
---|
| 94 | m_terminal->print( abs.ur(), er->border_color, er->border_chars[5] );
|
---|
| 95 | m_terminal->print( abs.ll(), er->border_color, er->border_chars[6] );
|
---|
| 96 | m_terminal->print( abs.lr, er->border_color, er->border_chars[7] );
|
---|
[444] | 97 | m_terminal->update();
|
---|
[355] | 98 | }
|
---|
| 99 | if ( !e->m_text.empty() )
|
---|
| 100 | {
|
---|
| 101 | position p = abs.ul;
|
---|
| 102 | for ( char c : e->m_text )
|
---|
| 103 | {
|
---|
[487] | 104 | m_terminal->print( p, er->text_color, c );
|
---|
[355] | 105 | ++p.x;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void ascii_renderer::draw()
|
---|
| 111 | {
|
---|
| 112 | m_terminal->update();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[444] | 115 | void nv::gui::ascii_renderer::on_style_change( element* e )
|
---|
[355] | 116 | {
|
---|
[444] | 117 | m_style.load_flags( e );
|
---|
[355] | 118 | }
|
---|
| 119 |
|
---|
[444] | 120 | void nv::gui::ascii_renderer::on_hover_change( element* e )
|
---|
[355] | 121 | {
|
---|
[444] | 122 | if ( e->m_flags[DIRTY_HOVER] ) e->m_flags[DIRTY] = true;
|
---|
[355] | 123 | }
|
---|
| 124 |
|
---|
[444] | 125 | void nv::gui::ascii_renderer::on_select_change( element* e )
|
---|
| 126 | {
|
---|
| 127 | if ( e->m_flags[DIRTY_SELECT] ) e->m_flags[DIRTY] = true;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[355] | 130 | rectangle ascii_renderer::get_area() const
|
---|
| 131 | {
|
---|
| 132 | return rectangle().dim( m_terminal->get_size() );
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | ascii_renderer::~ascii_renderer()
|
---|
| 136 | {
|
---|
| 137 |
|
---|
| 138 | }
|
---|