Skip to content
This repository was archived by the owner on Jun 9, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions data/io.github.fsobolev.Cavalier.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@
</choices>
<default>"wave"</default>
</key>
<key name="circle" type="b">
<summary>Circle</summary>
<description>Whether to modify drawing modes to draw in circle</description>
<default>false</default>
</key>
<key name="wave-inner-circle" type="b">
<summary>Wave - Show Inner Circle</summary>
<description>Whether to show the inner circle when using Wave mode Circle variant.</description>
<default>false</default>
</key>
<key name="radius" type="i">
<summary>Radius</summary>
<description>Radius of base circle (in percent)</description>
<range min="0" max="100"/>
<default>40</default>
</key>
<key name="margin" type="i">
<summary>Drawing area margin</summary>
<description>Size of gaps around drawing area (in pixels).</description>
Expand All @@ -52,8 +68,8 @@
<key name="items-roundness" type="i">
<summary>Roundness of items</summary>
<description>This setting affects "levels", "particles" and "spine" modes.</description>
<range min="0" max="50"/>
<default>10</default>
<range min="0" max="100"/>
<default>20</default>
</key>
<key name="line-thickness" type="i">
<summary>Thickness of lines</summary>
Expand Down
177 changes: 159 additions & 18 deletions src/draw_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,34 @@ def set_source(cr, height, colors, offset=0):
(red, green, blue, alpha) = colors[0]
cr.set_source_rgba(red / 255, green / 255, blue / 255, alpha)

def set_source_radial(cr, x, y, r0, r1, colors):
if len(colors) > 1:
pat = cairo.RadialGradient(x, y, r0, x, y, r1)
for i in range(len(colors)):
(red, green, blue, alpha) = colors[i]
pat.add_color_stop_rgba(1 / (i + 1), \
red / 255, green / 255, blue / 255, alpha)
cr.set_source(pat)
else:
(red, green, blue, alpha) = colors[0]
cr.set_source_rgba(red / 255, green / 255, blue / 255, alpha)

def draw_element(cr, x, y, width, height, radius):
degrees = math.pi / 180.0
width /= 2
height /= 2
cr.new_sub_path()
cr.arc(x + width * radius / 100, y + height * radius / 100, \
radius * min(width, height) / 100, -180 * degrees, -90 * degrees)
cr.arc(x + width - width * radius / 100, y + height * radius / 100, \
radius * min(width, height) / 100, -90 * degrees, 0)
cr.arc(x + width - width * radius / 100, y + height - height * radius / 100, \
radius * min(width, height) / 100, 0, 90 * degrees)
cr.arc(x + width * radius / 100, y + height - height * radius / 100, \
radius * min(width, height) / 100, 90 * degrees, -180 * degrees)
cr.arc(x + width - width * radius / 100, \
y + height - height * radius / 100, \
radius * min(width, height) / 100, 0, 0.5 * math.pi)
cr.arc(x - width + width * radius / 100, \
y + height - height * radius / 100, \
radius * min(width, height) / 100, 0.5 * math.pi, math.pi)
cr.arc(x - width + width * radius / 100, \
y - height + height * radius / 100, \
radius * min(width, height) / 100, math.pi, 1.5 * math.pi)
cr.arc(x + width - width * radius / 100, \
y - height + height * radius / 100, \
radius * min(width, height) / 100, 1.5 * math.pi, 0)
cr.close_path()

def wave(sample, cr, width, height, colors, fill, thickness):
Expand All @@ -74,6 +91,63 @@ def wave(sample, cr, width, height, colors, fill, thickness):
cr.set_line_width(thickness)
cr.stroke()

