将lua中字符串转换成table,富文本

来源:互联网 发布:用淘宝联盟卖家知道吗 编辑:程序博客网 时间:2024/06/05 06:59

  char* str = ((char*)  tolua_tostring(tolua_S,1,0));
  {
   lua_newtable(tolua_S);
   if (str == NULL) return 1;
   int n = 0;
   char ch = 0;
   char* ptr = str;
   while (true)
   {
    ch = *str;
    if (! ch)
    {
     tolua_pushnumber(tolua_S,(lua_Number)n);
     lua_pushlstring(tolua_S,ptr,(str-ptr));
     lua_settable(tolua_S,-3);

     break;
    }

    if (0x80 != (0xC0 & ch))
    {
     ++n;
     if (n > 1)
     {
      tolua_pushnumber(tolua_S,(lua_Number)(n-1));
      lua_pushlstring(tolua_S,ptr,(str-ptr));
      lua_settable(tolua_S,-3);

      ptr = str;
     }
    }
    ++str;
   }
  }
 }

 

备注:每个字符为table的一个元素

local str = "我爱你520girl";

 

ret = {

”我“, "爱", "你", "5", "2", "0", "g", "i", "r", "l",

};

 

 配合 lua和xml交互,自己封装了一个富文本组件。

短小的富文本可使用。过分长的富文本不建议使用,分页绘制尚未实现,效率不高。

该代码为伪代码:有自定义函数

--解析XML
require("script.base.LuaXml");
--[[
@biref:  富文本组件
--txt:  富文本
explme: 
str ='大小<b s="35" c="0x00ff00"/>pkissupupup<a href="www.baidu.com">百度</a><f s="15" c="0x0000ff">kissup999</f>'
str ='520<m u="10000010.png"/>shgaig<m u="10000010.png"/>la<m u="10000012.png"/>guihghlgh'
--isBMF: 是否从图字创建
--distancex: x边距
--distancey: y边距
备注:x边距应该大于y边距,效果才明显
]]--
editor = class();
function editor:ctor(txt,w,h,size,color,font,isBMF,distancex,distancey)
 self.txt = txt or GAME_TXT_LANGUAGE.normal;
 self.width = w or GAME_FRAME_SZIE.normal.width;
 self.height = h or GAME_FRAME_SZIE.normal.height;
 self.size = size or GAME_TXT_SIZE.normal;
 self.color = color or GAME_COLOR.normal;
 self.font = font or GAME_TXT_FONT.normal;
 self.isBMF = isBMF or GAME_STATE.FALSE;
 self.distancex = distancex or GAME_DISTANCE.normalx;
 self.distancey = distancey or GAME_DISTANCE.normaly;
end

