0

I am currently developing an interactive story using OpenFL and I am currently going through the process of optimising my engine for a C++ output, since that is what my focus will be on. The master branch over on Github has a functional Flash target working, but this uses anchor links to identify hyperlinks in the links to different passages, and the Haxe code to parse using HScript.

I know that hyperlinks do not work in the C++ target, so I took a look at using the “getCharIndexAtPoint()” function to determine the location of a link in the htmlText field. I do not know what HTML text supports what in C++, but if none of the tags are supported at all, then I may as well write my own parser.

This is my current code for an attempt to parse my links using traditional link formatting in interactive stories:

private function parseLink(s:String):String
{
    _parsedLinks.splice(0, _parsedLinks.length);
    var pos:Int = 0;
    var content = s;
    while (pos < content.length) 
    {
        if (!(content.indexOf("[") > -1 && content.indexOf("]") > -1)) break;
        pos = content.indexOf("[");
        var subs = content.substring(pos + 1, content.indexOf("]"));
        if (subs.indexOf("|") > -1)
        {
            var link = subs.split("|");
            var parsedLink:ParsedLink = new ParsedLink();
            parsedLink.code = link[1];
            parsedLink.startIndex = s.indexOf(link[0]);
            parsedLink.endIndex = s.indexOf(link[0].charAt(link[0].length));
            trace("Link text: " + link[0] + "\n" + "Link code: " + link[1]);
            _parsedLinks.push(parsedLink);
            content = content.substr(parsedLink.endIndex + 1);
            trace("Content: " + content);
            s = StringTools.replace(s, parsedLink.code, "");
        }
    }

    s = StringTools.replace(s, "[", "");
    s = StringTools.replace(s, "]", "");
    s = StringTools.replace(s, "|", "");

    return s;
}

private function onLinkClicked(e:MouseEvent):Void
{
    var idx:Int = e.currentTarget.getCharIndexAtPoint(e.localX, e.localY);
    for (i in 0..._parsedLinks.length)
    {
        if (idx >= _parsedLinks[i].startIndex && idx < _parsedLinks[i].endIndex)
        {
            runCode(_parsedLinks[i].code);
            break;
        }
    }
}

At least, that's the general idea. Unfortunately, the given code results in this output. Any thoughts on how to solve this?

tienery
  • 172
  • 7

1 Answers1

0

It looks like you use "indexOf" to find the first square bracket, but you leave the square bracket in the text. At the end, you replace the square brackets, but the while loop will still probably continue to pick up the same square bracket at the beginning?

I think you need to at least use "indexOf" with the previous "pos" value, in order to continue to progress through the text. You might also consider using double square brackets ([[), double colons, or something else that's less likely to appear in the text on it's own :)

Joshua Granick
  • 1,017
  • 6
  • 6