Skip to content

Commit

Permalink
add a new custom button class
Browse files Browse the repository at this point in the history
  • Loading branch information
LupaDevStudio committed Dec 14, 2023
1 parent d7086f9 commit 6a5366b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 3 deletions.
1 change: 1 addition & 0 deletions screens/custom_widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@


from screens.custom_widgets.bottom_bar import BottomBar
from screens.custom_widgets.custom_buttons import CustomButton
26 changes: 26 additions & 0 deletions screens/custom_widgets/custom_buttons.kv
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#:kivy 2.2.1

<CustomButton>:

# 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)
59 changes: 59 additions & 0 deletions screens/custom_widgets/custom_buttons.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 11 additions & 1 deletion screens/home.kv
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@
BottomBar:
size_hint: (1, BOTTOM_BAR_HEIGHT)
pos_hint: {"bottom":0,"left":0}
selected: "home"
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"
10 changes: 8 additions & 2 deletions screens/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


#############
Expand Down
2 changes: 2 additions & 0 deletions tools/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###

Expand Down
12 changes: 12 additions & 0 deletions tools/kivy_tools/image_button.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 6a5366b

Please sign in to comment.