Difference between revisions of "Zircon"

From RogueBasin
Jump to navigation Jump to search
 
(19 intermediate revisions by 2 users not shown)
Line 20: Line 20:
<th style="background:#C2D8EF; padding:.2em; margin:1em;">Released
<th style="background:#C2D8EF; padding:.2em; margin:1em;">Released
</th>
</th>
<td>{{{released}}}
<td>July 9 2017
</td>
</td>
</tr>
</tr>
Line 27: Line 27:
<th style="background:#C2D8EF; padding:.2em; margin:1em;">Updated
<th style="background:#C2D8EF; padding:.2em; margin:1em;">Updated
</th>
</th>
<td>{{{updated}}}
<td>Dec 23 2017 (Stable), Mar 22 2018 (Preview)
</td>
</td>
</tr>
</tr>
Line 41: Line 41:
<th style="background:#C2D8EF; padding:.2em; margin:1em;">[[Licensing]]
<th style="background:#C2D8EF; padding:.2em; margin:1em;">[[Licensing]]
</th>
</th>
<td>{{{licensing}}}
<td>MIT License
</td>
</td>
</tr>
</tr>
Line 55: Line 55:
<th style="background:#C2D8EF; padding:.2em; margin:1em;">[[Platforms]]
<th style="background:#C2D8EF; padding:.2em; margin:1em;">[[Platforms]]
</th>
</th>
<td>{{{platforms}}}
<td>Linux, Mac OS, Windows
</td>
</td>
</tr>
</tr>
Line 62: Line 62:
<th style="background:#C2D8EF; padding:.2em; margin:1em;">Dependencies
<th style="background:#C2D8EF; padding:.2em; margin:1em;">Dependencies
</th>
</th>
<td>Linux, Mac OS, Linux
<td>
</td>
</td>
</tr>
</tr>
Line 72: Line 72:


