/** ******************************************************************************* * @file widgetDrawer.cpp * @brief Ewol Drawer : element generator (Sources) * @author Edouard DUPIN * @date 03/03/2012 * @par Project * Edn * * @par Copyright * Copyright 2010 Edouard DUPIN, all right reserved * * This software is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY. * * Licence summary : * You can modify and redistribute the sources code and binaries. * You can send me the bug-fix * * Term of the licence in in the file licence.txt. * ******************************************************************************* */ #include #include #include #include "tinyXML/tinyxml.h" #include #include #include #undef __class__ #define __class__ "widgetDrawer" widgetDrawer::widgetDrawer(void) { m_fontNormal = -1; m_fontSize = 15; m_nearestDot = -1; m_movingPoint = false; m_textColorFg.red = 0.0; m_textColorFg.green = 0.0; m_textColorFg.blue = 0.0; m_textColorFg.alpha = 1.0; m_textColorBg.red = 0.0; m_textColorBg.green = 0.0; m_textColorBg.blue = 0.0; m_textColorBg.alpha = 0.25; m_triangleColor.red = 0.0; m_triangleColor.green = 1.0; m_triangleColor.blue = 0.0; m_triangleColor.alpha = 1.0; RegisterMultiCast(drawMsgGuiLinkNew); SetCanHaveFocus(true); } widgetDrawer::~widgetDrawer(void) { } //!< EObject name : extern const char * const TYPE_EOBJECT_WIDGET_DRAW_DRAWER = "widgetDrawer"; /** * @brief Check if the object has the specific type. * @note In Embended platforme, it is many time no -rtti flag, then it is not possible to use dynamic cast ==> this will replace it * @param[in] objectType type of the object we want to check * @return true if the object is compatible, otherwise false */ bool widgetDrawer::CheckObjectType(const char * const objectType) { if (NULL == objectType) { EWOL_ERROR("check error : \"" << TYPE_EOBJECT_WIDGET_DRAW_DRAWER << "\" != NULL(pointer) "); return false; } if (objectType == TYPE_EOBJECT_WIDGET_DRAW_DRAWER) { return true; } else { if(true == ewol::Widget::CheckObjectType(objectType)) { return true; } EWOL_ERROR("check error : \"" << TYPE_EOBJECT_WIDGET_DRAW_DRAWER << "\" != \"" << objectType << "\""); return false; } } /** * @brief Get the current Object type of the EObject * @note In Embended platforme, it is many time no -rtti flag, then it is not possible to use dynamic cast ==> this will replace it * @param[in] objectType type description * @return true if the object is compatible, otherwise false */ const char * const widgetDrawer::GetObjectType(void) { return TYPE_EOBJECT_WIDGET_DRAW_DRAWER; } bool widgetDrawer::CalculateMinSize(void) { m_minSize.x = 50; m_minSize.y = 50; return true; } void widgetDrawer::OnDraw(void) { m_OObjectsColored[ m_currentDrawId].Draw(); m_OObjectTextNormal[ m_currentDrawId].Draw(); } #define BORDER_SIZE (2) void widgetDrawer::OnRegenerateDisplay(void) { if (true == NeedRedraw()) { // clean internal elements ... m_OObjectTextNormal[ m_currentCreateId].SetFontID(m_fontNormal); m_OObjectTextNormal[ m_currentCreateId].Clear(); m_OObjectsColored[ m_currentCreateId].Clear(); // we set 3 pixels in the border (blue) and draw color_ts bandColor; bandColor.red = 0.0; bandColor.green = 0.0; bandColor.blue = 0.0; bandColor.alpha = 0.2; color_ts bgColor; bgColor.red = 0.0; bgColor.green = 0.0; bgColor.blue = 1.0; bgColor.alpha = 1.0; m_OObjectsColored[m_currentCreateId].SetColor(bgColor); m_OObjectsColored[m_currentCreateId].Rectangle( 0, 0, m_size.x, m_size.y); // we set a white background... and we draw a square ... bgColor.red = 0.8; bgColor.green = 0.8; bgColor.blue = 0.8; bgColor.alpha = 1.0; m_OObjectsColored[m_currentCreateId].SetColor(bgColor); coord2D_ts drawPosStart; coord2D_ts drawPosStop; if (m_size.x < m_size.y) { drawPosStart.x = BORDER_SIZE; drawPosStart.y = BORDER_SIZE; drawPosStop.x = m_size.x-2*BORDER_SIZE; drawPosStop.y = m_size.x-2*BORDER_SIZE; } else { drawPosStart.x = BORDER_SIZE; drawPosStart.y = BORDER_SIZE; drawPosStop.x = m_size.y-2*BORDER_SIZE; drawPosStop.y = m_size.y-2*BORDER_SIZE; } m_OObjectsColored[m_currentCreateId].Rectangle(drawPosStart.x, drawPosStart.y, drawPosStop.x, drawPosStop.y); m_OObjectsColored[m_currentCreateId].SetColor(bandColor); clipping_ts clip; clip.x = drawPosStart.x; clip.y = drawPosStart.y; clip.w = drawPosStop.x-drawPosStart.x + 2; clip.h = drawPosStop.y-drawPosStart.y + 2; m_OObjectsColored[m_currentCreateId].clippingSet(clip); int32_t nbElement = (drawPosStop.x-drawPosStart.x)/20 +1; for (int32_t iii=0; iii=0 ; iii--) { if (m_selectedList[iii] == select) { m_selectedList.Erase(iii); } } } } } if ( ewol::EVENT_INPUT_TYPE_DOWN == typeEvent) { // try to find the point int select = GetNearestPoint(position); if (m_selectedList.Size() == 1) { if (select == m_selectedList[0]) { MarkToReedraw(); m_movingPoint = true; } } } else if (ewol::EVENT_INPUT_TYPE_UP == typeEvent) { m_movingPoint = false; MarkToReedraw(); } else if (ewol::EVENT_INPUT_TYPE_MOVE == typeEvent && true == m_movingPoint) { if (m_selectedList.Size() != 1) { m_movingPoint = false; return false; } // set it in the drawing area relativePos.x = etk_avg(drawPosStart.x, relativePos.x, drawPosStop.x); relativePos.y = etk_avg(drawPosStart.y, relativePos.y, drawPosStop.y); // instanciate the value ... m_dotList[m_selectedList[0]] = position; MarkToReedraw(); } } else if (3 == IdInput && ewol::EVENT_INPUT_TYPE_SINGLE == typeEvent) { m_dotList.PushBack(position); MarkToReedraw(); } return true; } /** * @brief Receive a message from an other EObject with a specific eventId and data * @param[in] CallerObject Pointer on the EObject that information came from * @param[in] eventId Message registered by this class * @param[in] data Data registered by this class * @return --- */ void widgetDrawer::OnReceiveMessage(ewol::EObject * CallerObject, const char * eventId, etk::UString data) { DRAW_DEBUG("Extern Event : " << CallerObject << " type : " << eventId << " data=\"" << data << "\""); if (eventId == drawMsgGuiLinkNew) { if (m_selectedList.Size() == 3) { link_ts tmpLink; tmpLink.dot[0] = m_selectedList[0]; tmpLink.dot[1] = m_selectedList[1]; tmpLink.dot[2] = m_selectedList[2]; tmpLink.color[0] = m_triangleColor; tmpLink.color[1] = m_triangleColor; tmpLink.color[2] = m_triangleColor; m_linkList.PushBack(tmpLink); MarkToReedraw(); } } } void widgetDrawer::SetFontSize(int32_t size) { m_fontSize = size; } void widgetDrawer::SetFontNameNormal(etk::UString fontName) { int32_t fontID = ewol::LoadFont(fontName, m_fontSize); if (fontID >= 0) { m_fontNormal = fontID; } } void widgetDrawer::SetColorOnSelected(color_ts newColor) { m_triangleColor = newColor; // Remove all selected points ... for(int32_t iii=0; iii */ void widgetDrawer::Load(etk::UString newFileName) { // Remove all local elements : m_dotList.Clear(); m_linkList.Clear(); m_selectedList.Clear(); m_nearestDot = -1; m_movingPoint = false; m_fileName = newFileName; DRAW_DEBUG("open file (DRAWER) \"" << m_fileName << "\""); // allocate the document in the stack TiXmlDocument XmlDocument; // open the curent File etk::File fileName(m_fileName, etk::FILE_TYPE_DIRECT); if (false == fileName.Exist()) { DRAW_ERROR("File Does not exist : " << fileName); return; } int32_t fileSize = fileName.Size(); if (0==fileSize) { DRAW_ERROR("This file is empty : " << fileName); return; } if (false == fileName.fOpenRead()) { DRAW_ERROR("Can not open the file : " << fileName); return; } // allocate data char * fileBuffer = new char[fileSize+5]; if (NULL == fileBuffer) { DRAW_ERROR("Error Memory allocation size=" << fileSize); return; } memset(fileBuffer, 0, (fileSize+5)*sizeof(char)); // load data from the file : fileName.fRead(fileBuffer, 1, fileSize); // close the file: fileName.fClose(); // load the XML from the memory XmlDocument.Parse((const char*)fileBuffer, 0, TIXML_ENCODING_UTF8); TiXmlElement* root = XmlDocument.FirstChildElement( "e2d" ); if (NULL == root ) { DRAW_ERROR("(l ?) main node not find: \"e2d\" in \"" << fileName << "\""); return; } else { for(TiXmlNode * pNode = root->FirstChild(); NULL != pNode; pNode = pNode->NextSibling() ) { if (pNode->Type()==TiXmlNode::TINYXML_COMMENT) { // nothing to do, just proceed to next step } else if (!strcmp(pNode->Value(), "element")) { for(TiXmlNode * pGuiNode = pNode->FirstChild(); NULL != pGuiNode; pGuiNode = pGuiNode->NextSibling()) { if (pGuiNode->Type()==TiXmlNode::TINYXML_COMMENT) { // nothing to do, just proceed to next step } else if (!strcmp(pGuiNode->Value(), "dot")) { const char *xxx = pGuiNode->ToElement()->Attribute("x"); const char *yyy = pGuiNode->ToElement()->Attribute("y"); if( NULL != xxx && NULL != yyy) { coord2D_ts pos; double posX, posY; sscanf(xxx, "%lf", &posX); sscanf(yyy, "%lf", &posY); pos.x = posX; pos.y = posY; DRAW_DEBUG("load dot : " << xxx << "," << yyy << " ==>" << pos); m_dotList.PushBack(pos); } } else if (!strcmp(pGuiNode->Value(), "link")) { const char *id[3]; const char *color[3]; id[0] = pGuiNode->ToElement()->Attribute("id1"); id[1] = pGuiNode->ToElement()->Attribute("id2"); id[2] = pGuiNode->ToElement()->Attribute("id3"); color[0] = pGuiNode->ToElement()->Attribute("color1"); color[1] = pGuiNode->ToElement()->Attribute("color2"); color[2] = pGuiNode->ToElement()->Attribute("color3"); if( NULL != id[0] && NULL != id[1] && NULL != id[2] && NULL != color[0] && NULL != color[1] && NULL != color[2]) { link_ts localLink; int r=0; int v=0; int b=0; int a=-1; for(int32_t kkk=0; kkk<3; kkk++) { sscanf(id[kkk], "%d", &localLink.dot[kkk]); sscanf(color[kkk], "#%02x%02x%02x%02x", &r, &v, &b, &a); localLink.color[kkk].red = (float)r/255.0; localLink.color[kkk].green = (float)v/255.0; localLink.color[kkk].blue = (float)b/255.0; if (-1 == a) { localLink.color[kkk].alpha = 1; } else { localLink.color[kkk].alpha = (float)a/255.0; } } DRAW_DEBUG("load link : [" << localLink.dot[0] << "," << localLink.dot[1] << "," << localLink.dot[2] << "] "); DRAW_DEBUG(" col: [" << localLink.color[0] << "," << localLink.color[1] << "," << localLink.color[2] << "] "); m_linkList.PushBack(localLink); } } else { DRAW_ERROR("(l "<Row()<<") node not suported : \""<Value()<<"\" must be [dot,link]"); } } } else { DRAW_ERROR("(l "<Row()<<") node not suported : \""<Value()<<"\" must be [element]"); } } } if (NULL != fileBuffer) { delete[] fileBuffer; } MarkToReedraw(); } /* */ void widgetDrawer::Save(void) { if (m_fileName == "") { DRAW_ERROR("No filename set ..."); return; } TiXmlDocument doc; TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "" ); doc.LinkEndChild( decl ); TiXmlElement * mastertElement = new TiXmlElement( "e2d" ); mastertElement->SetAttribute( "version", "0.1" ); doc.LinkEndChild( mastertElement ); TiXmlElement * element = new TiXmlElement( "element" ); element->SetAttribute( "name", "???" ); mastertElement->LinkEndChild( element ); for(int32_t iii=0; iiiSetAttribute( "id", iii ); sprintf(tmpChar, "%f", m_dotList[iii].x); elementDot->SetAttribute( "x", tmpChar ); sprintf(tmpChar, "%f", m_dotList[iii].y); elementDot->SetAttribute( "y", tmpChar ); element->LinkEndChild( elementDot ); } for(int32_t iii=0; iiiSetAttribute( "id1", m_linkList[iii].dot[0] ); char colorText[256]; sprintf(colorText, "#%02X%02X%02X%02X", (uint8_t)(m_linkList[iii].color[0].red * 0xFF), (uint8_t)(m_linkList[iii].color[0].green * 0xFF), (uint8_t)(m_linkList[iii].color[0].blue * 0xFF), (uint8_t)(m_linkList[iii].color[0].alpha * 0xFF)); elementDot->SetAttribute( "color1", colorText ); elementDot->SetAttribute( "id2", m_linkList[iii].dot[1] ); sprintf(colorText, "#%02X%02X%02X%02X", (uint8_t)(m_linkList[iii].color[1].red * 0xFF), (uint8_t)(m_linkList[iii].color[1].green * 0xFF), (uint8_t)(m_linkList[iii].color[1].blue * 0xFF), (uint8_t)(m_linkList[iii].color[1].alpha * 0xFF)); elementDot->SetAttribute( "color2", colorText ); elementDot->SetAttribute( "id3", m_linkList[iii].dot[2] ); sprintf(colorText, "#%02X%02X%02X%02X", (uint8_t)(m_linkList[iii].color[2].red * 0xFF), (uint8_t)(m_linkList[iii].color[2].green * 0xFF), (uint8_t)(m_linkList[iii].color[2].blue * 0xFF), (uint8_t)(m_linkList[iii].color[2].alpha * 0xFF)); elementDot->SetAttribute( "color3", colorText ); element->LinkEndChild( elementDot ); } //Save Document doc.SaveFile( m_fileName.Utf8Data() ); }