Code Monkey home page Code Monkey logo

slick-ui's Introduction

Gitter

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

SlickUI

SlickUI beats the challenge of creating user interfaces easily in an object oriented way.

Try out some previews

Getting started

Install using git:

git clone https://github.com/Flaxis/slick-ui.git

Install using bower:

bower install slick-ui

Make 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!

Usage

To start using the UI manager, find a nice spot in your create() function and get started:

Adding a panel

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.

Adding a button

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.

Adding a DisplayObject

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.

Adding a Checkbox

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:

  • SlickUI.Element.Checkbox.TYPE_CHECKBOX (default type, no need to specify)
  • SlickUI.Element.Checkbox.TYPE_RADIO
  • SlickUI.Element.Checkbox.TYPE_CROSS

Adding a Slider

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); <- The last parameter indicates we want a vertical slider.
 */
slider.onDrag.add(function (value) {
    // This will log the slider's value on a scale of 100 every time the user moves the drag handle
    console.log(Math.round(value * 100) + '%');
});
slider.onDragStart.add(function (value) {
    // This will be logged when the user clicks on the drag handle
    console.log('Start dragging at ' + Math.round(value * 100) + '%');
});
slider.onDragStop.add(function (value) {
    // This will be logged when the user releases the drag handle
    console.log('Stop dragging at ' + Math.round(value * 100) + '%');
});

Adding a text input field

Text input fields are very useful for asking the name of the player. They use canvas embedded virtual keyboards.

// The last argument is used to determine the maximum amount of characters the input field can have. Defaults to 7 if kept empty.
var textField = panel.add(new SlickUI.Element.TextField(10,58, panel.width - 20, 40, 7));
textField.events.onOK.add(function () {
    alert('Your name is: ' + textField.value);
});
textField.events.onToggle.add(function (open) {
    console.log('You just ' + (open ? 'opened' : 'closed') + ' the virtual keyboard');
});
textField.events.onKeyPress.add(function(key) {
    console.log('You pressed: ' + key);
});

As you can see, there are three events you can listen to: onOK, onToggle and onKeyPress

  • onOK gets dispatched when the user clicks the 'OK' key to confirm their input
  • onToggle gets dispatched when the virtual keyboard opens or closes. A boolean parameter is provided telling whether the keyboard is opened (true) or closed (false)
  • onKeyPress gets dispatched whenever the user enters a key in the virtual keyboard. Note that the DEL key gets spelled out entirely in when accessing the key in the first parameter.

Remove an element from a panel

You can remove any element from a panel if you need to clean it up.

panel.remove(button);
panel.remove(cb);
panel.remove(slider);

This will remove and destroy the element from the panel.

Destroy the panel

If you don't need a panel anymore, you can destroy it.

panel.destroy();

slick-ui's People

Contributors

elsemieni avatar flaxis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

slick-ui's Issues

basic & minimum unit testing coverage

can we have some minimum unit testing code provided so that any broken change can be detected when a new change is introduced? is it possible to do so for game UI library?

Resize and repositioning elements

Hi,

i'm developing a responsive application. How can i manage the responsiveness of the elements? I need to resize and repoisition them on resize.

Thanks

last working version that works with Phaser CE is from August 22, 2016?

at this commit, i get this to work with Phaser CE v2.10.2 5a28594#diff-63f0e32da459bffac0edf8f1f427b5be

the issue is context is undefined:

