bitwidgets: Split classes out of button

There's a bunch of unrelated stuff in button, like Glyphs and ButtonBar, which
should be in their own modules.
This commit is contained in:
June Tate-Gans 2021-05-08 23:00:48 -05:00
parent 96f9fa122d
commit faf052b015
6 changed files with 125 additions and 122 deletions

View File

@ -12,7 +12,7 @@ from g13gui.bitwidgets.display import Display
from g13gui.bitwidgets.screen import Screen from g13gui.bitwidgets.screen import Screen
from g13gui.bitwidgets.label import Label from g13gui.bitwidgets.label import Label
from g13gui.bitwidgets.button import Button from g13gui.bitwidgets.button import Button
from g13gui.bitwidgets.button import Glyphs from g13gui.bitwidgets.glyph import Glyphs
from g13gui.bitwidgets.listview import ListView from g13gui.bitwidgets.listview import ListView
gi.require_version('GLib', '2.0') gi.require_version('GLib', '2.0')

View File

@ -1,126 +1,11 @@
import enum
from builtins import property from builtins import property
from g13gui.bitwidgets import DISPLAY_WIDTH
from g13gui.bitwidgets import DISPLAY_HEIGHT
from g13gui.bitwidgets.widget import Widget from g13gui.bitwidgets.widget import Widget
from g13gui.bitwidgets.glyph import Glyph
from g13gui.bitwidgets.rectangle import Rectangle from g13gui.bitwidgets.rectangle import Rectangle
from g13gui.observer.subject import ChangeType from g13gui.observer.subject import ChangeType
GLYPH_WIDTH = 5
GLYPH_HEIGHT = 5
class Glyphs(enum.Enum):
DOWN_ARROW = [(2, 0), (2, 4), (0, 2), (4, 2), (2, 4)]
UP_ARROW = [(2, 4), (2, 0), (4, 2), (0, 2), (2, 0)]
CHECKMARK = [(0, 3), (1, 4), (4, 1), (1, 4)]
XMARK = [(0, 0), (4, 4), (2, 2), (4, 0), (0, 4)]
BLANK = []
BOUNDS = (5, 5)
def transformTo(self, offsetx, offsety):
return [(offsetx + x, offsety + y) for (x, y) in self.value]
class ButtonBar(Widget):
MAX_BUTTONS = 4
TOP_LINE = 33
def __init__(self):
Widget.__init__(self)
self._children = [None] * ButtonBar.MAX_BUTTONS
self.position = (0, ButtonBar.TOP_LINE)
self.bounds = (DISPLAY_WIDTH, DISPLAY_HEIGHT - ButtonBar.TOP_LINE)
def _buttonBounds(self):
width = (DISPLAY_WIDTH // ButtonBar.MAX_BUTTONS) - 2
height = DISPLAY_HEIGHT - ButtonBar.TOP_LINE
return (width, height)
def _positionForSlot(self, buttonNum):
slotWidth = DISPLAY_WIDTH / ButtonBar.MAX_BUTTONS
slotX = (buttonNum * slotWidth)
slotY = ButtonBar.TOP_LINE + 1
return (int(slotX), int(slotY))
def button(self, buttonNum):
return self._children[buttonNum]
def setButton(self, buttonNum, button):
if self._children[buttonNum]:
self.removeChild(self._children[buttonNum])
self._children[buttonNum] = button
button.position = self._positionForSlot(buttonNum)
button.bounds = self._buttonBounds()
button.parent = self
self.addChange(ChangeType.ADD, 'child', button)
self.notifyChanged()
def addChild(self, button):
buttonNum = self._children.index(None)
if buttonNum > ButtonBar.MAX_BUTTONS:
raise ValueError('Can\'t store another button!')
self.setButton(buttonNum, button)
def removeChild(self, button):
buttonNum = self._children.index(button)
button = self._children[buttonNum]
self._children[buttonNum] = None
button.parent = None
self.addChange(ChangeType.REMOVE, 'child', button)
self.notifyChanged()
def draw(self, ctx):
if self.visible:
for child in self._children:
if child and child.visible:
child.draw(ctx)
# Top line
ctx.line(self.position + (DISPLAY_WIDTH,
ButtonBar.TOP_LINE),
fill=1)
# Dividing lines
for slot in range(0, ButtonBar.MAX_BUTTONS):
(x, y) = self._positionForSlot(slot)
x -= 1
ctx.line((x, y) + (x, DISPLAY_HEIGHT),
fill=1)
class Glyph(Widget):
def __init__(self, x, y, glyph=Glyphs.BLANK, fill=True):
Widget.__init__(self)
self.glyph = glyph
self.position = (x, y)
self.fill = fill
@property
def glyph(self):
return self._glyph
@glyph.setter
def glyph(self, glyph):
self.setProperty('glyph', glyph)
@property
def bounds(self):
return Glyphs.BOUNDS.value
def draw(self, ctx):
if self.visible:
(x, y) = self.position
xformedGlyph = self.glyph.transformTo(x, y)
ctx.line(xformedGlyph, fill=self._fill)
class Button(Widget): class Button(Widget):
def __init__(self, glyph, fill=True): def __init__(self, glyph, fill=True):
Widget.__init__(self) Widget.__init__(self)

View File

@ -0,0 +1,74 @@
from g13gui.observer.subject import ChangeType
from g13gui.bitwidgets import DISPLAY_WIDTH
from g13gui.bitwidgets import DISPLAY_HEIGHT
from g13gui.bitwidgets.widget import Widget
class ButtonBar(Widget):
MAX_BUTTONS = 4
TOP_LINE = 33
def __init__(self):
Widget.__init__(self)
self._children = [None] * ButtonBar.MAX_BUTTONS
self.position = (0, ButtonBar.TOP_LINE)
self.bounds = (DISPLAY_WIDTH, DISPLAY_HEIGHT - ButtonBar.TOP_LINE)
def _buttonBounds(self):
width = (DISPLAY_WIDTH // ButtonBar.MAX_BUTTONS) - 2
height = DISPLAY_HEIGHT - ButtonBar.TOP_LINE
return (width, height)
def _positionForSlot(self, buttonNum):
slotWidth = DISPLAY_WIDTH / ButtonBar.MAX_BUTTONS
slotX = (buttonNum * slotWidth)
slotY = ButtonBar.TOP_LINE + 1
return (int(slotX), int(slotY))
def button(self, buttonNum):
return self._children[buttonNum]
def setButton(self, buttonNum, button):
if self._children[buttonNum]:
self.removeChild(self._children[buttonNum])
self._children[buttonNum] = button
button.position = self._positionForSlot(buttonNum)
button.bounds = self._buttonBounds()
button.parent = self
self.addChange(ChangeType.ADD, 'child', button)
self.notifyChanged()
def addChild(self, button):
buttonNum = self._children.index(None)
if buttonNum > ButtonBar.MAX_BUTTONS:
raise ValueError('Can\'t store another button!')
self.setButton(buttonNum, button)
def removeChild(self, button):
buttonNum = self._children.index(button)
button = self._children[buttonNum]
self._children[buttonNum] = None
button.parent = None
self.addChange(ChangeType.REMOVE, 'child', button)
self.notifyChanged()
def draw(self, ctx):
if self.visible:
for child in self._children:
if child and child.visible:
child.draw(ctx)
# Top line
ctx.line(self.position + (DISPLAY_WIDTH,
ButtonBar.TOP_LINE),
fill=1)
# Dividing lines
for slot in range(0, ButtonBar.MAX_BUTTONS):
(x, y) = self._positionForSlot(slot)
x -= 1
ctx.line((x, y) + (x, DISPLAY_HEIGHT),
fill=1)

View File

@ -0,0 +1,44 @@
import enum
from builtins import property
from g13gui.bitwidgets.widget import Widget
class Glyphs(enum.Enum):
DOWN_ARROW = [(2, 0), (2, 4), (0, 2), (4, 2), (2, 4)]
UP_ARROW = [(2, 4), (2, 0), (4, 2), (0, 2), (2, 0)]
CHECKMARK = [(0, 3), (1, 4), (4, 1), (1, 4)]
XMARK = [(0, 0), (4, 4), (2, 2), (4, 0), (0, 4)]
BLANK = []
BOUNDS = (5, 5)
def transformTo(self, offsetx, offsety):
return [(offsetx + x, offsety + y) for (x, y) in self.value]
class Glyph(Widget):
def __init__(self, x, y, glyph=Glyphs.BLANK, fill=True):
Widget.__init__(self)
self.glyph = glyph
self.position = (x, y)
self.fill = fill
@property
def glyph(self):
return self._glyph
@glyph.setter
def glyph(self, glyph):
self.setProperty('glyph', glyph)
@property
def bounds(self):
return Glyphs.BOUNDS.value
def draw(self, ctx):
if self.visible:
(x, y) = self.position
xformedGlyph = self.glyph.transformTo(x, y)
ctx.line(xformedGlyph, fill=self._fill)

View File

@ -1,7 +1,7 @@
from g13gui.bitwidgets.widget import Widget from g13gui.bitwidgets.widget import Widget
from g13gui.bitwidgets.button import ButtonBar from g13gui.bitwidgets.buttonbar import ButtonBar
from g13gui.bitwidgets.button import Glyph from g13gui.bitwidgets.glyph import Glyph
from g13gui.bitwidgets.button import Glyphs from g13gui.bitwidgets.glyph import Glyphs
from g13gui.bitwidgets.rectangle import Rectangle from g13gui.bitwidgets.rectangle import Rectangle
from g13gui.bitwidgets.label import Label from g13gui.bitwidgets.label import Label
from g13gui.bitwidgets.fonts import Fonts from g13gui.bitwidgets.fonts import Fonts

View File

@ -2,7 +2,7 @@ import time
from builtins import property from builtins import property
from g13gui.bitwidgets.widget import Widget from g13gui.bitwidgets.widget import Widget
from g13gui.bitwidgets.button import ButtonBar from g13gui.bitwidgets.buttonbar import ButtonBar
MIN_NSECS_BETWEEN_FRAMES = 125000 MIN_NSECS_BETWEEN_FRAMES = 125000