75546a45c3208b3b66917db70f985bc954e0988f
[airbus-aircraft:a380.git] / Nasal / TextRegion.nas
1 #
2 #   Simple utility to manage an array of text lines in a region.
3 #
4 #
5 #   Copyright (C) 2010 Scott Hamilton
6 #
7 #   You should have received a copy of the GNU General Public License
8 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
9 #
10
11 var TextRegion = {
12     new : func(lines, width, prefix){
13         var me = {parents:[TextRegion]};
14
15         me.posLine = 0;
16         me.baseNode = props.globals.getNode(prefix,1);
17         me.maxWidth = width;
18         me.maxLines = lines;
19         for(var p = 0; p != lines; p=p+1) {
20           var lineNode = me.baseNode.getNode("line["~p~"]",1);
21           var redNode  = me.baseNode.getNode("red["~p~"]",1);
22           var greenNode  = me.baseNode.getNode("green["~p~"]",1);
23           var blueNode  = me.baseNode.getNode("blue["~p~"]",1);
24           lineNode.setValue("");
25           redNode.setValue(0.1);
26           greenNode.setValue(0.8);
27           blueNode.setValue(0.1);
28         }
29
30         return me;
31     },
32
33    #
34    # append some text in default colour
35    #
36    append : func(text) {
37      me.appendStyle(text, 0.1, 0.8, 0.1);
38    },
39
40    #
41    # append some text to the region and set the colour
42    #
43    appendStyle : func(text, red, green, blue) {
44      var lineNode = me.baseNode.getNode("line["~me.posLine~"]",1);
45      var redNode  = me.baseNode.getNode("red["~me.posLine~"]",1);
46      var greenNode  = me.baseNode.getNode("green["~me.posLine~"]",1);
47      var blueNode  = me.baseNode.getNode("blue["~me.posLine~"]",1);
48      redNode.setValue(red);
49      greenNode.setValue(green);
50      blueNode.setValue(blue);
51      lineNode.setValue(text);
52      if (me.posLine < me.maxLines) {
53        me.posLine = me.posLine+1;
54      }
55    },
56
57    #
58    # simply replace the text at a particular line
59    #
60    textAt : func(index, text) {
61      var lineNode = me.baseNode.getNode("line["~index~"]",1);
62      lineNode.setValue(text);
63      me.posLine = index;
64    },
65
66    #
67    # clears out all the lines and resets the pointer
68    #
69    clear : func() {
70      for(var p = 0; p != me.maxLines; p=p+1) {
71           var lineNode = me.baseNode.getNode("line["~p~"]",1);
72           lineNode.setValue("");
73      }
74      me.posLine = 0;
75    },
76
77    #
78    # call reset when you are done writing to the region
79    # it will reset the pointer so next frame you start writing from the top again
80    #
81    reset : func() {
82      for(var p = me.posLine; p != me.maxLines; p=p+1) {
83        var lineNode = me.baseNode.getNode("line["~p~"]",1);
84        lineNode.setValue("");
85      }
86      me.posLine = 0;
87    }
88 }