PikaUI_core.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import weakref
  2. # class ALIGN(_backend.ALIGN): (vm not pass)
  3. # pass
  4. class _ALIGN:
  5. CENTER = 0
  6. TOP_MID = 1
  7. ALIGN = _ALIGN
  8. _backend = None
  9. def set_backend(backend):
  10. global _backend
  11. global ALIGN
  12. global page
  13. _backend = backend
  14. ALIGN = _backend.ALIGN
  15. page = _Page()
  16. page._setPerent(page)
  17. page.isroot = True
  18. page.backend = _backend.scr_act()
  19. class Widget:
  20. backend = None
  21. width = 0
  22. height = 0
  23. pos = None
  24. parent = None
  25. align = None
  26. text = None
  27. isroot = False
  28. _label = None
  29. _child = []
  30. def __init__(self,
  31. width=100,
  32. height=100,
  33. pos=None,
  34. text=None,
  35. align=ALIGN.TOP_MID):
  36. self.width = width
  37. self.height = height
  38. self.pos = pos
  39. self.align = align
  40. self.text = text
  41. def _setPerent(self, parent):
  42. self.parent = weakref.ref(parent)
  43. def update(self):
  44. if self.parent is None:
  45. print('self.parent is None')
  46. return
  47. if self.parent.backend is None:
  48. print('self.parent.backend is None')
  49. return
  50. if self.backend is None:
  51. self.backend = self._createBackend(self.parent)
  52. if not self.isroot:
  53. self._updateAlign(self.align)
  54. self._updateAttr(self.width, self.height, self.pos)
  55. self._updateText(self.text)
  56. for c in self._child:
  57. c.update()
  58. def _createBackend(self, parent: "Widget"):
  59. return _backend.lv_obj(parent.backend)
  60. def _updateAttr(self,
  61. width,
  62. height,
  63. pos):
  64. self.backend.set_width(width)
  65. self.backend.set_height(height)
  66. if not pos is None:
  67. self.backend.set_pos(pos[0], pos[1])
  68. def _updateAlign(self, align):
  69. self.backend.align(align, 0, 0)
  70. def _updateText(self, text):
  71. if not None is text:
  72. self._label = _backend.label(self.backend)
  73. self._label.set_text(self.text)
  74. self._label.align(_backend.ALIGN.CENTER, 0, 0)
  75. def add(self, *child):
  76. for c in child:
  77. c._setPerent(self)
  78. self._child.append(c)
  79. return self
  80. class _Page(Widget):
  81. def _createBackend(self, parent: Widget):
  82. return _backend.scr_act()
  83. class Button(Widget):
  84. def _createBackend(self, parent: Widget):
  85. return _backend.btn(parent.backend)
  86. class Text(Widget):
  87. def _createBackend(self, parent: Widget):
  88. return _backend.label(parent.backend)
  89. def _updateText(self, text):
  90. self.backend.set_text(text)
  91. page = _Page()
  92. def Page():
  93. return page