📚 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 vqe_playground.utils.colors import BLUE, BLACK, WHITE, LIGHT_GREY18from vqe_playground.utils.fonts import *192021class Button(pygame.sprite.Sprite):22"""Button that may be clicked"""23def __init__(self, label, width, height, enabled=True):24pygame.sprite.Sprite.__init__(self)25self.label = None26self.width = width27self.height = height28self._enabled = enabled2930self.image = pygame.Surface([self.width, self.height])31self.image.convert()32self.rect = self.image.get_rect()33self.rectangle = pygame.Rect(0, 0, self.width, self.height)3435self.font_color = WHITE36self.font = ARIAL_363738self.set_label(label)39self.draw_button()4041def update(self):42self.draw_button()4344def set_label(self, label):45self.label = label4647def get_enabled(self):48return self._enabled4950def set_enabled(self, enabled):51self._enabled = enabled52self.draw_button()5354def draw_button(self):55self.image.fill(BLUE if self._enabled else LIGHT_GREY)56pygame.draw.rect(self.image, BLACK, self.rectangle, 1)5758text_surface = self.font.render(self.label, False, self.font_color)59text_xpos = (self.rect.width - text_surface.get_rect().width) / 260text_ypos = (self.rect.height - text_surface.get_rect().height) / 261self.image.blit(text_surface, (text_xpos, text_ypos))626364