editor pixel test
authorFrank DeMarco <if.self.end@gmail.com>
Fri, 15 Feb 2019 01:18:37 +0000 (20:18 -0500)
committerFrank DeMarco <if.self.end@gmail.com>
Fri, 15 Feb 2019 01:18:37 +0000 (20:18 -0500)
PictureProcessing.py
config
lib/pgfw
resource/progress

index ea70be0..57049d1 100644 (file)
@@ -4,7 +4,7 @@ from glob import glob
 from array import array
 from sys import argv
 
-from pygame import PixelArray, Surface, Color, joystick, version, mixer
+from pygame import PixelArray, Surface, Color, joystick, version, mixer, draw
 from pygame.event import clear, Event
 from pygame.image import load, save
 from pygame.transform import flip, scale
@@ -15,6 +15,7 @@ from lib.pgfw.pgfw.Game import Game
 from lib.pgfw.pgfw.GameChild import GameChild
 from lib.pgfw.pgfw.Animation import Animation
 from lib.pgfw.pgfw.Sprite import Sprite, RainbowSprite
+from lib.pgfw.pgfw.Vector import Vector
 from lib.pgfw.pgfw.extension import (get_color_swapped_surface, get_delta,
                                      get_busy_channel_count)
 
@@ -35,6 +36,7 @@ class PictureProcessing(Game):
         self.title = Title(self)
         self.introduction = Introduction(self)
         self.ending = Ending(self)
+        self.editor = Editor(self)
         self.subscribe(self.respond)
         self.subscribe(self.respond, MOUSEBUTTONDOWN)
         self.reset()
@@ -62,6 +64,11 @@ class PictureProcessing(Game):
            (self.get_configuration("mouse", "resets") and event.type == MOUSEBUTTONDOWN and
             (1 <= event.button <= 3)):
             self.reset()
+        elif self.delegate.compare(event, "context"):
+            if not self.editor.active:
+                self.editor.activate()
+            else:
+                self.editor.deactivate()
 
     def reset(self):
         self.title.reset()
@@ -69,6 +76,7 @@ class PictureProcessing(Game):
         self.interface.reset()
         self.introduction.reset()
         self.ending.reset()
+        self.editor.reset()
 
     def get_progress(self):
         return map(int, open(self.get_resource("progress")).read().split())
@@ -86,11 +94,84 @@ class PictureProcessing(Game):
         return not self.check_command_line("-keyboard") and joystick.get_count()
 
     def update(self):
-        self.introduction.update()
-        self.title.update()
-        self.levels.update()
-        self.interface.update()
-        self.ending.update()
+        if self.editor.active:
+            self.editor.update()
+        else:
+            self.introduction.update()
+            self.title.update()
+            self.levels.update()
+            self.interface.update()
+            self.ending.update()
+
+
+class Editor(GameChild):
+
+    TILE_SIZE = 8
+    PIXEL_ZOOM = 32
+    CURSOR_THICKNESS = 3
+
+    def __init__(self, parent):
+        GameChild.__init__(self, parent)
+        self.brush_position = Vector()
+        self.subscribe(self.respond)
+        self.painting = []
+        for _ in xrange(self.TILE_SIZE):
+            self.painting.append([0] * self.TILE_SIZE)
+
+    def reset(self):
+        self.deactivate()
+
+    def activate(self):
+        self.active = True
+
+    def deactivate(self):
+        self.active = False
+
+    def respond(self, event):
+        if self.active:
+            compare = self.get_delegate().compare
+            dx, dy = 0, 0
+            if compare(event, "right"):
+                dx = 1
+            elif compare(event, "down"):
+                dy = 1
+            elif compare(event, "left"):
+                dx = -1
+            elif compare(event, "up"):
+                dy = -1
+            elif compare(event, "paint"):
+                self.painting[self.brush_position.x][self.brush_position.y] = \
+                    not self.painting[self.brush_position.x][self.brush_position.y]
+            if dx != 0 or dy != 0:
+                self.move_cursor(dx, dy)
+
+    def move_cursor(self, dx, dy):
+        self.brush_position.move(dx, dy)
+        if self.brush_position.x >= self.TILE_SIZE:
+            self.brush_position.x = self.TILE_SIZE - 1
+        elif self.brush_position.x < 0:
+            self.brush_position.x = 0
+        if self.brush_position.y >= self.TILE_SIZE:
+            self.brush_position.y = self.TILE_SIZE - 1
+        elif self.brush_position.y < 0:
+            self.brush_position.y = 0
+
+    def update(self):
+        if self.active:
+            ds = self.get_display_surface()
+            ds.fill((0, 0, 0))
+            pz = self.PIXEL_ZOOM
+            for x, row in enumerate(self.painting):
+                for y, col in enumerate(row):
+                    if col == 0:
+                        color = (0, 0, 0)
+                    else:
+                        color = (255, 255, 255)
+                    ds.fill(color, (x * pz, y * pz, pz, pz))
+            bx, by = self.brush_position
+            cr = bx * pz, by * pz, pz, pz
+            draw.rect(ds, (255, 255, 255), cr, self.CURSOR_THICKNESS)
+            draw.rect(ds, (0, 0, 0), cr, 1)
 
 
 class SoundEffects(GameChild):
diff --git a/config b/config
index a7b4e02..be2aec0 100644 (file)
--- a/config
+++ b/config
@@ -17,6 +17,8 @@ show-framerate = no
 cancel = K_ESCAPE
 action = K_RETURN
 toggle = K_TAB
+context = K_F3
+paint = K_z
 
 [joy]
 advance = 9
index 498755e..717b2eb 160000 (submodule)
--- a/lib/pgfw
+++ b/lib/pgfw
@@ -1 +1 @@
-Subproject commit 498755ef0e4cadfe3784363e5eb0ec1aff016631
+Subproject commit 717b2eb6860cc1a4804bc258a0b07c5013caf3d5
index 32ac9cc..b748e2d 100644 (file)
@@ -1 +1 @@
-1 6
+0 0