注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

尐鬼じ☆ve伱

和你在一起的日子

 
 
 

日志

 
 

cocos2dx实现功能强大的RichText控件  

2015-02-04 15:19:53|  分类: Android cocos2d- |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
转http://www.tuicool.com/articles/6fUnmyq
lua使用代码如下
function ChatUI:initRichEdit()	
local widget = self:getWidget()
if widget then
--创建小喇叭控件
self._richBugle = ui.RichTextUI:create()
self._richBugle:setSize(cc.size(940, 35))
self._richBugle:setAnchorPoint(cc.p(0, 0))
self._richBugle:setPosition(cc.p(100, 510))
self._richBugle:setMaxLine(1)
--创建聊天控件
self._richChat= ui.RichTextUI:create()
self._richChat:setSize(cc.size(940, 420))
self._richChat:setAnchorPoint(cc.p(0, 0))
self._richChat:setPosition(cc.p(20, 70))

widget:addChild(self._richBugle)
widget:addChild(self._richChat)

local function callback(sender, eventType)
if eventType == ui.RICHTEXT_ANCHOR_CLICKED then
print(">>>>>>>>>>>addEventListenerRichText")
end
end
self._richChat:addEventListenerRichText(callback)
end
end

function ChatUI:addChatMsg(channel, roleName, chatMsg, signs)
local richText = (channel == Channel_ID_Bugle) and self._richBugle or self._richChat
if richText and channel and roleName and chatMsg then
local ChannelNameSwitch =
{

[Channel_ID_Team] = "【队伍】",
[Channel_ID_Privacy] = "【私聊】",
[Channel_ID_Faction] = "【帮会】",
[Channel_ID_World] = "【世界】",
[Channel_ID_System] = "【系统】"
}
local ChannelColor =
{
[Channel_ID_Team] = Color3B.ORANGE,
[Channel_ID_Privacy] = Color3B.ORANGE,
[Channel_ID_Faction] = Color3B.ORANGE,
[Channel_ID_World] = Color3B.ORANGE,
[Channel_ID_System] = Color3B.WHITE,
[Channel_ID_Bugle] = Color3B.ORANGE
}
local linkColor = Color3B.YELLOW
local linklineColor = Color4B.YELLOW
local outlineColor = Color4B.BLACK

if channel == Channel_ID_Bugle then
richText:insertNewLine()
end
if ChannelNameSwitch[channel] then
local rc = ui.RichItemText:create(channel, ChannelColor[channel], 255, strg2u(ChannelNameSwitch[channel]), "DFYuanW7-GB2312.ttf", 25)
rc:enableOutLine(outlineColor, 2)
richText:insertElement(rc)
end
if channel ~= Channel_ID_System then
local rcn = ui.RichItemText:create(channel, linkColor, 255, strg2u(roleName), "DFYuanW7-GB2312.ttf", 25)
rcn:enableLinkLine(linklineColor, 1)
rcn:enableOutLine(outlineColor, 2)
richText:insertElement(rcn)
chatMsg = ":" .. chatMsg
end
local rcm = ui.RichItemText:create(channel, ChannelColor[channel], 255, strg2u(chatMsg), "DFYuanW7-GB2312.ttf", 25)
richText:insertElement(rcm)
if channel ~= Channel_ID_Bugle then
richText:insertNewLine()
end
end
end

function ChatUI:initComponent()
self:addChatMsg(Channel_ID_Bugle, "王小二", "This is Bugle Msg")
self:addChatMsg(Channel_ID_System, "", "This is System Msg")
self:addChatMsg(Channel_ID_Team, "王小二", "This is Team Msg")
self:addChatMsg(Channel_ID_World, "王小二", "This is World Msg")
self:addChatMsg(Channel_ID_Faction, "王小二", "This is Faction Msg")

self._channel = Channel_ID_World
self:showChannel(Channel_ID_All)
local btnChannel = self:getChild("Button_Channel")
if btnChannel then
btnChannel:setTitleText(strg2u("世界"))
end
end
RichItem.h

