How can you position a HaxeFlixel canvas in the centre of a chrome window? - haxe

At the moment, I'm learning HaxeFlixel, and I can centre the text in the canvas, but i can't position the canvas in the centre of chrome the chrome window. Is there a way to do it and Main.hx?
PlayState.hx :
package;
import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;
class PlayState extends FlxState
{
override public function create()
{
super.create();
var text = new FlxText(0, 0, 0, "Hello World\n", 64);
text.screenCenter();
add(text);
}
override public function update(elapsed:Float)
{
super.update(elapsed);
enterFullscreen();
}
public function enterFullscreen()
{
if (FlxG.keys.justPressed.ENTER)
{
FlxG.fullscreen = !FlxG.fullscreen;
}
}
}
Main.hx :
package;
import flixel.FlxGame;
import openfl.display.Sprite;
class Main extends Sprite
{
public function new()
{
super();
addChild(new FlxGame(0, 0, PlayState));
}
}
Please help,
Thanks.

When you compile an HaxeFlixel project for the web, there are two parts:
all the Haxe code is converted to Javascript, executing inside a <canvas> HTML object; all the rest of the HTML page is taken from a template, and you need to change that to center your canvas or otherwise add other elements on the side.
The default template that HaxeFlixel uses is inside the Lime project: https://github.com/openfl/lime/blob/develop/templates/html5/template/index.html
You need to copy that file inside your project, for example in assets/index.html; then customize it, for example adding the <center> tag around <div id="content"></div> (the div where your game canvas will be placed):
<center>
<div id="content"></div>
</center>
You can use CSS to center it, too, if you're so inclined. Just be careful touching the lines with :: as those are template directives.
With your new template placed in assets/index.html, add this line inside project.xml:
<template path="assets/index.html" />

Related

Importing .dae file on Processing

What I am trying to do is export from Blender a .dae file of an animated object and then import this file on processing and use it there. I created a rotating cube, baked action and exported it as .dae file. the problem is when I am importing it on processing the animation doesn't work and I can see only a square.
import ch.dieseite.colladaloader.ColladaLoader;
import ch.dieseite.colladaloader.wrappers.ColladaModel;
ColladaModel model;
void setup() {
size(500,600,P3D);
background(255);
lights();
frameRate(10);
model = ColladaLoader.load("cuberot.dae", this, null);
model.scale(25);
}
void draw() {
translate(width/2,height/2);
model.draw();
}
Can someone explain how this can work?

How to highlight list of Elements in cucumber using JAVA when using POM as a design pattern

