def draw_measure_number(self, gp_measure: guitarpro.models.Measure): num = gp_measure.header.number num_glyph = CoreLabel(text=str(num), font_size=14, font_name='./fonts/Arial') num_glyph.refresh() num_x = self.barline_x - num_glyph.width num_y = self.y + self.step_y * 8 num_instr = Rectangle(pos=(num_x, num_y), size=num_glyph.texture.size) num_instr.texture = num_glyph.texture self.glyphs.add(num_instr)
def draw_timesig(self, gp_measure: guitarpro.models.Measure): num, den = gp_measure.timeSignature.numerator, gp_measure.timeSignature.denominator.value num_glyph = CoreLabel(text=str(num), font_size=50, font_name='./fonts/PatuaOne-Regular') den_glyph = CoreLabel(text=str(den), font_size=50, font_name='./fonts/PatuaOne-Regular') num_glyph.refresh() den_glyph.refresh() num_x = (self.barline_x / 2) - (num_glyph.texture.width / 2) den_x = (self.barline_x / 2) - (den_glyph.texture.width / 2) num_instr = Rectangle(pos=(num_x, self.y + self.step_y * 5), size=(num_glyph.texture.size)) den_instr = Rectangle(pos=(den_x, self.y + self.step_y * 3), size=(den_glyph.texture.size)) num_instr.texture = num_glyph.texture den_instr.texture = den_glyph.texture self.glyphs.add(num_instr) self.glyphs.add(den_instr)
def draw_measure_count(self): # TODO: Once copy/delete buttons are added, update measure counts. measure_count = str(self.idx + 1) + ' / ' + str(self.total_measures) count_glyph = CoreLabel(text=measure_count, font_size=14, font_name='./fonts/Arial') count_glyph.refresh() count_x = self.right - (count_glyph.width + 5) count_y = self.y count_instr = Rectangle(pos=(count_x, count_y), size=count_glyph.size) count_instr.texture = count_glyph.texture self.glyphs.add(count_instr)
def draw_flags(self, gp_beat: guitarpro.models.Beat, xpos: float): # Texture created by mscore-20 font is unnecessarily tall, hard to place exactly. flags = { 1: '', 2: '', 4: '', 8: u'\uE194', 16: u'\uE197', 32: u'\uE198' } flag = flags[gp_beat.duration.value] flag_glyph = CoreLabel(text=flag, font_size=32, font_name='./fonts/mscore-20') flag_glyph.refresh() flag_instr = Rectangle(pos=(xpos, self.y - self.step_y * 1.5), size=flag_glyph.texture.size) flag_instr.texture = flag_glyph.texture self.glyphs.add(flag_instr)
def draw_rest(self, gp_beat: guitarpro.models.Beat, xpos: float): # The only font I can find for rests doesn't follow the unicode standard. step_y = self.step_y unicode_rests = { 1: u'\u1D13B', 2: u'\u1D13C', 4: u'\u1D13D', 8: u'\u1D13E', 16: u'\u1D13F' } rests = { 1: u'\uE102', 2: u'\uE103', 4: u'\uE107', 8: u'\uE109', 16: u'\uE10A', 32: u'\uE10B' } rest_ys = { 1: step_y * 4, 2: step_y * 3, 4: step_y * 2.5, 8: step_y * 3, 16: step_y * 3, 32: step_y * 2.5 } rest, rest_y = rests[gp_beat.duration.value], rest_ys[ gp_beat.duration.value] rest_glyph = CoreLabel(text=rest, font_size=32, font_name='./fonts/mscore-20') rest_glyph.refresh() rest_instr = Rectangle(pos=(xpos, self.y + rest_y), size=rest_glyph.texture.size) rest_instr.texture = rest_glyph.texture background = Rectangle(pos=(xpos, self.y + rest_y), size=rest_glyph.texture.size) # self.backgrounds.add(background) # Looks bad because of mscore-20 font. self.glyphs.add(rest_instr) if gp_beat.duration.isDotted or gp_beat.duration.isDoubleDotted: ypos = rest_y + rest_glyph.texture.height * 0.75 self.draw_dots(gp_beat, xpos, ypos)
def draw_tuplet(self, gp_beat: guitarpro.models.Beat, xmids: List[float]): xmin, xmax = xmids[0], xmids[-1] for xpos in xmids: self.draw_stem(gp_beat, xpos) if gp_beat.duration.value == 8: self.draw_eightnote_beam(xmin, xmax) if gp_beat.duration.value == 16: self.draw_eightnote_beam(xmin, xmax) self.draw_sixteenthnote_beam(xmin, xmax) xmid = (xmin + xmax) / 2 text_glyph = CoreLabel(text=str(gp_beat.duration.tuplet.enters), font_size=14, font_name='./fonts/Arial') text_glyph.refresh() text_instr = Rectangle(pos=(xmid - text_glyph.texture.width / 2, self.y + self.step_y * 0.5), size=text_glyph.texture.size) text_instr.texture = text_glyph.texture self.glyphs.add(text_instr)
def draw_fretnum(self, gp_note: guitarpro.models.Note, xpos: float): fret_text = str(gp_note.value) if gp_note.type.name == 'tie': fret_text = ' ' if gp_note.type.name == 'dead': fret_text = 'X' if gp_note.effect.isHarmonic: fret_text = '<' + fret_text + '>' fret_num_glyph = CoreLabel(text=fret_text, font_size=16, font_name='./fonts/Arial', bold=True) fret_num_glyph.refresh() ypos = self.y + self.step_y * ( 8 - (gp_note.string - 1)) - fret_num_glyph.height / 2 background = Rectangle(pos=(xpos, ypos), size=fret_num_glyph.texture.size) fret_num_instr = Rectangle(pos=(xpos, ypos), size=fret_num_glyph.texture.size) fret_num_instr.texture = fret_num_glyph.texture self.backgrounds.add(background) self.glyphs.add(fret_num_instr) xmid = xpos + (fret_num_glyph.texture.width / 2) return xmid
def __init__(self, callBack): super(HomeScreen, self).__init__() w = Window.width//3 buttonSize = (w, w//3) buttonAnchor = (3*Window.width//4, Window.height//4) titleString = "ChordShed" startButton = BButton(pos=buttonAnchor, size=buttonSize, filePath="../images/start-game.png", callback=callBack) statsButton = BButton(pos=(buttonAnchor[0], buttonAnchor[1] - buttonSize[1]), size=buttonSize, filePath="../images/statistics.png", callback=None) title = Rectangle(pos=(150, Window.height//2), size=(3*Window.width//4, Window.height//4)) label = CoreLabel(text=titleString, font_size=56) label.refresh() text = label.texture title.texture = text # self.button = Button(filePath="../images/start-game.png", callback=None) self.buttons = [startButton, statsButton] self.objects = AnimGroup() [self.objects.add(button) for button in self.buttons] self.canvas.add(Rectangle(size=(Window.width,Window.height))) self.canvas.add(self.objects) self.canvas.add(Color(*(.05,.28,.1))) self.canvas.add(title) # self.canvas.add(Ellipse(pos=(50,50), size=(50,50))) print('homescreen buttons initialized')
def _show_mea_outline(self, config, transform_matrix=None, color=(1, 215 / 255, 0, .2)): from kivy.graphics import (Line, StencilPush, StencilUse, StencilUnUse, StencilPop, Rectangle, Color) from kivy.graphics.context_instructions import (PushMatrix, PopMatrix, Rotate, Translate, Scale, MatrixInstruction, BindTexture) from kivy.graphics.transformation import Matrix from kivy.base import EventLoop EventLoop.ensure_window() from kivy.core.text import Label from kivy.metrics import dp, sp size = config['orig_size'] pos = config['pos'] canvas = config['canvas'] mea_w = max(self.view_controller.mea_num_cols - 1, 0) * \ self.view_controller.mea_pitch mea_h = max(self.view_controller.mea_num_rows - 1, 0) * \ self.view_controller.mea_pitch last_col = "ABCDEFGHJKLMNOPQRSTUVWXYZ"[ self.view_controller.mea_num_cols - 1] with canvas: StencilPush() Rectangle(pos=pos, size=size) StencilUse() PushMatrix() if transform_matrix is not None: mat = Matrix() mat.set(array=transform_matrix) m = MatrixInstruction() m.matrix = mat Color(*color) Line(points=[0, 0, mea_w, 0, mea_w, mea_h, 0, mea_h], close=True) label = Label(text='A1', font_size=sp(12)) label.refresh() _w, _h = label.texture.size rect = Rectangle(pos=(mea_w, mea_h - _h / 2.), size=label.texture.size) rect.texture = label.texture label = Label(text='A{}'.format(self.view_controller.mea_num_rows), font_size=sp(12)) label.refresh() _w, _h = label.texture.size rect = Rectangle(pos=(-_w, mea_h - _h / 2.), size=label.texture.size) rect.texture = label.texture label = Label(text='{}1'.format(last_col), font_size=sp(12)) label.refresh() _w, _h = label.texture.size rect = Rectangle(pos=(mea_w, -_h / 2.), size=label.texture.size) rect.texture = label.texture label = Label(text='{}{}'.format( last_col, self.view_controller.mea_num_rows), font_size=sp(12)) label.refresh() _w, _h = label.texture.size rect = Rectangle(pos=(-_w, -_h / 2.), size=label.texture.size) rect.texture = label.texture PopMatrix() StencilUnUse() Rectangle(pos=pos, size=size) StencilPop()
def _paint_electrodes_data_setup(self, config, electrode_names, spacing=2, draw_size=(0, 0), draw_size_hint=(1, 1), draw_pos=(0, 0), draw_pos_hint=(None, None), volt_scale=1e-6, time_axis_s=1, volt_axis=100, transform_matrix=None, label_width=70): from kivy.graphics import (Mesh, StencilPush, StencilUse, StencilUnUse, StencilPop, Rectangle, Color) from kivy.graphics.context_instructions import (PushMatrix, PopMatrix, Scale, MatrixInstruction) from kivy.graphics.transformation import Matrix from kivy.base import EventLoop EventLoop.ensure_window() from kivy.core.text import Label from kivy.metrics import dp, sp n_rows = len(electrode_names) if not n_rows: raise ValueError("There must be at least one electrode specified") n_cols = len(electrode_names[0]) if not n_cols: raise ValueError("There must be at least one electrode specified") if not all((len(row) == n_cols for row in electrode_names)): raise ValueError( "The number of electrodes in all rows must be the same") n_electrodes = sum(map(len, electrode_names)) orig_w, orig_h = config['orig_size'] fbo = config['canvas'] label_height = 45 if label_width else 0 draw_w, draw_h = draw_size draw_hint_w, draw_hint_h = draw_size_hint w = int(draw_w if draw_hint_w is None else orig_w * draw_hint_w) h = int(draw_h if draw_hint_h is None else orig_h * draw_hint_h) draw_x, draw_y = draw_pos draw_hint_x, draw_hint_y = draw_pos_hint x = int(draw_x if draw_hint_x is None else orig_w * draw_hint_x) y = int(draw_y if draw_hint_y is None else orig_h * draw_hint_y) ew = int((w - label_width - max(0, n_cols - 1) * spacing) / n_cols) eh = int((h - label_height - max(0, n_rows - 1) * spacing) / n_rows) with fbo: PushMatrix() # center = x + w / 2., y + h / 2. # if scale: # Scale(scale, scale, 1, origin=center) if transform_matrix is not None: mat = Matrix() mat.set(array=transform_matrix) m = MatrixInstruction() m.matrix = mat positions = [ (0, 0), ] * n_electrodes graphics = [ None, ] * n_electrodes i = 0 electrode_color = 1, 215 / 255, 0, 1 for row, row_names in enumerate(reversed(electrode_names)): ey = y + label_height if row: ey += (eh + spacing) * row for col, name in enumerate(row_names): if name is None: i += 1 continue ex = x + label_width if col: ex += (ew + spacing) * col positions[i] = ex, ey fbo.add(Color(*electrode_color)) fbo.add(StencilPush()) fbo.add(Rectangle(pos=(ex, ey), size=(ew, eh))) fbo.add(StencilUse()) graphics[i] = Mesh(mode='line_strip') fbo.add(graphics[i]) fbo.add(StencilUnUse()) fbo.add(Rectangle(pos=(ex, ey), size=(ew, eh))) fbo.add(StencilPop()) i += 1 if label_width: if not col: fbo.add(Color(1, 1, 1, 1)) label = Label(text=name, font_size=sp(40)) label.refresh() _w, _h = label.texture.size rect = Rectangle(pos=(x, ey + (eh - _h) / 2.), size=label.texture.size) rect.texture = label.texture fbo.add(rect) if not row: fbo.add(Color(1, 1, 1, 1)) label = Label(text=name, font_size=sp(40)) label.refresh() _w, _h = label.texture.size rect = Rectangle(pos=(ex + (ew - _w) / 2., y), size=label.texture.size) rect.texture = label.texture fbo.add(rect) with fbo: Color(1, 1, 1, 1) PopMatrix() electrodes_data = [ None, ] * n_electrodes # y_min, y_max = float('inf'), float('-inf') alignment = np.array(self.electrode_intensity_alignment) # get the frequency from any channel name = None for row_names in electrode_names: for name in row_names: if name is not None: break if name is not None: break freq = self.electrodes_metadata[name]['sampling_frequency'] frame_n = int(1 / config['rate'] * freq) n_t = int(time_axis_s * freq) t_vals = np.arange(n_t) / (n_t - 1) * ew y_scale = (eh / 2) / volt_axis for i, name in enumerate(itertools.chain(*electrode_names)): if name is None: continue offset, scale = self.get_electrode_offset_scale(name) electrodes_data[i] = \ self.electrodes_data[name], offset, scale / volt_scale # y_min = min(np.min(data), y_min) # y_max = max(np.max(data), y_max) new_config = { 'alignment': alignment, 'frame_n': frame_n, 't_vals': t_vals, 'y_scale': y_scale, 'electrodes_data': electrodes_data, 'n_t': n_t, 'positions': positions, 'graphics': graphics, 'eh': eh } return CallableGen(self._paint_electrodes_data(new_config))
def _show_mea_outline(self, config, transform_matrix=None): from kivy.graphics import ( Line, StencilPush, StencilUse, StencilUnUse, StencilPop, Rectangle, Color) from kivy.graphics.context_instructions import ( PushMatrix, PopMatrix, Rotate, Translate, Scale, MatrixInstruction, BindTexture) from kivy.graphics.transformation import Matrix from kivy.base import EventLoop EventLoop.ensure_window() from kivy.core.text import Label from kivy.metrics import dp, sp size = config['orig_size'] pos = config['pos'] canvas = config['canvas'] mea_w = max(self.view_controller.mea_num_cols - 1, 0) * \ self.view_controller.mea_pitch mea_h = max(self.view_controller.mea_num_rows - 1, 0) * \ self.view_controller.mea_pitch last_col = "ABCDEFGHJKLMNOPQRSTUVWXYZ"[ self.view_controller.mea_num_cols - 1] with canvas: StencilPush() Rectangle(pos=pos, size=size) StencilUse() PushMatrix() if transform_matrix is not None: mat = Matrix() mat.set(array=transform_matrix) m = MatrixInstruction() m.matrix = mat Color(1, 215 / 255, 0, .2) Line(points=[0, 0, mea_w, 0, mea_w, mea_h, 0, mea_h], close=True) label = Label(text='A1', font_size=sp(12)) label.refresh() _w, _h = label.texture.size rect = Rectangle( pos=(mea_w, mea_h - _h / 2.), size=label.texture.size) rect.texture = label.texture label = Label( text='A{}'.format(self.view_controller.mea_num_rows), font_size=sp(12)) label.refresh() _w, _h = label.texture.size rect = Rectangle( pos=(-_w, mea_h - _h / 2.), size=label.texture.size) rect.texture = label.texture label = Label(text='{}1'.format(last_col), font_size=sp(12)) label.refresh() _w, _h = label.texture.size rect = Rectangle(pos=(mea_w, -_h / 2.), size=label.texture.size) rect.texture = label.texture label = Label( text='{}{}'.format(last_col, self.view_controller.mea_num_rows), font_size=sp(12)) label.refresh() _w, _h = label.texture.size rect = Rectangle(pos=(-_w, -_h / 2.), size=label.texture.size) rect.texture = label.texture PopMatrix() StencilUnUse() Rectangle(pos=pos, size=size) StencilPop()
def _paint_electrodes_data_setup( self, config, electrode_names, spacing=2, draw_size=(0, 0), draw_size_hint=(1, 1), draw_pos=(0, 0), draw_pos_hint=(None, None), volt_scale=1e-6, time_axis_s=1, volt_axis=100, transform_matrix=None, label_width=70): from kivy.graphics import ( Mesh, StencilPush, StencilUse, StencilUnUse, StencilPop, Rectangle, Color) from kivy.graphics.context_instructions import ( PushMatrix, PopMatrix, Scale, MatrixInstruction) from kivy.graphics.transformation import Matrix from kivy.base import EventLoop EventLoop.ensure_window() from kivy.core.text import Label from kivy.metrics import dp, sp n_rows = len(electrode_names) if not n_rows: raise ValueError("There must be at least one electrode specified") n_cols = len(electrode_names[0]) if not n_cols: raise ValueError("There must be at least one electrode specified") if not all((len(row) == n_cols for row in electrode_names)): raise ValueError( "The number of electrodes in all rows must be the same") n_electrodes = sum(map(len, electrode_names)) orig_w, orig_h = config['orig_size'] fbo = config['canvas'] label_height = 45 if label_width else 0 draw_w, draw_h = draw_size draw_hint_w, draw_hint_h = draw_size_hint w = int(draw_w if draw_hint_w is None else orig_w * draw_hint_w) h = int(draw_h if draw_hint_h is None else orig_h * draw_hint_h) draw_x, draw_y = draw_pos draw_hint_x, draw_hint_y = draw_pos_hint x = int(draw_x if draw_hint_x is None else orig_w * draw_hint_x) y = int(draw_y if draw_hint_y is None else orig_h * draw_hint_y) ew = int((w - label_width - max(0, n_cols - 1) * spacing) / n_cols) eh = int((h - label_height - max(0, n_rows - 1) * spacing) / n_rows) with fbo: PushMatrix() # center = x + w / 2., y + h / 2. # if scale: # Scale(scale, scale, 1, origin=center) if transform_matrix is not None: mat = Matrix() mat.set(array=transform_matrix) m = MatrixInstruction() m.matrix = mat positions = [(0, 0), ] * n_electrodes graphics = [None, ] * n_electrodes i = 0 electrode_color = 1, 215 / 255, 0, 1 for row, row_names in enumerate(reversed(electrode_names)): ey = y + label_height if row: ey += (eh + spacing) * row for col, name in enumerate(row_names): if name is None: i += 1 continue ex = x + label_width if col: ex += (ew + spacing) * col positions[i] = ex, ey fbo.add(Color(*electrode_color)) fbo.add(StencilPush()) fbo.add(Rectangle(pos=(ex, ey), size=(ew, eh))) fbo.add(StencilUse()) graphics[i] = Mesh(mode='line_strip') fbo.add(graphics[i]) fbo.add(StencilUnUse()) fbo.add(Rectangle(pos=(ex, ey), size=(ew, eh))) fbo.add(StencilPop()) i += 1 if label_width: if not col: fbo.add(Color(1, 1, 1, 1)) label = Label(text=name, font_size=sp(40)) label.refresh() _w, _h = label.texture.size rect = Rectangle( pos=(x, ey + (eh - _h) / 2.), size=label.texture.size) rect.texture = label.texture fbo.add(rect) if not row: fbo.add(Color(1, 1, 1, 1)) label = Label(text=name, font_size=sp(40)) label.refresh() _w, _h = label.texture.size rect = Rectangle( pos=(ex + (ew - _w) / 2., y), size=label.texture.size) rect.texture = label.texture fbo.add(rect) with fbo: Color(1, 1, 1, 1) PopMatrix() electrodes_data = [None, ] * n_electrodes # y_min, y_max = float('inf'), float('-inf') alignment = np.array(self.electrode_intensity_alignment) # get the frequency from any channel name = None for row_names in electrode_names: for name in row_names: if name is not None: break if name is not None: break freq = self.electrodes_metadata[name]['sampling_frequency'] frame_n = int(1 / config['rate'] * freq) n_t = int(time_axis_s * freq) t_vals = np.arange(n_t) / (n_t - 1) * ew y_scale = (eh / 2) / volt_axis for i, name in enumerate(itertools.chain(*electrode_names)): if name is None: continue offset, scale = self.get_electrode_offset_scale(name) electrodes_data[i] = \ self.electrodes_data[name], offset, scale / volt_scale # y_min = min(np.min(data), y_min) # y_max = max(np.max(data), y_max) new_config = { 'alignment': alignment, 'frame_n': frame_n, 't_vals': t_vals, 'y_scale': y_scale, 'electrodes_data': electrodes_data, 'n_t': n_t, 'positions': positions, 'graphics': graphics, 'eh': eh} return CallableGen(self._paint_electrodes_data(new_config))