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

Last change on this file since 525 was 520, checked in by epyon, 9 years ago
  • ecs updates
  • animation updates
  • ragdoll manager
  • lua has own random engine
  • several minor fixes
  • particle engine/particle group
  • shitload of other stuff
  • bullet world
File size: 3.8 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        bool       clear;
18        bool       border;
19        char       border_chars[8];
20        ivec2      padding;
21        term_color border_color;
22        term_color 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                e->m_render_data = er;
41        }
42        else
43                er = static_cast< ascii_render_data* >( e->m_render_data );
44
45        rectangle abs = e->m_absolute;
46        if ( abs != get_area() )
47        {
48                er->clear = true;
49                int color = 0;
50                string128 path;
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 = term_color::color( color );
59                er->border     = false;
60                vec4 padding;
61                m_style.get( e, "ascii_padding", selector, padding );
62                er->padding    = ivec2( int( padding.x ), int( padding.y ) );
63
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 ) )
70                                er->border_color = term_color::color( border_color );
71                        for ( uint32 i = 0; i < 8 && i < path.length(); i++ )
72                                er->border_chars[i] = path[i];
73                }
74        }
75}
76
77void ascii_renderer::draw( element* e )
78{
79        ascii_render_data* er = static_cast< ascii_render_data* >( e->m_render_data );
80        rectangle abs = e->m_absolute;
81        if ( er->clear ) m_terminal->clear( abs, term_color::LIGHTGRAY, term_color() );
82        if ( er->border )
83        {
84                for ( int x = 0; x < abs.get_width(); ++x )
85                {
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] );
88                }
89
90                for ( int y = 0; y < abs.get_height(); ++y )
91                {
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] );
94                }
95
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] );
100                //m_terminal->update();
101        }
102        if ( !e->m_text.empty() )
103        {
104                position p = abs.ul + er->padding;
105                for ( char c : e->m_text )
106                {
107                        m_terminal->print( p, er->text_color, term_color(), c );
108                        ++p.x;
109                }
110        }
111}
112
113void ascii_renderer::draw()
114{
115        m_terminal->update();
116}
117
118void nv::gui::ascii_renderer::on_style_change( element* e )
119{
120        m_style.load_flags( e );
121}
122
123void nv::gui::ascii_renderer::on_hover_change( element* e )
124{
125        if ( e->m_flags[DIRTY_HOVER] ) e->m_flags[DIRTY] = true;
126}
127
128void nv::gui::ascii_renderer::on_select_change( element* e )
129{
130        if ( e->m_flags[DIRTY_SELECT] ) e->m_flags[DIRTY] = true;
131}
132
133rectangle ascii_renderer::get_area() const
134{
135        return rectangle().dim( m_terminal->get_size() );
136}
137
138ascii_renderer::~ascii_renderer()
139{
140
141}
Note: See TracBrowser for help on using the repository browser.