I have certain list of elements that i want to highlight it. I am using POM as a design pattern. Using cucumber with Java. I am using DataTable to fetch list of records. Please guide me in the code part that i have written.
*My Feature File* :-
#TC1
Scenario: Verify the Dashboard of the Website / Home page of the website
Given user is on HomePage
Then user should be able to view following <links> such as
|links|
|Solutions|
|About Us |
|Services |
|Products |
|Locations|
|Home|
|About Us|
|Contact Us|
|ATM Services|
|Online Services|
|Register|
|Forgot link info|
I want to highlight these elements shown under links header.
How to use List as a parameter & highlight the elements? I want to define my function in homePage.java & call it under Step Definition file.
In 'src/main/java' , i have made a java class file named as "HomePage.java" where in i have defined my elements using #FindBy keyword & added some methods performing some functions .
View of my HomePage.Java
public class HomePage {
public static WebDriver driver;
/*public HomePage(WebDriver driver)
{
PageFactory.initElements(driver, this);
}*/
#FindBy(xpath="html/body/div[1]/div[2]/ul/li/a")
private List<WebElement> links;
public void user_homePage()
{
System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://parabank.parasoft.com/parabank/index.htm");
//driver.manage().window().maximize();
}
public void highlight_On_Element(DataTable dt) throws Throwable
{
List<String> data=dt.asList(String.class);
{
driver.findElement(By.name(data.get(0)));
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);",data, "color: blue; border: 2px solid Magenta;");
}
In 'src/test/java', I have made a step definitions file & Test Runner class.
So view of my step definition file is:
package StepDefinitions;
import java.util.List;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import PageObjects.HomePage;
public class Steps extends HomePage
{
private HomePage home;
#Given ("^user is on HomePage$")
public void home_page()
{
HomePage home=new HomePage();
home.user_homePage();
}
#Then ("^user should be able to view following <links> such as$")
public void verify_links(DataTable dt) throws Throwable
{
// List<String> data=dt.asList(String.class);
home.highlight_On_Element(dt); // Calling func "highlight_on _element define in Home page java file.
}
}
Error is displayed when i execute either feature file / Test runner java file , the error is
java.lang.NullPointerException
the error is highlighting on step
home.highlight_On_Element(dt);
defined in step definition file .
i am new to learning cucumber by myself.
Expected Result:
I want to highlight the elements "Links" written in feature file.
I have written the xpath's of following links in a form of List.
How can one use List as a parameter in a function & calling it in a step definition file . Using List i can traverse each & every element defined under header "Links" & highlight it using JavaScriptExecutor.
If your findElement By method is working and finding the element properly, you can highlight every field with following code block :
public void highlight_On_Element(DataTable dt) throws Throwable
{
List<String> data=dt.asList(String.class);
for(int i=0; i<data.size(); i++) {
driver.findElement(By.name(data.get(i)));
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);",data, "color: blue; border: 2px solid Magenta;");
}
}
And in your Step Definition file you can use it as following :
#Then ("^user should be able to view following links such as$")
public void verify_links(DataTable dt) throws Throwable
{
home.highlight_On_Element(dt);
}
Or you can convert that dataTable to list in your step definition file and you can pass that list as a parameter however before do it you must change the parameter for highlight_On_Element method from DataTable to List.
public void highlight_On_Element(List lst) throws Throwable
{
for(int i=0; i<lst.size(); i++) {
driver.findElement(By.name(lst.get(i)));
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);",data, "color: blue; border: 2px solid Magenta;");
}
}

How do I assign contents of a string to a text field in Unity 4.6?

I am working on a C# class that imports text data from a web site. That works OK. Where I'm losing it is how to display the text in Unity 4.6 once I have all the text in a string variable. Any advice is appreciated.
Unity 4.6 UI system has component named Text. You can watch video tutorial here. Also I suggest you to check out its API.
As always, you have two options on how to add this component to the game object. You can do it from editor (just click on game object you want to have this component in hierarchy and add Text component). Or you can do it from script using gameObject.AddComponent<Text>().
In case you are not familiar with components yet, I suggest you to read this article.
Anyway, in your script you'll need to add using UnityEngine.UI; at the very top of it, because the Text class is in UnityEngine.UI namespace. Ok, so now back to script that will set the value of Text component.
First you need variable that refers to Text component. It can be done via exposing it to editor:
public class MyClass : MonoBehaviour {
public Text myText;
public void SetText(string text) {
myText.text = text;
}
}
And attaching gameObject with text component to this value in Editor.
Another option:
public class MyClass : MonoBehaviour {
public void SetText(string text) {
// you can try to get this component
var myText = gameObject.GetComponent<Text>();
// but it can be null, so you might want to add it
if (myText == null) {
myText = gameObject.AddComponent<Text>();
}
myText.text = text;
}
}
Previous script is not a good example, because GetComponent is actually expensive. So you might want to cache it’s reference:
public class MyClass : MonoBehaviour {
Text myText;
public void SetText(string text) {
if (myText == null) {
// looks like we need to get it or add
myText = gameObject.GetComponent<Text>();
// and again it can be null
if (myText == null) {
myText = gameObject.AddComponent<Text>();
}
}
// now we can set the value
myText.text = text;
}
}
BTW, the patter of ‘GetComponent or Add if it doesn’t exist yet’ is so common, that usually in Unity you want to define function
static public class MethodExtensionForMonoBehaviourTransform {
static public T GetOrAddComponent<T> (this Component child) where T: Component {
T result = child.GetComponent<T>();
if (result == null) {
result = child.gameObject.AddComponent<T>();
}
return result;
}
}
So you can use it as:
public class MyClass : MonoBehaviour {
Text myText;
public void SetText(string text) {
if (myText == null) {
// looks like we need to get it or add
myText = gameObject.GetOrAddComponent<Text>();
}
// now we can set the value
myText.text = text;
}
}
make sure you import the ui library - using UnityEngine.UI
gameObject.GetComponent<Text>().text - replace .text with any other field for UI Text
I assume that the issue is creating dynamic sized "textbox" rather than just assigning the string to a GUIText GameObject. (If not - just put a GUIText GameObject into your scene, access it via a GUIText variable in your script and use myGUIText.text = myString in Start or Update.)
If I am correct in my assumption, then I think you should just be using a GUI Label:
http://docs.unity3d.com/ScriptReference/GUI.Label.html
If you need to split the string up to place text into different labels or GUITexts, you will need to use substrings

