using System;
using System.Collections.Generic;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Imaging;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.HighLevel.UI;
using System.Linq;
using Sce.PlayStation.Core.Graphics;
using System.Diagnostics;
using System.IO;
using System.Net;
using Sce.PlayStation.Core.Input;
namespace Test
{
public class AppMain
{
private static GraphicsContext graphics;
public static void Main (string[] args)
{
Initialize ();
while (true) {
SystemEvents.CheckEvents ();
Update ();
Render ();
}
}
public static void Initialize ()
{
// Set up the graphics system
graphics = new GraphicsContext();
UISystem.Initialize(graphics);
var scene = new TestScene();
UISystem.SetScene(scene);
}
public static void Update ()
{
// Query gamepad for current state
var gamePadData = GamePad.GetData (0);
List<TouchData> touchDataList = Touch.GetData (0);
UISystem.Update(touchDataList);
}
public static void Render ()
{
// Clear the screen
graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f);
graphics.Clear ();
UISystem.Render();
// Present the screen
graphics.SwapBuffers ();
}
}
public enum ConnectState
{
None,
Ready,
Connect,
Success,
Failed
}
class MyLabel : Label
{
private const int maxReadSize = 1024;
private static int totalReadSize;
private static MemoryStream readStream;
private static byte[] readBuffer;
private static string statusCode;
private static long contentLength;
public MyLabel ()
{
this.TouchEventReceived += (sender, e) => Touching (e.TouchEvents);
this.SetPosition (100, 100);
this.SetSize (200, 200);
this.BackgroundColor = new UIColor (1, 1, 1, 1);
this.TextColor = new UIColor (0, 0, 0, 1);
this.Text = "Start";
}
static ConnectState connectState;
void Touching (TouchEventCollection touches)
{
if (connectState == ConnectState.None && touches.PrimaryTouchEvent.Type == TouchEventType.Down) {
connectState = ConnectState.Ready;
this.Text = "Ready";
}
}
protected override void OnUpdate (float elapsedTime)
{
switch (connectState) {
case ConnectState.Ready:
//connectHttpTest ("http://w...content-available-to-author-only...o.jp/index_e.html");
connectHttpTest ("http://h...content-available-to-author-only...h.net/news4vip/subback.html");
break;
case ConnectState.Success:
this.Text = "Success";
if (readStream != null) {
//httpText = createTextSprite(readStream.ToArray(), statusCode, contentLength);
readStream.Dispose ();
readStream = null;
readBuffer = null;
}
connectState = ConnectState.None;
break;
case ConnectState.Failed:
//httpText = createTextSprite(null, statusCode, contentLength);
this.Text += " Failed";
connectState = ConnectState.None;
break;
}
}
/// Http connection test
bool connectHttpTest (string url)
{
statusCode = "Unknown";
contentLength = 0;
try {
var webRequest = HttpWebRequest.Create (url);
// If you use web proxy, uncomment this and set appropriate address.
//webRequest.Proxy = new System.Net.WebProxy("http://your_proxy.com:10080");
webRequest.BeginGetResponse (new AsyncCallback (requestCallBack), webRequest);
connectState = ConnectState.Connect;
} catch (Exception e) {
this.Text = e.GetType().Name + " " + e.Message;
connectState = ConnectState.Failed;
return false;
}
return true;
}
void requestCallBack (IAsyncResult ar)
{
try {
var webRequest = (HttpWebRequest)ar.AsyncState;
var webResponse = (HttpWebResponse)webRequest.EndGetResponse (ar);
statusCode = webResponse.StatusCode.ToString ();
contentLength = webResponse.ContentLength;
readBuffer = new byte[1024];
readStream = new MemoryStream ();
totalReadSize = 0;
var stream = webResponse.GetResponseStream ();
stream.BeginRead (readBuffer, 0, readBuffer.Length, new AsyncCallback (readCallBack), stream);
} catch (Exception e) {
this.Text = e.GetType().Name + " " + e.Message;
connectState = ConnectState.Failed;
}
}
void readCallBack (IAsyncResult ar)
{
try {
var stream = (Stream)ar.AsyncState;
int readSize = stream.EndRead (ar);
if (readSize > 0) {
totalReadSize += readSize;
readStream.Write (readBuffer, 0, readSize);
}
if (readSize <= 0) {
stream.Close ();
connectState = ConnectState.Success;
} else {
stream.BeginRead (readBuffer, 0, readBuffer.Length, new AsyncCallback (readCallBack), stream);
}
} catch (Exception e) {
this.Text = e.GetType().Name + " " + e.Message;
connectState = ConnectState.Failed;
}
}
}
public class TestScene : Scene
{
public TestScene ()
{
InitializeWidget (LayoutOrientation.Horizontal);
}
private void InitializeWidget (LayoutOrientation orientation)
{
var l = new MyLabel ();
this.RootWidget.AddChildLast (l);
Button b = new Button();
b.SetSize(100,100);
b.SetPosition(400,400);
b.Text = "ボタン";
this.RootWidget.AddChildLast(b);
}
}
}