sources/coloredline.cpp

branch
protocol5
changeset 106
7b156b764d11
parent 78
c1d43ade656e
parent 100
d301ead29d7c
child 131
4996c8684b93
equal deleted inserted replaced
104:a76af67a3a4b 106:7b156b764d11
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "coloredline.h" 31 #include "coloredline.h"
32 BEGIN_ZFC_NAMESPACE
32 33
33 static const struct 34 struct ColorCodeInfo
34 { 35 {
35 const char* name; 36 const char* name;
36 Color color; 37 Color color;
37 bool bold; 38 bool bold;
38 } g_colorCodes['v' - 'a' + 1] =
39 {
40 { "Brick", RED, true }, // a
41 { "Tan", YELLOW, true }, // b
42 { "Gray", WHITE, false }, // c
43 { "Green", GREEN, true }, // d
44 { "Brown", YELLOW, false }, // e
45 { "Gold", YELLOW, true }, // f
46 { "Red", RED, true }, // g
47 { "Blue", BLUE, false }, // h
48 { "Orange", YELLOW, false }, // i
49 { "White", WHITE, true }, // j
50 { "Yellow", YELLOW, true }, // k
51 { "Untranslated", DEFAULT, false }, // l
52 { "Black", BLACK, false }, // m
53 { "Blue", BLUE, true }, // n
54 { "Cream", YELLOW, true }, // o
55 { "Olive", GREEN, true }, // p
56 { "Dark Green", GREEN, false }, // q
57 { "Dark Red", RED, false }, // r
58 { "Dark Brown", YELLOW, false }, // s
59 { "Purple", MAGENTA, false }, // t
60 { "Dark Gray", BLACK, true }, // u
61 { "Cyan", CYAN, true }, // v
62 }; 39 };
40
41 ColoredLine::ColoredLine() :
42 m_length (0),
43 m_final (false),
44 m_activeColor (DEFAULT),
45 m_boldActive (false),
46 m_colorCodeStage (0) {}
63 47
64 // ------------------------------------------------------------------------------------------------- 48 // -------------------------------------------------------------------------------------------------
65 // 49 //
66 void ColoredLine::finalize() 50 void ColoredLine::finalize()
67 { 51 {
76 60
77 // ------------------------------------------------------------------------------------------------- 61 // -------------------------------------------------------------------------------------------------
78 // 62 //
79 void ColoredLine::add_char (char ch) 63 void ColoredLine::add_char (char ch)
80 { 64 {
65 static const ColorCodeInfo colorCodes[] =
66 {
67 { "Brick", RED, true }, // a
68 { "Tan", YELLOW, true }, // b
69 { "Gray", WHITE, false }, // c
70 { "Green", GREEN, true }, // d
71 { "Brown", YELLOW, false }, // e
72 { "Gold", YELLOW, true }, // f
73 { "Red", RED, true }, // g
74 { "Blue", BLUE, false }, // h
75 { "Orange", YELLOW, false }, // i
76 { "White", WHITE, true }, // j
77 { "Yellow", YELLOW, true }, // k
78 { "Untranslated", DEFAULT, false }, // l
79 { "Black", BLACK, false }, // m
80 { "Blue", BLUE, true }, // n
81 { "Cream", YELLOW, true }, // o
82 { "Olive", GREEN, true }, // p
83 { "Dark Green", GREEN, false }, // q
84 { "Dark Red", RED, false }, // r
85 { "Dark Brown", YELLOW, false }, // s
86 { "Purple", MAGENTA, false }, // t
87 { "Dark Gray", BLACK, true }, // u
88 { "Cyan", CYAN, true }, // v
89 };
90
81 if (m_final) 91 if (m_final)
82 return; // Don't touch finalized lines. 92 return; // Don't touch finalized lines.
83 93
84 if (ch == '\x1C' and m_colorCodeStage == 0) 94 if (ch == '\x1C' and m_colorCodeStage == 0)
85 { 95 {
99 if (ch >= 'A' and ch <= 'V') 109 if (ch >= 'A' and ch <= 'V')
100 ch += 'a' - 'A'; 110 ch += 'a' - 'A';
101 111
102 if (ch >= 'a' and ch <= 'v' and ch != 'l') 112 if (ch >= 'a' and ch <= 'v' and ch != 'l')
103 { 113 {
104 auto colorInfo = g_colorCodes[ch - 'a']; 114 auto colorInfo = colorCodes[ch - 'a'];
105 m_activeColor = colorInfo.color; 115 m_activeColor = colorInfo.color;
106 m_boldActive = colorInfo.bold; 116 m_boldActive = colorInfo.bold;
107 assert (m_activeColor < 8); 117 assert (m_activeColor < 8);
108 set_color (m_activeColor, true); 118 set_color (m_activeColor, true);
109 119
133 143
134 // ------------------------------------------------------------------------------------------------- 144 // -------------------------------------------------------------------------------------------------
135 // 145 //
136 void ColoredLine::set_color (Color a, bool on) 146 void ColoredLine::set_color (Color a, bool on)
137 { 147 {
138 switch (a) 148 assert (a < 8);
139 { 149 m_data << (a + (on ? RLINE_ON_COLOR : RLINE_OFF_COLOR));
140 case BLACK: m_data << (on ? RLINE_ON_BLACK : RLINE_OFF_BLACK); break;
141 case RED: m_data << (on ? RLINE_ON_RED : RLINE_OFF_RED); break;
142 case GREEN: m_data << (on ? RLINE_ON_GREEN : RLINE_OFF_GREEN); break;
143 case YELLOW: m_data << (on ? RLINE_ON_YELLOW : RLINE_OFF_YELLOW); break;
144 case BLUE: m_data << (on ? RLINE_ON_BLUE : RLINE_OFF_BLUE); break;
145 case MAGENTA: m_data << (on ? RLINE_ON_MAGENTA : RLINE_OFF_MAGENTA); break;
146 case CYAN: m_data << (on ? RLINE_ON_CYAN : RLINE_OFF_CYAN); break;
147 case WHITE: m_data << (on ? RLINE_ON_WHITE : RLINE_OFF_WHITE); break;
148 case NUM_COLORS:
149 case DEFAULT: assert (false); break;
150 }
151 } 150 }
152 151
153 // ------------------------------------------------------------------------------------------------- 152 // -------------------------------------------------------------------------------------------------
154 // How many rows does this line take up? 153 // How many rows does this line take up?
155 // 154 //
160 if (length() % cols != 0) 159 if (length() % cols != 0)
161 rows++; 160 rows++;
162 161
163 return max (rows, 1); 162 return max (rows, 1);
164 } 163 }
164
165 END_ZFC_NAMESPACE

mercurial