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

Last change on this file since 356 was 356, checked in by epyon, 10 years ago
  • minor fixes to ascii gui renderer
  • ascii gui renderer works
File size: 3.4 KB
RevLine 
[355]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
9using namespace nv;
10using namespace nv::gui;
11
12
13struct ascii_render_data : public render_data
14{
15        ascii_render_data() {}
[356]16        bool   clear;
[355]17        bool   border;
18        char   border_chars[8];
19        uint32 border_color;
20        uint32 text_color;
21};
22
23
24ascii_renderer::ascii_renderer( terminal* t )
25        : m_terminal(t)
26{
27
28}
29
30void ascii_renderer::redraw( element* e, uint32 )
31{
32        ascii_render_data* er = nullptr;
33        if ( e->m_render_data == nullptr )
34        {
35                er = new ascii_render_data;
36                er->border     = false;
[356]37                er->clear      = false;
[355]38                er->text_color = 0;
39                e->m_render_data = er;
40        }
41        else
42                er = (ascii_render_data*)( e->m_render_data );
43
44        rectangle abs = e->m_absolute;
45        if ( abs != get_area() )
46        {
[356]47                er->clear = true;
[355]48                int color = 0;
49                std::string path;
50                std::string text;
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 );
58                er->text_color = uint32( color );
59                er->border     = false;
60                if ( m_style.get( e, "ascii_border", selector, path ) )
61                {
62                        er->border = true;
63                        er->border_color = er->text_color;
64                        int border_color = 0;
65                        if ( m_style.get( e, "ascii_border_color", selector, border_color ) )
66                                er->border_color = uint32( border_color );
67                        for ( uint32 i = 0; i < 8 && i < path.length(); )
68                                er->border_chars[i] = path[i];
69                }
70        }
71}
72
73void ascii_renderer::draw( element* e )
74{
75        ascii_render_data* er = (ascii_render_data*)( e->m_render_data );
76        rectangle abs = e->m_absolute;
[356]77        if ( er->clear ) m_terminal->clear( abs );
[355]78        if ( er->border )
79        {
80                for ( int x = 0; x < abs.get_width(); ++x )
81                {
82                        m_terminal->print( position( abs.ul.y, abs.ul.x + x ), er->border_color, er->border_chars[0] );
83                        m_terminal->print( position( abs.lr.y, abs.ul.x + x ), er->border_color, er->border_chars[1] );
84                }
85
86                for ( int y = 0; y < abs.get_height(); ++y )
87                {
88                        m_terminal->print( position( abs.ul.y + y, abs.ul.x ), er->border_color, er->border_chars[2] );
89                        m_terminal->print( position( abs.ul.y + y, abs.lr.x ), er->border_color, er->border_chars[3] );
90                }
91
92                m_terminal->print( abs.ul,   er->border_color, er->border_chars[4] );
93                m_terminal->print( abs.ur(), er->border_color, er->border_chars[5] );
94                m_terminal->print( abs.ll(), er->border_color, er->border_chars[6] );
95                m_terminal->print( abs.lr,   er->border_color, er->border_chars[7] );
96        }
97        if ( !e->m_text.empty() )
98        {
99                position p = abs.ul;
100                for ( char c : e->m_text )
101                {
102                        m_terminal->print( p, er->text_color, c );
103                        ++p.x;
104                }
105        }
106}
107
108void ascii_renderer::draw()
109{
110        m_terminal->update();
111}
112
113void nv::gui::ascii_renderer::on_hover_change( element* e, bool hover )
114{
115        // TODO: FIX
116        NV_LOG( nv::LOG_DEBUG, "on_hover_change" );
117        e->m_flags[DIRTY] = true;
118}
119
120void nv::gui::ascii_renderer::on_select_change( element* e, bool select )
121{
122        // TODO: FIX
123        NV_LOG( nv::LOG_DEBUG, "on_select_change" );
124        e->m_flags[DIRTY] = true;
125}
126
127rectangle ascii_renderer::get_area() const
128{
129        return rectangle().dim( m_terminal->get_size() );
130}
131
132ascii_renderer::~ascii_renderer()
133{
134
135}
Note: See TracBrowser for help on using the repository browser.