Skip to content

Commit

Permalink
Canvas class integrated into CanvasSection class
Browse files Browse the repository at this point in the history
  • Loading branch information
Thxios committed Mar 6, 2020
1 parent 478804d commit cb8e889
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .idea/PixelEditor.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 55 additions & 29 deletions src/Brush.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
10 changes: 6 additions & 4 deletions src/Section.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions src/Window.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cb8e889

Please sign in to comment.