-
Notifications
You must be signed in to change notification settings - Fork 0
/
birth_rule_editor.cr
142 lines (115 loc) · 3.09 KB
/
birth_rule_editor.cr
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
133
134
135
136
137
138
139
140
141
142
# State for a birth rule editor, which consists of a single
# code buffer editor.
class BirthRuleEditorState < RuleEditorState
def min_size
1 # Code buffer
end
def max_size
1 # Code buffer
end
def code?
code = @states[0].as(RuleCodeRowState)
code.selected
end
def to_rule : Rule
code = code?.try &.string || ""
BirthRule.new(code)
end
def new_substate_for(index : Int)
RuleCodeRowState.new
end
end
# View for a birth rule editor.
class BirthRuleEditorView < RuleEditorView
include IRemixIconView
def icon
Icon::BirthRule
end
def icon_color
caption_color
end
def icon_font_size
11
end
def icon_span_x
16
end
# Returns the caption displayed above the rule.
def caption
"Born"
end
# Specifies the amount of vertical space to allocate for
# the caption text.
def caption_space_y
16
end
# Specifies the maximum amount of characters in the rule
# caption. The rest of the caption will be truncated with
# ellipsis `…`.
def caption_max_chars
12
end
# Specifies the color of the caption.
def caption_color
active? ? SF::Color.new(0xE0, 0xE0, 0xE0) : SF::Color.new(0xBD, 0xBD, 0xBD)
end
def padding
SF.vector2f(4, 4)
end
def origin
super + SF.vector2f(0, caption_space_y) + padding
end
def snapstep
SF.vector2f(12, 11)
end
def size
size = super + SF.vector2f(0, caption_space_y) + padding*2
size.max(min_size)
end
def describe(target, states)
super
#
# Draw background rectangle.
#
bgrect = SF::RectangleShape.new
bgrect.position = origin - padding
bgrect.size = size - (origin - position - padding)
bgrect.fill_color = background_color
bgrect.outline_color = outline_color
bgrect.outline_thickness = 2
bgrect.draw(target, states)
#
# Create caption and icon.
#
icon = icon_text
icon.position = position + SF.vector2f(padding.x, 0)
cap = SF::Text.new(caption.trunc(caption_max_chars), FONT_BOLD, 11)
cap.position = (icon.position + SF.vector2f(icon_span_x, 0)).to_i
cap.fill_color = caption_color
#
# Draw caption background followed by caption and icon.
#
capbg = SF::RectangleShape.new
capbg.position = icon.position - SF.vector2f(bgrect.outline_thickness, bgrect.outline_thickness) - SF.vector2f(padding.x, 0)
capbg.fill_color = bgrect.outline_color
capbg.size = SF.vector2f(bgrect.size.x, cap.size.y) + SF.vector2f(bgrect.outline_thickness*2, bgrect.outline_thickness*2)
capbg.draw(target, states)
icon.draw(target, states)
cap.draw(target, states)
end
end
# Birth rule editor allows to create and edit a birth rule.
#
# Birth rules are rules that are expressed once when the receiver
# cell is born. They are very similar to initializers in OOP.
class BirthRuleEditor < RuleEditor
include MonoBufferController(BirthRuleEditorState, BirthRuleEditorView)
include BufferEditorHandler
include BufferEditorRowHandler
include BufferEditorColumnHandler
include CellEditorEntity
include RuleEditorHandler
def to_rule : Rule
@state.to_rule
end
end