on 02-05-2012 06:52 PM
Hello, I am trying to make a very simple application: I just want to have 3 buttons on the screen with a background image each, and be able to drag them on the screen and when I click in one of them, I want to change the image to another... How can I do this? I have done it in C# in Visual Studio 2010 for a windows application but here I'm having trouble handling the events... Could someone please help me with the code in order to make the above?
any help will be greatly appreciated!!
Solved! Go to Solution.
on 02-05-2012 07:38 PM
Here is my sample code :
using System;
using System.Collections.Generic;
using Sce.Pss.Core;
using Sce.Pss.Core.Environment;
using Sce.Pss.Core.Graphics;
using Sce.Pss.Core.Input;
using Sce.Pss.HighLevel.UI;
namespace Test
{
public class AppMain
{
private static GraphicsContext graphics;
private static CustomPanel panel;
private static Button button;
private static bool drag;
private static Vector2 memo_pos;
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);
Scene scene = new Scene();
panel = new CustomPanel();
panel.SetSize(960, 544);
panel.TouchEventReceived += PanelTouchEventReceived;
button = new Button();
button.Text = "Button";
button.SetPosition(100, 100);
panel.AddChildLast(button);
drag = false;
scene.RootWidget.AddChildLast(panel);
UISystem.SetScene (scene, null);
}
static void PanelTouchEventReceived (object sender, TouchEventArgs e)
{
TouchEvent touchEvent = e.TouchEvents.PrimaryTouchEvent;
switch(touchEvent.Type){
case TouchEventType.Down:
if(button.HitTest(touchEvent.WorldPosition)){
drag = true;
memo_pos = touchEvent.WorldPosition;
}
break;
case TouchEventType.Up:
if(drag)
drag = false;
Console.WriteLine(touchEvent.Type.ToString());
break;
case TouchEventType.Move:
if(drag){
button.X += touchEvent.WorldPosition.X - memo_pos.X;
button.Y += touchEvent.WorldPosition.Y - memo_pos.Y;
memo_pos = touchEvent.WorldPosition;
}
break;
}
}
public static void Update ()
{
// Query gamepad for current state
var gamePadData = GamePad.GetData (0);
List<TouchData> list = Touch.GetData (0);
UISystem.Update (list);
}
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 ();
}
}
class CustomPanel : Panel {
public CustomPanel(){
HookChildTouchEvent = true;
}
}
}It has a button in scene and you can drag it. Some main points:
- scene.RootWidget can't receive touch event, so you need add those button into a panel and handle touch event of panel.
- You have to set panel's property HookChildTouchEvent = true. That way, your panel can receive touch event of button. Unfortunately, this property is protected and default is false, so you need a CustomPanel extends from Panel and set this property in constructor.
on 02-05-2012 08:09 PM
rey5137 wrote:Here is my sample code :
using System; using System.Collections.Generic; using Sce.Pss.Core; using Sce.Pss.Core.Environment; using Sce.Pss.Core.Graphics; using Sce.Pss.Core.Input; using Sce.Pss.HighLevel.UI; namespace Test { public class AppMain { private static GraphicsContext graphics; private static CustomPanel panel; private static Button button; private static bool drag; private static Vector2 memo_pos; 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); Scene scene = new Scene(); panel = new CustomPanel(); panel.SetSize(960, 544); panel.TouchEventReceived += PanelTouchEventReceived; button = new Button(); button.Text = "Button"; button.SetPosition(100, 100); panel.AddChildLast(button); drag = false; scene.RootWidget.AddChildLast(panel); UISystem.SetScene (scene, null); } static void PanelTouchEventReceived (object sender, TouchEventArgs e) { TouchEvent touchEvent = e.TouchEvents.PrimaryTouchEvent; switch(touchEvent.Type){ case TouchEventType.Down: if(button.HitTest(touchEvent.WorldPosition)){ drag = true; memo_pos = touchEvent.WorldPosition; } break; case TouchEventType.Up: if(drag) drag = false; Console.WriteLine(touchEvent.Type.ToString()); break; case TouchEventType.Move: if(drag){ button.X += touchEvent.WorldPosition.X - memo_pos.X; button.Y += touchEvent.WorldPosition.Y - memo_pos.Y; memo_pos = touchEvent.WorldPosition; } break; } } public static void Update () { // Query gamepad for current state var gamePadData = GamePad.GetData (0); List<TouchData> list = Touch.GetData (0); UISystem.Update (list); } 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 (); } } class CustomPanel : Panel { public CustomPanel(){ HookChildTouchEvent = true; } } }It has a button in scene and you can drag it. Some main points:
- scene.RootWidget can't receive touch event, so you need add those button into a panel and handle touch event of panel.
- You have to set panel's property HookChildTouchEvent = true. That way, your panel can receive touch event of button. Unfortunately, this property is protected and default is false, so you need a CustomPanel extends from Panel and set this property in constructor.
thank you very much my friend!!! I will try your code tonight!!! If I have any questions I will let you know!
on 02-05-2012 08:52 PM
Your code works perfectly!!! However, I wanted to add a second button (button2), so I created a second panel (panel2)... I added the button2 to the panel2 and created a similar function like yours to handle the touch events... But, when I ran the application and tried to drag the 2nd button, it dragged the 1st!!! I may have done something wrong ... (definitely done something wrong)...
on 02-05-2012 09:47 PM
Why do you need two panels, and do they overlap?
I also wonder if you can't just handle the drag events by hooking up to events on the button directly? You'd only need to have them on the panel level I think if you also need to be able to drag outside of the buttons?
Just guessing.
on 03-05-2012 05:10 AM
03-05-2012 05:46 AM - edited 03-05-2012 05:59 AM
it worked... I created a panel for each button and now everything works fine!!! thanks to all for your help!!!
One final question: I am trying to play an intro video for my application. I have the file in AVI format.... Which function do I use to show it when my application starts?
PSM Developer Registration (for free) on PSM DevPortal is required to post on the forum.
Please sign out then sign in again to the forum and PSM DevPortal after you have completed the registration.
フォーラムへ投稿をするにはPSM DevPortalへの登録(無料)が必要です。
登録後はフォーラムと
PSM DevPortalを一度ログアウトし、再度ログインしてください。


Website ©2013 Sony Computer Entertainment Europe
All content, game titles, trade names and/or trade dress, trademarks, artwork and associated imagery are trademarks and/or copyright material of their respective owners. All rights reserved. [more info]
%%http://community.eu.playstation.com/t5/Announcements/Beta-Trial-Information/td-p/11386362
best_shooter.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_driver.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_performer.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_footballer.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_fighter.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_creator.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
best_action_player.png%%http://community.eu.playstation.com/t5/Announcements/Introducing-Best-of-PlayStation/td-p/13741979
dev2.png%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/Producer-and-Developer-Ranks/td-p/18407352
trophy.gif%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/The-Community-Awards-FAQ/td-p/18407096
PSlogoSM.png%%http://community.eu.playstation.com/t5/Website-and-Forum-Help-Feedback/Online-Support-Coordinator-rank/td-p/18414870