Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
Thxios committed Mar 6, 2020
1 parent 9c1e17c commit 53efba3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 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.

42 changes: 42 additions & 0 deletions src/Canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 30 additions & 20 deletions src/Section.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,37 @@ 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

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 -----

Expand All @@ -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


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

0 comments on commit 53efba3

Please sign in to comment.