Using Heaps Components

DomKit is a library that can be used together with Heaps to create complex UI and integrate custom 2D components into it.

You should get install it first using:

haxelib git domkit https://github.com/HeapsIO/domkit.git

You can then add it to your project libraries with -lib domkit (together with -lib heaps)

You can see compile and run the corresponding Heaps Sample

In order to use Domkit to create a heaps components, you simply need to implements h2d.domkit.Object and define your document in the SRC static as the following sample shows:

  1. class SampleView extends h2d.Flow implements h2d.domkit.Object {
  2. static var SRC =
  3. <sample-view layout="vertical">
  4. Hello World!
  5. <bitmap src={tile} public id="mybmp"/>
  6. </sample-view>
  7. public function new(tile:h2d.Tile,?parent) {
  8. super(parent);
  9. initComponent();
  10. }
  11. }
  12. ...
  13. var view = new SampleView(h2d.Tile.fromColor(0xFF,32,32),s2d);
  14. view.mybmp.alpha = 0.8;

You can read more about Domkit Markup Syntax in the reference below.

Applying CSS

You can then apply CSS at runtime to your document by using the following code:

  1. var style = new h2d.domkit.Style();
  2. // resource referencing res/style.css (see Heaps Resources documentation)
  3. style.load(hxd.Res.style);
  4. style.addObject(view);

Here’s an example CSS that can apply to previous view:

  1. .box {
  2. padding : 20;
  3. background : #400;
  4. }

Runtime CSS reload

In order to get runtime CSS reload you need to: use a hxd.Res.initLocal() so you use a local filesystem to load resources. This is only available to some platforms such as HashLink enable resources live update with hxd.Res.LIVE_UPDATE = true;

Then every time you modify your CSS file, the style will be reapplied to all your currently displayed components. Errors will be displayed in case of invalid CSS property or wrongly formatted value.

Compile time CSS strong checking

You can add an init macro that will process at compile time your CSS and will report any issue at Haxe compilation time. This can be done by calling inside a macro:

  1. domkit.Macros.checkCSS("res/style.css");

Or my adding to your Haxe compilation parameters :

  1. --macro domkit.Macros.checkCSS('res/style.css')

Please note that since each CSS property can be used by different components in different ways, and since you can’t always tell by CSS rules on which component type the property will be applied, we only check that the property is valid and the CSS accepted for one of the components that defines it.

Accessing the DOM

When compiled with -lib domkit, each h2d.Object will have an extra dom : domkit.Properties field that can be used for changing the state of the object wrt CSS :

  1. mybmp.dom.addClass("myclass");
  2. mybmp.dom.hover = true;

More code can be found in the Heaps Domkit sample.

Domkit Inspector

You can inspect your components by enabling the domkit inspector. This is done using

  1. style.allowInspect = true;

You can then using middle mouse button click anytime to enable/disable domkit inspector. Move your mouse over any component to see its name, id, classes and properties. You can use the mouse wheel to browse the component hierarchy. Maintaining Ctrl while clicking will also display the hierarchy in a separate tree-view.

image

Defining custom Components

In order to define custom components, you need to: implements h2d.domkit.Object (if not already inherited from your superclass) add @:p for each property you wish to expose to domkit markup/CSS * you can add @:uiComp("name") metadata in order to customize the component name

Here’s a small example:

  1. // MyComp.hx
  2. enum CustomStyle {
  3. Big;
  4. Small;
  5. Medium;
  6. }
  7. @:uiComp("my")
  8. class MyComp extends h2d.Flow implements h2d.domkit.Object {
  9. @:p public var style(default,set) : CustomStyle;
  10. function set_style(s) {
  11. this.style = s;
  12. // ....
  13. return s;
  14. }
  15. }

It is now possible to use your component from any document SRC by doing the following:

  1. <my style="medium"/>

Components with SRC

It is possible to have a component that also have a SRC. In that case, you need to reference the component name in your SRC root node name so its properties can be applied correctly.

Components resolution

Components are resolved by name by adding Comp to the capitalized component name. For instance <something/> will try to load SomethingComp class from local context.

If you wish to customize this name resolution, you can use an init macro such as:

  1. // add --macro Init.setup() to your HXML / Haxe compilation parameters
  2. class Init {
  3. public static function setup() {
  4. domkit.Macros.registerComponentsPath("my.comps.$");
  5. }
  6. }

In the path, the $ character is the capitalized component name. By default "$Comp" is a registered path in Heaps.

Custom CSS parsing

Some properties requires custom parsing. For instance color codes, padding boxes, etc. You can specify which parser method to use by changing @:p metadata in the following way:

  1. // will use parseTile CSS parser method
  2. @:p(tile) public var tile : h2d.Tile;

You can use any identifier that is allowed in the current CSS parser. The default Heaps parser can be found in h2d/domkit/BaseComponents.hx

You can extend this parser with your own custom parser to support additional CSS parsing rules. Here’s an example:

  1. class MyParser extends h2d.domkit.BaseComponents.CustomParser {
  2. public function parseIntValues( value : domkit.CssValue ) : Array<Int> {
  3. return switch( value ) {
  4. case VList(vl): [for( v in vl ) parseInt(v)]; // comma separated values
  5. default: [parseInt(value)]; // single value
  6. }
  7. }
  8. }

And then in your component you need to specify which custom parser to use:

  1. @:uiComp("my") @:parser(MyParser)
  2. class MyComp extends h2d.Flow implements h2d.domkit.Object {
  3. ...
  4. @:p(intValues) public var values : Array<Int>;
  5. }

And it means you can now use values this way in attributes or CSS:

  1. <my values="1,2,3"/>

Domkit Markup Reference