</table>
</table>
= Zircon - A user-friendly Text GUI &amp; Tile Engine =
== Important information ==
To read the official docs of Zircon navigate [https://github.com/Hexworks/zircon here]
-----
== Getting Started ==
If you want to work with Zircon you can add it to your project as a dependency.
from Maven:
<source lang="xml"><dependency>
    <groupId>com.github.hexworks.zircon</groupId>
    <artifactId>zircon.jvm.swing</artifactId>
    <version>2018.3.27-PREVIEW</version>
</dependency></source>
or you can also use Gradle:
<source lang="groovy">compile("com.github.hexworks.zircon:zircon.jvm.swing:2018.3.27-PREVIEW")</source>
Note that you need to use [https://jitpack.io/#Hexworks/Zircon Jitpack] for the above dependencies to work.
Want to use a <code>PREVIEW</code>? Check [https://github.com/Hexworks/zircon/wiki/Release-process-and-versioning-scheme#snapshot-releases this Wiki page]
=== Some rules of thumb ===
Before we start there are some guidelines which can help you if you are stuck:
* If you want to build something (a <code>TileGraphics</code>, a <code>Component</code> or anything which is part of the public API) it is almost sure that there is a <code>Builder</code> or a <code>Factory</code> for it. The convention is that if you want to create an <code>TileGraphics</code> for example, then you can use the <code>DrawSurfaces</code> class to do so. (so it is the plural form of the thing which you want to build). Your IDE will help you with this. These classes reside in the <code>org.hexworks.zircon.api</code> package.
* If you want to work with external files like tilesets or REXPaint files check the same package (<code>org.hexworks.zircon.api</code>), and look for classes which end with <code>*Resources</code>. There are a bunch of built-in tilesets for example which you can choose from but you can also load your own. The rule of thumb is that if you need something external there is probably a <code>*Resources</code> class for it (like the [https://github.com/Hexworks/zircon/blob/master/zircon.jvm/src/main/kotlin/org/hexworks/zircon/api/CP437TilesetResources.kt CP437TilesetResources]).
* You can use ''anything'' you can find in the [https://github.com/Hexworks/zircon/tree/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api API] package, they are part of the public API, and safe to use. The [https://github.com/Hexworks/zircon/tree/master/zircon.core/src/main/kotlin/org/hexworks/zircon/internal internal] package however is considered private to Zircon so don’t depend on anything in it.
* Some topics are explained in depth on the [https://github.com/Hexworks/zircon/wiki Wiki]
* If you want to see some example code look [https://github.com/Hexworks/zircon/tree/master/zircon.examples/src/main here].<br />
* If all else fails read the javadoc. API classes are well documented.
* If you have any problems which are not answered here feel free to ask us at the [https://discord.gg/hbzytQJ Hexworks Discord server].
=== Creating an Application ===
In Zircon almost every object you might want to use has a helper class for building it. This is the same for [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/application/Application.kt Application]s as well so let’s create one using the [https://github.com/Hexworks/zircon/blob/master/zircon.jvm.swing/src/main/kotlin/org/hexworks/zircon/api/SwingApplications.kt SwingApplications] class:
<blockquote>Note that these examples reside in the <code>org.hexworks.zircon.examples.docs</code> package in the <code>zircon.examples</code> project, you can try them all out.
</blockquote>
<source lang="java">import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.application.Application;
public class CreatingAnApplication {
    public static void main(String[] args) {
        Application application = SwingApplications.startApplication();
    }
}</source>
Running this snippet will result in this screen:
[[File:https://cdn.discordapp.com/attachments/363771631727804416/477466202982055939/CreatingAnApplication.png]]
Not very useful, is it? So what happens here? An [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/application/Application.kt Application] is just an object which has a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/internal/renderer/Renderer.kt Renderer] for rendering [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/data/Tile.kt Tile]s on your screen), and a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/grid/TileGrid.kt TileGrid], which is the main interface which you will use to interact with Zircon. An [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/application/Application.kt Application] is responsible for ''continuously rendering'' the contents of the grid on the screen. Here we use the ''Swing'' variant, but there are other types in the making (one for LibGDX, and one which works in the browser).
Since most of the time you don’t care about the [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/application/Application.kt Application] itself, there is a function for creating a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/grid/TileGrid.kt TileGrid] directly:
<source lang="java">import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.grid.TileGrid;
public class CreatingATileGrid {
    public static void main(String[] args) {
        TileGrid tileGrid = SwingApplications.startTileGrid();
    }
}</source>
Now let’s see how we can specify how a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/grid/TileGrid.kt TileGrid] is created. We’ll use the [https://github.com/Hexworks/zircon/blob/master/zircon.jvm/src/main/kotlin/org/hexworks/zircon/api/AppConfigs.kt AppConfigs] helper for this:
<source lang="java">import org.hexworks.zircon.api.AppConfigs;
import org.hexworks.zircon.api.CP437TilesetResources;
import org.hexworks.zircon.api.Sizes;
import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.grid.TileGrid;
public class CreatingATileGrid {
    public static void main(String[] args) {
        TileGrid tileGrid = SwingApplications.startTileGrid(
                AppConfigs.newConfig()
                        .defaultSize(Sizes.create(10, 10))
                        .defaultTileset(CP437TilesetResources.rexPaint16x16())
                        .build());
    }
}</source>
Adding and formatting [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/data/Tile.kt Tile]s is very simple:
<source lang="java">import org.hexworks.zircon.api.AppConfigs;
import org.hexworks.zircon.api.CP437TilesetResources;
import org.hexworks.zircon.api.Positions;
import org.hexworks.zircon.api.Sizes;
import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.Tiles;
import org.hexworks.zircon.api.color.ANSITileColor;
import org.hexworks.zircon.api.grid.TileGrid;
public class CreatingATileGrid {
    public static void main(String[] args) {
        TileGrid tileGrid = SwingApplications.startTileGrid(
                AppConfigs.newConfig()
                        .defaultSize(Sizes.create(10, 10))
                        .defaultTileset(CP437TilesetResources.rexPaint16x16())
                        .build());
        tileGrid.setTileAt(
                Positions.create(2, 3),
                Tiles.newBuilder()
                        .backgroundColor(ANSITileColor.CYAN)
                        .foregroundColor(ANSITileColor.WHITE)
                        .character('x')
                        .build());
        tileGrid.setTileAt(
                Positions.create(3, 4),
                Tiles.newBuilder()
                        .backgroundColor(ANSITileColor.RED)
                        .foregroundColor(ANSITileColor.GREEN)
                        .character('y')
                        .build());
        tileGrid.setTileAt(
                Positions.create(4, 5),
                Tiles.newBuilder()
                        .backgroundColor(ANSITileColor.BLUE)
                        .foregroundColor(ANSITileColor.MAGENTA)
                        .character('z')
                        .build());
    }
}</source>
Running the above code will result in something like this:
[[File:https://cdn.discordapp.com/attachments/363771631727804416/477469640205926401/CreatingATileGrid.png]]
As you can see there is a helper for every class which you might want to use. Here we used <code>Positions.create</code> to create a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/data/Position.kt Position], <code>Sizes.create</code> for creating [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/data/Size.kt Size]s and the [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/builder/data/TileBuilder.kt TileBuilder] to create tiles.
A <code>Position</code> denotes a coordinate on a <code>TileGrid</code>, so for example a <code>Position</code> of (<code>3</code>, <code>4</code>) points to the 3rd column and the 4th row (x, y) on the grid.
Conversely a <code>Size</code> denotes an area with a width and a height. These two classes are used throughout Zircon.
A [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/data/Tile.kt Tile] is the most basic graphical unit Zircon supports. In most cases it is a simple character with a foreground and a background color (like in the example above).
In addition to colors and characters you can also use [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/modifier/Modifier.kt Modifier]s in your [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/data/Tile.kt Tile]s.
<blockquote>A lot of fancy stuff can be done with [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/modifier/Modifier.kt Modifier]s, like this:
[[File:https://cdn.discordapp.com/attachments/363771631727804416/477470683513880576/modifiers.gif]]
If interested check out the code examples [https://github.com/Hexworks/zircon/tree/master/zircon.examples/src/main here].
</blockquote>
<blockquote>Tileset (and by extension resource) handling in Zircon is very simple and if you are interested in how to load your custom fonts and other resources take a look at the [https://github.com/Hexworks/zircon/wiki/Resource-Handling Resource handling wiki page].
</blockquote>
=== Working with Screens ===
[https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/grid/TileGrid.kt TileGrid]s alone won’t suffice if you want to get any serious work done since they are rather rudimentary.
A [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen] has its own buffer and it can be <code>display</code>ed on a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/grid/TileGrid.kt TileGrid] any time. This means that you can have multiple [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen]s at the same time representing your actual game screens. ''Note that'' only ''one'' [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen] can be displayed at a given moment. <code>display</code>ing one deactivates the previous [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen].
Let’s create a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen] and fill it up with some stuff:
<source lang="java">import org.hexworks.zircon.api.AppConfigs;
import org.hexworks.zircon.api.CP437TilesetResources;
import org.hexworks.zircon.api.ColorThemes;
import org.hexworks.zircon.api.Positions;
import org.hexworks.zircon.api.Screens;
import org.hexworks.zircon.api.Sizes;
import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.DrawSurfaces;
import org.hexworks.zircon.api.Tiles;
import org.hexworks.zircon.api.component.ColorTheme;
import org.hexworks.zircon.api.graphics.TileGraphics;
import org.hexworks.zircon.api.grid.TileGrid;
import org.hexworks.zircon.api.screen.Screen;
public class CreatingAScreen {
    public static void main(String[] args) {
        TileGrid tileGrid = SwingApplications.startTileGrid(
                AppConfigs.newBuilder()
                        .defaultSize(Sizes.create(20, 8))
                        .defaultTileset(CP437TilesetResources.wanderlust16x16())
                        .build());
        final Screen screen = Screens.createScreenFor(tileGrid);
        final ColorTheme theme = ColorThemes.adriftInDreams();
        final TileGraphics image = DrawSurfaces.tileGraphicsBuilder()
                .size(tileGrid.size)
                .build()
                .fill(Tiles.newBuilder()
                        .foregroundColor(theme.getBrightForegroundColor())
                        .backgroundColor(theme.getBrightBackgroundColor())
                        .character('~')
                        .build());
        screen.draw(image, Positions.defaultPosition());
        screen.display();
    }
}</source>
and we’ve got a nice ocean:
[[File:https://cdn.discordapp.com/attachments/363771631727804416/477475680594952223/CreatingAScreen.png]]
What happens here is that we:
* Create a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen]
* Fetch a nice [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/component/ColorTheme.kt ColorTheme] which has colors we need
* Create a [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/graphics/TileGraphics.kt TileGraphics] with the colors added and fill it with <code>~</code>s
* Draw the graphic onto the [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen]
For more explanation about these jump to the [https://github.com/Hexworks/zircon#how-zircon-works How Zircon works] section.
<blockquote>You can do so much more with [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen]s. If interested then check out [https://github.com/Hexworks/zircon/wiki/A-primer-on-Screens A primer on Screens] on the Wiki!
</blockquote>
=== Components ===
Zircon supports a bunch of [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/component/Component.kt Component]s out of the box:
* <code>Button</code>: A simple clickable component with corresponding event listeners
* <code>CheckBox</code>: Like a BUTTON but with checked / unchecked state
* <code>Label</code>: Simple component with text
* <code>Header</code>: Like a label but this one has emphasis (useful when using [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/component/ColorTheme.kt ColorTheme]s)
* <code>Panel</code>: A [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/component/Container.kt Container] which can hold multiple [https://github.com/Hexworks/zircon/wiki/The-component-system Components]
* <code>RadioButtonGroup</code> and <code>RadioButton</code>: Like a <code>CheckBox</code> but only one can be selected at a time
* <code>TextBox</code>: Similar to a text area in HTML this [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/component/Component.kt Component] can be written into
These components are rather simple and you can expect them to work in a way you might be familiar with:
* You can click on them (press and release are different events)
* You can attach event listeners on them
* Zircon implements focus handling so you can navigate between the components using the <code>[Tab]</code> key (forwards) or the <code>[Shift]+[Tab]</code> key stroke (backwards).
* Components can be hovered and they can change their styling when you do so
Let’s look at an example (notes about how it works are in the comments):
<source lang="java">import org.hexworks.zircon.api.*;
import org.hexworks.zircon.api.component.Button;
import org.hexworks.zircon.api.component.CheckBox;
import org.hexworks.zircon.api.component.Header;
import org.hexworks.zircon.api.component.Panel;
import org.hexworks.zircon.api.grid.TileGrid;
import org.hexworks.zircon.api.screen.Screen;
public class UsingComponents {
    public static void main(String[] args) {
        final TileGrid tileGrid = SwingApplications.startTileGrid(
                AppConfigs.newConfig()
                        .defaultSize(Sizes.create(34, 18))
                        .defaultTileset(CP437TilesetResources.wanderlust16x16())
                        .build());
        final Screen screen = Screens.createScreenFor(tileGrid);
        Panel panel = Components.panel()
                .wrapWithBox() // panels can be wrapped in a box
                .title("Panel") // if a panel is wrapped in a box a title can be displayed
                .wrapWithShadow() // shadow can be added
                .size(Sizes.create(32, 16)) // the size must be smaller than the parent's size
                .position(Positions.offset1x1())
                .build(); // position is always relative to the parent
        final Header header = Components.header()
                // this will be 1x1 left and down from the top left
                // corner of the panel
                .position(Positions.offset1x1())
                .text("Header")
                .build();
        final CheckBox checkBox = Components.checkBox()
                .text("Check me!")
                .position(Positions.create(0, 1)
                        // the position class has some convenience methods
                        // for you to specify your component's position as
                        // relative to another one
                        .relativeToBottomOf(header))
                .build();
        final Button left = Components.button()
                .position(Positions.create(0, 1) // this means 1 row below the check box
                        .relativeToBottomOf(checkBox))
                .text("Left")
                .build();
        final Button right = Components.button()
                .position(Positions.create(1, 0) // 1 column right relative to the left BUTTON
                        .relativeToRightOf(left))
                .text("Right")
                .build();
        panel.addComponent(header);
        panel.addComponent(checkBox);
        panel.addComponent(left);
        panel.addComponent(right);
        screen.addComponent(panel);
        // we can apply color themes to a screen
        screen.applyColorTheme(ColorThemes.techLight());
        // this is how you can define interactions with a component
        left.onMouseReleased((mouseAction -> {
            screen.applyColorTheme(ColorThemes.adriftInDreams());
        }));
        right.onMouseReleased((mouseAction -> {
            screen.applyColorTheme(ColorThemes.solarizedDarkOrange());
        }));
        // in order to see the changes you need to display your screen.
        screen.display();
    }
}</source>
And the result will look like this:
[[File:https://cdn.discordapp.com/attachments/363771631727804416/363813193488924673/image.png]]
You can check out more examples [https://github.com/Hexworks/zircon/tree/master/zircon.examples/src/main here]. Here are some screenshots of them:
==== Tileset example: ====
[[File:https://cdn.discordapp.com/attachments/277739394641690625/348400285879894018/image.png]]
==== Animations: ====
[[File:https://cdn.discordapp.com/attachments/277739394641690625/360086607380086807/GIF.gif]]
==== Components: ====
[[File:https://cdn.discordapp.com/attachments/335444788167966720/361297190863241218/GIF.gif]]
== Additional features ==
There are a bunch of other things Zircon can do which are not detailed in this README but you can read about them in either the source code or the [https://github.com/Hexworks/zircon/wiki Wiki]:
=== Layering ===
Both the [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/grid/TileGrid.kt TileGrid] and the [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen] interfaces implement [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/behavior/Layerable.kt Layerable] which means that you can add [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/graphics/Layer.kt Layer]s on top of them. Every [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/behavior/Layerable.kt Layerable] can have an arbitrary amount of [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/graphics/Layer.kt Layer]s. [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/graphics/Layer.kt Layer]s are like [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/graphics/TileGraphics.kt TileGraphics]s and you can also have transparency in them which can be used to create fancy effects. For more details check the [https://github.com/Hexworks/zircon/blob/master/zircon.jvm/src/main/kotlin/org/hexworks/zircon/api/Layers.kt layers] Wiki page.
<blockquote>Note that when creating <code>Layer</code>s you can set their <code>offset</code> from the builder but after attaching it to a <code>TileGrid</code> or <code>Screen</code> you can change its position by calling <code>moveTo</code> with a new <code>Position</code>.
</blockquote>
=== Input handling ===
Both the [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/grid/TileGrid.kt TileGrid] and the [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/screen/Screen.kt Screen] interfaces implement [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/behavior/InputEmitter.kt InputEmitter] which means that they re-emit all inputs from your users (key strokes and mouse actions) and you can listen on them. There is a [https://github.com/Hexworks/zircon/wiki/Input-handling Wiki page] with more info.
=== Shape and box drawing ===
You can draw [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/shape/Shape.kt Shape]s like rectangles and triangles by using one of the [https://github.com/Hexworks/zircon/blob/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api/shape/ShapeFactory.kt ShapeFactory] implementations. Check the corresponding [https://github.com/Hexworks/zircon/wiki/Shapes Wiki page] for more info.
=== Fonts and tilesets ===
Zircon comes with a bunch of built-in tilesets. These come in 2 flavors:
* CP437 tilesets ''(More on using them [https://github.com/Hexworks/zircon/wiki/Resource-Handling#cp437-tilesets here])''
* and Graphic tilesets ''(Usage info [https://github.com/Hexworks/zircon/wiki/Resource-Handling#graphic-tilesets here])''
Read more about them in the [https://github.com/Hexworks/zircon/wiki/Resource-Handling resource handling Wiki page] if you want to know more or if you want to use your own tilesets and fonts.
Zircon also comes with '''its own tileset format (<code>ztf</code>: Zircon Tileset Format)''' which is '''very easy to use'''. Its usage is detailed [https://github.com/Hexworks/zircon/wiki/Resource-Handling#graphic-tilesets here].
=== REXPaint file loading ===
REXPaint files (<code>.xp</code>) can be loaded into Zircon <code>Layer</code>s. Read about this feature [https://github.com/Hexworks/zircon/wiki/Resource-Handling#rexpaint-files here].
=== Color themes ===
Zircon comes with a bunch of built-in color themes which you can apply to your components. If interested you can read more about how this works [https://github.com/Hexworks/zircon/wiki/Working-with-ColorThemes here].
=== Animations (BETA) ===
Animations are a beta feature. More info [https://github.com/Hexworks/zircon/wiki/Animation-support here].
=== The API ===
If you just want to peruse the Zircon API just navigate [https://github.com/Hexworks/zircon/tree/master/zircon.core/src/main/kotlin/org/hexworks/zircon/api here]. Everything which is intended to be the public API is there.
== Road map ==
If you want to see a new feature feel free to [https://github.com/Hexworks/zircon/issues/new create a new Issue] or discuss it with us on [https://discord.gg/hbzytQJ discord]. Here are some features which are either under way or planned:
* libGDX support
* Layouts for Components with resizing support
* Components for games like MapDisplay
* Multi-Font support
* Next to <code>ColorTheme</code>s we’ll introduce <code>ComponentTheme</code>s as well (custom look and feel for your components)
== License ==
Zircon is made available under the [http://www.opensource.org/licenses/mit-license.php MIT License].
== Credits ==
Zircon is created and maintained by Adam Arold, Milan Boleradszki and Gergely Lukacsy
''We’re open to suggestions, feel free to message us on [https://discord.gg/hbzytQJ Discord] or open an issue.'' ''Pull requests are also welcome!''
Zircon is powered by:
<a href="https://www.jetbrains.com/idea/"> <img src="https://github.com/Hexworks/zircon/blob/master/images/idea_logo.png" width="40" height="40" /> </a> <a href="https://kotlinlang.org/"> <img src="https://github.com/Hexworks/zircon/blob/master/images/kotlin_logo.png" width="40" height="40" /> </a> <a href="https://www.yourkit.com/java/profiler/"> <img src="https://github.com/Hexworks/zircon/blob/master/images/yklogo.png" width="168" height="40" /> </a>


[[category:Library]]
[[category:Library]]

Latest revision as of 11:13, 14 October 2018

Zircon
Library project
Developer Adam Arold
Released July 9 2017
Updated Dec 23 2017 (Stable), Mar 22 2018 (Preview)
Status Stable
Licensing MIT License
P. Language Kotlin, Java
Platforms Linux, Mac OS, Windows
Dependencies
Official site of Zircon

Zircon - A user-friendly Text GUI & Tile Engine

Important information

To read the official docs of Zircon navigate here


Getting Started

If you want to work with Zircon you can add it to your project as a dependency.

from Maven:

<dependency>
    <groupId>com.github.hexworks.zircon</groupId>
    <artifactId>zircon.jvm.swing</artifactId>
    <version>2018.3.27-PREVIEW</version>
</dependency>

or you can also use Gradle:

compile("com.github.hexworks.zircon:zircon.jvm.swing:2018.3.27-PREVIEW")

Note that you need to use Jitpack for the above dependencies to work.

Want to use a PREVIEW? Check this Wiki page

Some rules of thumb

Before we start there are some guidelines which can help you if you are stuck:

  • If you want to build something (a TileGraphics, a Component or anything which is part of the public API) it is almost sure that there is a Builder or a Factory for it. The convention is that if you want to create an TileGraphics for example, then you can use the DrawSurfaces class to do so. (so it is the plural form of the thing which you want to build). Your IDE will help you with this. These classes reside in the org.hexworks.zircon.api package.
  • If you want to work with external files like tilesets or REXPaint files check the same package (org.hexworks.zircon.api), and look for classes which end with *Resources. There are a bunch of built-in tilesets for example which you can choose from but you can also load your own. The rule of thumb is that if you need something external there is probably a *Resources class for it (like the CP437TilesetResources).
  • You can use anything you can find in the API package, they are part of the public API, and safe to use. The internal package however is considered private to Zircon so don’t depend on anything in it.
  • Some topics are explained in depth on the Wiki
  • If you want to see some example code look here.
  • If all else fails read the javadoc. API classes are well documented.
  • If you have any problems which are not answered here feel free to ask us at the Hexworks Discord server.

Creating an Application

In Zircon almost every object you might want to use has a helper class for building it. This is the same for Applications as well so let’s create one using the SwingApplications class:

Note that these examples reside in the org.hexworks.zircon.examples.docs package in the zircon.examples project, you can try them all out.

import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.application.Application;

public class CreatingAnApplication {

    public static void main(String[] args) {

        Application application = SwingApplications.startApplication();
    }
}

Running this snippet will result in this screen:

File:Https://cdn.discordapp.com/attachments/363771631727804416/477466202982055939/CreatingAnApplication.png

Not very useful, is it? So what happens here? An Application is just an object which has a Renderer for rendering Tiles on your screen), and a TileGrid, which is the main interface which you will use to interact with Zircon. An Application is responsible for continuously rendering the contents of the grid on the screen. Here we use the Swing variant, but there are other types in the making (one for LibGDX, and one which works in the browser).

Since most of the time you don’t care about the Application itself, there is a function for creating a TileGrid directly:

import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.grid.TileGrid;

public class CreatingATileGrid {

    public static void main(String[] args) {

        TileGrid tileGrid = SwingApplications.startTileGrid();
    }
}

Now let’s see how we can specify how a TileGrid is created. We’ll use the AppConfigs helper for this:

import org.hexworks.zircon.api.AppConfigs;
import org.hexworks.zircon.api.CP437TilesetResources;
import org.hexworks.zircon.api.Sizes;
import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.grid.TileGrid;

public class CreatingATileGrid {

    public static void main(String[] args) {

        TileGrid tileGrid = SwingApplications.startTileGrid(
                AppConfigs.newConfig()
                        .defaultSize(Sizes.create(10, 10))
                        .defaultTileset(CP437TilesetResources.rexPaint16x16())
                        .build());
    }
}

Adding and formatting Tiles is very simple:

import org.hexworks.zircon.api.AppConfigs;
import org.hexworks.zircon.api.CP437TilesetResources;
import org.hexworks.zircon.api.Positions;
import org.hexworks.zircon.api.Sizes;
import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.Tiles;
import org.hexworks.zircon.api.color.ANSITileColor;
import org.hexworks.zircon.api.grid.TileGrid;

public class CreatingATileGrid {

    public static void main(String[] args) {

        TileGrid tileGrid = SwingApplications.startTileGrid(
                AppConfigs.newConfig()
                        .defaultSize(Sizes.create(10, 10))
                        .defaultTileset(CP437TilesetResources.rexPaint16x16())
                        .build());

        tileGrid.setTileAt(
                Positions.create(2, 3),
                Tiles.newBuilder()
                        .backgroundColor(ANSITileColor.CYAN)
                        .foregroundColor(ANSITileColor.WHITE)
                        .character('x')
                        .build());

        tileGrid.setTileAt(
                Positions.create(3, 4),
                Tiles.newBuilder()
                        .backgroundColor(ANSITileColor.RED)
                        .foregroundColor(ANSITileColor.GREEN)
                        .character('y')
                        .build());

        tileGrid.setTileAt(
                Positions.create(4, 5),
                Tiles.newBuilder()
                        .backgroundColor(ANSITileColor.BLUE)
                        .foregroundColor(ANSITileColor.MAGENTA)
                        .character('z')
                        .build());
    }
}

Running the above code will result in something like this:

File:Https://cdn.discordapp.com/attachments/363771631727804416/477469640205926401/CreatingATileGrid.png

As you can see there is a helper for every class which you might want to use. Here we used Positions.create to create a Position, Sizes.create for creating Sizes and the TileBuilder to create tiles.

A Position denotes a coordinate on a TileGrid, so for example a Position of (3, 4) points to the 3rd column and the 4th row (x, y) on the grid.

Conversely a Size denotes an area with a width and a height. These two classes are used throughout Zircon.

A Tile is the most basic graphical unit Zircon supports. In most cases it is a simple character with a foreground and a background color (like in the example above).

In addition to colors and characters you can also use Modifiers in your Tiles.

A lot of fancy stuff can be done with Modifiers, like this:

File:Https://cdn.discordapp.com/attachments/363771631727804416/477470683513880576/modifiers.gif

If interested check out the code examples here.

Tileset (and by extension resource) handling in Zircon is very simple and if you are interested in how to load your custom fonts and other resources take a look at the Resource handling wiki page.

Working with Screens

TileGrids alone won’t suffice if you want to get any serious work done since they are rather rudimentary.

A Screen has its own buffer and it can be displayed on a TileGrid any time. This means that you can have multiple Screens at the same time representing your actual game screens. Note that only one Screen can be displayed at a given moment. displaying one deactivates the previous Screen.

Let’s create a Screen and fill it up with some stuff:

import org.hexworks.zircon.api.AppConfigs;
import org.hexworks.zircon.api.CP437TilesetResources;
import org.hexworks.zircon.api.ColorThemes;
import org.hexworks.zircon.api.Positions;
import org.hexworks.zircon.api.Screens;
import org.hexworks.zircon.api.Sizes;
import org.hexworks.zircon.api.SwingApplications;
import org.hexworks.zircon.api.DrawSurfaces;
import org.hexworks.zircon.api.Tiles;
import org.hexworks.zircon.api.component.ColorTheme;
import org.hexworks.zircon.api.graphics.TileGraphics;
import org.hexworks.zircon.api.grid.TileGrid;
import org.hexworks.zircon.api.screen.Screen;

public class CreatingAScreen {

    public static void main(String[] args) {

        TileGrid tileGrid = SwingApplications.startTileGrid(
                AppConfigs.newBuilder()
                        .defaultSize(Sizes.create(20, 8))
                        .defaultTileset(CP437TilesetResources.wanderlust16x16())
                        .build());

        final Screen screen = Screens.createScreenFor(tileGrid);

        final ColorTheme theme = ColorThemes.adriftInDreams();

        final TileGraphics image = DrawSurfaces.tileGraphicsBuilder()
                .size(tileGrid.size)
                .build()
                .fill(Tiles.newBuilder()
                        .foregroundColor(theme.getBrightForegroundColor())
                        .backgroundColor(theme.getBrightBackgroundColor())
                        .character('~')
                        .build());

        screen.draw(image, Positions.defaultPosition());

        screen.display();
    }
}

and we’ve got a nice ocean:

File:Https://cdn.discordapp.com/attachments/363771631727804416/477475680594952223/CreatingAScreen.png

What happens here is that we:

For more explanation about these jump to the How Zircon works section.

You can do so much more with Screens. If interested then check out A primer on Screens on the Wiki!

Components

Zircon supports a bunch of Components out of the box:

  • Button: A simple clickable component with corresponding event listeners
  • CheckBox: Like a BUTTON but with checked / unchecked state
  • Label: Simple component with text
  • Header: Like a label but this one has emphasis (useful when using ColorThemes)
  • Panel: A Container which can hold multiple Components
  • RadioButtonGroup and RadioButton: Like a CheckBox but only one can be selected at a time
  • TextBox: Similar to a text area in HTML this Component can be written into

These components are rather simple and you can expect them to work in a way you might be familiar with:

  • You can click on them (press and release are different events)
  • You can attach event listeners on them
  • Zircon implements focus handling so you can navigate between the components using the [Tab] key (forwards) or the [Shift]+[Tab] key stroke (backwards).
  • Components can be hovered and they can change their styling when you do so

Let’s look at an example (notes about how it works are in the comments):

import org.hexworks.zircon.api.*;
import org.hexworks.zircon.api.component.Button;
import org.hexworks.zircon.api.component.CheckBox;
import org.hexworks.zircon.api.component.Header;
import org.hexworks.zircon.api.component.Panel;
import org.hexworks.zircon.api.grid.TileGrid;
import org.hexworks.zircon.api.screen.Screen;

public class UsingComponents {

    public static void main(String[] args) {

        final TileGrid tileGrid = SwingApplications.startTileGrid(
                AppConfigs.newConfig()
                        .defaultSize(Sizes.create(34, 18))
                        .defaultTileset(CP437TilesetResources.wanderlust16x16())
                        .build());
        final Screen screen = Screens.createScreenFor(tileGrid);

        Panel panel = Components.panel()
                .wrapWithBox() // panels can be wrapped in a box
                .title("Panel") // if a panel is wrapped in a box a title can be displayed
                .wrapWithShadow() // shadow can be added
                .size(Sizes.create(32, 16)) // the size must be smaller than the parent's size
                .position(Positions.offset1x1())
                .build(); // position is always relative to the parent

        final Header header = Components.header()
                // this will be 1x1 left and down from the top left
                // corner of the panel
                .position(Positions.offset1x1())
                .text("Header")
                .build();

        final CheckBox checkBox = Components.checkBox()
                .text("Check me!")
                .position(Positions.create(0, 1)
                        // the position class has some convenience methods
                        // for you to specify your component's position as
                        // relative to another one
                        .relativeToBottomOf(header))
                .build();

        final Button left = Components.button()
                .position(Positions.create(0, 1) // this means 1 row below the check box
                        .relativeToBottomOf(checkBox))
                .text("Left")
                .build();

        final Button right = Components.button()
                .position(Positions.create(1, 0) // 1 column right relative to the left BUTTON
                        .relativeToRightOf(left))
                .text("Right")
                .build();

        panel.addComponent(header);
        panel.addComponent(checkBox);
        panel.addComponent(left);
        panel.addComponent(right);

        screen.addComponent(panel);

        // we can apply color themes to a screen
        screen.applyColorTheme(ColorThemes.techLight());

        // this is how you can define interactions with a component
        left.onMouseReleased((mouseAction -> {
            screen.applyColorTheme(ColorThemes.adriftInDreams());
        }));

        right.onMouseReleased((mouseAction -> {
            screen.applyColorTheme(ColorThemes.solarizedDarkOrange());
        }));

        // in order to see the changes you need to display your screen.
        screen.display();
    }
}

And the result will look like this:

File:Https://cdn.discordapp.com/attachments/363771631727804416/363813193488924673/image.png

You can check out more examples here. Here are some screenshots of them:

Tileset example:

File:Https://cdn.discordapp.com/attachments/277739394641690625/348400285879894018/image.png

Animations:

File:Https://cdn.discordapp.com/attachments/277739394641690625/360086607380086807/GIF.gif

Components:

File:Https://cdn.discordapp.com/attachments/335444788167966720/361297190863241218/GIF.gif

Additional features

There are a bunch of other things Zircon can do which are not detailed in this README but you can read about them in either the source code or the Wiki:

Layering

Both the TileGrid and the Screen interfaces implement Layerable which means that you can add Layers on top of them. Every Layerable can have an arbitrary amount of Layers. Layers are like TileGraphicss and you can also have transparency in them which can be used to create fancy effects. For more details check the layers Wiki page.

Note that when creating Layers you can set their offset from the builder but after attaching it to a TileGrid or Screen you can change its position by calling moveTo with a new Position.

Input handling

Both the TileGrid and the Screen interfaces implement InputEmitter which means that they re-emit all inputs from your users (key strokes and mouse actions) and you can listen on them. There is a Wiki page with more info.

Shape and box drawing

You can draw Shapes like rectangles and triangles by using one of the ShapeFactory implementations. Check the corresponding Wiki page for more info.

Fonts and tilesets

Zircon comes with a bunch of built-in tilesets. These come in 2 flavors:

  • CP437 tilesets (More on using them here)
  • and Graphic tilesets (Usage info here)

Read more about them in the resource handling Wiki page if you want to know more or if you want to use your own tilesets and fonts.

Zircon also comes with its own tileset format (ztf: Zircon Tileset Format) which is very easy to use. Its usage is detailed here.

REXPaint file loading

REXPaint files (.xp) can be loaded into Zircon Layers. Read about this feature here.

Color themes

Zircon comes with a bunch of built-in color themes which you can apply to your components. If interested you can read more about how this works here.

Animations (BETA)

Animations are a beta feature. More info here.

The API

If you just want to peruse the Zircon API just navigate here. Everything which is intended to be the public API is there.

Road map

If you want to see a new feature feel free to create a new Issue or discuss it with us on discord. Here are some features which are either under way or planned:

  • libGDX support
  • Layouts for Components with resizing support
  • Components for games like MapDisplay
  • Multi-Font support
  • Next to ColorThemes we’ll introduce ComponentThemes as well (custom look and feel for your components)

License

Zircon is made available under the MIT License.

Credits

Zircon is created and maintained by Adam Arold, Milan Boleradszki and Gergely Lukacsy

We’re open to suggestions, feel free to message us on Discord or open an issue. Pull requests are also welcome!

Zircon is powered by:

<a href="https://www.jetbrains.com/idea/"> <img src="idea_logo.png" width="40" height="40" /> </a> <a href="https://kotlinlang.org/"> <img src="kotlin_logo.png" width="40" height="40" /> </a> <a href="https://www.yourkit.com/java/profiler/"> <img src="yklogo.png" width="168" height="40" /> </a>