function editor:create()
 self.mainLayer = cc.LayerColor:create(cc.c4b(255,0,0,180));
 self.mainLayer:setContentSize(cc.size(self.width + 2 ,self.height + 2));
 self.mainLayer:ignoreAnchorPointForPosition(false)
 self.mainLayer:setAnchorPoint(cc.p(0.5,0.5));
 --从左上角开始绘制,三个成员用于控制换行
 self.currheight = self.height - self.distancey; --当前绘制高度,预留边距离
 self.currwidth = self.distancex;    --当前绘制宽度
 self.lastheight = 0;       --当行最后绘制元素的高度
 self.imageheight = 0;       --当行元素,高度的最大值 

 self.list = {};         --存储元素

 self.maxwidth = self.width - self.distancex; --每行允许最大宽度

 self.bigindex = 0;        --暂时未使用,预留分页绘制
 self.smallindex = 0;       --暂时未使用

 local str11 = string.format("%s%s%s","<root>",self.txt,"</root>");
    local xf=xml.eval(str11);
    local data=xf:find("root");

    if type(data) == "table" and table.getn(data) > 0 then
     for i = 1,#data do
      if type(data[i]) ~= "table" then
       if type(data[i]) == "string" then
        local ret = utils.StringToTable(data[i]);
        self:createString(ret,self.size,self.color,self.font);
       end
      elseif table.getn(data[i]) == 0 then
       ----<br/>换行   0 - br
       local mydata = data[i];
       local tag = string.lower(mydata[0]);
       if tag == "br" then
        self:returnRow();
       --<m u = "1000.png">  <m source = "1000.png">
       elseif tag == "img" or tag == "m" then
        self:createImage(data[i]);
       ---<b s = "10" c = "0xff0000">   <b size = "10" color = "0xff0000">
       elseif tag == "body" or tag == "b" then
        local s = mydata.s or mydata.size;
        local c = mydata.c or mydata.color;
        self:setString(s,c);
       end
      elseif table.getn(data[i]) > 0 then
       local mydata = data[i];
       local tag = string.lower(mydata[0]);
       if tag == "font" or tag == "f" then
        local ret = utils.StringToTable(mydata[1]);
        local size = mydata.size or mydata.s;
        local color = mydata.color or mydata.c;
        color = GameUtils.getStringToc3b(color);
        self:createString(ret,size,color,self.font);
       elseif tag == "a" then
        self:createHref(tostring(mydata[1]),tostring(mydata[1].href));
       end
      end
     end
    end
 --[[
 @测试代码,暂未注释
 ]]--
    print("@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    table.foreach(data,print);
    print("--------------------");
    table.foreach(data[2],print);
    print("--------------------");
    table.foreach(data[4],print);

    self:returnRow();  --确保最后一行,也能调整位置,手动换行
 return self.mainLayer;
end
--创建文字,一个字建一个lable,拼接而成
function editor:createString(str,size,color,font)
 for i = 1,#str do
  local label = nil;
  if self.isBMF == true then
   label = createLabelBMF(str[i],size,color,font);
  else
   label = createLabelTTF(str[i],size,color,font);
  end
  local size = label:getContentSize();
  label:ignoreAnchorPointForPosition(false);
  label:setAnchorPoint(cc.p(0,1));
  local width = self.currwidth + size.width;
  self.lastheight = size.height;
  --换行重新计算宽,高
  if width > self.maxwidth then
   self:returnRow()
  end
  label:setPosition(self.currwidth,self.currheight);
  self.currwidth = self.currwidth + size.width;
  if size.height > self.imageheight then
   self.imageheight = size.height;
  end
  self.mainLayer:addChild(label);
  --用于调整高度
  local ement = {content = label};
  table.insert(self.list,ement);
 end
end

--创建图片。
function editor:createImage(data)
 local img = data.source or data.u;
 if img == nil then return;end;
 local str = GAME_SOURCE_IMAGE.."/"..img;
 local sprite = createSprite(str);
 local size = sprite:getContentSize();
 sprite:ignoreAnchorPointForPosition(false);
 sprite:setAnchorPoint(cc.p(0,1));
 --设置位置
 local width = self.currwidth + size.width;
 if width > self.maxwidth then
  self:returnRow()
 end
 sprite:setPosition(self.currwidth,self.currheight);
 self.currwidth = self.currwidth + size.width;
 if size.height > self.imageheight then
  self.imageheight = size.height;
 end
 self.mainLayer:addChild(sprite);
 --用于调整高度
 local ement = {content = sprite};
 table.insert(self.list,ement);
end

--换行,重新计算高度,宽度
function editor:returnRow()
 --printlog("max======> ",self.imageheight);
 --换行调整已经绘制的高度
 if self.imageheight ~= 0 then
  for i = 1,#self.list do
   local size = self.list[i].content:getContentSize();
   local xx,yy = self.list[i].content:getPosition();
   if self.imageheight > size.height and self.imageheight ~= size.height then
    self.list[i].content:setPosition(xx,yy - self.imageheight/2 + size.height/2);
   end
  end
 end
 self.list = {};
 --计算本行高度
 local height = self.lastheight;
 if self.imageheight > self.lastheight then
  height = self.imageheight;
 end
 --换行计算下行高度
 self.currheight = self.currheight - height;
 self.currwidth = self.distancex;
 self.imageheight = 0;
end

--设置字体大小好颜色。
function editor:setString(size,color)
 local size = size or self.size;
 local color = color or self.color;
 self.size = size;
 color = GameUtils.getStringToc3b(color);
 self.color = color;
end

--创建href标签,标签一起创建。因此标签不会出现在两行
function editor:createHref(str,href)
 if str == nil or href == nil then return;end;
 if self.isBMF == true then
  label = createLabelBMF(str,self.size,cc.c3b(0,0,255),self.font);
 else
  label = createLabelTTF(str,self.size,cc.c3b(0,0,255),self.font);
 end
 local size = label:getContentSize();
 label:ignoreAnchorPointForPosition(false);
 label:setAnchorPoint(cc.p(0.5,0.5));
 label:setPosition(size.width/2,size.height/2);
 local herf = cc.Layer:create();
 herf:setContentSize(size);
 herf:addChild(label);
 herf:ignoreAnchorPointForPosition(false);
 herf:setAnchorPoint(cc.p(0,1));

 local width = self.currwidth + size.width;
 self.lastheight = size.height;
 --换行重新计算宽,高
 if width > self.maxwidth then
  self:returnRow()
 end
 herf:setPosition(self.currwidth,self.currheight);
 self.currwidth = self.currwidth + size.width;
 if size.height > self.imageheight then
  self.imageheight = size.height;
 end
 self.mainLayer:addChild(herf);

 local function onTouchEvent(event,x,y)
  local locationInNode = herf:convertToNodeSpace(cc.p(x,y));
        local s = herf:getContentSize()
        local rect = cc.rect(0, 0, s.width, s.height)
        if cc.rectContainsPoint(rect, locationInNode) then
         print("open url ....... ");
        end
        return false;
 end
 herf:registerScriptTouchHandler(onTouchEvent);
 herf:setTouchEnabled(true);
 --用于调整高度
 local ement = {content = herf};
 table.insert(self.list,ement);
end

function GameUtils.getStringToc3b(str)
 local data = utils.StringToTable(str);
 local tag = "0x";
 local cr = "00";
 local cg = "00";
 local cb = "00";
 if #data == 8 then
  tag = tostring(data[1])..tostring(data[2]);
  cr = tostring(data[3])..tostring(data[4]);
  cg = tostring(data[5])..tostring(data[6]);
  cb = tostring(data[7])..tostring(data[8]);
 end
 return cc.c3b(tag..cr,tag..cg,tag..cb);
end

 

 

 

0 0
原创粉丝点击