int Frigid::init() { //Load stuff //TODO: Change to the refresh rate of the monitor. //mWindow.setFramerateLimit(30); //Get the user folder path. char path[MAX_PATH]; if (SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path) != S_OK) { _error("Could not get the user path") } mPrefs = Preferences::instance(); mPrefs->setHomeDir(path); mPrefs->setConfigDir(String(path) + "/Documents/_quark"); BindingManager::instance()->setDefaultBindings(); loadConfig(); uint32 w = mPrefs->getStartingWindowWidth(); uint32 h = mPrefs->getStartingWindowHeight(); bool m = mPrefs->getStartMaximized(); if (w == 0 || h == 0) { m = true; w = 1024; h = 600; } mWindow = new RenderWindow(VideoMode(w, h, 32, m), "Quark"); mMainPanel = Panel(); tempHelpText = Text("Press control + ? for help.", *mPrefs->getFont(), 12); getPreferences(); CodeEditor *editor = new CodeEditor(); mMainPanel.setWidget(editor); Input::initInput(); WindowEvent e; e.type = WindowEvent::Resized; e.size.width = mWindow->getWidth(); e.size.height = mWindow->getHeight(); Rectf visibleArea(0, 0, staticCastf(e.size.width), staticCastf(e.size.height)); mWindow->setView(View(visibleArea)); mMainPanel.resizeEvent(e); update(); quit(); return 0; }
void Sprite::updateTexCoords() { float left = staticCastf(mTextureRect.left); float right = left + mTextureRect.width; float top = staticCastf(mTextureRect.top); float bottom = top + mTextureRect.height; mVertices[0].texCoords = Vector2f(left, top); mVertices[1].texCoords = Vector2f(left, bottom); mVertices[2].texCoords = Vector2f(right, top); mVertices[3].texCoords = Vector2f(right, bottom); }
Vector2f Text::findCharPos(sizeT index) const { if (!mFont) { return Vector2f(); } if (index > mString.getSize()) { index = mString.getSize(); } bool bold = (mStyle & Bold) != 0; float hspace = staticCastf(mFont->getGlyph(L'', mCharSize, bold).advance); float vspace = staticCastf(mFont->getLineSpacing(mCharSize)); Vector2f pos; uint32 prevChar = 0; for (sizeT i = 0; i < index; ++i) { uint32 currChar = mString[i]; pos.x += staticCastf(mFont->getKerning(prevChar, currChar, mCharSize)); prevChar = currChar; switch (currChar) { case ' ': { pos.x += hspace; } continue; case '\t': { pos.x += hspace * 4; } continue; case '\n': { pos.y += vspace; pos.x = 0; } continue; } pos.x += staticCastf(mFont->getGlyph(currChar, mCharSize, bold).advance); } pos = getTransform().transformPoint(pos); return pos; }
void Frigid::getPreferences() { if (!mFirstTimePrefsSet && false) { uint32 ow = mPrefs->getOldStartingWindowWidth(); uint32 oh = mPrefs->getOldStartingWindowHeight(); uint32 w = mPrefs->getStartingWindowWidth(); uint32 h = mPrefs->getStartingWindowHeight(); bool m = mPrefs->getStartMaximized(); if (ow != w || oh != h) { std::cerr << "killing window sparing childeren\n"; if (w == 0 || h == 0) { m = true; w = 1024; h = 600; } delete mWindow; mWindow = new RenderWindow(VideoMode(w, h, 32, m), "Quark"); WindowEvent e; e.type = WindowEvent::Resized; e.size.width = mWindow->getWidth(); e.size.height = mWindow->getHeight(); Rectf visibleArea(0, 0, staticCastf(e.size.width), staticCastf(e.size.height)); mWindow->setView(View(visibleArea)); mMainPanel.resizeEvent(e); } } mFirstTimePrefsSet = false; mClearColor = mPrefs->getTheme().clearColor; mWindow->setOpacity(mPrefs->getWindowOpacity()); mWindow->setFramerateLimit(mPrefs->getFrameRate()); mMainPanel.getPreferences(); tempHelpText.setColor(mPrefs->getTheme().textColor); tempHelpText.setFont(*mPrefs->getFont()); }
void Frigid::update() { while (mWindow->isOpen()) { WindowEvent e; Input::updateInput(); while (mWindow->pollEvent(e)) { switch (e.type) { case WindowEvent::Closed: { mWindow->close(); } break; case WindowEvent::Resized: { Rectf visibleArea(0, 0, staticCastf(e.size.width), staticCastf(e.size.height)); mWindow->setView(View(visibleArea)); mMainPanel.resizeEvent(e); } break; case WindowEvent::TextEntered: { //TODO: Create proper filter. if (e.text.unicode >= 32 && e.text.unicode <= 126) { mMainPanel.enterText(e.text.unicode); } } break; case WindowEvent::KeyPressed: { Input::Key k = e.key.code; checkBindings(); mMainPanel.keyPressed(e); } break; case WindowEvent::MouseWheelMoved: { mMainPanel.mouseWheelMoved(e); } break; //TODO!: Come up with a GUI system! case WindowEvent::GainedFocus: { mMainPanel.setHasFocus(true); } break; case WindowEvent::LostFocus: { mMainPanel.setHasFocus(false); } break; case WindowEvent::MouseButtonPressed: { mMainPanel.mouseButtonPressed(e); } break; } } lua_State *L = mPrefs->getLuaState(); if (L) { lua_getglobal(L, "update"); if (lua_isfunction(L, -1)) { lua_pcall(L, 0, 0, 0); } else { lua_pop(L, 1); } } uint32 w = tempHelpText.getLocalBounds().width; tempHelpText.setPosition((mWindow->getWidth() / 2) - (w / 2), 2); mWindow->clear(mClearColor); mMainPanel.update(mWindow); mWindow->draw(tempHelpText); mWindow->display(/*mWindow.hasFocus()*/); } lua_close(mPrefs->getLuaState()); }
void CodeEditor::update(RenderWindow *window) { mTopbar.setSize(window->getWidth() - 8, mTopbar.getSize().y); if (mLineHighlightType != LineHighlightType::LineNumberArea) { mLineHighlightRect.setSize(window->getWidth() - 8, mLineHighlightRect.getSize().y); } else { mLineHighlightRect.setSize(mLineNumberAreaWidth, mLineHighlightRect.getSize().y); } //TODO: move the cursor when the text is scrolled. //TODO: make the above optional //TODO: make it so that when the cursor is moved outside the view, the view is back on the cursor. uint32 cursorY = mText[mCurrentLine].getPosition().y + 2; //Check to see if we scrolled to far in either direction. if (mText.size() > 1) { if (mText[0].getPosition().y > mPos.y) { uint32 deltaY = (mText[0].getPosition().y - mPos.y); for (uint32 i = 0; i < mText.size(); ++i) { mText[i].setPosition(mText[i].getPosition().x, mText[i].getPosition().y - deltaY); mLineNumbers[i].setPosition(mLineNumbers[i].getPosition().x, mLineNumbers[i].getPosition().y - deltaY); } } uint32 max = mText.size() - 1; if (mText[max].getPosition().y < mPos.y) { uint32 deltaY = (mPos.y - mText[max].getPosition().y); for (uint32 i = 0; i < mText.size(); ++i) { mText[i].setPosition(mText[i].getPosition().x, mText[i].getPosition().y + deltaY); mLineNumbers[i].setPosition(mLineNumbers[i].getPosition().x, mLineNumbers[i].getPosition().y + deltaY); } } cursorY = mText[mCurrentLine].getPosition().y + 2; if (mText[mCurrentLine].getPosition().y < mPos.y) { for (uint32 i = mCurrentLine; i < mText.size(); ++i) { if (mText[i].getPosition().y >(mPos.y - mFontSize)) { mCurrentLine = i; cursorY = mText[i].getPosition().y + 2; break; } } } else if (mText[mCurrentLine].getPosition().y > (mPos.y + mSize.y - (mFontSize * 2))) { for (int32 i = mCurrentLine; i >= 0; --i) { if (mText[i].getPosition().y < (mPos.y + mSize.y - (mFontSize * 2))) { mCurrentLine = i; cursorY = mText[i].getPosition().y + 2; break; } } } } if (mCursorType == CursorType::Underscore) { cursorY += Preferences::instance()->getFont()->getLineSpacing(mFontSize) - 2; } uint32 cursorX = 0; mUpperBound = 0; if (mLineIndex >= getLineLength(mCurrentLine)) { mUpperBound = getLineLength(mCurrentLine); } else { mUpperBound = mLineIndex; } for (int i = 0; i < mUpperBound; ++i) { uint32 c = getLine(mCurrentLine)[i]; if (c == '\t') { cursorX += (getCharWidth(L'') * mTabWidth); } else { cursorX += getCharWidth(c); } } uint32 markX = 0; for (int i = 0; i < mMarkIndex; ++i) { uint32 c = getLine(mMarkLine)[i]; if (c == '\t') { markX += (getCharWidth(L'') * mTabWidth); } else { markX += getCharWidth(c); } } uint32 markY = mText[mMarkLine].getPosition().y + 2; mCursorRect.setPosition(mPos.x + mLineNumberAreaWidth + cursorX /*offset*/, cursorY); mCurrentChar.setPosition(mCursorRect.getPosition().x, mCursorRect.getPosition().y - 2); mMarkRect.setPosition(mPos.x + mLineNumberAreaWidth + markX /*offset*/, markY); if (mLineHighlightType != LineHighlightType::CodeArea) { mLineHighlightRect.setPosition(mPos.x, cursorY); } else { mLineHighlightRect.setPosition(mPos.x + mLineNumberAreaWidth, cursorY); } if (mCursorType != CursorType::CursorLine) { mCursorRect.setWidth(getCharWidth(getCurrentChar())); } mMarkRect.setWidth(getCharWidth(getMarkChar())); if (!mFirstMarkSet) { setMark(); mFirstMarkSet = true; } mCurrentChar.setString(getCurrentChar()); bool useMilitaryTime = Preferences::instance()->useMilitaryTime(); Clock clock(useMilitaryTime); int32 hour = clock.getCurrentHour(); int32 minute = clock.getCurrentMinute(); int32 second = clock.getCurrentSecond(); String ampm = clock.isPm() ? "PM" : "AM"; ampm = useMilitaryTime ? "" : ampm; String hourStr = String::number(hour); String minuteStr = String::number(minute); String secondStr = String::number(second); if (minuteStr.getLength() == 1) { minuteStr.prepend("0"); } if (secondStr.getLength() == 1) { secondStr.prepend("0"); } Date date = Date::currentDate(); String time = Preferences::instance()->showClock() ? hourStr + ":" + minuteStr + ":" + secondStr + " " + ampm : ""; String dateStr = date.getDayName() + " " + date.getMonthName() + " " + String::number(date.dayOfMonth()) + " " + String::number(date.getYear()) + " week of the year: " + String::number(date.weekNumber()); if (mClock.getCurrentTimeInMilliseconds() >= (timeNow + 1000)) { timeNow = mClock.getCurrentTimeInMilliseconds(); frameRate = frames; frames = 0; } uint32 p = (staticCastf(mCurrentLine + 1) / staticCastf(getLineCount())) * 100; mTopBarText.setString(mFilename + "\t" + String::number(p) + "% " + String::number(mCurrentLine + 1) + ":u" + String::number(mUpperBound) + ", i" + String::number(mLineIndex) + "\t" + dateStr + " " + time + "\tfps: " + String::number(frameRate)); //Draw///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (mShowLineNumbers) { window->draw(mLineNumberArea); } if (mShowLineHighlight) { window->draw(mLineHighlightRect); } for (uint32 i = 0; i < mText.size(); ++i) { if ((mText[i].getPosition().y < (mPos.y - mFontSize)) || (mText[i].getPosition().y >= mPos.y + mSize.y)) { continue; } if (mShowLineHighlight && mLineHighlightType != LineHighlightType::LineNumberArea) { if (i == mCurrentLine) { mText[i].setColor(mLineHighlightTextColor); } if (i != mCurrentLine && mText[i].getColor() != mTextColor) { mText[i].setColor(mTextColor); } } if (mShowLineNumbers) { window->draw(mLineNumbers[i]); } window->draw(mText[i]); } if (mClock.getCurrentTimeInMilliseconds() >= (mLastBlinkTime + 500)) { mLastBlinkTime = mClock.getCurrentTimeInMilliseconds(); mShowCursor = !mShowCursor; } if (!mBlinkCursor) { mShowCursor = true; } if (mShowCursor) { window->draw(mCursorRect); } if (mShowMark) { window->draw(mMarkRect); } String str = mCurrentChar.getString(); if (mShowCursor && mCursorType == CursorType::Block && !str.isEmpty() && str != "\r") { window->draw(mCurrentChar); } mTopbar.setFillColor(Preferences::instance()->getTheme().barColor); window->draw(mTopbar); window->draw(mTopBarText); ++frames; }
void Text::ensureGeometryUpdates() const { if (!mGeometryNeedsUpdate) { return; } mGeometryNeedsUpdate = false; mVertices.clear(); mBounds = Rectf(); if (!mFont) { return; } if (mString.isEmpty()) { return; } bool bold = (mStyle & Bold) != 0; bool underlined = (mStyle & Underlined) != 0; bool strikeThrough = (mStyle & StrikeThrough) != 0; float italic = (mStyle & Italic) ? 0.208f : 0.f; float underlineOffset = mFont->getUnderlinePosition(mCharSize); float underlineThickness = mFont->getUnderlineThickness(mCharSize); Rectf xBounds = mFont->getGlyph(L'x', mCharSize, bold).bounds; float strikeThroughOffset = xBounds.top + xBounds.height / 2.f; float hspace = staticCastf(mFont->getGlyph(L'', mCharSize, bold).advance); float vspace = staticCastf(mFont->getLineSpacing(mCharSize)); float x = 0.f; float y = staticCastf(mCharSize); float minX = staticCastf(mCharSize); float minY = staticCastf(mCharSize); float maxX = 0.f; float maxY = 0.f; uint32 prevChar = 0; bool expextClosingBracket = false; Color color = mColor; uint32 foundWordEndPos = 0; bool wordFound = false; for (sizeT i = 0; i < mString.getSize(); ++i) { uint32 currChar = mString[i]; if (i != 0) { prevChar = mString[i - 1]; } uint32 nextChar = 0; if (i < mString.getSize()) { nextChar = mString[i + 1]; } x += staticCastf(mFont->getKerning(prevChar, currChar, mCharSize)); if (underlined && (currChar == L'\n')) { float top = std::floor(y + underlineOffset - (underlineThickness / 2) + 0.5f); float botttom = top + std::floor(underlineThickness + 0.5f); mVertices.append(Vertex(Vector2f(0, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(0, botttom), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(0, botttom), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, botttom), mColor, Vector2f(1, 1))); } if (strikeThrough && (currChar == L'\n')) { float top = std::floor(y + strikeThroughOffset - (underlineThickness / 2) + 0.5f); float botttom = top + std::floor(underlineThickness + 0.5f); mVertices.append(Vertex(Vector2f(0, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(0, botttom), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(0, botttom), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, botttom), mColor, Vector2f(1, 1))); } if (currChar == ' ' || currChar == '\t' || currChar == '\n') { minX = std::min(minX, x); minY = std::min(minY, y); switch (currChar) { case ' ': { x += hspace; } break; case '\t': { x += hspace * 4; } break; case '\n': { y += vspace; x = 0; } break; } maxX = std::max(maxX, x); maxY = std::max(maxY, y); continue; } //* if (mDoMultiColor) { if (currChar == '[' && prevChar != '\\') { String str = ""; while (currChar != ']') { currChar = mString[++i]; if (currChar != ']') { str += currChar; } } if (str == "red") { color = Color::Red; } else if (str == "blue") { color = Color::Blue; } else if (str == "white") { color = Color::White; } else if (str == "black") { color = Color::Black; } else if (str == "green") { color = Color::Green; } else if (str == "yellow") { color = Color::Yellow; } else if (str == "Magenta") { color = Color::Magenta; } else if (str == "cyan") { color = Color::Cyan; } else if (str == "transparent") { color = Color::Transparent; } else { if (str.contains(',')) { String newColorStr[4]; for (uint32 k = 0; k <= 3; ++k) { newColorStr[k] = "255"; } if (isdigit(str[0])) { newColorStr[0] = ""; } uint c = 0; for (uint32 j = 0; j < str.getLength(); ++j) { if (isdigit(str[j]) && c <= 3) { newColorStr[c] += str[j]; } else if (str[j] == ',') { ++c; if (j != str.getLength() - 1) { newColorStr[c] = ""; } } } color = Color(newColorStr[0].toUint8(), newColorStr[1].toUint8(), newColorStr[2].toUint8(), newColorStr[3].toUint8()); } else { color = mColor; } } continue; } } else { //Does not cause open file slowdown. uint32 wordWidth = 0; String word = mString.nextWord(i, wordWidth); if (!wordFound) { color = mColor; } if (mString.startsWith("#", true)) { color = Color(142, 192, 124); foundWordEndPos = mString.getLength(); wordFound = true; } else if (mString.startsWith("//", true) || mString.startsWith("--", true)) { color = Color(146, 131, 116); foundWordEndPos = mString.getLength(); wordFound = true; }/* else if (word == "uint32" || word == "int" || word == "float" || word == "char" || word == "void") { color = Color(242, 189, 47); foundWordEndPos = --wordWidth; wordFound = true; } else if (word == "if" || word == "else" || word == "return" || word == "for" || word == "break" || word == "switch" || word == "case" || word == "while" || word == "continue" || word =="do" || word == "goto" || word == "function" || word == "end" || word == "then" || word == "and" || word =="or") { color = Color(251, 73 ,44); foundWordEndPos = --wordWidth; wordFound = true; } else if (word == "true" || word == "false") { color = Color(211, 134, 155); foundWordEndPos = --wordWidth; wordFound = true; }//*/ //std::cerr << word.toStdString() << "\n"; if (i > foundWordEndPos) { color = mColor; wordFound = false; } } //*/ const Glyph &glyph = mFont->getGlyph(currChar, mCharSize, bold); float left = glyph.bounds.left; float top = glyph.bounds.top; float right = glyph.bounds.left + glyph.bounds.width; float bottom = glyph.bounds.top + glyph.bounds.height; float u1 = staticCastf(glyph.textureRect.left); float v1 = staticCastf(glyph.textureRect.top); float u2 = staticCastf(glyph.textureRect.left + glyph.textureRect.width); float v2 = staticCastf(glyph.textureRect.top + glyph.textureRect.height); mVertices.append(Vertex(Vector2f(x + left - italic * top, y + top), color, Vector2f(u1, v1))); mVertices.append(Vertex(Vector2f(x + right - italic * top, y + top), color, Vector2f(u2, v1))); mVertices.append(Vertex(Vector2f(x + left - italic * bottom, y + bottom), color, Vector2f(u1, v2))); mVertices.append(Vertex(Vector2f(x + left - italic * bottom, y + bottom), color, Vector2f(u1, v2))); mVertices.append(Vertex(Vector2f(x + right - italic * top, y + top), color, Vector2f(u2, v1))); mVertices.append(Vertex(Vector2f(x + right - italic * bottom, y + bottom), color, Vector2f(u2, v2))); minX = std::min(minX, x + left - italic * bottom); maxX = std::max(maxX, x + right - italic * top); minY = std::min(minY, y + top); maxY = std::max(maxY, y + bottom); x += glyph.advance; } if (underlined) { float top = std::floor(y + underlineOffset - (underlineThickness / 2) + 0.5f); float botttom = top + std::floor(underlineThickness + 0.5f); mVertices.append(Vertex(Vector2f(0, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(0, botttom), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(0, botttom), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, botttom), mColor, Vector2f(1, 1))); } if (strikeThrough) { float top = std::floor(y + strikeThroughOffset - (underlineThickness / 2) + 0.5f); float botttom = top + std::floor(underlineThickness + 0.5f); mVertices.append(Vertex(Vector2f(0, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(0, botttom), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(0, botttom), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, top), mColor, Vector2f(1, 1))); mVertices.append(Vertex(Vector2f(x, botttom), mColor, Vector2f(1, 1))); } mBounds.left = minX; mBounds.top = minY; mBounds.width = maxX - minX; mBounds.height = maxY - minY; }
Rectf Sprite::getLocalBounds() const { float w = staticCastf(std::abs(mTextureRect.width)); float h = staticCastf(std::abs(mTextureRect.height)); return Rectf(0.f, 0.f, w, h); }