diff --git a/.idea/PixelEditor.iml b/.idea/PixelEditor.iml index 9710855..686892a 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 65531ca..110ffc2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/src/Brush.py b/src/Brush.py index 3721f65..2ceb72e 100644 --- a/src/Brush.py +++ b/src/Brush.py @@ -2,54 +2,80 @@ from src.Layer import Layer -class Brush: - _layer = None - - def SetCurrentLayer(self, layer: Layer): - self._layer = layer - - class _Brush: currentColor = (0, 0, 0, 0) - _layer = None - - def OnMouseDown(self, clickedPixel): - raise NotImplementedError - - def OnDrag(self): + def OnMouseDown(self, clickedPixel, layer: Layer): pass - def OnMouseDrag(self, clickedPixel): + def OnMouseDrag(self, clickedPixel, layer: Layer): pass - def SetCurrentLayer(self, layer): - if isinstance(layer, Layer): - self._layer = layer + def OnMouseUp(self, clickedPixel, layer: Layer): + pass def SetCurrentColor(self, color): self.currentColor = color -class PencilBrush(_Brush): - def OnMouseDown(self, clickedPixel): - self._layer.SetPixel(*clickedPixel, self.currentColor) +class _PencilBrush(_Brush): + def OnMouseDown(self, clickedPixel, layer: Layer): + layer.SetPixel(*clickedPixel, self.currentColor) - def OnMouseDrag(self, clickedPixel): - self._layer.SetPixel(*clickedPixel, self.currentColor) + def OnMouseDrag(self, clickedPixel, layer: Layer): + layer.SetPixel(*clickedPixel, self.currentColor) -class PickerBrush(_Brush): - def OnMouseDown(self, clickedPixel): - self.currentColor = self._layer.GetPixel(*clickedPixel) +class _EraserBrush(_Brush): + def OnMouseDown(self, clickedPixel, layer: Layer): + layer.SetPixel(*clickedPixel, self.currentColor) + + def OnMouseDrag(self, clickedPixel, layer: Layer): + layer.SetPixel(*clickedPixel, self.currentColor) + + +class _FloodBrush(_Brush): + def OnMouseDown(self, clickedPixel, layer: Layer): + pass + + +class _PickerBrush(_Brush): + def OnMouseDown(self, clickedPixel, layer: Layer): + self.currentColor = layer.GetPixel(*clickedPixel) return self.currentColor -class FloodBrush(_Brush): +class Brush: + _layer = None + + pencil = _PencilBrush() + eraser = _EraserBrush() + flood = _FloodBrush() + picker = _PickerBrush() + + _currentBrush: _Brush + def OnMouseDown(self, clickedPixel): - pass + self._currentBrush.OnMouseDown(clickedPixel, self._layer) + + def OnMouseDrag(self, clickedPixel): + self._currentBrush.OnMouseDrag(clickedPixel, self._layer) + + def OnMouseUp(self, clickedPixel): + self._currentBrush.OnMouseUp(clickedPixel, self._layer) + + def SetBrush(self, brush): + if brush == 'Pencil' or brush == 0: + self._currentBrush = self.pencil + elif brush == 'Eraser' or brush == 1: + self._currentBrush = self.eraser + elif brush == 'Flood' or brush == 2: + self._currentBrush = self.flood + elif brush == 'Picker' or brush == 3: + self._currentBrush = self.picker + + def SetCurrentLayer(self, layer: Layer): + self._layer = layer Brush = Brush() -PencilBrush = PencilBrush() -FloodBrush = FloodBrush() diff --git a/src/Section.py b/src/Section.py index 84cdeda..e7c8f3b 100644 --- a/src/Section.py +++ b/src/Section.py @@ -1,6 +1,6 @@ import pygame as pg from src.Sprite import Sprite -from src.Brush import PencilBrush +from src.Brush import Brush from math import ceil, floor @@ -99,10 +99,12 @@ def Update(self): ) def OnClicked(self, button, x, y): + print(x, y) if self.canvas.collidepoint(x, y): - _clickedPixelX = (x - self.canvas.x) // self.magnification - _clickedPixelY = (y - self.canvas.y) // self.magnification - PencilBrush.OnMouseDown((_clickedPixelX, _clickedPixelY)) + _localX, _localY = self.LocalPosition((x, y)) + _pixelX = _localX // self.magnification + _pixelY = _localY // self.magnification + Brush.OnMouseDown((_pixelX, _pixelY)) class UISection(Section): diff --git a/src/Window.py b/src/Window.py index 3eaec38..9ff1703 100644 --- a/src/Window.py +++ b/src/Window.py @@ -1,6 +1,7 @@ import pygame as pg from pygame.locals import * from src.Section import CanvasSection, UISection +from src.Brush import Brush class MainWindow: @@ -24,6 +25,11 @@ class MainWindow: mouseX, mouseY = 0, 0 _mousePreviousX, _mousePreviousY = 0, 0 + Brush.SetBrush('Pencil') + # ----- for test ----- + Brush.pencil.SetCurrentColor((255, 255, 255, 255)) + + def Run(self): pg.init() self.screen = pg.display.set_mode((self.w, self.h), SRCALPHA, 32) @@ -61,6 +67,9 @@ def EventFeedback(self, event): if event.button == 1: self.mouseButtonDown[0] = 1 + # ----- for test ----- + CanvasSection.OnClicked(1, self.mouseX, self.mouseY) + elif event.button == 2: self.mouseButtonDown[1] = 1 self._mousePreviousX, self._mousePreviousY = self.mouseX, self.mouseY