Can't use embed font

I'm doing everything just like in the instruction.
Class Fonts.hx
import flash.text.Font;
#:font("assets/fonts/OPENSANS-REGULAR_0.TTF") class OpenSansFont extends Font { }
class Fonts
{
public static inline var OPEN_SANS_PATH = "assets/fonts/OPENSANS-REGULAR_0.TTF";
public static inline var OPEN_SANS_FONTNAME = "OPENSANS-REGULAR_0.TTF";
public function new()
{
Font.registerFont(OpenSansFont);
}
}
But when I try create TextFormat with this:
var tf:TextFormat;
var openSans:Font = Assets.getFont(Fonts.OPEN_SANS_PATH);
tf = new TextFormat(openSans.fontName);
I catch this error:
Assets.hx:257: [openfl.Assets] There is no Font asset with an ID of
"assets/fonts/OPENSANS-REGULAR_0.TTF"
What am I doing wrong?
My project structure:
You can't use openfl.Assets for Assets embedded via #:font / #:bitmap etc.
You should use the font's name for the TextFormat constructor. I assume you've already tried that, seeing how there's an OPEN_SANS_FONTNAME variable. However, that's not the font's name, just its file name.
On Windows, you should be able to find out the name by double-clicking on the font (right under the print / install buttons).
Alternatively, this should work as well:
import flash.text.Font;
#:font("assets/fonts/OPENSANS-REGULAR_0.TTF") class OpenSansFont extends Font { }
class Fonts
{
public static var OPEN_SANS_FONTNAME;
public function new()
{
Font.registerFont(OpenSansFont);
OPEN_SANS_FONTNAME = new OpenSansFont().fontName;
}
}

Can I use canvas and form together in a application

Can I use Canvas and form together in application? If yes then how can I access form from Canvas?
Yes you can use both but not at the same time. You can switch between them by using Display.setCurrent().
You need your Midlet to find its display (and the form).You would to send a reference of your MIDlet to the canvas constructor.So your canvas would be look like this:
class myCanvas extends Canvas implements ... {
myMIDlet myHost; // the breadcrumb
public myCanvas(... , myMIDlet host) {
myHost = host; // remember our host MIDlet
...
}
...
}
Where "myMIDlet" is name of your MIDlet.
In your MIDlet:
public class myMIDlet extends MIDlet implements ... {
...
Form myForm = new Form( ...
...
myCanvas ggg = new myCanvas(... , this) // <=== note the last parameter-the key to it all
...
//switch display to myCanvas
display.setCurrent(ggg);
...
}
When you want to got to the form from "myCanvas" (probably in a commandAction or keyPressed methods of your canvas) do this:
Display disp = myHost.getDisplay();
disp.setCurrent(myHost.myForm);
Reference:
codeproject
You can switch form to canvas. For example:
canvasName c=new canvasName(this);
Display.getDisplay(this).setCurrent(c);
But, in Canvas to Form, I don't know.

Resources