The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

ZoomBar.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

Revisions

View the latest version of libbirdfont/ZoomBar.vala.
New color theme
1 /* 2 Copyright (C) 2014 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 ZoomBar : Tool { 21 22 double zoom_level = 1 / 3.0; 23 bool update_zoom = false; 24 25 public signal void new_zoom (double zoom_level); 26 27 double margin_percent = 0.05; 28 29 public ZoomBar () { 30 base (); 31 32 panel_press_action.connect ((selected, button, tx, ty) => { 33 if (y <= ty <= y + h) { 34 set_zoom_from_mouse (tx); 35 update_zoom = true; 36 } 37 }); 38 39 panel_move_action.connect ((selected, button, tx, ty) => { 40 if (update_zoom) { 41 set_zoom_from_mouse (tx); 42 } 43 44 return true; 45 }); 46 47 panel_release_action.connect ((selected, button, tx, ty) => { 48 update_zoom = false; 49 }); 50 } 51 52 /** Zoom level from 0 to 1. */ 53 public void set_zoom (double z) { 54 zoom_level = z; 55 56 if (!MenuTab.background_thread) { 57 Toolbox.redraw_tool_box (); 58 } 59 } 60 61 void set_zoom_from_mouse (double tx) { 62 double margin = w * margin_percent; 63 double bar_width = w - margin - x; 64 65 tx -= x; 66 zoom_level = tx / bar_width; 67 68 if (zoom_level > 1) { 69 zoom_level = 1; 70 } 71 72 if (zoom_level < 0) { 73 zoom_level = 0; 74 } 75 76 set_zoom (zoom_level); 77 78 if (!MenuTab.suppress_event) { 79 new_zoom (zoom_level); 80 } 81 82 FontDisplay.dirty_scrollbar = true; 83 } 84 85 public override void draw (Context cr) { 86 double margin = w * margin_percent; 87 double bar_width = w - margin - x; 88 89 // filled 90 cr.save (); 91 Theme.color (cr, "Button Border 1"); 92 draw_bar (cr); 93 cr.fill (); 94 cr.restore (); 95 96 // remove non filled parts 97 cr.save (); 98 Theme.color (cr, "Default Background"); 99 cr.rectangle (x + bar_width * zoom_level, y, w, h); 100 cr.fill (); 101 cr.restore (); 102 103 // border 104 cr.save (); 105 Theme.color (cr, "Button Border 1"); 106 cr.set_line_width (0.8); 107 draw_bar (cr); 108 cr.stroke (); 109 cr.restore (); 110 } 111 112 void draw_bar (Context cr) { 113 double height = h; 114 double radius = height / 2; 115 double margin = w * margin_percent; 116 117 cr.move_to (x + radius, y + height); 118 cr.arc (x + radius, y + radius, radius, PI / 2, 3 * (PI / 2)); 119 cr.line_to (w - margin - radius, y); 120 cr.arc (w - margin - radius, y + radius, radius, 3 * (PI / 2), 5 * (PI / 2)); 121 cr.line_to (x + radius, y + height); 122 cr.close_path (); 123 } 124 } 125 126 } 127