def wave_circle(sample, cr, width, height, colors, radius, fill, thickness, inner_circle):
ls = len(sample)
cr.move_to(width / 2, height / 2)
min_radius = min(width, height) * radius / 400
max_radius = min(width, height) / 2
set_source_radial(cr, width / 2, height / 2, min_radius, \
max_radius, colors)
if inner_circle:
cr.rectangle(0, 0, width, height)
cr.arc_negative(width / 2, height / 2, min_radius - thickness / 2, \
2 * math.pi, 0)
cr.clip()
cr.new_path()
cr.move_to(width / 2 + min_radius, height / 2)
cr.arc(width / 2, height / 2, min_radius, 0, 2 * math.pi)
cr.set_line_width(thickness)
cr.stroke()
min_radius += thickness
cr.move_to( \
width / 2 + math.cos(2 * math.pi / ls * (ls - 0.5) - 0.5 * math.pi) * \
(min_radius + sample[-1] * (max_radius - min_radius)), \
height / 2 + math.sin(2 * math.pi / ls * (ls - 0.5) - 0.5 * math.pi) * \
(min_radius + sample[-1] * (max_radius - min_radius))
)
cr.curve_to( \
width / 2 + math.cos(2 * math.pi / ls * (0) - 0.5 * math.pi) * \
(min_radius + sample[-1] * (max_radius - min_radius)), \
height / 2 + math.sin(2 * math.pi / ls * (0) - 0.5 * math.pi) * \
(min_radius + sample[-1] * (max_radius - min_radius)), \
width / 2 + math.cos(2 * math.pi / ls * (0) - 0.5 * math.pi) * \
(min_radius + sample[0] * (max_radius - min_radius)), \
height / 2 + math.sin(2 * math.pi / ls * (0) - 0.5 * math.pi) * \
(min_radius + sample[0] * (max_radius - min_radius)), \
width / 2 + math.cos(2 * math.pi / ls * (0.5) - 0.5 * math.pi) * \
(min_radius + sample[0] * (max_radius - min_radius)), \
height / 2 + math.sin(2 * math.pi / ls * (0.5) - 0.5 * math.pi) * \
(min_radius + sample[0] * (max_radius - min_radius))
)
for i in range(ls - 1):
cr.curve_to( \
width / 2 + math.cos(2 * math.pi / ls * (i + 1) - 0.5 * math.pi) * \
(min_radius + sample[i] * (max_radius - min_radius)), \
height / 2 + math.sin(2 * math.pi / ls * (i + 1) - 0.5 * math.pi) * \
(min_radius + sample[i] * (max_radius - min_radius)), \
width / 2 + math.cos(2 * math.pi / ls * (i + 1) - 0.5 * math.pi) * \
(min_radius + sample[i+1] * (max_radius - min_radius)), \
height / 2 + math.sin(2 * math.pi / ls * (i + 1) - 0.5 * math.pi) * \
(min_radius + sample[i+1] * (max_radius - min_radius)), \
width / 2 + math.cos(2 * math.pi / ls * (i + 1.5) - 0.5 * math.pi) * \
(min_radius + sample[i+1] * (max_radius - min_radius)), \
height / 2 + math.sin(2 * math.pi / ls * (i + 1.5) - 0.5 * math.pi) * \
(min_radius + sample[i+1] * (max_radius - min_radius))
)
cr.close_path() # required to avoid artifact with thick lines
cr.set_line_width(thickness)
cr.fill() if fill else cr.stroke()

