void ElementDocumentWrapper::PreprocessCode(Rocket::Core::String &code, Rocket::Core::Stream *stream)
{
	// Load in the script	
	Rocket::Core::String buffer;	
	stream->Read(buffer, stream->Length());	

	// Strip comments and build up code
	code = "";
	
	size_t i = 0;
	size_t line_start = 0;
	enum ParseState { START, COMMENT, DATA };
	ParseState state = START;

	while (i < buffer.Length())
	{		
		// Python doesn't like \r's, strip them
		if (buffer[i] == '\r')
		{
			buffer.Erase(i, 1);

			// Make sure we get out if there are no characters left
			if (i == buffer.Length())
				continue;
		}

		switch (state)
		{
			case START:
			{
				// Check for the start of comments or non whitespace data
				if (buffer[i] == '#')
					state = COMMENT;
				else if (!Rocket::Core::StringUtilities::IsWhitespace(buffer[i]))
					state = DATA;
			}
			break;

			default:		
			{
				// If we've hit the end of the line, process as required
				if (buffer[i] == '\n')
				{				
					if (state == DATA)
						code += buffer.Substring(line_start, i - line_start + 1);

					state = START;
					line_start = i + 1;
				}
			}
			break;
		}
		
		i++;
	}
}
// Called by Rocket when a texture is required by the library.		
bool RocketSDL2Renderer::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
{

    Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
    Rocket::Core::FileHandle file_handle = file_interface->Open(source);
    if (!file_handle)
        return false;

    file_interface->Seek(file_handle, 0, SEEK_END);
    size_t buffer_size = file_interface->Tell(file_handle);
    file_interface->Seek(file_handle, 0, SEEK_SET);

    char* buffer = new char[buffer_size];
    file_interface->Read(buffer, buffer_size, file_handle);
    file_interface->Close(file_handle);

    size_t i;
    for(i = source.Length() - 1; i > 0; i--)
    {
        if(source[i] == '.')
            break;
    }

    Rocket::Core::String extension = source.Substring(i+1, source.Length()-i);

    SDL_Surface* surface = IMG_LoadTyped_RW(SDL_RWFromMem(buffer, buffer_size), 1, extension.CString());

    if (surface) {
        SDL_Texture *texture = SDL_CreateTextureFromSurface(mRenderer, surface);

        if (texture) {
            texture_handle = (Rocket::Core::TextureHandle) texture;
            texture_dimensions = Rocket::Core::Vector2i(surface->w, surface->h);
            SDL_FreeSurface(surface);
        }
        else
        {
            return false;
        }

        return true;
    }

    return false;
}
Пример #3
0
Rocket::Core::String Rocket_QuakeToRML(const char* in, int parseFlags = 0) {
    const char* p;
    Rocket::Core::String out;
    Rocket::Core::String spanstr;
    bool span = false;
    bool spanHasContent = false;

    if (!*in) {
        return "";
    }

    for (p = in; p && *p; ++p) {
        if (*p == '<') {
            if (span && !spanHasContent) {
                spanHasContent = true;
                out.Append(spanstr);
            }
            out.Append("&lt;");
        } else if (*p == '>') {
            if (span && !spanHasContent) {
                spanHasContent = true;
                out.Append(spanstr);
            }
            out.Append("&gt;");
        } else if (*p == '&') {
            if (span && !spanHasContent) {
                spanHasContent = true;
                out.Append(spanstr);
            }
            out.Append("&amp;");
        } else if (*p == '\n') {
            out.Append(span && spanHasContent ? "</span><br />" : "<br />");
            span = false;
            spanHasContent = false;
        } else if (Q_IsColorString(p)) {
            if (span && spanHasContent) {
                out.Append("</span>");
                span = false;
                spanHasContent = false;
            }

            char rgb[32];
            int code = ColorIndex(*++p);

            Com_sprintf(rgb, sizeof(rgb), "<span style='color: #%02X%02X%02X;'>", (int) (g_color_table[code][0] * 255), (int) (g_color_table[code][1] * 255), (int) (g_color_table[code][2] * 255));

            // don't add the span yet, because it might be empty
            spanstr = rgb;

            span = true;
            spanHasContent = false;
        } else {
            if (span && !spanHasContent) {
                out.Append(spanstr);
                spanHasContent = true;
            }
            out.Append(*p);
        }
    }

    if (span && spanHasContent) {
        out.Append("</span>");
    }

    // ^^ -> ^
    while (out.Find("^^") != Rocket::Core::String::npos) {
        out = out.Replace("^^", "^");
    }

    if (parseFlags & RP_EMOTICONS) {
        // Parse emoticons
        size_t openBracket = 0;
        size_t closeBracket = 0;
        size_t currentPosition = 0;

        while (1) {
            Rocket::Core::String emoticon;
            const char* path;

            openBracket = out.Find("[", currentPosition);
            if (openBracket == Rocket::Core::String::npos) {
                break;
            }

            closeBracket = out.Find("]", openBracket);
            if (closeBracket == Rocket::Core::String::npos) {
                break;
            }

            emoticon = out.Substring(openBracket + 1, closeBracket - openBracket - 1);

            // Certain characters are invalid
            if (emoticon.Empty() || IsInvalidEmoticon(emoticon)) {
                currentPosition = closeBracket + 1;
                continue;
            }

            // TODO: Dont hardcode the extension.
            path = va("emoticons/%s.crn", emoticon.CString());
            if (FS_FOpenFileRead(path, nullptr, true)) {
                out.Erase(openBracket, closeBracket - openBracket + 1);
                path = va("<img class='trem-emoticon' src='/emoticons/%s' />",
                          emoticon.CString());
                out.Insert(openBracket, path);
                currentPosition = openBracket + strlen(path) + 1;
            } else {
                currentPosition = closeBracket + 1;
            }
        }
    }

    return out;
}