#ifndef __RICHITEM_H__
#define __RICHITEM_H__

#include "ui/UIWidget.h"

NS_CC_BEGIN

namespace ui
{
typedef enum
{
RICH_ITEM_TEXT,
RICH_ITEM_NEWLINE,
RICH_ITEM_IMAGE,
RICH_ITEM_CUSTOM
}
RichItemType;

class RichItem : public Ref
{
public:
RichItem(){};
virtual ~RichItem(){};
bool init(int tag, const Color3B& color, GLubyte opacity);

std::string getContext() {return context;}
void setContext(std::string cxt) {context = cxt;}

protected:
RichItemType _type;
int _tag;
Color3B _color;
GLubyte _opacity;
std::string context;
friend class RichTextUI;
};

class RichItemNewLine : public RichItem
{
public:
RichItemNewLine(){_type = RICH_ITEM_NEWLINE;};
virtual ~RichItemNewLine(){};
bool init(int tag);
static RichItemNewLine* create(int tag);
protected:
friend class RichTextUI;
};

class RichItemText : public RichItem
{
public:
RichItemText(){_type = RICH_ITEM_TEXT;};
virtual ~RichItemText(){};
bool init(int tag, const Color3B& color, GLubyte opacity, const char* text, const char* fontName, float fontSize);
static RichItemText* create(int tag, const Color3B& color, GLubyte opacity, const char* text, const char* fontName, float fontSize);
void enableOutLine(const Color4B& outcolor, GLubyte outlinesize)
{
_outcolor = outcolor;
_outlinesize = outlinesize;
}
void enableLinkLine(const Color4B& linkcolor, GLubyte linksize)
{
_linkcolor = linkcolor;
_linksize = linksize;
}

protected:
std::string _text;
std::string _fontName;
float _fontSize;
Color4B _outcolor;
Color4B _linkcolor;
GLubyte _linksize;
GLubyte _outlinesize;
friend class RichTextUI;
};

class RichItemImage : public RichItem
{
public:
RichItemImage(){_type = RICH_ITEM_IMAGE;};
virtual ~RichItemImage(){};
bool init(int tag, const Color3B& color, GLubyte opacity, const char* filePath);
static RichItemImage* create(int tag, const Color3B& color, GLubyte opacity, const char* filePath);
protected:
std::string _filePath;
Rect _textureRect;
int _textureType;
friend class RichTextUI;
};

class RichItemCustom : public RichItem
{
public:
RichItemCustom(){_type = RICH_ITEM_CUSTOM;};
virtual ~RichItemCustom(){CC_SAFE_RELEASE(_customNode);};
bool init(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
static RichItemCustom* create(int tag, const Color3B& color, GLubyte opacity, Node* customNode);
protected:
Node* _customNode;
friend class RichTextUI;
};

class LinkLable;
class LableDelegate
{
public:
virtual void labelClicked(LinkLable* lab) = 0;
};

class LinkLable : public Label
{
public:
LinkLable(FontAtlas *atlas = nullptr, TextHAlignment hAlignment = TextHAlignment::LEFT,
TextVAlignment vAlignment = TextVAlignment::TOP, bool useDistanceField = false, bool useA8Shader = false);

virtual ~LinkLable();

static LinkLable* create(const std::string& text, const std::string& fontFile, float fontSize, const Size& dimensions = Size::ZERO,
TextHAlignment hAlignment = TextHAlignment::LEFT, TextVAlignment vAlignment = TextVAlignment::TOP);

void setLableDelegate(LableDelegate* ld) { _delegate = ld; }
void enableLinkLine(const Color4B& linkcolor, GLubyte linksize) ;
bool onTouchBegan(Touch *touch, Event *unusedEvent);
void onTouchEnded(Touch *touch, Event *unusedEvent);

LayerColor* getLinkline() { return _linkline;}

protected:
EventListenerTouchOneByOne* _touchListener;
LayerColor* _linkline;
LableDelegate* _delegate;
Color4B _linkcolor;
GLubyte _linksize;
};
}

NS_CC_END

#endif /* defined(__RichItem__) */



RichItem.cpp

#include "RichItem.h"

NS_CC_BEGIN

namespace ui
{
bool RichItem::init(int tag, const Color3B &color, GLubyte opacity)
{
_tag = tag;
_color = color;
_opacity = opacity;
return true;
}

RichItemNewLine* RichItemNewLine::create(int tag)
{
RichItemNewLine* element = new RichItemNewLine();
if (element && element->init(tag))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return NULL;
}

bool RichItemNewLine::init(int tag)
{
if (RichItem::init(tag, Color3B(0,0,0), 0))
{
return true;
}
return false;
}

RichItemText* RichItemText::create(int tag, const Color3B &color, GLubyte opacity, const char *text, const char *fontName, float fontSize)
{
RichItemText* element = new RichItemText();
if (element && element->init(tag, color, opacity, text, fontName, fontSize))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return NULL;
}

bool RichItemText::init(int tag, const Color3B &color, GLubyte opacity, const char *text, const char *fontName, float fontSize)
{
if (RichItem::init(tag, color, opacity))
{
_text = text;
_fontName = fontName;
_fontSize = fontSize;
_outcolor = Color4B::WHITE;
_linkcolor = Color4B::WHITE;
_outlinesize = 0;
_linksize = 0;
return true;
}
return false;
}

RichItemImage* RichItemImage::create(int tag, const Color3B &color, GLubyte opacity, const char *filePath)
{
RichItemImage* element = new RichItemImage();
if (element && element->init(tag, color, opacity, filePath))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return NULL;
}

bool RichItemImage::init(int tag, const Color3B &color, GLubyte opacity, const char *filePath)
{
if (RichItem::init(tag, color, opacity))
{
_filePath = filePath;
return true;
}
return false;
}

RichItemCustom* RichItemCustom::create(int tag, const Color3B &color, GLubyte opacity, cocos2d::Node *customNode)
{
RichItemCustom* element = new RichItemCustom();
if (element && element->init(tag, color, opacity, customNode))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return NULL;
}

bool RichItemCustom::init(int tag, const Color3B &color, GLubyte opacity, cocos2d::Node *customNode)
{
if (RichItem::init(tag, color, opacity))
{
_customNode = customNode;
_customNode->retain();
return true;
}
return false;
}

LinkLable::LinkLable(FontAtlas *atlas, TextHAlignment hAlignment, TextVAlignment vAlignment, bool useDistanceField, bool useA8Shader)
: Label(atlas, hAlignment, vAlignment, useDistanceField, useA8Shader), _touchListener(NULL),
_delegate(NULL), _linkcolor(Color4B::WHITE), _linksize(0), _linkline(NULL)
{

}

LinkLable::~LinkLable()
{
if (_linksize)
{
_eventDispatcher->removeEventListener(_touchListener);
CC_SAFE_RELEASE_NULL(_touchListener);
}
}

LinkLable* LinkLable::create(const std::string& text, const std::string& fontFile, float fontSize, const Size& dimensions,
TextHAlignment hAlignment, TextVAlignment vAlignment)
{
auto ret = new LinkLable(nullptr, hAlignment, vAlignment);
if (ret)
{
if (FileUtils::getInstance()->isFileExist(fontFile))
{
TTFConfig ttfConfig(fontFile.c_str(), fontSize, GlyphCollection::DYNAMIC);
if (ret->setTTFConfig(ttfConfig))
{
ret->setDimensions(dimensions.width, dimensions.height);
ret->setString(text);
ret->autorelease();
return ret;
}
}
else
{
ret->setSystemFontName(fontFile.c_str());
ret->setSystemFontSize(fontSize);
ret->setDimensions(dimensions.width, dimensions.height);
ret->setString(text);
ret->autorelease();
return ret;
}
}
CC_SAFE_DELETE(ret);
return ret;
}

void LinkLable::enableLinkLine(const Color4B& linkcolor, GLubyte linksize)
{
_linkcolor = linkcolor;
_linksize = linksize;
if (_linksize > 0)
{
_touchListener = EventListenerTouchOneByOne::create();
CC_SAFE_RETAIN(_touchListener);
_touchListener->setSwallowTouches(true);
_touchListener->onTouchBegan = CC_CALLBACK_2(LinkLable::onTouchBegan, this);
_touchListener->onTouchEnded = CC_CALLBACK_2(LinkLable::onTouchEnded, this);
_eventDispatcher->addEventListenerWithFixedPriority(_touchListener, -1);

_linkline = LayerColor::create(_linkcolor);

_linkline->setContentSize(Size(getContentSize().width, _linksize));
}
}

bool LinkLable::onTouchBegan(Touch *touch, Event *unusedEvent)
{
Point _touchStartPos = touch->getLocation();
Point nsp = convertToNodeSpace(_touchStartPos);
Rect bb;
bb.size = _contentSize;
if (bb.containsPoint(nsp))
{
return true;
}
return false;
}

void LinkLable::onTouchEnded(Touch *touch, Event *unusedEvent)
{
if (_delegate)
{
_delegate->labelClicked(this);
}
}
}

NS_CC_END


RichTextUI.h

#ifndef __RICHTEXTUI_H__
#define __RICHTEXTUI_H__
#include "ui/UIScrollView.h"
#include "RichItem.h"

NS_CC_BEGIN

namespace ui
{
typedef enum
{
RICHTEXT_ANCHOR_CLICKED,
}
RichTextEventType;

typedef void (Ref::*SEL_RichTextClickEvent)(Ref*, RichTextEventType);
#define richtextclickselector(_SELECTOR) (SEL_RichTextClickEvent)(&_SELECTOR)

class RichTextUI : public ScrollView, public LableDelegate
{
public:
RichTextUI();
virtual ~RichTextUI();
static RichTextUI* create();

void clearAll();
void insertNewLine();
void showItemByTag(short tag);
void insertElement(RichItem* element);

void setMaxLine(short lineCount) {_maxline = lineCount; }
void setLineSpace(short lineSpace) {_lineSpace = lineSpace; }

virtual void setAnchorPoint(const Point &pt);
virtual std::string getDescription() const override;
virtual void visit(cocos2d::Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) override;

void labelClicked(LinkLable* lab);

//add a call back function would called when checkbox is selected or unselected.
void addEventListenerRichText(Ref* target, SEL_RichTextClickEvent selector);

CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;

protected:
virtual void initRenderer();

void formatText();
void pushToContainer(Node* renderer);
void handleTextRenderer(RichItemText* item, const char* text);
void handleImageRenderer(const char* fileParh, const Color3B& color, GLubyte opacity);
void handleCustomRenderer(Node* renderer);
void formarRenderers();
void changeLine();

LinkLable* createLable(RichItemText* item, const char* text);

protected:
short _showTag;
short _maxline;
short _lineSpace;
float _leftSpaceWidth;
bool _formatTextDirty;
Vector<RichItem*> _richElements;
std::vector<Vector<Node*>*> _elementRenders;

Ref* _richTextEventListener;
SEL_RichTextClickEvent _richTextEventSelector;

std::string _context;
};
}

NS_CC_END

#endif /* defined(__RichTextUI__) */



RichTextUI.cpp

#include "RichTextUI.h"

NS_CC_BEGIN

namespace ui
{
static int _calcCharCount(const char * pszText)
{
int n = 0;
char ch = 0;
while ((ch = *pszText))
{
CC_BREAK_IF(!ch);
if (0x80 != (0xC0 & ch))
{
++n;
}
++pszText;
}
return n;
}

/*----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------*/
RichTextUI::RichTextUI(): _formatTextDirty(true), _leftSpaceWidth(0.0f), _maxline(200), _lineSpace(2), _showTag(0),
_richTextEventListener(nullptr),
_richTextEventSelector(nullptr)
{
_context = "";
}

RichTextUI::~RichTextUI()
{
_richElements.clear();
_richTextEventListener = nullptr;
_richTextEventSelector = nullptr;
}

RichTextUI* RichTextUI::create()
{
RichTextUI* widget = new RichTextUI();
if (widget && widget->init())
{
widget->setDirection(SCROLLVIEW_DIR_VERTICAL);
widget->setInertiaScrollEnabled(true);
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return NULL;
}

bool RichTextUI::init()
{
if (ScrollView::init())
{
return true;
}
return false;
}

void RichTextUI::initRenderer()
{
ScrollView::initRenderer();
}

void RichTextUI::insertNewLine()
{
RichItemNewLine* item = RichItemNewLine::create(0);
insertElement(item);
}

void RichTextUI::clearAll()
{
_richElements.clear();
_formatTextDirty = true;
}

void RichTextUI::showItemByTag(short tag)
{
if (_showTag != tag)
{
_showTag = tag;
_formatTextDirty = true;
}
}

void RichTextUI::insertElement(RichItem *element)
{
_richElements.pushBack(element);
_formatTextDirty = true;
}

void RichTextUI::formatText()
{
if (_formatTextDirty)
{
removeAllChildren();
_elementRenders.clear();

changeLine();
for (ssize_t i = 0; i < _richElements.size(); i++)
{
RichItem* element = static_cast<RichItem*>(_richElements.at(i));
if (_showTag == 0 || element->_tag == 0 || element->_tag == _showTag)
{
switch (element->_type)
{
case RICH_ITEM_NEWLINE:
{
changeLine();
}
break;
case RICH_ITEM_TEXT:
{
RichItemText* elmtText = static_cast<RichItemText*>(element);
handleTextRenderer(elmtText, elmtText->_text.c_str());
}
break;
case RICH_ITEM_IMAGE:
{
RichItemImage* elmtImage = static_cast<RichItemImage*>(element);
handleImageRenderer(elmtImage->_filePath.c_str(), elmtImage->_color, elmtImage->_opacity);
}
break;
case RICH_ITEM_CUSTOM:
{
RichItemCustom* elmtCustom = static_cast<RichItemCustom*>(element);
handleCustomRenderer(elmtCustom->_customNode);
}
break;
default:
break;
}
}
}
formarRenderers();
_formatTextDirty = false;
jumpToBottom();
}
}

void RichTextUI::addEventListenerRichText(Ref* target, SEL_RichTextClickEvent selector)
{
_richTextEventListener = target;
_richTextEventSelector = selector;
}

void RichTextUI::labelClicked(LinkLable* lab)
{
RichItemText* itemText = static_cast<RichItemText*>(lab->getUserObject());
if (itemText)
{
_context = itemText->getContext();
}
if (_richTextEventListener && _richTextEventSelector)
{
(_richTextEventListener->*_richTextEventSelector)(this, RICHTEXT_ANCHOR_CLICKED);
}
}

LinkLable* RichTextUI::createLable(RichItemText* item, const char* text)
{
LinkLable* textRenderer = LinkLable::create(text, item->_fontName, item->_fontSize);
if (textRenderer)
{
if (item->_outlinesize > 0)
{
textRenderer->enableOutline(item->_outcolor, item->_outlinesize);
}
if (item->_linksize > 0)
{
textRenderer->enableLinkLine(item->_linkcolor, item->_linksize);
}
}
textRenderer->setLableDelegate(this);
return textRenderer;
}

void RichTextUI::handleTextRenderer(RichItemText* item, const char* text)
{
LinkLable* textRenderer = createLable(item, text);
float textRendererWidth = textRenderer->getContentSize().width;
_leftSpaceWidth -= textRendererWidth;
if (_leftSpaceWidth < 0.0f)
{
float overstepPercent = (-_leftSpaceWidth) / textRendererWidth;
std::string curText = text;
size_t stringLength = _calcCharCount(text);
int leftLength = stringLength * (1.0f - overstepPercent);
std::string leftWords = curText.substr(0, leftLength);
std::string cutWords = curText.substr(leftLength, curText.length()-1);
if (leftLength > 0)
{
LinkLable* leftRenderer = createLable(item, leftWords.substr(0, leftLength).c_str());
if (leftRenderer)
{
leftRenderer->setColor(item->_color);
leftRenderer->setOpacity(item->_opacity);
leftRenderer->setUserObject(item);
pushToContainer(leftRenderer);
}
}
changeLine();
handleTextRenderer(item, cutWords.c_str());
}
else
{
textRenderer->setColor(item->_color);
textRenderer->setOpacity(item->_opacity);
textRenderer->setUserObject(item);
pushToContainer(textRenderer);
}
}

void RichTextUI::handleImageRenderer(const char *fileParh, const Color3B &color, GLubyte opacity)
{
Sprite* imageRenderer = Sprite::create(fileParh);
handleCustomRenderer(imageRenderer);
}

void RichTextUI::handleCustomRenderer(cocos2d::Node *renderer)
{
Size imgSize = renderer->getContentSize();
_leftSpaceWidth -= imgSize.width;
if (_leftSpaceWidth < 0.0f)
{
changeLine();
pushToContainer(renderer);
_leftSpaceWidth -= imgSize.width;
}
else
{
pushToContainer(renderer);
}
}

void RichTextUI::changeLine()
{
_leftSpaceWidth = _customSize.width;
_elementRenders.push_back(new Vector<Node*>());
}

void RichTextUI::formarRenderers()
{
while (_elementRenders.size() > _maxline)
{
_elementRenders.erase(_elementRenders.begin());
}
float newContentSizeHeight = 0.0f;
float *maxHeights = new float[_elementRenders.size()];

for (size_t i = 0; i < _elementRenders.size(); i++)
{
Vector<Node*>* row = (_elementRenders[i]);
float maxHeight = 0.0f;
for (ssize_t j = 0; j < row->size(); j++)
{
Node* l = row->at(j);
maxHeight = MAX(l->getContentSize().height, maxHeight);
}
maxHeights[i] = maxHeight + _lineSpace;
newContentSizeHeight += maxHeights[i];
}

float nextPosY = newContentSizeHeight;
for (size_t i = 0; i < _elementRenders.size(); i++)
{
Vector<Node*>* row = (_elementRenders[i]);
float nextPosX = 0.0f;
nextPosY -= maxHeights[i];

for (ssize_t j=0; j < row->size(); j++)
{
Node* l = row->at(j);
l->setAnchorPoint(Point::ZERO);
l->setPosition(Point(nextPosX, nextPosY));
addChild(l, 1, (int)(i * 10 + j));
LinkLable* la = dynamic_cast<LinkLable*>(l);
if (la && la->getLinkline())
{

la->getLinkline()->setPosition(nextPosX, nextPosY - 1);
addChild(la->getLinkline(), 1, (int)(i * 10 + j));
}
nextPosX += l->getContentSize().width;
}
}
delete [] maxHeights;

size_t length = _elementRenders.size();
for (size_t i = 0; i<length; i++)
{
Vector<Node*>* l = _elementRenders[i];
l->clear();
delete l;
}
_elementRenders.clear();
if (newContentSizeHeight < _customSize.height)
{
newContentSizeHeight = _customSize.height;
}
setInnerContainerSize(Size(_customSize.width, newContentSizeHeight));
}

void RichTextUI::pushToContainer(cocos2d::Node *renderer)
{
if (_elementRenders.size() <= 0)
{
return;
}
_elementRenders[_elementRenders.size()-1]->pushBack(renderer);
}

void RichTextUI::visit(cocos2d::Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated)
{
if (_enabled)
{
formatText();
ScrollView::visit(renderer, parentTransform, parentTransformUpdated);
}
}

void RichTextUI::setAnchorPoint(const Point &pt)
{
ScrollView::setAnchorPoint(pt);
}

std::string RichTextUI::getDescription() const
{
return "RichTextUI";
}
}

NS_CC_END


lua_cocos2dx_extend_ui.cpp

#include "cocos2d.h"
#include "tolua_fix.h"
#include "CCLuaValue.h"
#include "CCLuaEngine.h"
#include "RichItem.h"
#include "RichTextUI.h"
#include "LuaBasicConversions.h"
#include "LuaScriptHandlerMgr.h"

USING_NS_CC;

using namespace ui;

int register_extend_cocos2dx_ui_manual(lua_State* L);

struct LuaStudioEventListenerDataEx
{
Ref* objTarget;
int eventType;
LuaStudioEventListenerDataEx(Ref* _objTarget, int _eventType) : objTarget(_objTarget),eventType(_eventType){}
};

class LuaCocoStudioEventListenerEx : public Ref
{
public:
LuaCocoStudioEventListenerEx(){}
virtual ~LuaCocoStudioEventListenerEx(){}

static LuaCocoStudioEventListenerEx* create();

virtual void eventCallbackFunc(Ref* sender,int eventType);
};

LuaCocoStudioEventListenerEx* LuaCocoStudioEventListenerEx::create()
{
LuaCocoStudioEventListenerEx* listener = new LuaCocoStudioEventListenerEx();
if (nullptr == listener)
return nullptr;

listener->autorelease();

return listener;
}

void LuaCocoStudioEventListenerEx::eventCallbackFunc(Ref* sender,int eventType)
{
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER);
if (0 != handler)
{
LuaStudioEventListenerDataEx eventData(sender, eventType);
BasicScriptData data(this, (void*)&eventData);
LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER, (void*)&data);
}
}

