📚 The CoCalc Library - books, templates and other resources
License: OTHER
#!/usr/bin/env python1#2# Copyright 2019 the original author or authors.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15#16import pygame17from cmath import isclose18from vqe_playground.utils.colors import WHITE, BLACK, LIGHT_GREY19from vqe_playground.utils.fonts import *202122class NumberPicker(pygame.sprite.Sprite):23"""Displays a number that may be modified by clicking and dragging"""24def __init__(self, number, width, height, enabled=True):25pygame.sprite.Sprite.__init__(self)26self.number = None27self.width = width28self.height = height29self.enabled = enabled3031self.image = None32self.rect = None33self.background_color = WHITE34self.font_color = BLACK35self.font = ARIAL_363637self.set_number(number)38self.draw_number_picker()3940# def update(self):41# self.draw_number_picker()42#43def set_number(self, number):44self.number = number4546def draw_number_picker(self):47self.image = pygame.Surface([self.width, self.height])48self.image.convert()49self.image.fill(WHITE if self.enabled else LIGHT_GREY)50self.rect = self.image.get_rect()5152rectangle = pygame.Rect(0, 0, self.width, self.height)53pygame.draw.rect(self.image, BLACK, rectangle, 1)5455if not isclose(self.number, 0):56text_surface = self.font.render(str(self.number), False, BLACK)57text_xpos = (self.rect.width - text_surface.get_rect().width) / 258text_ypos = (self.rect.height - text_surface.get_rect().height) / 259self.image.blit(text_surface, (text_xpos, text_ypos))60616263