Phaser.Plugin.SlickUI.prototype.getRenderer = function(name) {
    if (void 0 !== this.renderer[name]) return this.renderer[name];
    var theme = this.game.cache.getJSON("slick-ui-theme"), resolveObject = function(name) {
        var namespace = name.split("."), context = window;
        for (var i in namespace)  context = context[namespace[i]];        
        return context;
    };

where it tries to loop through all the namespace and tries to pull it from context[namespace[i]] but context is just a window - is that the expected?

cc @Flaxis

Vertical Slider (new Feature)

I love this plugin already, but for my purposes I needed a Vertical Slider. It is not really hard to do, and it is a really simple way of adding more features. My dirty way of doing it was:

  1. copy SliderRenderer.js, Slider.js
  2. alter the code to use y and height instead of x and width

I would love to have a vertical slider added for the next release, and I simply propose a new parameter when calling VSlider so that instead of doing
var slider = new SlickUI.Element.Slider(16,100, someSize, someValue);

You do
var slider = new SlickUI.Element.Slider(16,100, someSize, someValue, "vertical"); or
var slider = new SlickUI.Element.Slider(16,100, someSize, someValue, "horizontal");

All that is required is to change a couple of lines in Slider.js (this is very straightforward), and to add VSliderRenderer ( I had lots of trouble with that part so I just didn't display anything fancy with it for the vertical slider)

Upload to npm?

Hello,

A friend of mine wants to use this library for a small game we are making and currently has it downloaded into our repo. Me being a neat freak, wanted to integrate this library using npm and I found that this library isn't on npm.

So could it be possible to upload slick-ui to npm?

Thank you

Borders as offset

Hi, i was using the plugin when i noticed that panels and probably all elements have a small offset.

After a while i noticed it was due to the border-x and border-y field on the kenney json.

Long story short, i had to modify the panel script by removing all the 'border-x' and 'border-y' calls.

Hope this helps someone :D

PD: The same could PROBABLY be done on all elements, haven't test it yet.

Uncaught TypeError at this.DisplayGroup

When trying to set this.displayGroup to game.add.group(), game is an undefined reference and throws a TypeError. I've included the log from Chrome on Windows 10 as I'm not sure where this issue is coming from.
192.168.1.103-1472522944776.txt

Uncaught TypeError: b is not a constructor
   Phaser v2.6.1 | Pixi.js | WebGL | WebAudio     http://phaser.io ♥♥♥
Tue, 30 Aug 2016 01:59:56 GMT, INFO: Preloading game assets
Tue, 30 Aug 2016 01:59:56 GMT, INFO: Loading Phaser plugins
Tue, 30 Aug 2016 01:59:56 GMT, INFO: Configuring Phaser plugins
Uncaught TypeError: Cannot read property 'group' of undefined
Failed to load resource: the server responded with a status of 404 (Not Found)

Update button/element text and/or destroy panel?

Hi there, thanks for this useful plugin. I'm trying to use it to dynamically generate multiple choice questions (see image). I either need to destroy the panel and create a new one each time a new question prompt occurs, or update the text on the buttons/elements.

I can't find anything in the documentation to destroy() or kill() panels once they have been created, nor any way to dynamically update button/element text.

Any help gratefully received.

Best,
Paul
screen shot 2017-01-31 at 3 29 05 pm

Phaser 3 support?

Sorry, this is not an issue, but a question.

I would like to know if this is compatible with Phaser 3, and if it is how do I load this with Phaser 3?

Thanks,

Typescript definitions

Have you considered writing some Typescript definitions for SlickUI? I am currently using it in my Typescript project but i get a bunch of annoying errors and code completion doesn't work properly.

Changing text color

Can't find anything in documentation about this, and as far as I can tell Element.Text has no color attribute. Am I missing something here?

Text Inputs?

I've just recently found this project and I can already see it being super useful. A major thing that is currently missing from this is text input. The only way I know to accept text input on Phaser is to make a "hack" using the HTML element, but something that uses the canvas natively would be extremely helpful.

Must have global "game" variable?

Hi,

I'm getting a reference error:
"slick-ui.min.js:1 Uncaught ReferenceError: game is not defined"

Do I need to have a global game variable? Why?
Thanks

Texture Atlas support

Hey, thanks for your work on this library.

I was wondering if you're planning on adding Texture Atlas support? From what I can tell there doesn't seem to be an option to use an Atlas.

multiple sliders on a page do not get move callback

I think there is a flaw in the move callback if there are multiple sliders in a panel. I changed all references in Element/Slider.js from sprite_handle to this.sprite_handle and it seems to have fixed the problem.

Also, I'm curious about why there are different color textures for the handle, when sprite.tint seems to work just fine. I just left all the sprite_handles as grey and then just tinted them whatever color I wanted.

dropdown elements

Hi,

will be useful to add dropdowns too? I mean like the <select> HTML tag.

I dont' think that a "multiline" <select> will be useful, because you can obtain the same result using checkboxes with a clickable label (as happens using the <label> HTML tag).

Thanks.

can't add "Panel"

Hi,

this is a piece of my code.

//== PRELOAD STATE
...
preload() {
        //== SlickUi
        slickUI = Game.plugins.add(Phaser.Plugin.SlickUI);
        slickUI.load(kenney.json');
    }
...
//**
   * MENU STATE
   *  the "Preload State" call the "Menu State" where i create the "Panel" element.  
   * "_G.SLICKUI" is a global variable
*/
...
create() {
        let panel;
        console.log(SlickUI.Element); // ==> 'Undefined'
        _G.SLICKUI.add(panel = SlickUI.Element.Panel(8, 8, 150, 16)); // ==> Cannot read property 'Panel' of undefined
}
...

I can't figure out how to solve it.
If call SlickUI.Element.Panel in the console it returns a function (so it should work), but return Undefined if called inside create() method

slick-ui.js

Please make slick-ui.js, not minified file, not many files.

Scrollbar in containers

At some point, I'll want to make scrolling possible in containers, such as panels. This also needs to be implemented before work can be started on #4, because in the dropdown options, you'll want to have the ability of setting a max height.
Swiping for mobile users should be considered.

Roll on/off a button

Can you add the ability to drag finger / mouse off and back on to a selected button in case user decides they don't want to click / cancel their action

Congrats on the Slick project. You had more time that me to get this thing out there! I look forward to using it.

how to hide/delete panels / elements?

Hey, sorry if this is a dumb question...

I'm not sure how to delete elements (say, hide/show additional menus). Is there a builtin way to do this, or should I directly show/hide the sprites associated with each element?

phaser object variable name

it appears if you don't spell the phaser object exactly as game and make it global, slick fails to create some components

ReferenceError: game is not defined
    at SlickUI.Element.Slider.init (slick-ui.min.js:1)
    at SlickUI.Container.Container.add (slick-ui.min.js:1)
    at SlickUI.Element.Panel.add (slick-ui.min.js:1)
    at Object.create (game.js:22)

line is this.displayGroup=game.add.group();

license?

Firstly, this is really nice work and thank you for making it open source regardless of your license choice.

I'm a bit wary of using something that doesn't include a license. Phaser is licensed under the very permissive MIT license --- which I would be happy to see here too, but the choice is yours of course.

Slider handle snaps to position onDragStop

It would be useful if the slider handles were able to have defined snap points along the slider lines.

Is this already possible somehow or would it require some changes to the underlying Slider element?

Keyboard not hiding when parent.visible = false

I've got a menu panel with multiple text fields in it and I'm closing the menu by setting the panel's visibility to false. Unfortunately if the keyboard is open when I close the panel, the keyboard stays open. It also lets you open multiple keyboards on top of each other.

I've tried a few things but I'm not sure where the reference to the keyboard is - destroying the panel also doesn't destroy the keyboard, and it doesn't seem accessible through the TextField object. Any ideas?

How do you bring a panel to top

Hi, and sorry if I'm asking something stupid.
I built my panel ahead of time, and many other objects have been created later. I thought doing game.world.bringToTop(panel) or game.world.bringToTop(panel._sprite) would do the trick, but my panel is still behind everything else... Do you have any idea of what I'm doing wrong ?

thansk for reading

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.