1 // Written in the D programming language.
2 /++
3  + Authors: KanzakiKino
4  + Copyright: KanzakiKino 2018
5  + License: LGPL-3.0
6 ++/
7 module w4d.parser.colorset;
8 import w4d.parser.exception,
9        w4d.style.color,
10        w4d.style.widget;
11 import gl3n.linalg;
12 import dyaml;
13 import std.uni;
14 
15 private @property WidgetState toState ( string v )
16 {
17     switch ( v.toLower ) with ( WidgetState ) {
18         case "focused" : return Focused;
19         case "tracked" : return Tracked;
20         case "pressed" : return Pressed;
21         case "hovered" : return Hovered;
22         case "selected": return Selected;
23         case "locked"  : return Locked;
24         case "disabled": return Disabled;
25         case "default" : return None;
26 
27         default:
28             throw new ParsingException( "Unknown state." );
29     }
30 }
31 
32 /// Parses Node as color.
33 vec4 parseColor ( Node seq )
34 {
35     enforce( seq.isSequence, "Color must be specified by sequence." );
36 
37     auto arr = seq.sequence!float;
38     switch ( arr.length ) {
39         case 0:
40             return vec4( 0, 0, 0, 0 );
41         case 1:
42             return vec4( arr[0], arr[0], arr[0], 1 );
43         case 3:
44             return vec4( arr[0], arr[1], arr[2], 1 );
45         case 4:
46             return vec4( arr[0], arr[1], arr[2], arr[3] );
47 
48         default:
49             throw new ParsingException( "Invalid color sequence." );
50     }
51 }
52 
53 /// Parses Node as colorset.
54 void parseColorSet ( Node block, ref ColorSet colorset )
55 {
56     foreach ( string name, Node node; block ) {
57         colorset[name] = parseColor( node );
58     }
59 }
60 
61 /// Parses Node as collection of colorset.
62 void parseColorSets ( Node root, WidgetStyle style )
63 {
64     foreach ( string name, Node node; root ) {
65         auto state = name.toState;
66         if ( state !in style.colorsets ) {
67             style.colorsets[state] = new ColorSet();
68         }
69         parseColorSet( node, style.colorsets[state] );
70     }
71 }
72 
73 /// Parses file as collection of colorset dynamically.
74 void parseColorSetsFromFile ( string path, WidgetStyle style )
75 {
76     Loader.fromFile(path).load().parseColorSets( style );
77 }
78 /// Parses file as collection of colorset statically.
79 void parseColorSetsFromFile ( string path ) ( WidgetStyle style )
80 {
81     Loader.fromString(import(path)).load().parseColorSets( style );
82 }