summaryrefslogtreecommitdiffhomepage
path: root/deploy_template/mygame/documentation/02-labels.md
blob: ea2642e240d192a62530ee9cdbae9ff3b352fce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Labels

Labels display text.

## Sample Apps Related to Label Usage (ordered by size of codebase increasing)

- 01_api_01_labels
- 01_api_99_tech_demo (includes recording)
- 10_save_load_game (includes recording)
- 18_moddable_game
- 19_lowrez_jam_01_hello_world
- 99_sample_game_return_of_serenity

## Minimum Code

Creates a label with black text at location 100, 100.

```ruby
#                         X    Y  TEXT
args.outputs.labels << [100, 100, "Hello world"]
```

## Font Size

The size can be a number between `-10` and `+10`. The default size is `0`.

```ruby
#                        X    Y   TEXT           SIZE
args.outputs.labels << [100, 100, "Hello world",    5]
```

## Alignment

Alignment values are `0` (left, default), `1` (center), and `2`
(right). The value must come after the size.

A label smack dab in the center of the screen, with a center alignment:

```ruby
#                         X    Y  TEXT           SIZE  ALIGNMENT
args.outputs.labels << [640, 360, "Hello world",    0,         1]
```

## RGBA - Colors and Alpha

Labels can have colors. The value for the color is an number between
`0` and `255`.

A green label with 50% opacity.

```ruby
#                         X    Y  TEXT           RED  GREEN  BLUE  ALPHA
args.outputs.labels << [640, 360, "Hello world",   0,   255,    0,   128]
```

A green label with size and alignment.

```ruby
#                         X    Y  TEXT           SIZE  ALIGNMENT  RED  GREEN  BLUE  ALPHA
args.outputs.labels << [640, 360, "Hello world",    0,         1,   0,   255,    0,   128]
```

## Custom Font

You can override the font for a label. The font needs to be under the
`mygame` directory. It's recommended that you create a `fonts` folder
to keep things organized.

Here is how you create a label with a font named `coolfont.ttf` under a directory `mygame/fonts`.

```ruby
#                         X    Y  TEXT           SIZE  ALIGNMENT  RED  GREEN  BLUE  ALPHA  FONT FILE
args.outputs.labels << [640, 360, "Hello world",    0,         1,   0,     0,    0,   255, "fonts/coolfont.ttf"]
```

## Hashes (Advanced)

If you want a more readable invocation. You can use the following hash to create a label.
Any parameters that are not specified will be given a default value. The keys of the hash can
be provided in any order.

Here is how you create a green label with a font named `coolfont.ttf` under a directory `mygame/fonts`
using the helper method (providing all the parameters).

```ruby
args.outputs.labels << {
  x:              200,
  y:              550,
  text:           "dragonruby",
  size_enum:      2,
  alignment_enum: 1,
  r:              155,
  g:              50,
  b:              50,
  a:              255,
  font:           "fonts/manaspc.ttf"
}
```

## Duck Typing (Advanced)

You can also create a class with line properties and render it as a primitive.
ALL properties must on the class. ADDITIONALLY, a method called
`primitive_marker` must be defined on the class.

Here is an example:

```ruby
# Create type with ALL sprite properties AND primitive_marker
class Label
  attr_accessor :x, :y, :text, :size_enum, :alignment_enum, :font, :r, :g, :b, :a

  def primitive_marker
    :label
  end
end

# Inherit from type
class TitleLabel < Label

  # constructor
  def initialize x, y, text
    self.x = x
    self.y = y
    self.text = text
  end
end

# render layer label

args.outputs.label << TitleLabel.new(10, 10, "The Game")
```