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 "default" : return None; 24 25 default: 26 throw new ParsingException( "Unknown state." ); 27 } 28 } 29 30 /// Parses Node as color. 31 vec4 parseColor ( Node seq ) 32 { 33 enforce( seq.isSequence, "Color must be specified by sequence." ); 34 35 auto arr = seq.sequence!float; 36 switch ( arr.length ) { 37 case 0: 38 return vec4( 0, 0, 0, 0 ); 39 case 1: 40 return vec4( arr[0], arr[0], arr[0], 1 ); 41 case 3: 42 return vec4( arr[0], arr[1], arr[2], 1 ); 43 case 4: 44 return vec4( arr[0], arr[1], arr[2], arr[3] ); 45 46 default: 47 throw new ParsingException( "Invalid color sequence." ); 48 } 49 } 50 51 /// Parses Node as colorset. 52 void parseColorSet ( Node block, ref ColorSet colorset ) 53 { 54 foreach ( string name, Node node; block ) { 55 colorset[name] = parseColor( node ); 56 } 57 } 58 59 /// Parses Node as collection of colorset. 60 void parseColorSets ( Node root, WidgetStyle style ) 61 { 62 foreach ( string name, Node node; root ) { 63 auto state = name.toState; 64 if ( state !in style.colorsets ) { 65 style.colorsets[state] = new ColorSet(); 66 } 67 parseColorSet( node, style.colorsets[state] ); 68 } 69 } 70 71 /// Parses file as collection of colorset dynamically. 72 void parseColorSetsFromFile ( string path, WidgetStyle style ) 73 { 74 Loader.fromFile(path).load().parseColorSets( style ); 75 } 76 /// Parses file as collection of colorset statically. 77 void parseColorSetsFromFile ( string path ) ( WidgetStyle style ) 78 { 79 Loader.fromString(import(path)).load().parseColorSets( style ); 80 }