You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.5 KiB

  1. cc.VideoView = class("VideoView");
  2. cc.VideoView.ClassName = "VideoView"
  3. function cc.VideoView:saveToXmlNode(xmlNode)
  4. cc.Widget.saveToXmlNode(self , xmlNode);
  5. xmlNode.FilePath = self.FilePath;
  6. end
  7. function cc.VideoView:loadFromXmlNode(xmlNode)
  8. cc.Widget.loadFromXmlNode(self , xmlNode);
  9. self:setFilePath(xmlNode.FilePath);
  10. end
  11. function cc.VideoView:extend(node)
  12. cc.Widget:extend(node);
  13. node:setName("VideoView");
  14. node.FilePath = "";
  15. node.VideoNode = cc.Node3D:create();
  16. node.VideoSource = cc.VideoSource:create();
  17. node.VideoNode:addComponent(node.VideoSource);
  18. local sprite = cc.Sprite:create();
  19. sprite:setPosition(cc.p(0,0));
  20. sprite:setAnchorPoint(cc.p(0,0));
  21. self.VideoSprite = sprite;
  22. node:addProtectedChild(node.VideoNode);
  23. node:addProtectedChild(sprite);
  24. local function onWidgetEvent(widget , event)
  25. -- SIZE_CHANGED
  26. if event == 3 then
  27. if not node:isAutoSize() then
  28. local contentSize = node:getContentSize();
  29. if texture then
  30. local texture = node.VideoSource:getTexture();
  31. local size = texture:getContentSize();
  32. node.VideoSprite:setScale(contentSize.width / size.width , contentSize.height / size.height);
  33. end
  34. else
  35. node.VideoSprite:setScale(1,1);
  36. end
  37. end
  38. end
  39. node:setWidgetEventListener(onWidgetEvent);
  40. node:extendClass(self);
  41. end
  42. function cc.VideoView:setAutoSize(auto)
  43. local texture = self.VideoSource:getTexture();
  44. if texture then
  45. local size = texture:getContentSize();
  46. self:setContentSize(size);
  47. end
  48. cc.Widget.setAutoSize(self , auto);
  49. end
  50. function cc.VideoView:setFilePath(filePath)
  51. self.FilePath = filePath;
  52. self.VideoSource:setVideoFile(filePath);
  53. self.VideoSource:stop();
  54. self.VideoSource:play();
  55. local texture = self.VideoSource:getTexture();
  56. if texture then
  57. self.VideoSprite:setTexture(texture);
  58. local rect = cc.rect(0,0,0,0);
  59. local size = texture:getContentSize();
  60. rect.width = size.width;
  61. rect.height = size.height;
  62. self.VideoSprite:setTextureRect(rect);
  63. if self:isAutoSize() then
  64. self:setContentSize(size);
  65. self:setSize(size);
  66. self.VideoSprite:setScale(1,1);
  67. else
  68. local contentSize = self:getContentSize();
  69. self.VideoSprite:setScale(contentSize.width / size.width , contentSize.height / size.height);
  70. end
  71. end
  72. end
  73. function cc.VideoView:createNode()
  74. local layer = cc.Widget:create();
  75. cc.VideoView:extend(layer);
  76. return layer;
  77. end
  78. -- 设置默认值
  79. function cc.VideoView:setDefaults()
  80. -- 默认需要响应touch事件
  81. self:setTouchEnabled(false)
  82. self:setAutoSize(true);
  83. --self:setSize(cc.size(300,300));
  84. end