// // ToyPlainLabel.cpp // WattsenglishToyApp-mobile // // Created by Katarzyna Kalinowska-Górska on 19/11/2019. // #include #include "ToyPlainLabel.h" #include "ToyScalingUtils.h" #include "ToyJSONParseUtils.h" ToyPlainLabel* ToyPlainLabel::create(const std::string& text, const std::string fontPath, int fontBaseSize) { ToyPlainLabel * label = new (std::nothrow) ToyPlainLabel(); if(label && label->init(text, fontPath, fontBaseSize)) { label->autorelease(); return label; } CC_SAFE_DELETE(label); return nullptr; } bool ToyPlainLabel::init(const std::string& text, const std::string fontPath, int fontBaseSize){ if(!cocos2d::Label::initWithTTF(text, "fonts/ComicSansMSBold.ttf", fontBaseSize*ToyScalingUtils::getScaleForFont())){ //120 return false; } return true; } void ToyPlainLabel::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, ToyLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder) { this->loadCommonPropertiesFromJSON(jsonValue); if(ToyJSONParseUtils::hasMemberColor3B(jsonValue, "colour")){ setColor(ToyJSONParseUtils::getColor3B(jsonValue["colour"])); } } void ToyPlainLabel::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height) { width = this->getBoundingBox().size.width; height = this->getBoundingBox().size.height; // width = this->getContentSize().width; // height = this->getContentSize().height; } void ToyPlainLabel::setProperty(std::string propertyName, const rapidjson::Value& newValue, ActionParseDelegate* parseDelegate) { if(propertyName == "opacity"){ auto value = newValue.GetInt(); this->setOpacity(value); } } void ToyPlainLabel::callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function callback) { ToyLayoutObject::callFunctionByName(methodName, arguments, parseDelegate); // auto scale = 1/cocos2d::Director::getInstance()->getContentScaleFactor(); // if(methodName == "setVisible"){ // if((*arguments).IsArray()){ // this->setVisible((*arguments).GetArray()[0].GetBool()); // } // } // else if(methodName == "setFlippedX"){ // if((*arguments).IsArray()){ // this->setFlippedX((*arguments).GetArray()[0].GetBool()); // } // } // else if(methodName == "setFlippedY"){ // if((*arguments).IsArray()){ // this->setFlippedY((*arguments).GetArray()[0].GetBool()); // } // } // else if (methodName == "setScale"){ // if((*arguments).IsArray()){ // this->setScale((*arguments).GetArray()[0].GetFloat(), (*arguments).GetArray()[0].GetFloat()); // } // } // else if(methodName == "setOpacity"){ // if((*arguments).IsArray()){ // this->setOpacity((*arguments).GetArray()[0].GetInt()); // } // } // else if(methodName == "setPositionX"){ // if((*arguments).IsArray()){ // this->setPositionX((*arguments).GetArray()[0].GetFloat()*scale); // } // } // else if(methodName == "setPositionY"){ // if((*arguments).IsArray()){ // this->setPositionY((*arguments).GetArray()[0].GetFloat()*scale); // } // } // else if(methodName == "setPosition"){ // if((*arguments).IsArray()){ // auto point = ToyJSONParseUtils::getPoint((*arguments).GetArray()[0]); // ToyMathUtils::multiplyPoint(point, scale); // this->setPosition(point); // } // } // else if(methodName == "setTextureRect"){ // if((*arguments).IsArray()){ // auto newRect = ToyJSONParseUtils::getRect((*arguments).GetArray()[0]); // ToyMathUtils::multiplyRectPosition(newRect, scale); // this->setTextureRect(newRect); // } // } // else if(methodName == "setContentSize"){ // if((*arguments).IsArray()){ // this->setContentSize(ToyJSONParseUtils::getSize((*arguments).GetArray()[0])); // } // } // else if(methodName == "setRotation"){ // if((*arguments).IsArray()){ // this->setRotation(((*arguments).GetArray()[0]).GetFloat()); // } // } // else if(methodName == "clipSprite"){ // if((*arguments).IsArray()){ // auto by = ToyJSONParseUtils::getPoint((*arguments)[0]); // auto prevTextureRect = this->getTextureRect(); // this->setTextureRect(cocos2d::Rect(prevTextureRect.origin.x - by.x, prevTextureRect.origin.y - by.y, prevTextureRect.size.width, prevTextureRect.size.height)); // } // } // else if(methodName == "stopAnimations"){ // this->stopAllActions(); // } // else if(methodName == "setPicture"){ // if((*arguments).IsArray()){ // std::string imagePath = ((*arguments).GetArray()[0]).GetString(); // auto texture = cocos2d::Director::getInstance()->getTextureCache()->addImage(imagePath); // this->setTexture(texture); // this->setTextureRect(cocos2d::Rect(cocos2d::Point::ZERO, texture->getContentSize())); // } // } }