static int lua_cocos2dx_RichTextUI_addEventListenerRichTextUI(lua_State* L)
{
if (nullptr == L)
return 0;

int argc = 0;
RichTextUI* self = nullptr;

#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertype(L,1,"ui::RichTextUI",0,&tolua_err)) goto tolua_lerror;
#endif

self = static_cast<RichTextUI*>(tolua_tousertype(L,1,0));

#if COCOS2D_DEBUG >= 1
if (nullptr == self) {
tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_RichTextUI_addEventListenerRichTextUI'\n", NULL);
return 0;
}
#endif
argc = lua_gettop(L) - 1;
if (1 == argc)
{
#if COCOS2D_DEBUG >= 1
if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err))
{
goto tolua_lerror;
}
#endif
LuaCocoStudioEventListenerEx* listener = LuaCocoStudioEventListenerEx::create();
if (nullptr == listener)
{
tolua_error(L,"LuaCocoStudioEventListenerEx create fail\n", NULL);
return 0;
}

LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0));

ScriptHandlerMgr::getInstance()->addObjectHandler((void*)listener, handler, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER);

__Dictionary* dict = static_cast<__Dictionary*>(self->getUserObject());
if (nullptr == dict)
{
dict = Dictionary::create();
self->setUserObject(dict);
}
dict->setObject(listener, "richTextUIEventListener");

self->addEventListenerRichText(listener, richtextclickselector(LuaCocoStudioEventListenerEx::eventCallbackFunc));

return 0;
}

CCLOG("'addEventListenerRichText' function of RichTextUI has wrong number of arguments: %d, was expecting %d\n", argc, 1);
return 0;

#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'addEventListenerRichText'.",&tolua_err);
return 0;
#endif
}

static void extendRichTextUI(lua_State* L)
{
lua_pushstring(L, "ui::RichTextUI");
lua_rawget(L, LUA_REGISTRYINDEX);
if (lua_istable(L,-1))
{
tolua_function(L, "addEventListenerRichText", lua_cocos2dx_RichTextUI_addEventListenerRichTextUI);
}
lua_pop(L, 1);
}

int register_extend_cocos2dx_ui_manual(lua_State* L)
{
if (nullptr == L) return 0;

extendRichTextUI(L);

return 0;
}



  评论这张
 
阅读(1043)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017