source: trunk/src/gui/gui_ascii_renderer.cc @ 437

Last change on this file since 437 was 406, checked in by epyon, 10 years ago
  • code compiles cleanly on maximum warning level
File size: 3.5 KB
Line 
1// Copyright (C) 2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/gui/gui_ascii_renderer.hh"
8
9#include "nv/core/logging.hh"
10
11using namespace nv;
12using namespace nv::gui;
13
14
15struct ascii_render_data : public render_data
16{
17        ascii_render_data() {}
18        bool   clear;
19        bool   border;
20        uchar8 border_chars[8];
21        uint32 border_color;
22        uint32 text_color;
23};
24
25
26ascii_renderer::ascii_renderer( terminal* t )
27        : m_terminal(t)
28{
29
30}
31
32void 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;
39                er->clear      = false;
40                er->text_color = 0;
41                e->m_render_data = er;
42        }
43        else
44                er = static_cast< ascii_render_data* >( e->m_render_data );
45
46        rectangle abs = e->m_absolute;
47        if ( abs != get_area() )
48        {
49                er->clear = true;
50                int color = 0;
51                std::string path;
52                std::string text;
53                const char* stext[] = { nullptr, "selected", "hover" };
54                const char* selector = stext[0];
55                if ( e->m_flags[HOVER] )    selector = stext[2];
56                if ( e->m_flags[SELECTED] ) selector = stext[1];
57
58               
59                m_style.get( e, "ascii_color", selector, color );
60                er->text_color = uint32( color );
61                er->border     = false;
62                if ( m_style.get( e, "ascii_border", selector, path ) )
63                {
64                        er->border = true;
65                        er->border_color = er->text_color;
66                        int border_color = 0;
67                        if ( m_style.get( e, "ascii_border_color", selector, border_color ) )
68                                er->border_color = uint32( border_color );
69                        for ( uint32 i = 0; i < 8 && i < path.length(); )
70                                er->border_chars[i] = static_cast< uchar8 >( path[i] );
71                }
72        }
73}
74
75void ascii_renderer::draw( element* e )
76{
77        ascii_render_data* er = static_cast< ascii_render_data* >( e->m_render_data );
78        rectangle abs = e->m_absolute;
79        if ( er->clear ) m_terminal->clear( abs );
80        if ( er->border )
81        {
82                for ( int x = 0; x < abs.get_width(); ++x )
83                {
84                        m_terminal->print( position( abs.ul.y, abs.ul.x + x ), er->border_color, er->border_chars[0] );
85                        m_terminal->print( position( abs.lr.y, abs.ul.x + x ), er->border_color, er->border_chars[1] );
86                }
87
88                for ( int y = 0; y < abs.get_height(); ++y )
89                {
90                        m_terminal->print( position( abs.ul.y + y, abs.ul.x ), er->border_color, er->border_chars[2] );
91                        m_terminal->print( position( abs.ul.y + y, abs.lr.x ), er->border_color, er->border_chars[3] );
92                }
93
94                m_terminal->print( abs.ul,   er->border_color, er->border_chars[4] );
95                m_terminal->print( abs.ur(), er->border_color, er->border_chars[5] );
96                m_terminal->print( abs.ll(), er->border_color, er->border_chars[6] );
97                m_terminal->print( abs.lr,   er->border_color, er->border_chars[7] );
98        }
99        if ( !e->m_text.empty() )
100        {
101                position p = abs.ul;
102                for ( char c : e->m_text )
103                {
104                        m_terminal->print( p, er->text_color, static_cast< unsigned char >( c ) );
105                        ++p.x;
106                }
107        }
108}
109
110void ascii_renderer::draw()
111{
112        m_terminal->update();
113}
114
115void nv::gui::ascii_renderer::on_hover_change( element* e, bool /*hover*/ )
116{
117        // TODO: FIX
118        int fix_me;
119        NV_LOG_DEBUG( "on_hover_change" );
120        e->m_flags[DIRTY] = true;
121}
122
123void nv::gui::ascii_renderer::on_select_change( element* e, bool /*select*/ )
124{
125        // TODO: FIX
126        int fix_me;
127        NV_LOG_DEBUG( "on_select_change" );
128        e->m_flags[DIRTY] = true;
129}
130
131rectangle ascii_renderer::get_area() const
132{
133        return rectangle().dim( m_terminal->get_size() );
134}
135
136ascii_renderer::~ascii_renderer()
137{
138
139}
Note: See TracBrowser for help on using the repository browser.