Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

746 lines
24KB

  1. '''
  2. Filename nodes.py
  3. Author: Bryan "ObsidianBlk" Miller
  4. Date Created: 8/1/2018
  5. Python Version: 3.7
  6. '''
  7. from .display import Display
  8. from .events import Events
  9. from .resource import ResourceManager
  10. import pygame
  11. class NodeError(Exception):
  12. pass
  13. class Node:
  14. def __init__(self, name="Node", parent=None):
  15. self._NODE_DATA={
  16. "parent":None,
  17. "name":name,
  18. "children":[],
  19. "resource":None,
  20. "position":(0,0)
  21. }
  22. if parent is not None:
  23. try:
  24. self.parent = parent
  25. except NodeError as e:
  26. raise e
  27. @property
  28. def parent(self):
  29. return self._NODE_DATA["parent"]
  30. @parent.setter
  31. def parent(self, new_parent):
  32. try:
  33. self.parent_to_node(new_parent)
  34. except NodeError as e:
  35. raise e
  36. @property
  37. def root(self):
  38. if self.parent is None:
  39. return self
  40. return self.parent.root
  41. @property
  42. def name(self):
  43. return self._NODE_DATA["name"]
  44. @name.setter
  45. def name(self, value):
  46. if self.parent is not None:
  47. if self.parent.get_node(value) is not None:
  48. raise NodeError("Parent already contains node named '{}'.".format(name))
  49. self._NODE_DATA["name"] = value
  50. @property
  51. def full_name(self):
  52. if self.parent is None:
  53. return self.name
  54. return self.parent.full_name + "." + self.name
  55. @property
  56. def resource(self):
  57. if self._NODE_DATA["resource"] is None:
  58. # Only bother creating the instance if it's being asked for.
  59. # All ResourceManager instances access same data.
  60. self._NODE_DATA["resource"] = ResourceManager()
  61. return self._NODE_DATA["resource"]
  62. @property
  63. def child_count(self):
  64. return len(this._NODE_DATA["children"])
  65. @property
  66. def position(self):
  67. p = self._NODE_DATA["position"]
  68. return (p[0], p[1])
  69. @position.setter
  70. def position(self, pos):
  71. if not isinstance(pos, (list, tuple)):
  72. raise TypeError("Expected a list or tuple.")
  73. if len(pos) != 2:
  74. raise ValueError("Wrong number of values given.")
  75. try:
  76. self.position_x = pos[0]
  77. self.position_y = pos[1]
  78. except Exception as e:
  79. raise e
  80. @property
  81. def position_x(self):
  82. return self._NODE_DATA["position"][0]
  83. @position_x.setter
  84. def position_x(self, v):
  85. if not isinstance(v, (int, float)):
  86. raise TypeError("Excepted an number value.")
  87. self._NODE_DATA["position"][0] = float(v)
  88. @property
  89. def position_y(self):
  90. return self._NODE_DATA["position"][1]
  91. @position_y.setter
  92. def position_y(self, v):
  93. if not isinstance(v, (int, float)):
  94. raise TypeError("Excepted an number value.")
  95. self._NODE_DATA["position"][1] = float(v)
  96. def get_world_position(self):
  97. if self.parent is None:
  98. return (0,0)
  99. pos = self.position
  100. ppos = self.parent.get_world_position()
  101. return (pos[0] + ppos[0], pos[1] + ppos[1])
  102. def parent_to_node(self, parent, allow_reparenting=False):
  103. if not isinstance(value, Node):
  104. raise NodeError("Node may only parent to another Node instance.")
  105. if self.parent is None or self.parent != parent:
  106. if self.parent is not None:
  107. if allow_Reparenting == False:
  108. raise NodeError("Node already assigned a parent Node.")
  109. if self.parent.remove_node(self) != self:
  110. raise NodeError("Failed to remove self from current parent.")
  111. try:
  112. parent.attach_node(self)
  113. except NodeError as e:
  114. raise e
  115. def attach_node(self, node, reparent=False, index=-1):
  116. if node.parent is not None:
  117. if node.parent == self:
  118. return # Nothing to do. Given node already parented to this node.
  119. if reparent == False:
  120. raise NodeError("Node already parented.")
  121. if node.parent.remove_node(node) != node:
  122. raise NodeError("Failed to remove given node from it's current parent.")
  123. if self.get_node(node.name) is not None:
  124. raise NodeError("Node with name '{}' already attached.".format(node.name))
  125. node._NODE_DATA["parent"] = self
  126. children = self._NODE_DATA["children"]
  127. if index < 0 or index >= len(children):
  128. children.append(node)
  129. else:
  130. children.insert(index, node)
  131. def remove_node(self, node):
  132. if isinstance(node, (str, unicode)):
  133. n = self.get_node(node)
  134. if n is not None:
  135. try:
  136. return self.remove_node(n)
  137. except NodeError as e:
  138. raise e
  139. elif isinstance(node, Node):
  140. if node.parent != self:
  141. if node.parent == None:
  142. raise NodeError("Cannot remove an unparented node.")
  143. try:
  144. return node.parent.remove_node(node)
  145. except NodeError as e:
  146. raise e
  147. if node in self._NODE_DATA["children"]:
  148. self._NODE_DATA["children"].remove(node)
  149. node._NODE_DATA["parent"] = None
  150. return node
  151. else:
  152. raise NodeError("Expected a Node instance or a string.")
  153. return None
  154. def get_node(self, name):
  155. if self.child_count <= 0:
  156. return None
  157. subnames = name.split(".")
  158. for c in self._NODE_DATA["children"]:
  159. if c.name == subnames[0]:
  160. if len(subnames) > 1:
  161. return c.get_node(".".join(subnames[1:-1]))
  162. return c
  163. return None
  164. def listen(self, signal, callback_fn):
  165. try:
  166. Events.listen(signal, callback_fn)
  167. except Exception as e:
  168. raise e
  169. def unlisten(self, signal, callback_fn):
  170. Events.unlisten(signal, callback_fn)
  171. def emit(self, signal, data={}):
  172. # NOTE: I'm currently forcing the existance of this "NODE" key in the dictionary given. Not sure if I'll keep this.
  173. data["NODE"] = {
  174. "name":self.name,
  175. "n":self
  176. }
  177. Events.emit(signal, data)
  178. def _init(self):
  179. if hasattr(self, "on_init"):
  180. self.on_init()
  181. for c in self._NODE_DATA["children"]:
  182. c._init()
  183. def _close(self):
  184. if hasattr(self, "on_close"):
  185. self.on_close()
  186. for c in self._NODE_DATA["children"]:
  187. c._close()
  188. def _pause(self):
  189. if hasattr(self, "on_pause"):
  190. self.on_pause()
  191. for c in self._NODE_DATA["children"]:
  192. c._pause()
  193. def _start(self):
  194. if hasattr(self, "on_start"):
  195. self.on_start()
  196. for c in self._NODE_DATA["children"]:
  197. c._start()
  198. def _update(self, dt):
  199. if hasattr(self, "on_update"):
  200. self.on_update(dt)
  201. for c in self._NODE_DATA["children"]:
  202. c._update(dt)
  203. def _render(self, surface):
  204. for c in self._NODE_DATA["children"]:
  205. c._render(surface)
  206. class Node2D(Node):
  207. def __init__(self, name="Node2D", parent=None):
  208. try:
  209. Node.__init__(self, name, parent)
  210. except NodeError as e:
  211. raise e
  212. def _callOnRender(self, surface):
  213. if hasattr(self, "on_render"):
  214. self._ACTIVE_SURF = surface
  215. self.on_render()
  216. del self._ACTIVE_SURF
  217. def _render(self, surface):
  218. self._callOnRender(surface)
  219. Node._render(self, surface)
  220. def draw_image(self, img, pos=(0,0), rect=None):
  221. if not hasattr(self, "_ACTIVE_SURF"):
  222. return
  223. self._ACTIVE_SURF.blit(img, pos, rect)
  224. def fill(self, color):
  225. if not hasattr(self, "_ACTIVE_SURF"):
  226. return
  227. self._ACTIVE_SURF.fill(color)
  228. def draw_lines(self, points, color, thickness=1, closed=False):
  229. if not hasattr(self, "_ACTIVE_SURF"):
  230. return
  231. pygame.draw.lines(self._ACTIVE_SURF, color, closed, points, thickness)
  232. def draw_rect(self, rect, color, thickness=1):
  233. if not hasattr(self, "_ACTIVE_SURF"):
  234. return
  235. pygame.draw.rect(self._ACTIVE_SURF, color, rect, thickness)
  236. def draw_ellipse(self, rect, color, thickness=1, fill_color=None):
  237. if not hasattr(self, "_ACTIVE_SURF"):
  238. return
  239. if fill_color is not None:
  240. pygame.draw.ellipse(self._ACTIVE_SURF, fill_color, rect)
  241. if thickness > 0:
  242. pygame.draw.ellipse(self._ACTIVE_SURF, color, rect, thickness)
  243. def draw_circle(self, pos, radius, color, thickness=1, fill_color=None):
  244. if not hasattr(self, "_ACTIVE_SURF"):
  245. return
  246. if fill_color is not None:
  247. pygame.draw.circle(self._ACTIVE_SURF, fill_color, pos, radius)
  248. if thickness > 0:
  249. pygame.draw.circle(self._ACTIVE_SURF, color, pos, radius, thickness)
  250. def draw_polygon(self, points, color, thickness=1, fill_color=None):
  251. if not hasattr(self, "_ACTIVE_SURF"):
  252. return
  253. if fill_color is not None:
  254. pygame.draw.polygon(self._ACTIVE_SURF, fill_color, points)
  255. if thickness >= 1:
  256. pygame.draw.polygon(self._ACTIVE_SURF, color, points, thickness)
  257. class NodeSurface(Node2D):
  258. def __init__(self, name="NodeSurface", parent=None):
  259. try:
  260. Node2D.__init__(self, name, parent)
  261. except NodeError as e:
  262. raise e
  263. # TODO: Update this class to use the _NODE*_DATA={} structure.
  264. self._scale = (1.0, 1.0)
  265. self._scaleToDisplay = False
  266. self._scaleDirty = False
  267. self._keepAspectRatio = False
  268. self._alignCenter = False
  269. self._surface = None
  270. self._tsurface = None
  271. self.set_surface()
  272. def _updateTransformSurface(self):
  273. if self._surface is None:
  274. return
  275. self._scaleDirty = False
  276. if self._scaleToDisplay:
  277. dsize = Display.resolution
  278. ssize = self._surface.get_size()
  279. self._scale = (dsize[0] / ssize[0], dsize[1] / ssize[1])
  280. if self._keepAspectRatio:
  281. if self._scale[0] < self._scale[1]:
  282. self._scale = (self._scale[0], self._scale[0])
  283. else:
  284. self._scale = (self._scale[1], self._scale[1])
  285. if self._scale[0] == 1.0 and self._scale[1] == 1.0:
  286. self._tsurface = None
  287. return
  288. size = self._surface.get_size()
  289. nw = size[0] * self._scale[0]
  290. nh = 0
  291. if self._keepAspectRatio:
  292. nh = size[1] * self._scale[0]
  293. else:
  294. nh = size[1] * self._scale[1]
  295. self._tsurface = pygame.Surface((nw, nh), pygame.SRCALPHA, self._surface)
  296. self._tsurface.fill(pygame.Color(0,0,0,0))
  297. @property
  298. def resolution(self):
  299. if self._surface is None:
  300. return (0,0)
  301. return self._surface.get_size()
  302. @resolution.setter
  303. def resolution(self, res):
  304. try:
  305. self.set_surface(res)
  306. except (TypeError, ValueError) as e:
  307. raise e
  308. @property
  309. def width(self):
  310. return self.resolution[0]
  311. @property
  312. def height(self):
  313. return self.resolution[1]
  314. @property
  315. def scale(self):
  316. return self._scale
  317. @scale.setter
  318. def scale(self, scale):
  319. if self._keepAspectRatio:
  320. if not isinstance(scale, (int, float)):
  321. raise TypeError("Expected number value.")
  322. self._scale = (scale, self._scale[1])
  323. else:
  324. if not isinstance(scale, tuple):
  325. raise TypeError("Expected a tuple")
  326. if len(scale) != 2:
  327. raise ValueError("Expected tuple of length two.")
  328. if not isinstance(scale[0], (int, float)) or not isinstance(scale[1], (int, float)):
  329. raise TypeError("Expected number values.")
  330. self._scale = scale
  331. self._updateTransformSurface()
  332. @property
  333. def keep_aspect_ratio(self):
  334. return self._keepAspectRatio
  335. @keep_aspect_ratio.setter
  336. def keep_aspect_ratio(self, keep):
  337. self._keepAspectRatio = (keep == True)
  338. self._updateTransformSurface()
  339. @property
  340. def align_center(self):
  341. return self._alignCenter
  342. @align_center.setter
  343. def align_center(self, center):
  344. self._alignCenter = (center == True)
  345. @property
  346. def scale_to_display(self):
  347. return self._scaleToDisplay
  348. @scale_to_display.setter
  349. def scale_to_display(self, todisplay):
  350. if todisplay == True:
  351. self._scaleToDisplay = True
  352. Events.listen("VIDEORESIZE", self._OnVideoResize)
  353. else:
  354. self._scaleToDisplay = False
  355. Events.unlisten("VIDEORESIZE", self._OnVideoResize)
  356. self._updateTransformSurface()
  357. def scale_to(self, target_resolution):
  358. if self._surface is not None:
  359. size = self._surface.get_size()
  360. nscale = (float(size[0]) / float(target_resolution[0]), float(size[1]) / float(target_resolution[1]))
  361. self.scale = nscale
  362. def set_surface(self, resolution=None):
  363. dsurf = Display.surface
  364. if resolution is None:
  365. if dsurf is not None:
  366. self._surface = dsurf.convert_alpha()
  367. self._surface.fill(pygame.Color(0,0,0,0))
  368. self._updateTransformSurface()
  369. else:
  370. if not isinstance(resolution, tuple):
  371. raise TypeError("Expected a tuple.")
  372. if len(resolution) != 2:
  373. raise ValueError("Expected a tuple of length two.")
  374. if not isinstance(resolution[0], int) or not isinstance(resolution[1], int):
  375. raise TypeError("Tuple expected to contain integers.")
  376. if dsurf is not None:
  377. self._surface = pygame.Surface(resolution, pygame.SRCALPHA, dsurf)
  378. else:
  379. self._surface = pygame.Surface(resolution, pygame.SRCALPHA)
  380. self._surface.fill(pygame.Color(0,0,0,0))
  381. self._updateTransformSurface()
  382. def _render(self, surface):
  383. if self._surface is None:
  384. self.set_surface()
  385. if self._surface is not None:
  386. if self._scaleDirty:
  387. self._updateTransformSurface()
  388. Node2D._render(self, self._surface)
  389. else:
  390. Node2D._render(self, surface)
  391. self._scale_and_blit(surface)
  392. def _scale_and_blit(self, dest):
  393. dsize = dest.get_size()
  394. src = self._surface
  395. if self._tsurface is not None:
  396. pygame.transform.scale(self._surface, self._tsurface.get_size(), self._tsurface)
  397. src = self._tsurface
  398. ssize = src.get_size()
  399. posx = self.position_x
  400. posy = self.position_y
  401. if self._alignCenter:
  402. if dsize[0] > ssize[0]:
  403. posx += (dsize[0] - ssize[0]) * 0.5
  404. if dsize[1] > ssize[1]:
  405. posy += (dsize[1] - ssize[1]) * 0.5
  406. pos = (int(posx), int(posy))
  407. dest.blit(src, pos)
  408. def _OnVideoResize(self, event, data):
  409. if self._scaleToDisplay:
  410. self._scaleDirty = True
  411. class NodeSprite(Node2D):
  412. def __init__(self, name="NodeSprite", parent=None):
  413. try:
  414. Node2D.__init__(self, name, parent)
  415. except NodeError as e:
  416. raise e
  417. self._NODESPRITE_DATA={
  418. "rect":[0,0,0,0],
  419. "image":"",
  420. "scale":[1.0, 1.0],
  421. "surface":None
  422. }
  423. @property
  424. def image_width(self):
  425. if self._NODESPRITE_DATA["surface"] is None:
  426. return 0
  427. surf = self._NODESPRITE_DATA["surface"]()
  428. return surf.get_width()
  429. @property
  430. def image_height(self):
  431. if self._NODESPRITE_DATA["surface"] is None:
  432. return 0
  433. surf = self._NODESPRITE_DATA["surface"]()
  434. return surf.get_height()
  435. @property
  436. def sprite_width(self):
  437. return int(self.rect_width * self.scale_x)
  438. @property
  439. def sprite_height(self):
  440. return int(self.rect_height * self.scale_y)
  441. @property
  442. def rect(self):
  443. return (self._NODESPRITE_DATA["rect"][0],
  444. self._NODESPRITE_DATA["rect"][1],
  445. self._NODESPRITE_DATA["rect"][2],
  446. self._NODESPRITE_DATA["rect"][3])
  447. @rect.setter
  448. def rect(self, rect):
  449. if not isinstance(rect, (list, tuple)):
  450. raise TypeError("Expected a list or tuple.")
  451. if len(rect) != 4:
  452. raise ValueError("rect value contains wrong number of values.")
  453. try:
  454. self.rect_x = rect[0]
  455. self.rect_y = rect[1]
  456. self.rect_width = rect[2]
  457. self.rect_height = rect[3]
  458. except Exception as e:
  459. raise e
  460. @property
  461. def rect_x(self):
  462. return self._NODESPRITE_DATA["rect"][0]
  463. @rect_x.setter
  464. def rect_x(self, v):
  465. if not isinstance(v, int):
  466. raise TypeError("Expected integer value.")
  467. self._NODESPRITE_DATA["rect"][0] = v
  468. self._NODESPRITE_ValidateRect()
  469. @property
  470. def rect_y(self):
  471. return self._NODESPRITE_DATA["rect"][1]
  472. @rect_y.setter
  473. def rect_y(self, v):
  474. if not isinstance(v, int):
  475. raise TypeError("Expected integer value.")
  476. self._NODESPRITE_DATA["rect"][1] = v
  477. self._NODESPRITE_ValidateRect()
  478. @property
  479. def rect_width(self):
  480. return self._NODESPRITE_DATA["rect"][2]
  481. @rect_width.setter
  482. def rect_width(self, v):
  483. if not isinstance(v, int):
  484. raise TypeError("Expected integer value.")
  485. self._NODESPRITE_DATA["rect"][2] = v
  486. self._NODESPRITE_ValidateRect()
  487. @property
  488. def rect_height(self):
  489. return self._NODESPRITE_DATA["rect"][3]
  490. @rect_height.setter
  491. def rect_height(self, v):
  492. if not isinstance(v, int):
  493. raise TypeError("Expected integer value.")
  494. self._NODESPRITE_DATA["rect"][3] = v
  495. self._NODESPRITE_ValidateRect()
  496. @property
  497. def center(self):
  498. r = self._NODESPRITE_DATA["rect"]
  499. return (int(r[0] + (r[2] * 0.5)), int(r[1] + (r[3] * 0.5)))
  500. @property
  501. def scale(self):
  502. return (self._NODESPRITE_DATA["scale"][0], self._NODESPRITE_DATA["scale"][1])
  503. @scale.setter
  504. def scale(self, scale):
  505. if not isinstance(scale, (list, tuple)):
  506. raise TypeError("Expected a list or tuple.")
  507. if len(scale) != 2:
  508. raise ValueError("Scale contains wrong number of values.")
  509. try:
  510. self.scale_x = scale[0]
  511. self.scale_y = scale[1]
  512. except Exception as e:
  513. raise e
  514. @property
  515. def scale_x(self):
  516. return self._NODESPRITE_DATA["scale"][0]
  517. @scale_x.setter
  518. def scale_x(self, v):
  519. if not isinstance(v, (int, float)):
  520. raise TypeError("Expected number value.")
  521. self._NODESPRITE_DATA["scale"][0] = float(v)
  522. self._NODESPRITE_DATA["scale_dirty"] = True
  523. @property
  524. def scale_y(self):
  525. return self._NODESPRITE_DATA["scale"][1]
  526. @scale_y.setter
  527. def scale_y(self, v):
  528. if not isinstance(v, (int, float)):
  529. raise TypeError("Expected number value.")
  530. self._NODESPRITE_DATA["scale"][1] = float(v)
  531. self._NODESPRITE_DATA["scale_dirty"] = True
  532. @property
  533. def image(self):
  534. return self._NODESPRITE_DATA["image"]
  535. @image.setter
  536. def image(self, src):
  537. src = src.strip()
  538. if self._NODESPRITE_DATA["image"] == src:
  539. return # Nothing to change... lol
  540. if self._NODESPRITE_DATA["image"] != "":
  541. self._NODESPRITE_DATA["surface"] = None # Clear reference to original surface.
  542. if src != "":
  543. rm = self.resource
  544. try:
  545. if not rm.has("graphic", src):
  546. rm.store("graphic", src)
  547. self._NODESPRITE_DATA["image"] = src
  548. self._NODESPRITE_DATA["surface"] = rm.get("graphic", src)
  549. if self._NODESPRITE_DATA["surface"] is None:
  550. self._NODESPRITE_DATA["image"] = ""
  551. self._NODESPRITE_DATA["rect"]=[0,0,0,0]
  552. else:
  553. # Resetting the rect to identity for the new image.
  554. surf = self._NODESPRITE_DATA["surface"]()
  555. size = surf.get_size()
  556. self._NODESPRITE_DATA["rect"]=[0,0,size[0], size[1]]
  557. except Exception as e:
  558. raise e
  559. else:
  560. self._NODESPRITE_DATA["image"] = ""
  561. self._NODESPRITE_DATA["rect"]=[0,0,0,0]
  562. def _render(self, surface):
  563. # Call the on_render() method, if any
  564. Node2D._callOnRender(self, surface)
  565. # Paint the sprite onto the surface
  566. if self._NODESPRITE_DATA["surface"] is not None:
  567. rect = self._NODESPRITE_DATA["rect"]
  568. scale = self._NODESPRITE_DATA["scale"]
  569. surf = self._NODESPRITE_DATA["surface"]()
  570. # Of course, only bother if we have a surface to work with.
  571. if surf is not None:
  572. # Do some prescaling work, if needed.
  573. if self._NODESPRITE_DATA["scale_dirty"]:
  574. self._NODESPRITE_UpdateScaleSurface(scale, surf)
  575. fsurf = surf # Initial assumption that the surface is also the "frame"
  576. # If we have a "frame" surface, however, let's get it and blit the rect into the frame surface.
  577. if "frame_surf" in self._NODESPRITE_DATA:
  578. fsurf = self._NODESPRITE_DATA["frame_surf"]
  579. fsurf.blit(surf, (0, 0), rect)
  580. # If scaling, then transform (scale) the frame surface into the scale surface and set the frame surface to the scale surface.
  581. if "scale_surf" in self._NODESPRITE_DATA:
  582. ssurf = self._NODESPRITE_DATA["scale_surf"]
  583. pygame.transform.scale(fsurf, ssurf.get_size(), ssurf)
  584. fsurf = ssurf
  585. # Place the sprite! WHEEEEE!
  586. pos = self.position
  587. surface.blit(fsurf, pos)
  588. # Call on all children
  589. Node._render(self, surface)
  590. def _NODESPRITE_UpdateScaleSurface(self, scale, surf):
  591. self._NODESPRITE_DATA["scale_dirty"] = False
  592. ssurf = None
  593. if "scale_surf" in self._NODESPRITE_DATA:
  594. ssurf = self._NODESPRITE_DATA["scale_surf"]
  595. if scale[0] == 1.0 and scale[1] == 1.0:
  596. if ssurf is not None:
  597. del self._NODESPRITE_DATA["scale_surf"]
  598. return
  599. nw = int(self.rect_width * scale[0])
  600. nh = int(self.rect_height * scale[1])
  601. if nw != ssurf.get_width() or nh != ssurf.get_height():
  602. ssurf = pygame.Surface((nw, nh), pygame.SRCALPHA, surf)
  603. ssurf.fill(pygame.Color(0,0,0,0))
  604. self._NODESPRITE_DATA["scale_surf"] = ssurf
  605. def _NODESPRITE_ValidateRect(self):
  606. if self._NODESPRITE_DATA["surface"] is None:
  607. self._NODESPRITE_DATA["rect"] = [0,0,0,0]
  608. else:
  609. rect = self._NODESPRITE_DATA["rect"]
  610. isize = (self.image_width, self.image_height)
  611. if rect[0] < 0:
  612. rect[2] += rect[0]
  613. rect[0] = 0
  614. elif rect[0] >= isize[0]:
  615. rect[0] = isize[0]-1
  616. rect[2] = 0
  617. if rect[1] < 0:
  618. rect[3] += rect[1]
  619. rect[1] = 0
  620. elif rect[1] >= isize[1]:
  621. rect[1] = isize[1]-1
  622. rect[3] = 0
  623. if rect[2] < 0:
  624. rect[2] = 0
  625. elif rect[0] + rect[2] > isize[0]:
  626. rect[2] = isize[0] - rect[0]
  627. if rect[3] < 0:
  628. rect[3] = 0
  629. elif rect[1] + rect[3] > isize[1]:
  630. rect[3] = isize[1] - rect[1]
  631. fssize = [0,0]
  632. if rect[2] > 0 and rect[3] > 0:
  633. if rect[2] < isize[0] or rect[1] < isize[1]:
  634. surf = self._NODESPRITE_DATA["surface"]()
  635. self._NODESPRITE_DATA["frame_surf"] = pygame.Surface((rect[2], rect[3]), pygame.SRCALPHA, surf)
  636. if fssize[0] > 0 and fssize[1] > 0:
  637. pass
  638. elif "frame_surf" in self._NODESPRITE_DATA:
  639. del self._NODESPRITE_DATA["frame_surf"]