From 6a5366bc7208056f59614d32e0c6e41b46a79029 Mon Sep 17 00:00:00 2001 From: LupaDevStudio Date: Thu, 14 Dec 2023 15:44:54 +0100 Subject: [PATCH] add a new custom button class --- screens/custom_widgets/__init__.py | 1 + screens/custom_widgets/custom_buttons.kv | 26 +++++++++++ screens/custom_widgets/custom_buttons.py | 59 ++++++++++++++++++++++++ screens/home.kv | 12 ++++- screens/home.py | 10 +++- tools/constants.py | 2 + tools/kivy_tools/image_button.py | 12 +++++ 7 files changed, 119 insertions(+), 3 deletions(-) diff --git a/screens/custom_widgets/__init__.py b/screens/custom_widgets/__init__.py index 5689155..cc814a2 100644 --- a/screens/custom_widgets/__init__.py +++ b/screens/custom_widgets/__init__.py @@ -4,3 +4,4 @@ from screens.custom_widgets.bottom_bar import BottomBar +from screens.custom_widgets.custom_buttons import CustomButton diff --git a/screens/custom_widgets/custom_buttons.kv b/screens/custom_widgets/custom_buttons.kv index e69de29..1f45bec 100644 --- a/screens/custom_widgets/custom_buttons.kv +++ b/screens/custom_widgets/custom_buttons.kv @@ -0,0 +1,26 @@ +#:kivy 2.2.1 + +: + + # Background + canvas.before: + Color: + rgba: self.background_color + RoundedRectangle: + pos:self.pos + size:self.size + radius:[40,] + + # Main text + Label: + text: root.text + font_size: root.parent.font_ratio * root.font_size + pos: root.pos + size: root.size + shorten: False + text_size: (root.width*root.text_filling_ratio,None) + font_name: root.text_font_name + halign: "center" + valign: "center" + line_height: 1 + color: (0,0,0,1) diff --git a/screens/custom_widgets/custom_buttons.py b/screens/custom_widgets/custom_buttons.py index e69de29..56bb70d 100644 --- a/screens/custom_widgets/custom_buttons.py +++ b/screens/custom_widgets/custom_buttons.py @@ -0,0 +1,59 @@ +""" +Module to create custom buttons with round transparent white background. +""" + +############### +### Imports ### +############### + +### Kivy imports ### +from kivy.uix.widget import Widget +from kivy.uix.behaviors import ButtonBehavior +from kivy.properties import ( + StringProperty, + NumericProperty +) + +### Local imports ### +from tools.path import ( + PATH_TEXT_FONT +) +from tools.constants import ( + CUSTOM_BUTTON_BACKGROUND_COLOR, + MAIN_BUTTON_FONT_SIZE +) + +############# +### Class ### +############# + + +class CustomButton(ButtonBehavior, Widget): + + background_color = CUSTOM_BUTTON_BACKGROUND_COLOR + text = StringProperty() + text_filling_ratio = NumericProperty() + font_size = NumericProperty() + + def __init__( + self, + text="", + text_font_name=PATH_TEXT_FONT, + text_filling_ratio=0.8, + font_size=MAIN_BUTTON_FONT_SIZE, + release_function=lambda: 1 + 1, + **kwargs): + super().__init__(**kwargs) + self.release_function = release_function + self.always_release = True + self.text_font_name = text_font_name + self.text = text + self.text_filling_ratio = text_filling_ratio + self.font_size = font_size + + def on_press(self): + self.opacity = 0.8 + + def on_release(self): + self.release_function() + self.opacity = 1 diff --git a/screens/home.kv b/screens/home.kv index e1f45c5..d00f792 100644 --- a/screens/home.kv +++ b/screens/home.kv @@ -22,4 +22,14 @@ BottomBar: size_hint: (1, BOTTOM_BAR_HEIGHT) pos_hint: {"bottom":0,"left":0} - selected: "home" \ No newline at end of file + selected: "home" + + CustomButton: + size_hint: (0.5,0.15) + pos_hint: {"center_x":0.5, "center_y":0.6} + text:"Free Mode" + + CustomButton: + size_hint: (0.5,0.15) + pos_hint: {"center_x":0.5, "center_y":0.4} + text:"Daily Mode" diff --git a/screens/home.py b/screens/home.py index 52290a3..f5be8b4 100644 --- a/screens/home.py +++ b/screens/home.py @@ -13,8 +13,14 @@ USER_DATA, THEMES_DICT ) -from screens.custom_widgets import BottomBar -from tools.kivy_tools import ImprovedScreen, ImageButton +from screens.custom_widgets import ( + BottomBar, + CustomButton +) +from tools.kivy_tools import ( + ImprovedScreen, + ImageButton +) ############# diff --git a/tools/constants.py b/tools/constants.py index b1b12c9..5351fce 100644 --- a/tools/constants.py +++ b/tools/constants.py @@ -145,7 +145,9 @@ def __init__(self) -> None: TITLE_FONT_SIZE = 45 TITLE_OUTLINE_WIDTH = 2 TITLE_OUTLINE_COLOR = (1, 1, 1, 1) +MAIN_BUTTON_FONT_SIZE = 30 BOTTOM_BAR_HEIGHT = 0.12 +CUSTOM_BUTTON_BACKGROUND_COLOR = (1, 1, 1, 0.7) ### Musics ### diff --git a/tools/kivy_tools/image_button.py b/tools/kivy_tools/image_button.py index d727dba..b64dcf9 100644 --- a/tools/kivy_tools/image_button.py +++ b/tools/kivy_tools/image_button.py @@ -1,6 +1,18 @@ +""" +Module to create an image button. +""" + +############### +### Imports ### +############### + from kivy.uix.image import Image from kivy.uix.behaviors import ButtonBehavior +############# +### Class ### +############# + class ImageButton(ButtonBehavior, Image): def __init__(self, source="", release_function=lambda: 1 + 1, **kwargs):