def levels(sample, cr, width, height, colors, offset, radius, fill, thickness):
set_source(cr, height, colors)
ls = len(sample)
Expand All @@ -89,8 +163,8 @@ def levels(sample, cr, width, height, colors, offset, radius, fill, thickness):
for i in range(ls):
q = int(round(sample[i], 1) * 10)
for r in range(q):
draw_element(cr, step * i + offset_px + thickness / 2, \
height - (height / 10 * (r + 1)) + offset_px / 2 + thickness / 2, \
draw_element(cr, step * (i + 0.5), \
height - (height / 10 * (r + 0.5)), \
max(step - offset_px * 2 - thickness, 1), \
max(height / 10 - offset_px - thickness, 1), radius)
cr.fill() if fill else cr.stroke()
Expand All @@ -108,8 +182,8 @@ def particles(sample, cr, width, height, colors, offset, radius, fill, thickness
(height / 10) / 2)
cr.set_line_width(thickness)
for i in range(ls):
draw_element(cr, step * i + offset_px + thickness / 2, \
height * 0.9 - height * 0.9 * sample[i] + thickness / 2, \
draw_element(cr, step * (i + 0.5), \
height * 0.95 - (height * 0.9) * sample[i], \
max(step - offset_px * 2 - thickness, 1), \
max(height / 10 - thickness, 1), radius)
cr.fill() if fill else cr.stroke()
Expand All @@ -128,8 +202,7 @@ def spine(sample, cr, width, height, colors, offset, radius, fill, thickness):
thickness = min(thickness, \
(step * sample[i] - offset_px * 2) / 2)
cr.set_line_width(thickness)
draw_element(cr, width / 2 - sample[i] * step / 2 + offset_px + thickness / 2, \
step * i + step / 2 - sample[i] * step / 2 + offset_px + thickness / 2, \
draw_element(cr, width / 2, step * (i + 0.5), \
step * sample[i] - offset_px * 2 - thickness, \
step * sample[i] - offset_px * 2 - thickness, radius)
cr.fill() if fill else cr.stroke()
Expand All @@ -140,15 +213,14 @@ def spine(sample, cr, width, height, colors, offset, radius, fill, thickness):
offset_px = step * offset / 100 * sample[i]
if not fill:
offset_px += thickness / 2
draw_element(cr, \
step * i + step / 2 - sample[i] * step / 2 + offset_px, \
height / 2 - sample[i] * step / 2 + offset_px, \
draw_element(cr, step * (i + 0.5), height / 2,
step * sample[i] - offset_px * 2, \
step * sample[i] - offset_px * 2, radius)
cr.fill() if fill else cr.stroke()

def bars(sample, cr, width, height, colors, offset, fill, thickness):
set_source(cr, height, colors)
cr.set_line_width(thickness)
ls = len(sample)
step = width / ls
offset_px = step * offset / 100
Expand All @@ -162,3 +234,72 @@ def bars(sample, cr, width, height, colors, offset, fill, thickness):
height - height * sample[i] + thickness / 2, \
max(step - offset_px * 2 - thickness, 1), height * sample[i] - thickness)
cr.fill() if fill else cr.stroke()

def bars_circle(sample, cr, width, height, colors, offset, fill, thickness, \
radius):
ls = len(sample)
min_radius = min(width, height) * radius / 400
max_radius = min(width, height) / 2
set_source_radial(cr, width / 2, height / 2, min_radius, max_radius, colors)
if fill:
thickness = 0
thickness_inner_angle = 0
else:
thickness = min(thickness, \
min_radius * math.pi * 2 / ls * 0.25)
cr.set_line_width(thickness)
thickness_inner_angle = math.asin(math.sin(0.25 * math.pi) / \
math.sqrt(min_radius ** 2 + (thickness / 2) ** 2 - 2 * \
min_radius * thickness / 2 * math.cos(0.25 * math.pi)) * \
thickness / 2)
for i in range(ls):
cr.move_to(width / 2 + math.cos( \
2 * math.pi / ls * (i + offset / 100) - 0.5 * math.pi + \
thickness_inner_angle) * (min_radius + thickness / 2),
height / 2 + math.sin( \
2 * math.pi / ls * (i + offset / 100) - 0.5 * math.pi + \
thickness_inner_angle) * (min_radius + thickness / 2))
cr.line_to(width / 2 + math.cos( \
2 * math.pi / ls * (i + offset / 100) - 0.5 * math.pi + \
math.asin(math.sin(0.25 * math.pi) / math.sqrt((min_radius + \
max(sample[i] * (max_radius - min_radius), thickness * 2)) ** 2 + \
(thickness / 2) ** 2 - 2 * (min_radius + sample[i] * \
(max_radius - min_radius)) * thickness / 2 * \
math.cos(0.25 * math.pi)) * thickness / 2)) * \
(min_radius + max(sample[i] * \
(max_radius - min_radius), thickness * 2) - thickness / 2), \
height / 2 + math.sin( \
2 * math.pi / ls * (i + offset / 100) - 0.5 * math.pi + \
math.asin(math.sin(0.25 * math.pi) / math.sqrt((min_radius + \
max(sample[i] * (max_radius - min_radius), thickness * 2)) ** 2 + \
(thickness / 2) ** 2 - 2 * (min_radius + sample[i] * \
(max_radius - min_radius)) * thickness / 2 * \
math.cos(0.25 * math.pi)) * thickness / 2)) * \
(min_radius + max(sample[i] * \
(max_radius - min_radius), thickness * 2) - thickness / 2))
cr.line_to(width / 2 + math.cos( \
2 * math.pi / ls * (i + 1 - offset / 100) - 0.5 * math.pi - \
math.asin(math.sin(0.25 * math.pi) / math.sqrt((min_radius + \
max(sample[i] * (max_radius - min_radius), thickness * 2)) ** 2 + \
(thickness / 2) ** 2 - 2 * (min_radius + max(sample[i] * \
(max_radius - min_radius), thickness * 2)) * thickness / 2 * \
math.cos(0.25 * math.pi)) * thickness / 2)) * \
(min_radius + max(sample[i] * \
(max_radius - min_radius), thickness * 2) - thickness / 2), \
height / 2 + math.sin( \
2 * math.pi / ls * (i + 1 - offset / 100) - 0.5 * math.pi - \
math.asin(math.sin(0.25 * math.pi) / math.sqrt((min_radius + \
max(sample[i] * (max_radius - min_radius), thickness * 2)) ** 2 + \
(thickness / 2) ** 2 - 2 * (min_radius + max(sample[i] * \
(max_radius - min_radius), thickness * 2)) * thickness / 2 * \
math.cos(0.25 * math.pi)) * thickness / 2)) * \
(min_radius + max(sample[i] * \
(max_radius - min_radius), thickness * 2) - thickness / 2))
cr.line_to(width / 2 + math.cos( \
2 * math.pi / ls * (i + 1 - offset / 100) - 0.5 * math.pi - \
thickness_inner_angle) * (min_radius + thickness / 2), \
height / 2 + math.sin( \
2 * math.pi / ls * (i + 1 - offset / 100) - 0.5 * math.pi - \
thickness_inner_angle) * (min_radius + thickness / 2))
cr.close_path()
cr.fill() if fill else cr.stroke()
23 changes: 18 additions & 5 deletions src/drawing_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from gi.repository import Gtk, GObject
from threading import Thread
from cavalier.cava import Cava
from cavalier.draw_functions import wave, levels, particles, spine, bars
from cavalier.draw_functions import *
from cavalier.settings import CavalierSettings

class CavalierDrawingArea(Gtk.DrawingArea):
Expand Down Expand Up @@ -64,6 +64,9 @@ def run(self):

def on_settings_changed(self, key):
self.draw_mode = self.settings['mode']
self.circle = self.settings['circle']
self.wave_inner_circle = self.settings['wave-inner-circle']
self.radius = self.settings['radius']
self.set_margin_top(self.settings['margin'])
self.set_margin_bottom(self.settings['margin'])
self.set_margin_start(self.settings['margin'])
Expand Down Expand Up @@ -98,8 +101,13 @@ def on_settings_changed(self, key):
def draw_func(self, area, cr, width, height, data, n):
if len(self.cava_sample) > 0:
if self.draw_mode == 'wave':
wave(self.cava_sample, cr, width, height, self.colors, \
self.fill, self.thickness)
if self.circle:
wave_circle(self.cava_sample, cr, width, height, \
self.colors, self.radius, self.fill, self.thickness, \
self.wave_inner_circle)
else:
wave(self.cava_sample, cr, width, height, self.colors, \
self.fill, self.thickness)
elif self.draw_mode == 'levels':
levels(self.cava_sample, cr, width, height, self.colors, \
self.offset, self.roundness, self.fill, self.thickness)
Expand All @@ -110,8 +118,13 @@ def draw_func(self, area, cr, width, height, data, n):
spine(self.cava_sample, cr, width, height, self.colors, \
self.offset, self.roundness, self.fill, self.thickness)
elif self.draw_mode == 'bars':
bars(self.cava_sample, cr, width, height, self.colors, \
self.offset, self.fill, self.thickness)
if self.circle:
bars_circle(self.cava_sample, cr, width, height, \
self.colors, self.offset, self.fill, \
self.thickness, self.radius)
else:
bars(self.cava_sample, cr, width, height, self.colors, \
self.offset, self.fill, self.thickness)

def redraw(self):
self.queue_draw()
Expand Down
Loading