love: add text shape, line-width modifier
s-ol
1 year, 13 days ago
0 | import Constant, Op, PureOp, Input, T, Array, any from require 'alv.base' | |
0 | import Constant, Op, PureOp, Input, Error, T, Array, any from require 'alv.base' | |
1 | 1 | |
2 | 2 | unpack or= table.unpack |
3 | 3 | |
92 | 92 | @out\set -> |
93 | 93 | love.graphics.rectangle mode, x, y, w, h |
94 | 94 | |
95 | text = Constant.meta | |
96 | meta: | |
97 | name: 'text' | |
98 | summary: "create a text shape." | |
99 | examples: { '(love/text str [align] [font])' } | |
100 | description: " | |
101 | Create a shape that draws the text `str` with font `font` (or the default font). | |
102 | `align` should be on of the following strings: | |
103 | - `center` (the default) | |
104 | - `left` | |
105 | - `right`" | |
106 | ||
107 | ||
108 | value: class extends PureOp | |
109 | pattern: any.str + -any.str + -any['love/font'] | |
110 | type: T['love/shape'] | |
111 | ||
112 | tick: => | |
113 | { text, align, font } = @unwrap_all! | |
114 | ||
115 | wm = switch align or 'center' | |
116 | when 'left' then 0 | |
117 | when 'center' then -0.5 | |
118 | when 'right' then -1 | |
119 | else | |
120 | error Error 'argument', "unknown text alignment '#{align}'" | |
121 | ||
122 | @out\set -> | |
123 | font or= love.graphics.getFont! | |
124 | width = font\getWidth text | |
125 | height = font\getHeight! | |
126 | love.graphics.print text, font, width*wm, -height/2 | |
127 | ||
95 | 128 | color = Constant.meta |
96 | 129 | meta: |
97 | 130 | name: 'color' |
109 | 142 | @out\set -> |
110 | 143 | love.graphics.setColor r, g, b, a |
111 | 144 | shape! |
112 | love.graphics.setColor 255, 255, 255 | |
145 | love.graphics.setColor 1, 1, 1 | |
146 | ||
147 | line_width = Constant.meta | |
148 | meta: | |
149 | name: 'line-width' | |
150 | summary: "set line-width of a shape." | |
151 | examples: { '(love/line-width width shape)' } | |
152 | ||
153 | value: class extends PureOp | |
154 | pattern: any.num + any['love/shape'] | |
155 | type: T['love/shape'] | |
156 | ||
157 | tick: => | |
158 | { width, shape } = @unwrap_all! | |
159 | ||
160 | @out\set -> | |
161 | love.graphics.setLineWidth width | |
162 | shape! | |
163 | love.graphics.setLineWidth 1 | |
113 | 164 | |
114 | 165 | translate = Constant.meta |
115 | 166 | meta: |
252 | 303 | :draw |
253 | 304 | |
254 | 305 | 'no-shape': no_shape |
255 | :circle, :ellipse, :line, :rectangle | |
306 | :circle, :ellipse, :line, :rectangle, :text | |
256 | 307 | |
257 | 308 | :translate, :rotate, :scale, :shear |
258 | :color | |
309 | :color, 'line-width': line_width | |
259 | 310 | |
260 | 311 | 'mouse-pos': mouse_pos |