The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

GlyphMaster.vala in /libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
Circle boundaries heads/master
1 /* 2 Copyright (C) 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 using Math; 17 18 namespace BirdFont { 19 20 public class GlyphMaster : GLib.Object { 21 public Gee.ArrayList<Glyph> glyphs = new Gee.ArrayList<Glyph> (); 22 public int selected = 0; 23 24 public string id = "Master 1"; 25 26 public GlyphMaster () { 27 } 28 29 public GlyphMaster.for_id (string id) { 30 this.id = id; 31 } 32 33 public string get_id () { 34 return id; 35 } 36 37 public void remove (int index) { 38 return_if_fail (0 <= index < glyphs.size); 39 40 if (selected >= index) { 41 selected--; 42 } 43 44 glyphs.remove_at (index); 45 } 46 47 public void set_selected (Glyph g) { 48 int i = 0; 49 50 foreach (Glyph gl in glyphs) { 51 if (gl == g) { 52 selected = i; 53 return; 54 } 55 i++; 56 } 57 58 selected = 0; 59 warning ("Glyph is not a part of the collection."); 60 } 61 62 public void add_glyph (Glyph g) { 63 glyphs.add (g); 64 } 65 66 public Glyph? get_current () { 67 if (likely (0 <= selected < glyphs.size)) { 68 return glyphs.get (selected); 69 } 70 71 warning (@"No glyph $selected glyphs.size: $(glyphs.size)"); 72 73 return null; 74 } 75 76 public void insert_glyph (Glyph g, bool selected_glyph) { 77 glyphs.add (g); 78 79 if (selected_glyph) { 80 selected = glyphs.size - 1; 81 } 82 } 83 84 public void set_selected_version (int version_id) { 85 int i = 0; 86 foreach (Glyph g in glyphs) { 87 if (g.version_id == version_id) { 88 selected = i; 89 break; 90 } 91 i++; 92 } 93 } 94 95 public int get_last_id () { 96 return_val_if_fail (glyphs.size > 0, 0); 97 return glyphs.get (glyphs.size - 1).version_id; 98 } 99 100 public GlyphMaster copy_deep () { 101 GlyphMaster n = new GlyphMaster (); 102 103 foreach (Glyph g in glyphs) { 104 n.glyphs.add (g.copy ()); 105 } 106 107 n.selected = selected; 108 n.id = id; 109 110 return n; 111 } 112 113 public GlyphMaster copy () { 114 GlyphMaster n = new GlyphMaster (); 115 116 foreach (Glyph g in glyphs) { 117 n.glyphs.add (g); 118 } 119 120 n.selected = selected; 121 n.id = id; 122 123 return n; 124 } 125 } 126 127 } 128 129