void MOText::draw(void) { MFont * font = getFont(); const char * text = m_text.getData(); if(! (strlen(text) > 0 && font)) return; if(m_linesOffset.size() == 0) return; MRenderingContext * render = MEngine().getInstance()->getRenderingContext(); render->enableBlending(); render->enableTexture(); render->disableLighting(); render->setColor4(m_color); render->setBlendingMode(M_BLENDING_ALPHA); MVector2 vertices[4]; MVector2 texCoords[4]; render->disableNormalArray(); render->disableColorArray(); render->enableVertexArray(); render->enableTexCoordArray(); render->setVertexPointer(M_FLOAT, 2, vertices); render->setTexCoordPointer(M_FLOAT, 2, texCoords); render->bindTexture(font->getTextureId()); unsigned int lineId = 0; float lineOffset = m_linesOffset[0]; float tabSize = m_size*2; float fontSize = (float)font->getFontSize(); float widthFactor = font->getTextureWith() / fontSize; float heightFactor = font->getTextureHeight() / fontSize; float xc = 0, yc = 0; unsigned int i; unsigned int size = strlen(text); for(i=0; i<size; i++) { if(text[i] == '\n') // return { if(((i+1) < size)) { lineId++; lineOffset = m_linesOffset[lineId]; yc += m_size; xc = 0; } continue; } if(text[i] == '\t') // tab { int tab = (int)(xc / tabSize) + 1; xc = tab*tabSize; continue; } // get character unsigned int charCode = (unsigned int)((unsigned char)text[i]); MCharacter * character = font->getCharacter(charCode); if(! character) continue; MVector2 pos = character->getPos(); MVector2 scale = character->getScale(); MVector2 offset = character->getOffset() * m_size + MVector2(lineOffset, 0); float width = scale.x * widthFactor * m_size; float height = scale.y * heightFactor * m_size; // construct quad texCoords[0] = MVector2(pos.x, (pos.y + scale.y)); vertices[0] = MVector2(xc, (yc + height)) + offset; texCoords[1] = MVector2((pos.x + scale.x), (pos.y + scale.y)); vertices[1] = MVector2((xc + width), (yc + height)) + offset; texCoords[3] = MVector2((pos.x + scale.x), pos.y); vertices[3] = MVector2((xc + width), yc) + offset; texCoords[2] = MVector2(pos.x, pos.y); vertices[2] = MVector2(xc, yc) + offset; // draw quad render->drawArray(M_PRIMITIVE_TRIANGLE_STRIP, 0, 4); //move to next character xc += character->getXAdvance() * m_size; } }
void MGuiEditText::draw(void) { MRenderingContext * render = MGui::getInstance()->getRenderingContext(); if(! isVisible()) return; if(! isPressed()) updateFromVariable(); // draw selection if(isPressed()) { render->disableTexture(); render->setColor4(MVector4(1, 0.1f, 0.1f, 0.3f)); getFont()->drawSelection(getText(), getPosition(), getTextSize(), m_startSelectionId, m_endSelectionId); } // set text color if(isPressed()){ render->setColor4(getPressedColor()); } else if(isHighLight()){ render->setColor4(getHighLightColor()); } else{ render->setColor4(getNormalColor()); } // draw text render->enableTexture(); getFont()->draw(getText(), getPosition(), getTextSize()); if(isPressed() && (m_startSelectionId == m_endSelectionId)) // cursor { render->disableTexture(); render->setColor4(MVector4(1, 0, 0, 0.5f)); // position MVector2 position = getFont()->getCharacterPosition( getText(), getPosition(), getTextSize(), getCharId() ); // scale MVector2 scale = MVector2(getTextSize() * 0.1f, getTextSize()); float offset = (getTextSize() - (getTextSize()*getFont()->getSpace()))*0.5f; float px = (float)((int)(position.x + offset)); float py = (float)((int)position.y); // draw render->disableNormalArray(); render->disableTexCoordArray(); render->disableColorArray(); render->enableVertexArray(); MVector2 vertices[4] = { MVector2(px, py), MVector2(px, py + scale.y), MVector2(px + scale.x, py), MVector2(px + scale.x, py + scale.y) }; render->setVertexPointer(M_FLOAT, 2, vertices); render->drawArray(M_PRIMITIVE_TRIANGLE_STRIP, 0, 4); } MGuiEvent guiEvent; guiEvent.type = MGUI_EVENT_DRAW; if(m_pointerEvent) m_pointerEvent(this, &guiEvent); }