Domkit markup allows the following syntaxes.

  1. <node attr="value"/>

A component with a CSS attribute value. Please note that the value has to be valid CSS and is parsed then interpreted based on the components custom parsing rules in order to be translated to the actual runtime value for the corresponding node property.

  1. <node attr={expr}/>

Set the attribute based on an Haxe code expression. Unlike previous syntax, here the expression must directly evaluates to the property runtime value, without going through CSS interpretation.

  1. <node attr/>

A shortcut for attr="true". Allows to easily set boolean attributes

  1. <node id="identifier"/>

Creates a field on the current class and set it to the component value upon initialization. By default the field is private.

  1. <node public id="identifier"/>

Same as above, expect the field is public.

  1. <node id="group[]"/>

Creates an Array of components on the current class and push the component value into it upon initialization.

  1. <node(value1,value2,value3)/>

Allows to pass constructor arguments to your custom component. ATM only Haxe values are allowed to be passed, DomKit does not allow passing CSS values to constructor arguments.

  1. <flow>
  2. ${ for( t in tiles ) <bitmap src={t} id="btns[]"/> }
  3. </flow>

When using ${..} syntax, you can inject any Haxe code into the markup language, allowing you to add if/for/method calls/etc.

  1. Some Text

This is the equivalent of <text text={"Some Text"}/>, so you can apply CSS to the text components created this way.

  1. <node>$component</node>

When just a Haxe variable identifier is injected into the document content, we assume it contains another component value to be inserted there.

  1. <some>
  2. // commented <a/></a>
  3. </some>

A single line comment within body

  1. <some>
  2. <a> /* commented <a/></a> */ </a>
  3. </some>

A multiline / delimited comment within body

  1. @something
  2. @call(value)
  3. @custom.path(value1,value2,value3)

These are markup macros that can be processed in a custom manner by setting domkit.Macros.processMacro in an init macro. It allows you to process identifier path and arguments (Haxe expressions) to return some markup syntax that will be then processed.

Heaps CSS reference

This is the complete documentation for allowed CSS attributes for native Heaps components.

object

See corresponding heaps documentation

  1. x : 0.5

x position

  1. y : 0.5

y position

  1. alpha : 0.5

opacity

  1. rotation : 45

rotation (unlike Heaps, value is expressed in degrees, not radians)

  1. visible : true

toggle visibility

  1. scale : 2.5
  2. scale : 0.5 0.8
  3. scale-x : 0.5
  4. scale-y : 2

Uniform or X/Y scaling

  1. blend : none
  2. blend : alpha (default)
  3. blend : add

Blendmode

Flow properties

These properties are only valid when the object parent is a <flow/> (see corresponding heaps documentation)

  1. margin : 5
  2. margin : 10 20
  3. margin-left : 20
  4. margin-top : 10
  5. margin-bottom : 5
  6. margin-right : 0
  7. margin : 10 20 5 0

Margins around the object (these are FlowProperties.padding values)

  1. align : left
  2. align : top
  3. align : bottom right
  4. halign : right
  5. valign : bottom

Allows to override alignment for a single object in the flow

  1. position : absolute
  2. position : auto

Tells if the object position is automatically set by the flow or manually managed

  1. offset : 10 20
  2. offset-x : 10
  3. offset-y : 20

Offset the object after final position calculus.

  1. min-width: 100
  2. min-height: none

Gives minimal width/height to the object wrt flow calculus.

drawable

See corresponding heaps documentation

  1. color : #ff0000
  2. color : #f00

Color tint

  1. smooth : true
  2. smooth : auto

Toggles bilinear filtering or keep auto mode

  1. tile-wrap : true

Enables tile wrapping

bitmap

See corresponding heaps documentation

  1. src : url("my/tile.png")
  2. src : #f00 20 20

Load the tile or creates one with the specific color and size

text

See corresponding heaps documentation

  1. text : "some text"

Text content to display

  1. font : url("path/to/bitmapfont.fnt")

Set font to be used for display

  1. letter-spacing : 2

Space between letters

  1. line-spacing : 4

Space between lines

  1. max-width : none
  2. max-width : 300

Maximum width for auto text wrapping

  1. text-align : left
  2. text-align : right
  3. text-align : middle

Alignment of the text (usually not used within a flow, instead use the align property)

  1. text-shadow : 1.5 2 #f00 0.5

Set a drop shadow for the text (dx,dy,color,alpha)

  1. text-color : #f00

Changes text color

flow

See corresponding heaps documentation

  1. width : auto
  2. width : 500
  3. min-width : 450
  4. max-height : none

Set min/max width/height constraints

  1. background : url("my/resource")
  2. background : url("my/resource") 5 8
  3. background : #f00
  4. background : transparent

Flow background with optional borderWidth/Height

  1. background : tile("my/resource",5)
  2. background-tile-pos: 3
  3. background : tile("my/resource",5,8)
  4. background-tile-pos: 3 7

Flow background with split tile (vertical and optional horizontal frames) and separate attribute for setting the sub tile

  1. debug : true

Display debug lines

  1. layout : vertical
  2. layout : horizontal
  3. layout : stack

Sets the flow in either layout display mode

  1. multline : true

Activates multiline

  1. padding : 5
  2. padding : 10 20
  3. padding-left : 20
  4. padding-top : 10
  5. padding-bottom : 5
  6. padding-right : 0
  7. padding : 10 20 5 0

Padding values around contained elements

  1. hspacing : 10
  2. vspacing : 5

Horizontal and vertical spacing between contained elements

  1. content-align : left
  2. content-align : right top
  3. content-valign : middle
  4. content-halign : right

Changes horizontal/vertical default alignment for contained elements (flow horizontalAlign/verticalAlign)