4
4
#
5
5
6
6
defmodule Scenic.Primitive do
7
- @ moduledoc false
7
+ @ moduledoc """
8
+ Please see [`Primitives Overview`](overview_primitives.html) for a high-level description.
9
+
10
+ ## What is a primitive
11
+
12
+ A primitive is the simplest thing Scenic knows how to draw on the screen. There is is only
13
+ a small, fixed set of them, but they can be combined in a graph to draw very complicated
14
+ images.
15
+
16
+ In general, each Primitive type has a piece of data that it expects to operate on. For
17
+ example, Primitive.Text requires a bitstring. Primitive.Circle requires a radius. Please
18
+ see the documentation for each primitive for details.
19
+
20
+ ## How to use primitives
21
+
22
+ By far, the easiest way to use primitives is to import the helper functions in
23
+ [`Scenic.Primitives`](Scenic.Primitives.html). These helpers can both add primitives to
24
+ a scene you are building and modify later as you react to events.
25
+
26
+ Once you get a primitive out of a graph via functions such as `Graph.modify`, or `Graph.get`,
27
+ You can use the generic helpers in this module to access or manipulate them.
28
+ """
29
+
8
30
9
31
alias Scenic.Graph
10
32
alias Scenic.Primitive
@@ -149,6 +171,21 @@ defmodule Scenic.Primitive do
149
171
# build a new primitive
150
172
# in general, default the various lists and the assign map to nil to save space
151
173
# assume most elements do not hvae these items set.
174
+
175
+ @ doc """
176
+ Generic builder to create a new primitive.
177
+
178
+ This function is used internally. You should almost always use the helpers in
179
+ [Scenic.Primitives](Scenic.Primitives.html) instead.
180
+
181
+ Parameters:
182
+ `module` - The module of the primitive you are building
183
+ `data` - the primitive-specific data being set into the primitive
184
+ `opts` - a list of style and transform options to apply to the primitive
185
+
186
+ Returns the built primitive.
187
+ """
188
+
152
189
@ spec build ( module :: atom , data :: any , opts :: keyword ) :: Primitive . t ( )
153
190
def build ( module , data , opts \\ [ ] ) do
154
191
# first build the map with the non-optional fields
@@ -256,22 +293,32 @@ defmodule Scenic.Primitive do
256
293
257
294
# ============================================================================
258
295
# styles
259
- # I'm allowing the styles to not be present on the primitive, which is why
260
- # I'm not parsing it out in the function match
261
296
297
+ @ doc """
298
+ Get the styles map from a primitive.
299
+
300
+ Parameters:
301
+ `primitive` - The primitive
302
+
303
+ Returns the a map of styles set directly onto this primitive. This does
304
+ not include any inherited styles.
305
+ """
262
306
@ spec get_styles ( primitive :: Primitive . t ( ) ) :: map
263
307
def get_styles ( primitive )
264
308
265
309
def get_styles ( % Primitive { } = p ) do
266
310
Map . get ( p , :styles , % { } )
267
311
end
268
312
269
- # def get_primitive_styles( primitive )
270
- # def get_primitive_styles( %Primitive{} = p ) do
271
- # Map.get(p, :styles, %{})
272
- # |> Style.primitives()
273
- # end
313
+ @ doc """
314
+ Update the styles map in a primitive.
315
+
316
+ Parameters:
317
+ `primitive` - The primitive
318
+ `styles` - The new styles map
274
319
320
+ Returns the primitive with the updated styles.
321
+ """
275
322
@ spec put_styles ( primitive :: Primitive . t ( ) , styles :: map ) :: Primitive . t ( )
276
323
def put_styles ( primitve , styles )
277
324
def put_styles ( % Primitive { } = p , nil ) , do: Map . delete ( p , :styles )
@@ -284,13 +331,33 @@ defmodule Scenic.Primitive do
284
331
@ spec get_style ( primitive :: Primitive . t ( ) , type :: atom , default :: any ) :: any
285
332
def get_style ( primitive , type , default \\ nil )
286
333
334
+ @ doc """
335
+ Get the value of a specific style set on the primitive.
336
+
337
+ If the style is not set, it returns default
338
+
339
+ Parameters:
340
+ `primitive` - The primitive
341
+ `type` - atom representing the style to get.
342
+ `default` - default value to return if the style is not set.
343
+
344
+ Returns the value of the style.
345
+ """
287
346
def get_style ( % Primitive { } = p , type , default ) when is_atom ( type ) do
288
347
Map . get ( p , :styles , % { } )
289
348
|> Map . get ( type , default )
290
349
end
291
350
292
- # the public facing put_style gives the primitive module a chance to filter the styles
293
- # do_put_style does the actual work
351
+ @ doc """
352
+ Update the value of a specific style set on the primitive.
353
+
354
+ Parameters:
355
+ `primitive` - The primitive
356
+ `type` - atom representing the style to get.
357
+ `data` - the value to set on the style.
358
+
359
+ Returns the updated primitive.
360
+ """
294
361
@ spec put_style ( primitive :: Primitive . t ( ) , type :: atom , data :: any ) :: Primitive . t ( )
295
362
def put_style ( % Primitive { } = p , type , nil ) when is_atom ( type ) do
296
363
Map . get ( p , :styles , % { } )
@@ -310,6 +377,17 @@ defmodule Scenic.Primitive do
310
377
end )
311
378
end
312
379
380
+ @ doc """
381
+ Deletes a specified style from a primitive.
382
+
383
+ Does nothing if the style is not set.
384
+
385
+ Parameters:
386
+ `primitive` - The primitive
387
+ `type` - atom representing the style to delete.
388
+
389
+ Returns the updated primitive.
390
+ """
313
391
@ spec delete_style ( primitive :: Primitive . t ( ) , type :: atom ) :: Primitive . t ( )
314
392
def delete_style ( primitive , type )
315
393
@@ -321,16 +399,32 @@ defmodule Scenic.Primitive do
321
399
322
400
# ============================================================================
323
401
# transforms
324
- # I'm allowing the transforms to not be present on the primitive, which is why
325
- # I'm not parsing it out in the function match
326
402
403
+ @ doc """
404
+ Get the transforms map from a primitive.
405
+
406
+ Parameters:
407
+ `primitive` - The primitive
408
+
409
+ Returns the a map of transforms set directly onto this primitive. This does
410
+ not include any inherited transforms.
411
+ """
327
412
@ spec get_transforms ( primitive :: Primitive . t ( ) ) :: map
328
413
def get_transforms ( primitive )
329
414
330
415
def get_transforms ( % Primitive { } = p ) do
331
416
Map . get ( p , :transforms , % { } )
332
417
end
333
418
419
+ @ doc """
420
+ Update the transforms map in a primitive.
421
+
422
+ Parameters:
423
+ `primitive` - The primitive
424
+ `transforms` - The new transforms map
425
+
426
+ Returns the primitive with the updated transforms.
427
+ """
334
428
@ spec put_transforms ( primitive :: Primitive . t ( ) , transforms :: map ) :: Primitive . t ( )
335
429
def put_transforms ( primitve , transforms )
336
430
def put_transforms ( % Primitive { } = p , nil ) , do: Map . delete ( p , :transforms )
@@ -340,16 +434,37 @@ defmodule Scenic.Primitive do
340
434
Map . put ( p , :transforms , txs )
341
435
end
342
436
437
+
343
438
@ spec get_transform ( primitive :: Primitive . t ( ) , type :: atom , default :: any ) :: any
344
439
def get_transform ( primitive , tx_type , default \\ nil )
345
440
441
+ @ doc """
442
+ Get the value of a specific transform set on the primitive.
443
+
444
+ If the transform is not set, it returns default
445
+
446
+ Parameters:
447
+ `primitive` - The primitive
448
+ `type` - atom representing the transform to get.
449
+ `default` - default value to return if the transform is not set.
450
+
451
+ Returns the value of the transform.
452
+ """
346
453
def get_transform ( % Primitive { } = p , tx_type , default ) when is_atom ( tx_type ) do
347
454
Map . get ( p , :transforms , % { } )
348
455
|> Map . get ( tx_type , default )
349
456
end
350
457
351
- # the public facing put_style gives the primitive module a chance to filter the styles
352
- # do_put_style does the actual work
458
+ @ doc """
459
+ Update the value of a specific transform set on the primitive.
460
+
461
+ Parameters:
462
+ `primitive` - The primitive
463
+ `type` - atom representing the transform to get.
464
+ `data` - the value to set on the transform.
465
+
466
+ Returns the updated primitive.
467
+ """
353
468
@ spec put_transform ( primitive :: Primitive . t ( ) , type :: atom , transform :: any ) :: Primitive . t ( )
354
469
def put_transform ( % Primitive { } = p , tx_type , nil ) when is_atom ( tx_type ) do
355
470
Map . get ( p , :transforms , % { } )
@@ -369,6 +484,18 @@ defmodule Scenic.Primitive do
369
484
end )
370
485
end
371
486
487
+
488
+ @ doc """
489
+ Deletes a specified transform from a primitive.
490
+
491
+ Does nothing if the transform is not set.
492
+
493
+ Parameters:
494
+ `primitive` - The primitive
495
+ `type` - atom representing the transform to delete.
496
+
497
+ Returns the updated primitive.
498
+ """
372
499
@ spec delete_transform ( primitive :: Primitive . t ( ) , type :: atom ) :: Primitive . t ( )
373
500
def delete_transform ( primitive , tx_type )
374
501
@@ -380,6 +507,15 @@ defmodule Scenic.Primitive do
380
507
381
508
# ============================================================================
382
509
# primitive-specific data
510
+
511
+ @ doc """
512
+ Get the value of the primitive-specific data.
513
+
514
+ Parameters:
515
+ `primitive` - The primitive
516
+
517
+ Returns the value of the primitive-specific data.
518
+ """
383
519
@ spec get ( primitive :: Primitive . t ( ) ) :: any
384
520
def get ( primitive )
385
521
@@ -388,13 +524,47 @@ defmodule Scenic.Primitive do
388
524
mod . get ( p )
389
525
end
390
526
527
+ @ deprecated "Use Primitive.merge_opts instead."
391
528
@ spec put_opts ( primitive :: Primitive . t ( ) , opts :: keyword ) :: Primitive . t ( )
392
529
def put_opts ( primitive , opts )
393
530
394
531
def put_opts ( % Primitive { } = p , opts ) when is_list ( opts ) do
532
+ raise "Primitive.put_opts has been deprecated. Use Primitive.merge_opts instead."
533
+ end
534
+
535
+
536
+ @ doc """
537
+ Merge an options-list of styles and transforms onto a primitive.
538
+
539
+ This function might go through a name-change in the future. It is really
540
+ more of a merge. The supplied list of styles and transforms
541
+
542
+ Parameters:
543
+ `primitive` - The primitive
544
+
545
+ Returns the value of the primitive-specific data.
546
+ """
547
+ @ spec merge_opts ( primitive :: Primitive . t ( ) , opts :: keyword ) :: Primitive . t ( )
548
+ def merge_opts ( primitive , opts )
549
+
550
+ def merge_opts ( % Primitive { } = p , opts ) when is_list ( opts ) do
395
551
apply_options ( p , opts )
396
552
end
397
553
554
+ @ doc """
555
+ Put primitive-specific data onto the primitive.
556
+
557
+ Like many of the functions in the Scenic.Primitive module, you are usually better
558
+ off using the helper functions in [`Scenic.Primitives`](Scenic.Primitives.html) instead.
559
+
560
+ Parameters:
561
+ `primitive` - The primitive
562
+ `data` - The data to set
563
+ `opts` - A list of style/transform options to merge
564
+
565
+ Returns the updated primitive.
566
+ """
567
+
398
568
@ spec put ( primitive :: Primitive . t ( ) , data :: any , opts :: list ) :: Primitive . t ( )
399
569
def put ( primitive , data , opts \\ [ ] )
400
570
0 commit comments