mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 10:50:00 +00:00
Python: Improve Struct mapping
When reading, create the local equivalent of a dataclass, so that access doesn't require ["foo"] key syntax. Also implement the copy protocol, so that we can safely make clones of the references returned by the ListModel.
This commit is contained in:
parent
0b6381d012
commit
e3aab79fdb
7 changed files with 103 additions and 25 deletions
|
@ -6,6 +6,7 @@ from datetime import timedelta, datetime
|
|||
import os
|
||||
import random
|
||||
import itertools
|
||||
import copy
|
||||
import slint
|
||||
from slint import Color, ListModel, Timer, TimerMode
|
||||
|
||||
|
@ -14,31 +15,32 @@ class MainWindow(slint.loader.memory.MainWindow):
|
|||
def __init__(self):
|
||||
super().__init__()
|
||||
initial_tiles = self.memory_tiles
|
||||
tiles = ListModel(itertools.chain(initial_tiles, initial_tiles))
|
||||
tiles = ListModel(itertools.chain(
|
||||
map(copy.copy, initial_tiles), map(copy.copy, initial_tiles)))
|
||||
random.shuffle(tiles)
|
||||
self.memory_tiles = tiles
|
||||
|
||||
@slint.callback
|
||||
def check_if_pair_solved(self):
|
||||
flipped_tiles = [(index, tile) for index, tile in enumerate(
|
||||
self.memory_tiles) if tile["image-visible"] and not tile["solved"]]
|
||||
flipped_tiles = [(index, copy.copy(tile)) for index, tile in enumerate(
|
||||
self.memory_tiles) if tile.image_visible and not tile.solved]
|
||||
if len(flipped_tiles) == 2:
|
||||
tile1_index, tile1 = flipped_tiles[0]
|
||||
tile2_index, tile2 = flipped_tiles[1]
|
||||
is_pair_solved = tile1["image"].path == tile2["image"].path
|
||||
is_pair_solved = tile1.image.path == tile2.image.path
|
||||
if is_pair_solved:
|
||||
tile1["solved"] = True
|
||||
tile1.solved = True
|
||||
self.memory_tiles[tile1_index] = tile1
|
||||
tile2["solved"] = True
|
||||
tile2.solved = True
|
||||
self.memory_tiles[tile2_index] = tile2
|
||||
else:
|
||||
self.disable_tiles = True
|
||||
|
||||
def reenable_tiles():
|
||||
self.disable_tiles = False
|
||||
tile1["image-visible"] = False
|
||||
tile1.image_visible = False
|
||||
self.memory_tiles[tile1_index] = tile1
|
||||
tile2["image-visible"] = False
|
||||
tile2.image_visible = False
|
||||
self.memory_tiles[tile2_index] = tile2
|
||||
|
||||
Timer.single_shot(timedelta(seconds=1), reenable_tiles)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue