summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/functiondebugstring.cpp21
-rw-r--r--src/functiondebugstring.h19
-rw-r--r--src/interfaceconsole.cpp218
-rw-r--r--src/interfaceconsole.h15
-rw-r--r--src/interfacegats.cpp50
-rw-r--r--src/smlrenderer.cpp10
-rw-r--r--src/smlrenderer.h17
-rw-r--r--src/smlrendererhtml.cpp62
-rw-r--r--src/smlrendererhtml.h15
-rw-r--r--src/smlrenderervt100.cpp229
-rw-r--r--src/smlrenderervt100.h32
11 files changed, 413 insertions, 275 deletions
diff --git a/src/functiondebugstring.cpp b/src/functiondebugstring.cpp
new file mode 100644
index 0000000..1c138d7
--- /dev/null
+++ b/src/functiondebugstring.cpp
@@ -0,0 +1,21 @@
1#include "functiondebugstring.h"
2
3#include <bu/sio.h>
4#include "gamestate.h"
5
6using namespace Bu;
7
8FunctionDebugString::FunctionDebugString()
9{
10}
11
12FunctionDebugString::~FunctionDebugString()
13{
14}
15
16void FunctionDebugString::call( class GameState &gState )
17{
18 Variable v = gState.popDeref();
19 gState.push( Variable( Bu::String("%1").arg( v ) ) );
20}
21
diff --git a/src/functiondebugstring.h b/src/functiondebugstring.h
new file mode 100644
index 0000000..cc7c6ac
--- /dev/null
+++ b/src/functiondebugstring.h
@@ -0,0 +1,19 @@
1#ifndef FUNCTION_DEBUG_STRING_H
2#define FUNCTION_DEBUG_STRING_H
3
4#include "function.h"
5
6class FunctionDebugString : public Function
7{
8public:
9 FunctionDebugString();
10 virtual ~FunctionDebugString();
11
12 virtual Bu::String getName() const { return "debugString"; }
13 virtual void call( class GameState &gState );
14
15private:
16 Bu::String format( const Bu::String &sSrc );
17};
18
19#endif
diff --git a/src/interfaceconsole.cpp b/src/interfaceconsole.cpp
index 94b45b9..caa13bf 100644
--- a/src/interfaceconsole.cpp
+++ b/src/interfaceconsole.cpp
@@ -7,6 +7,8 @@
7 7
8#include <bu/plugger.h> 8#include <bu/plugger.h>
9 9
10#include "smlrenderervt100.h"
11
10PluginInterface3( plugin_interface_console, console, InterfaceConsole, 12PluginInterface3( plugin_interface_console, console, InterfaceConsole,
11 Interface, "Mike Buland", 1, 0 ); 13 Interface, "Mike Buland", 1, 0 );
12 14
@@ -35,219 +37,9 @@ void InterfaceConsole::run( Game *pGame )
35 } 37 }
36} 38}
37 39
38void InterfaceConsole::appendToken( Bu::String &sCurLine, 40void InterfaceConsole::display( const class SmlNode *pSml )
39 Bu::String &sNextToken, int &iLineLen, int &iNextLen )
40{
41 if( iLineLen + iNextLen + 1 >= 78 )
42 {
43 sio << sCurLine << sio.nl;
44 iLineLen = 0;
45 sCurLine = sNextToken;
46 }
47 else
48 {
49 sCurLine += sNextToken;
50 }
51 iLineLen += iNextLen + 1;
52 sCurLine += " ";
53 iNextLen = 0;
54 sNextToken.clear();
55}
56
57Bu::String InterfaceConsole::getVt100Style( const StyleStack &sStyle )
58{
59#ifdef WIN32
60 // Windows...we don't do colors for windows...
61 return "";
62#endif
63
64 if( sStyle.isEmpty() )
65 {
66 return "\x1B[0m";
67 }
68
69 int sCurStyle = 0;
70 for( StyleStack::const_iterator i = sStyle.begin(); i; i++ )
71 {
72// sio << "Merging in: " << Fmt::hex() << *i << sio.nl;
73 if( ((sCurStyle&stTypeMask) & ((*i)&stTypeMask)) == 0 )
74 {
75 sCurStyle |= *i;
76// sio << " -> curStyle = " << Fmt::hex() << *i << sio.nl;
77 }
78 }
79
80 Bu::String sRet;
81
82// sio << "Color: " << Fmt::hex() << sCurStyle << sio.nl;
83 switch( sCurStyle&stColor )
84 {
85 case stRed:
86 sRet += "\x1B[1;31m";
87 break;
88
89 case stGreen:
90 sRet += "\x1B[1;32m";
91 break;
92 }
93
94 return sRet;
95}
96
97void InterfaceConsole::display( const SmlNode *pNode )
98{ 41{
99 Bu::String sCurLine; 42 SmlRendererVt100 rend;
100 Bu::String sNextToken; 43 rend.render( sio, pSml );
101 int iLineLen = 0;
102 int iNextLen = 0;
103 int iState = 0;
104 typedef Bu::List<SmlNode::SmlNodeList::const_iterator> NodeStack;
105 NodeStack sNode;
106
107 StyleStack sStyle;
108
109 sNode.push( pNode->getChildren().begin() );
110
111 for(;;)
112 {
113 if( !sNode.peek() )
114 {
115 sNode.pop();
116 if( sNode.isEmpty() )
117 break;
118 if( sNode.peek() )
119 {
120 // sio << "Pop'd: " << (*sNode.peek())->getText() << sio.nl;
121 Bu::String sTag = (*sNode.peek())->getText();
122 if( sTag == "green" || sTag == "red" )
123 {
124 sStyle.pop();
125 sNextToken += getVt100Style( sStyle );
126 }
127 sNode.peek()++;
128 continue;
129 }
130 }
131 if( sNode.isEmpty() )
132 {
133 break;
134 }
135 const SmlNode *pNode = (*sNode.peek());
136 switch( pNode->getType() )
137 {
138 case SmlNode::typeRoot:
139 throw Bu::ExceptionBase("Invalid root.");
140
141 case SmlNode::typeText:
142 {
143 // sio << "Process text node: " << pNode->getText() <<
144 // sio.nl;
145 Bu::String::const_iterator iBgn = pNode->getText().begin();
146 Bu::String::const_iterator iEnd = iBgn;
147 int iTmpLen = 0;
148 for(;iBgn;)
149 {
150 switch( iState )
151 {
152 case 0: // begining of paragraph
153 if( iBgn && ( *iBgn == ' ' || *iBgn == '\n' ||
154 *iBgn == '\r' || *iBgn == '\t' ) )
155 {
156 iBgn++;
157 }
158 else
159 {
160 // Here is where you would indent paragraphs
161 iEnd = iBgn;
162 iState = 1;
163 }
164 break;
165
166 case 1: // non-whitespace
167 if( !iEnd )
168 {
169 sNextToken.append( iBgn, iEnd );
170 iBgn = iEnd;
171 iNextLen += iTmpLen;
172 iTmpLen = 0;
173 }
174 else if( *iEnd == ' ' || *iEnd == '\n' ||
175 *iEnd == '\r' || *iEnd == '\t' )
176 {
177 sNextToken.append( iBgn, iEnd );
178 iNextLen += iTmpLen;
179 iTmpLen = 0;
180 iState = 2;
181 iBgn = iEnd;
182 }
183 else
184 {
185 iEnd++;
186 iTmpLen++;
187 }
188 break;
189
190 case 2: // Whitespace
191 if( iBgn && (*iBgn == ' ' || *iBgn == '\n' ||
192 *iBgn == '\r' || *iBgn == '\t') )
193 {
194 iBgn++;
195 }
196 else
197 {
198 iEnd = iBgn;
199 iState = 1;
200 appendToken( sCurLine, sNextToken,
201 iLineLen, iNextLen );
202 }
203 break;
204 }
205 }
206 }
207 break;
208
209 case SmlNode::typeTag:
210 if( pNode->getChildren().isEmpty() )
211 {
212 if( pNode->getText() == "break" )
213 {
214 appendToken( sCurLine, sNextToken, iLineLen, iNextLen );
215 if( !sCurLine.isEmpty() )
216 sio << sCurLine << sio.nl;
217 sCurLine.clear();
218 iLineLen = 0;
219 iState = 0;
220 }
221 }
222 else
223 {
224// sio << "Push'd: " << pNode->getText() << sio.nl;
225 Bu::String sTag = pNode->getText();
226 if( sTag == "green" )
227 {
228 sStyle.push( stGreen );
229 }
230 else if( sTag == "red" )
231 {
232 sStyle.push( stRed );
233 }
234 sNextToken += getVt100Style( sStyle );
235 sNode.push( pNode->getChildren().begin() );
236 continue;
237/* for( SmlNode::SmlNodeList::const_iterator i =
238 pNode->getChildren().begin(); i; i++ )
239 {
240 s
241 smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState );
242 }
243*/
244 }
245 break;
246 }
247 sNode.peek()++;
248 }
249 if( !sNextToken.isEmpty() )
250 appendToken( sCurLine, sNextToken, iLineLen, iNextLen );
251 sio << sCurLine << sio.nl;
252} 44}
253 45
diff --git a/src/interfaceconsole.h b/src/interfaceconsole.h
index 17b07e4..ce48003 100644
--- a/src/interfaceconsole.h
+++ b/src/interfaceconsole.h
@@ -17,21 +17,6 @@ public:
17 virtual void display( const class SmlNode *pSml ); 17 virtual void display( const class SmlNode *pSml );
18 18
19private: 19private:
20 enum Style
21 {
22 stRed = 0x010001,
23 stGreen = 0x010002,
24
25 stColor = 0x01000f,
26
27 stTypeMask = 0xff0000,
28 };
29
30 typedef Bu::List<Style> StyleStack;
31
32 void appendToken( Bu::String &sCurLine, Bu::String &sNextToken,
33 int &iLineLen, int &iNextLen );
34 Bu::String getVt100Style( const StyleStack &sStyle );
35}; 20};
36 21
37#endif 22#endif
diff --git a/src/interfacegats.cpp b/src/interfacegats.cpp
index c14a8f2..696c6b6 100644
--- a/src/interfacegats.cpp
+++ b/src/interfacegats.cpp
@@ -1,6 +1,7 @@
1#include "interfacegats.h" 1#include "interfacegats.h"
2 2
3#include "smlnode.h" 3#include "smlnode.h"
4#include "smlrendererhtml.h"
4 5
5#include <bu/plugger.h> 6#include <bu/plugger.h>
6#include <bu/sio.h> 7#include <bu/sio.h>
@@ -24,52 +25,7 @@ void InterfaceGats::run( class Game *pGame )
24 25
25void InterfaceGats::display( const SmlNode *pSml ) 26void InterfaceGats::display( const SmlNode *pSml )
26{ 27{
27 switch( pSml->getType() ) 28 SmlRendererHtml rend;
28 { 29 rend.render( sio, pSml );
29 case SmlNode::typeRoot:
30 sio << "<p>";
31 for( SmlNode::SmlNodeList::const_iterator i =
32 pSml->getChildren().begin(); i; i++ )
33 {
34 display( *i );
35 }
36 sio << "</p>";
37 break;
38
39 case SmlNode::typeText:
40 sio << pSml->getText();
41 break;
42
43 case SmlNode::typeTag:
44 if( pSml->getChildren().isEmpty() )
45 {
46 if( pSml->getText() == "break" )
47 sio << "</p><p>";
48 }
49 else
50 {
51 if( pSml->getText() == "red" )
52 {
53 sio << "<span style=\"color: red;\">";
54 for( SmlNode::SmlNodeList::const_iterator i =
55 pSml->getChildren().begin(); i; i++ )
56 {
57 display( *i );
58 }
59 sio << "</span>";
60 }
61 else if( pSml->getText() == "green" )
62 {
63 sio << "<span style=\"color: green;\">";
64 for( SmlNode::SmlNodeList::const_iterator i =
65 pSml->getChildren().begin(); i; i++ )
66 {
67 display( *i );
68 }
69 sio << "</span>";
70 }
71 }
72 break;
73 }
74} 30}
75 31
diff --git a/src/smlrenderer.cpp b/src/smlrenderer.cpp
new file mode 100644
index 0000000..a009439
--- /dev/null
+++ b/src/smlrenderer.cpp
@@ -0,0 +1,10 @@
1#include "smlrenderer.h"
2
3SmlRenderer::SmlRenderer()
4{
5}
6
7SmlRenderer::~SmlRenderer()
8{
9}
10
diff --git a/src/smlrenderer.h b/src/smlrenderer.h
new file mode 100644
index 0000000..aa79189
--- /dev/null
+++ b/src/smlrenderer.h
@@ -0,0 +1,17 @@
1#ifndef SML_RENDERER_H
2#define SML_RENDERER_H
3
4#include <bu/formatter.h>
5
6class SmlNode;
7
8class SmlRenderer
9{
10public:
11 SmlRenderer();
12 virtual ~SmlRenderer();
13
14 virtual void render( Bu::Formatter &f, const SmlNode *pRoot )=0;
15};
16
17#endif
diff --git a/src/smlrendererhtml.cpp b/src/smlrendererhtml.cpp
new file mode 100644
index 0000000..372d1e3
--- /dev/null
+++ b/src/smlrendererhtml.cpp
@@ -0,0 +1,62 @@
1#include "smlrendererhtml.h"
2#include "smlnode.h"
3
4SmlRendererHtml::SmlRendererHtml()
5{
6}
7
8SmlRendererHtml::~SmlRendererHtml()
9{
10}
11
12void SmlRendererHtml::render( Bu::Formatter &f, const SmlNode *pSml )
13{
14 switch( pSml->getType() )
15 {
16 case SmlNode::typeRoot:
17 f << "<p>";
18 for( SmlNode::SmlNodeList::const_iterator i =
19 pSml->getChildren().begin(); i; i++ )
20 {
21 render( f, *i );
22 }
23 f << "</p>";
24 break;
25
26 case SmlNode::typeText:
27 f << pSml->getText();
28 break;
29
30 case SmlNode::typeTag:
31 if( pSml->getChildren().isEmpty() )
32 {
33 if( pSml->getText() == "break" )
34 f << "</p><p>";
35 }
36 else
37 {
38 if( pSml->getText() == "red" )
39 {
40 f << "<span style=\"color: red;\">";
41 for( SmlNode::SmlNodeList::const_iterator i =
42 pSml->getChildren().begin(); i; i++ )
43 {
44 render( f, *i );
45 }
46 f << "</span>";
47 }
48 else if( pSml->getText() == "green" )
49 {
50 f << "<span style=\"color: green;\">";
51 for( SmlNode::SmlNodeList::const_iterator i =
52 pSml->getChildren().begin(); i; i++ )
53 {
54 render( f, *i );
55 }
56 f << "</span>";
57 }
58 }
59 break;
60 }
61}
62
diff --git a/src/smlrendererhtml.h b/src/smlrendererhtml.h
new file mode 100644
index 0000000..fe2e1b3
--- /dev/null
+++ b/src/smlrendererhtml.h
@@ -0,0 +1,15 @@
1#ifndef SML_RENDERER_HTML_H
2#define SML_RENDERER_HTML_H
3
4#include "smlrenderer.h"
5
6class SmlRendererHtml : public SmlRenderer
7{
8public:
9 SmlRendererHtml();
10 virtual ~SmlRendererHtml();
11
12 virtual void render( Bu::Formatter &f, const SmlNode *pRoot );
13};
14
15#endif
diff --git a/src/smlrenderervt100.cpp b/src/smlrenderervt100.cpp
new file mode 100644
index 0000000..2b59d5f
--- /dev/null
+++ b/src/smlrenderervt100.cpp
@@ -0,0 +1,229 @@
1#include "smlrenderervt100.h"
2#include "smlnode.h"
3
4SmlRendererVt100::SmlRendererVt100()
5{
6}
7
8SmlRendererVt100::~SmlRendererVt100()
9{
10}
11
12void SmlRendererVt100::appendToken( Bu::Formatter &f, Bu::String &sCurLine,
13 Bu::String &sNextToken, int &iLineLen, int &iNextLen )
14{
15 if( iLineLen + iNextLen + 1 >= 78 )
16 {
17 f << sCurLine << f.nl;
18 iLineLen = 0;
19 sCurLine = sNextToken;
20 }
21 else
22 {
23 sCurLine += sNextToken;
24 }
25 iLineLen += iNextLen + 1;
26 sCurLine += " ";
27 iNextLen = 0;
28 sNextToken.clear();
29}
30
31Bu::String SmlRendererVt100::getStyle( const StyleStack &sStyle )
32{
33#ifdef WIN32
34 // Windows...we don't do colors for windows...
35 return "";
36#endif
37
38 if( sStyle.isEmpty() )
39 {
40 return "\x1B[0m";
41 }
42
43 int sCurStyle = 0;
44 for( StyleStack::const_iterator i = sStyle.begin(); i; i++ )
45 {
46// f << "Merging in: " << Fmt::hex() << *i << f.nl;
47 if( ((sCurStyle&stTypeMask) & ((*i)&stTypeMask)) == 0 )
48 {
49 sCurStyle |= *i;
50// f << " -> curStyle = " << Fmt::hex() << *i << f.nl;
51 }
52 }
53
54 Bu::String sRet;
55
56// f << "Color: " << Fmt::hex() << sCurStyle << f.nl;
57 switch( sCurStyle&stColor )
58 {
59 case stRed:
60 sRet += "\x1B[1;31m";
61 break;
62
63 case stGreen:
64 sRet += "\x1B[1;32m";
65 break;
66 }
67
68 return sRet;
69}
70
71void SmlRendererVt100::render( Bu::Formatter &f, const SmlNode *pNode )
72{
73 Bu::String sCurLine;
74 Bu::String sNextToken;
75 int iLineLen = 0;
76 int iNextLen = 0;
77 int iState = 0;
78 typedef Bu::List<SmlNode::SmlNodeList::const_iterator> NodeStack;
79 NodeStack sNode;
80
81 StyleStack sStyle;
82
83 sNode.push( pNode->getChildren().begin() );
84
85 for(;;)
86 {
87 if( !sNode.peek() )
88 {
89 sNode.pop();
90 if( sNode.isEmpty() )
91 break;
92 if( sNode.peek() )
93 {
94 // f << "Pop'd: " << (*sNode.peek())->getText() << f.nl;
95 Bu::String sTag = (*sNode.peek())->getText();
96 if( sTag == "green" || sTag == "red" )
97 {
98 sStyle.pop();
99 sNextToken += getStyle( sStyle );
100 }
101 sNode.peek()++;
102 continue;
103 }
104 }
105 if( sNode.isEmpty() )
106 {
107 break;
108 }
109 const SmlNode *pNode = (*sNode.peek());
110 switch( pNode->getType() )
111 {
112 case SmlNode::typeRoot:
113 throw Bu::ExceptionBase("Invalid root.");
114
115 case SmlNode::typeText:
116 {
117 // f << "Process text node: " << pNode->getText() <<
118 // f.nl;
119 Bu::String::const_iterator iBgn = pNode->getText().begin();
120 Bu::String::const_iterator iEnd = iBgn;
121 int iTmpLen = 0;
122 for(;iBgn;)
123 {
124 switch( iState )
125 {
126 case 0: // begining of paragraph
127 if( iBgn && ( *iBgn == ' ' || *iBgn == '\n' ||
128 *iBgn == '\r' || *iBgn == '\t' ) )
129 {
130 iBgn++;
131 }
132 else
133 {
134 // Here is where you would indent paragraphs
135 iNextLen += 4;
136 sNextToken += " ";
137 iEnd = iBgn;
138 iState = 1;
139 }
140 break;
141
142 case 1: // non-whitespace
143 if( !iEnd )
144 {
145 sNextToken.append( iBgn, iEnd );
146 iBgn = iEnd;
147 iNextLen += iTmpLen;
148 iTmpLen = 0;
149 }
150 else if( *iEnd == ' ' || *iEnd == '\n' ||
151 *iEnd == '\r' || *iEnd == '\t' )
152 {
153 sNextToken.append( iBgn, iEnd );
154 iNextLen += iTmpLen;
155 iTmpLen = 0;
156 iState = 2;
157 iBgn = iEnd;
158 }
159 else
160 {
161 iEnd++;
162 iTmpLen++;
163 }
164 break;
165
166 case 2: // Whitespace
167 if( iBgn && (*iBgn == ' ' || *iBgn == '\n' ||
168 *iBgn == '\r' || *iBgn == '\t') )
169 {
170 iBgn++;
171 }
172 else
173 {
174 iEnd = iBgn;
175 iState = 1;
176 appendToken( f, sCurLine, sNextToken,
177 iLineLen, iNextLen );
178 }
179 break;
180 }
181 }
182 }
183 break;
184
185 case SmlNode::typeTag:
186 if( pNode->getChildren().isEmpty() )
187 {
188 if( pNode->getText() == "break" )
189 {
190 appendToken( f, sCurLine, sNextToken, iLineLen, iNextLen );
191 if( !sCurLine.isEmpty() )
192 f << sCurLine << f.nl;
193 sCurLine.clear();
194 iLineLen = 0;
195 iState = 0;
196 }
197 }
198 else
199 {
200// f << "Push'd: " << pNode->getText() << f.nl;
201 Bu::String sTag = pNode->getText();
202 if( sTag == "green" )
203 {
204 sStyle.push( stGreen );
205 }
206 else if( sTag == "red" )
207 {
208 sStyle.push( stRed );
209 }
210 sNextToken += getStyle( sStyle );
211 sNode.push( pNode->getChildren().begin() );
212 continue;
213/* for( SmlNode::SmlNodeList::const_iterator i =
214 pNode->getChildren().begin(); i; i++ )
215 {
216 s
217 smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState );
218 }
219*/
220 }
221 break;
222 }
223 sNode.peek()++;
224 }
225 if( !sNextToken.isEmpty() )
226 appendToken( f, sCurLine, sNextToken, iLineLen, iNextLen );
227 f << sCurLine << f.nl;
228}
229
diff --git a/src/smlrenderervt100.h b/src/smlrenderervt100.h
new file mode 100644
index 0000000..ed5dd42
--- /dev/null
+++ b/src/smlrenderervt100.h
@@ -0,0 +1,32 @@
1#ifndef SML_RENDERER_VT100_H
2#define SML_RENDERER_VT100_H
3
4#include "smlrenderer.h"
5
6class SmlRendererVt100 : public SmlRenderer
7{
8public:
9 SmlRendererVt100();
10 virtual ~SmlRendererVt100();
11
12 virtual void render( Bu::Formatter &f, const SmlNode *pRoot );
13
14private:
15 enum Style
16 {
17 stRed = 0x010001,
18 stGreen = 0x010002,
19
20 stColor = 0x01000f,
21
22 stTypeMask = 0xff0000,
23 };
24
25 typedef Bu::List<Style> StyleStack;
26
27 void appendToken( Bu::Formatter &f, Bu::String &sCurLine,
28 Bu::String &sNextToken, int &iLineLen, int &iNextLen );
29 Bu::String getStyle( const StyleStack &sStyle );
30};
31
32#endif