Skip to content

Instantly share code, notes, and snippets.

@jonasbark
Created August 6, 2015 10:57
Show Gist options
  • Save jonasbark/fe221cd5eae085824734 to your computer and use it in GitHub Desktop.
Save jonasbark/fe221cd5eae085824734 to your computer and use it in GitHub Desktop.
CocosSharp Issue
using System;
using System.Collections.Generic;
using CocosSharp;
using Microsoft.Xna.Framework;
using System.IO;
using Windows.Storage.Streams;
using Windows.Storage;
using Windows.Graphics.Imaging;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
namespace CocosSharpGame1
{
public class IntroLayer : CCLayerColor
{
private CCPoint positionBegin;
private StorageFile currentShareFile1;
public IntroLayer() : base(CCColor4B.Blue)
{
// create and initialize a Label
for (int i = 0; i < 20; i++)
{
CCLabel label = new CCLabel("Line " + 1, "Helvetica", 22, CCLabelFormat.SystemFont);
label.Color = CCColor3B.White;
//CCLabel label = new CCLabel("Line " + 1, "fonts/MarkerFelt", 22, CCLabelFormat.SpriteFont);
label.Position = new CCPoint(i * 30, i * 30);
AddChild(label);
}
// Register for touch events
var touchListener = new CCEventListenerTouchAllAtOnce();
touchListener.OnTouchesBegan = OnTouchesBegin;
touchListener.OnTouchesMoved = OnTouchesMoved;
touchListener.OnTouchesEnded = OnTouchesEnded;
AddEventListener(touchListener, this);
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareTextHandler);
}
private async void OnTouchesEnded(List<CCTouch> arg1, CCEvent arg2)
{
CCRenderTexture renTxture = new CCRenderTexture(Window.WindowSizeInPixels, Window.WindowSizeInPixels, CCSurfaceFormat.Color, CCDepthFormat.None, CCRenderTargetUsage.PreserveContents);
renTxture.BeginWithClear(new CCColor4F(CCColor3B.White));
Visit();
renTxture.End();
MemoryStream ms = new MemoryStream();
renTxture.SaveToStream(ms, CCImageFormat.Jpg);
var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Test.jpg", CreationCollisionOption.GenerateUniqueName);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
byte[] data = ms.ToArray();
using (IOutputStream outputStream = stream.GetOutputStreamAt(0))
{
using (DataWriter dataWriter = new DataWriter(outputStream))
{
dataWriter.WriteBytes(data);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
await outputStream.FlushAsync();
outputStream.Dispose();
stream.Dispose();
}
}
}
currentShareFile1 = file;
DataTransferManager.ShowShareUI();
}
private void OnTouchesBegin(List<CCTouch> touches, CCEvent touchEvent)
{
this.positionBegin = touches[0].LocationOnScreen;
}
private void OnTouchesMoved(List<CCTouch> touches, CCEvent touchEvent)
{
float newPosX = Position.X + (touches[0].LocationOnScreen.X - positionBegin.X)/3.0f;
Position = new CCPoint(newPosX, Position.Y);
}
private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e)
{
DataRequest request = e.Request;
// The Title is mandatory
request.Data.Properties.Title = "Share";
List<StorageFile> files = new List<StorageFile>();
files.Add(currentShareFile1);
request.Data.SetStorageItems(files);
// Now add the data you want to share.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment