diff --git a/.idea/PixelEditor.iml b/.idea/PixelEditor.iml index 686892a..9710855 100644 --- a/.idea/PixelEditor.iml +++ b/.idea/PixelEditor.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 110ffc2..65531ca 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/src/Canvas.py b/src/Canvas.py index 944d8df..6b52957 100644 --- a/src/Canvas.py +++ b/src/Canvas.py @@ -47,3 +47,45 @@ def Empty(wid, hei, color=(0, 0, 0, 0)): _canvas.frame = [Sprite.Empty(wid, hei, color)] _canvas.w, _canvas.h = wid, hei return _canvas + + +class CanvasV2(pg.Rect): + magnification = 10 + + frame = [] + currentFrame = 0 + + def SetRect(self, x, y, w, h): + self.x = x + self.y = y + self.w = w + self.h = h + + def Move(self, dx, dy): + self.move_ip(dx, dy) + + def CurrentFrame(self): + return self.frame[self.currentFrame] + + def Draw(self, surface): + surface.blit(pg.transform.scale(self.CurrentFrame().GetSurface(), + (self.w * self.magnification, self.h * self.magnification)), + (self.x, self.y)) + + def GetSurface(self): + return pg.transform.scale(self.CurrentFrame().GetSurface(), + (self.w * self.magnification, self.h * self.magnification)) + + def ScreenSpaceResolution(self): + # _xs, _xe = max(self.x, 0), min(self.x + self.w * self.magnification, xLimit) + # _ys, _ye = max(self.y, 0), min(self.y + self.h * self.magnification, yLimit) + # + # return (_xs, _xe), (_ys, _ye) + return self.x, self.y, self.w * self.magnification, self.h * self.magnification + + @staticmethod + def Empty(wid, hei, color=(0, 0, 0, 0)): + _canvas = Canvas() + _canvas.frame = [Sprite.Empty(wid, hei, color)] + _canvas.w, _canvas.h = wid, hei + return _canvas diff --git a/src/Section.py b/src/Section.py index ac4f0e5..4e841b5 100644 --- a/src/Section.py +++ b/src/Section.py @@ -19,6 +19,8 @@ def __init__(self, x, y, w, h, color): self.surface.fill(self.bgColor) self.sub = [] + self._hasChange = True + def OnClicked(self, button, x, y): if not self.rect.collidepoint(x, y): return @@ -26,24 +28,28 @@ def OnClicked(self, button, x, y): for sub in self.sub: sub.OnClicked(button, x, y) + def Changed(self): + self._hasChange = True + def Draw(self, screen): - self.Update() - screen.blit(self.surface, (self.x, self.y)) - pg.draw.rect(screen, OUTLINE_COLOR, self.rect, 3) + if self._hasChange: + self.Update() + screen.blit(self.surface, (self.x, self.y)) + pg.draw.rect(screen, OUTLINE_COLOR, self.rect, 3) + self._hasChange = False def LocalPosition(self, position): _x, _y = position return _x - self.x, _y - self.y def Update(self): - raise NotImplementedError() + self.surface.fill(self.bgColor) class CanvasSection(Section): - canvasX, canvasY = 0, 0 - canvasWidth, canvasHeight = 20, 15 magnification = 10 - canvasRect = pg.Rect(canvasX, canvasY, canvasWidth * magnification, canvasHeight * magnification) + canvasWidth, canvasHeight = 32, 32 + canvas = pg.Rect(0, 0, canvasWidth * magnification, canvasHeight * magnification) # ----- for test ----- @@ -56,32 +62,36 @@ def __init__(self, x, y, w, h, color): self.bgImage = pg.image.load('data/TransparentBG.png') def MoveCanvas(self, dx, dy): - self.canvasX += dx - self.canvasY += dy - self.canvasRect.move_ip(dx, dy) + self.canvas.move_ip(dx, dy) + self.Changed() def Magnify(self, mag, pivot): if self.magnification + mag < 1: return p_x, p_y = pivot - dx = (self.canvasX - p_x) / self.magnification - dy = (self.canvasY - p_y) / self.magnification + dx = (self.canvas.x - p_x) / self.magnification + dy = (self.canvas.y - p_y) / self.magnification self.magnification += mag new_dx = round(dx * self.magnification) new_dy = round(dy * self.magnification) - self.canvasX = new_dx + p_x - self.canvasY = new_dy + p_y - self.canvasRect.w = self.canvasWidth * self.magnification - self.canvasRect.h = self.canvasHeight * self.magnification + self.canvas.x = new_dx + p_x + self.canvas.y = new_dy + p_y + self.canvas.w = self.canvasWidth * self.magnification + self.canvas.h = self.canvasHeight * self.magnification + self.Changed() def Update(self): self.surface.fill(self.bgColor) - self.surface.blit(self.bgImage, (self.canvasX, self.canvasY), self.canvasRect) + self.surface.blit(self.bgImage, (self.canvas.x, self.canvas.y), self.canvas) def OnClicked(self, button, x, y): - if self.canvasRect.collidepoint(x, y): - _clickedPixelX = (x - self.canvasX) // self.magnification - _clickedPixelY = (y - self.canvasY) // self.magnification + if self.canvas.collidepoint(x, y): + _clickedPixelX = (x - self.canvas.x) // self.magnification + _clickedPixelY = (y - self.canvas.y) // self.magnification self.pencilBrush.OnMouseDown((_clickedPixelX, _clickedPixelY)) +class UISection(Section): + pass + + diff --git a/src/Window.py b/src/Window.py index 96dd0e6..24db129 100644 --- a/src/Window.py +++ b/src/Window.py @@ -1,23 +1,23 @@ import pygame as pg from pygame.locals import * -from src.Section import CanvasSection +from src.Section import CanvasSection, UISection from src import utility class MainWindow: - BG_COLOR = (43, 43, 43) + UI_BG_COLOR = (43, 43, 43) CANVAS_BG_COLOR = (60, 63, 65) CANVAS_BG_COLOR_int = utility.RGBA2INT(CANVAS_BG_COLOR) MOVE_SPEED = 1 - w, h = 1280, 720 + w, h = 1280, 960 running = False screen = None clock = pg.time.Clock() fps = 125 - canvasRect = pg.Rect(320, 0, 960, 960) canvasSection = CanvasSection(320, 0, 960, 960, CANVAS_BG_COLOR) + uiSection = UISection(0, 0, 320, 960, UI_BG_COLOR) # ----- for test ----- # sprite = Canvas.Empty(20, 15, (0, 0, 0, 255)) @@ -43,9 +43,9 @@ def MainLoop(self): self.LateFeedback() - self.screen.fill(self.BG_COLOR) # ----- for test ----- self.canvasSection.Draw(self.screen) + self.uiSection.Draw(self.screen) pg.display.update() self.clock.tick(self.fps) @@ -74,7 +74,6 @@ def EventFeedback(self, event): self.canvasSection.Magnify(1, self.canvasSection.LocalPosition((self.mouseX, self.mouseY))) elif event.button == 5: - pass self.canvasSection.Magnify(-1, self.canvasSection.LocalPosition((self.mouseX, self.mouseY))) elif event.type == MOUSEBUTTONUP: