Lightweight Phaser UI plugin
Released as public domain - feel free to do anything to the library code you like. (this excludes the assets and the Phaser game engine)
SlickUI beats the challenge of creating user interfaces easily in an object oriented way.
Install using git:
git clone https://github.com/Flaxis/slick-ui.gitInstall using bower:
bower install slick-uiMake sure you have the [Default Kenney theme] in your project assets and ready to load.
Add the following to the bottom of your preload function:
// You can use your own methods of making the plugin publicly available. Setting it as a global variable is the easiest solution.
slickUI = game.plugins.add(Phaser.Plugin.SlickUI);
slickUI.load('assets/ui/kenney/kenney.json'); // Use the path to your kenney.json. This is the file that defines your theme.That's it! You're ready to start UI-ing!
To start using the UI manager, find a nice spot in your create() function and get started:
Assuming you're using the variable slickUI for the plugin object
var panel;
slickUI.add(panel = new SlickUI.Element.Panel(8, 8, 150, game.height - 16));This tells the UI manager to add a new panel at X and Y = 8, width of 150 pixels and as high as the game minus 16 pixels.
We can now use the panel's container to add new elements to it.
Let's say we wanted to add a button to the panel we just created:
var button;
panel.add(button = new SlickUI.Element.Button(0,0, 140, 80));
button.events.onInputUp.add(function () {console.log('Clicked button');});
button.add(new SlickUI.Element.Text(0,0, "My button")).center();We now added a button to the panel with the label 'My button'. When we click on it, the console will output 'Clicked Button'.
So what if we wanted to use SlickUI to be a bit more generic? We can also add DisplayObjects to the user interface in the same way.
We'll assume we have a sprite cached as 'menu-button'
var menuButton;
slickUI.add(menuButton = new SlickUI.Element.DisplayObject(8, 8, game.make.sprite(0, 0, 'menu-button')));That's it! You might be thinking, why would you add a DisplayObject using the UI manager if we can do that just by using phaser's built in tools?
The answer is, because UI elements are cascading and they take care of that themselves by using containers. When adding a Panel, Button or DisplayObject, the UI manager puts it in a container and adds a Phaser group to keep the descending elements organized so you can manipulate entire containers.
Checkboxes can be added using 3 sprites: checkbox, radio and cross. This is how you add a checkbox:
var cb;
panel.add(cb = new SlickUI.Element.Checkbox(0,10, SlickUI.Element.Checkbox.TYPE_RADIO));
cb.events.onInputDown.add(function () {
console.log(cb.checked ? 'Checked' : 'Unchecked');
}, this);If you don't provide a type using the last parameter, the default type will be used. You can choose between the following types:
Sliders are used to give the illusion of analog control over an object's property. For example, the game's music volume.
var slider;
panel.add(slider = new SlickUI.Element.Slider(16,100, panel.width - 32));
/*
* To create a slider rendered vertically:
* new SlickUI.Element.Slider(game.width - 64,130, game.height - 178, 0.5, true);No open issues yet, or sync has not completed.