From a9749910488c5dc6803b74954964603994bd32a0 Mon Sep 17 00:00:00 2001 From: themixray <35273590+themixray@users.noreply.github.com> Date: Wed, 3 Nov 2021 16:46:56 +0300 Subject: [PATCH] Add files via upload --- pygwin/__init__.py | 1 + pygwin/gamepad.py | 3 +- pygwin/rect.py | 5 +- pygwin/surface.py | 41 ++--- pygwin/ui.py | 392 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 420 insertions(+), 22 deletions(-) create mode 100644 pygwin/ui.py diff --git a/pygwin/__init__.py b/pygwin/__init__.py index 2d12791..0302483 100644 --- a/pygwin/__init__.py +++ b/pygwin/__init__.py @@ -9,4 +9,5 @@ from pygame.locals import * from pygwin.font import * from pygwin._win import * from pygwin._pg import pg +from pygwin.ui import * gamepad = _gp.gamepad(pg) diff --git a/pygwin/gamepad.py b/pygwin/gamepad.py index 597c065..7f585ec 100644 --- a/pygwin/gamepad.py +++ b/pygwin/gamepad.py @@ -29,7 +29,8 @@ class gamepad: self._start() def _tick(self): events = _inputs.get_gamepad() - if not self._pygame.display.get_active():return + if not self._pygame.display.get_active(): + return self.founded = True if events: for event in events: diff --git a/pygwin/rect.py b/pygwin/rect.py index a5a2503..ab8892b 100644 --- a/pygwin/rect.py +++ b/pygwin/rect.py @@ -6,6 +6,7 @@ class rect: self.y = y self.w = w self.h = h + self._rect = pg.Rect(x,y,w,h) def width(): def fget(self): return self.w @@ -25,6 +26,6 @@ class rect: return locals() height = property(**height()) def collide(self, x): - return pg.Rect(self.x,self.y,self.w,self.h).colliderect(pg.Rect(x.x,x.y,x.w,x.h)) + return self._rect.colliderect(x._rect_rect) def contains(self, x, y): - return pg.Rect(self.x,self.y,self.w,self.h).collidepoint(x,y) + return self._rect.collidepoint(x,y) diff --git a/pygwin/surface.py b/pygwin/surface.py index 6d55228..b7dc1cc 100644 --- a/pygwin/surface.py +++ b/pygwin/surface.py @@ -5,7 +5,6 @@ class surface: def __init__(self, size): self._size = size self._orig = _pg.Surface(size, _pg.SRCALPHA) - self._pixels = [] @property def pixels(self): pixels = [] @@ -26,8 +25,9 @@ class surface: center[1]-(self.size[1]/2), self.size[0], self.size[1]) def copy(self): - surf = surface(self.size) + surf = surface(self._size) surf._surface_orig = self._orig + surf._surface_size = self._size return surf def getPixel(self, x, y): return self._orig.get_at((x,y)) @@ -38,7 +38,10 @@ class surface: if type(surf) != surface: from pygwin.font import defaultFont as _df surf = _df.render(surf, 25, (0,0,0)) - self._orig.blit(surf._surface_orig, xy) + try: + self._orig.blit(surf._surface_orig, xy) + except: + self._orig.blit(surf._orig, xy) return self.copy() def fill(self, color): self._orig.fill(color) @@ -75,9 +78,9 @@ class surface: borderTopRightRadius=-1, borderBottomLeftRadius=-1, borderBottomRightRadius=-1): - if type(self._surf) == surface: + try: orig = self._surf._surface_orig - else: + except: orig = self._surf._orig _pg.draw.rect(orig,color,_pg.Rect(rect.x,rect.y,rect.w,rect.h), width,borderRadius,borderTopLeftRadius, @@ -85,44 +88,44 @@ class surface: borderBottomRightRadius) return self._surf.copy() def polygon(self, color, points, width=0): - if type(self._surf) == surface: + try: orig = self._surf._surface_orig - else: + except: orig = self._surf._orig _pg.draw.polygon(orig,color,points,width) return self._surf.copy() def circle(self,color,center, radius,width=0, - drawTopLeft=None, - drawTopRight=None, - drawBottomLeft=None, - drawBottomRight=None): - if type(self._surf) == surface: + drawTopLeft=1, + drawTopRight=1, + drawBottomLeft=1, + drawBottomRight=1): + try: orig = self._surf._surface_orig - else: + except: orig = self._surf._orig _pg.draw.circle(orig,color,center,radius, width,drawTopRight,drawTopLeft, drawBottomLeft,drawBottomRight) return self._surf.copy() def ellipse(self,color,rect,width=0): - if type(self._surf) == surface: + try: orig = self._surf._surface_orig - else: + except: orig = self._surf._orig _pg.draw.ellipse(orig,color,_pg.Rect(rect.x,rect.y,rect.w,rect.h),width) return self._surf.copy() def line(self,color,start,end,width=1): - if type(self._surf) == surface: + try: orig = self._surf._surface_orig - else: + except: orig = self._surf._orig _pg.draw.line(orig,color,start,end,width) return self._surf.copy() def arc(self,color,rect,startAngle,stopAngle,width=1): - if type(self._surf) == surface: + try: orig = self._surf._surface_orig - else: + except: orig = self._surf._orig _pg.draw.arc(orig,color, _pg.Rect(rect.x,rect.y,rect.w,rect.h), diff --git a/pygwin/ui.py b/pygwin/ui.py new file mode 100644 index 0000000..d320700 --- /dev/null +++ b/pygwin/ui.py @@ -0,0 +1,392 @@ +from pygwin._pg import pg as _pg +from pygwin.surface import surface as _s +from pygwin.font import defaultFont as _df +from pygwin.image import load as _l +from pygwin.rect import rect as _r +import pygwin.mouse as _m +import pygwin.keyboard as _k +import ctypes as _ct + +class widget: + power = True + destroyed = False + def __init__(self, surface): + self.surface = surface + def draw(self, win, pos): + win.blit(self.surface,pos) + def on(self): + self.power = True + def off(self): + self.power = False + def destroy(self): + self.destroyed = True +class button(widget): + def __init__(self,text, + func=lambda:None, + fontSize=30,font=_df, + width=None,height=None, + bg=(70,70,70),fg=(180,180,200), + afg=(50,50,50),abg=(200,200,200), + borderColor=(50,50,50),borderWidth=5): + self.text = text + self.fontSize = fontSize + self.font = font + self.width = width + self.height = height + self.bg = bg + self.fg = fg + self.afg = afg + self.abg = abg + self.func = func + self.cl0 = False + self.cl1 = False + self.nc0 = True + self.borderColor = borderColor + self.borderWidth = borderWidth + self._generate() + def _generate(self, position=None): + if self.width == None or self.height == None: + textSize = self.font.size(self.text,self.fontSize) + if self.width != None: + self.surface = _s((self.width,textSize[1]+10)) + elif self.height != None: + self.surface = _s((textSize[0]+50,self.height)) + else: + self.surface = _s((textSize[0]+50,textSize[1]+10)) + else: + self.surface = _s((self.width,self.height)) + if position != None: + contains = self.surface.rect(position[0], position[1]).contains( + _m.getPosition()[0], _m.getPosition()[1]) + cacm = contains and _m.isPressed('left') + else: + contains = False + cacm = False + if contains and not self.cl0: + _m.setCursor(_pg.SYSTEM_CURSOR_HAND) + self.cl0 = True + self.nc0 = True + elif not contains: + if self.nc0: + _m.setCursor(_pg.SYSTEM_CURSOR_ARROW) + self.nc0 = False + self.cl0 = False + if cacm and not self.cl1: + self.func() + self.cl1 = True + elif not cacm: + self.cl1 = False + self.surface.fill(self.borderColor) + if cacm: + self.surface.draw.rect(self.abg,_r(self.borderWidth,self.borderWidth, + self.surface.size[0]-self.borderWidth*2, + self.surface.size[1]-self.borderWidth*2)) + else: + self.surface.draw.rect(self.bg,_r(self.borderWidth,self.borderWidth, + self.surface.size[0]-self.borderWidth*2, + self.surface.size[1]-self.borderWidth*2)) + if cacm: + text = self.font.render(self.text,self.fontSize,self.afg) + else: + text = self.font.render(self.text,self.fontSize,self.fg) + pos = text.rect(center=( + self.surface.size[0]/2, + self.surface.size[1]/2)) + pos = [pos.x, pos.y] + self.surface.blit(text,pos) + def draw(self, win, pos): + self._generate(pos) + win.blit(self.surface,pos) +class label(widget): + def __init__(self,text,size=30, + color=(0,0,0),font=_df): + self.surface = font.render(text,size,color) +class entry(widget): + def __init__(self,hint='',fontSize=30,font=_df, + bg=(70,70,70),fg=(180,180,200), + afg=(200,200,200),abg=(50,50,50), + hintColor=(100,100,100), + lineColor=(200,200,200), + borderColor=(50,50,50), + borderWidth=5, + width=None,height=None): + self.text = '' + self.focus = False + self.tick = 0 + self.hint = hint + self.fontSize = fontSize + self.font = font + self.bg = bg + self.fg = fg + self.abg = abg + self.afg = afg + self.width = width + self.height = height + self.wcl = False + self.startHint = self.hint + self.hintColor = hintColor + self.lineColor = lineColor + self.borderColor = borderColor + self.borderWidth = borderWidth + self.ws = False + if self.width == None or self.height == None: + if self.hint != '': + hintSize = self.font.size(self.hint,self.fontSize) + else: + hintSize = (150,self.font.size('X',self.fontSize)[1]) + if self.height == None: + self.height = hintSize[1]+10 + if self.width == None: + self.width = hintSize[0]+50 + self.surface = _s((self.width,self.height)) + self.wclk = [] + def get(self): + return text + def _generate(self,position=None): + self.surface.fill(self.borderColor) + if self.focus: + self.surface.draw.rect(self.abg,_r(self.borderWidth,self.borderWidth, + self.surface.size[0]-self.borderWidth*2, + self.surface.size[1]-self.borderWidth*2)) + if self.text == '': + text = self.font.render(self.hint,self.fontSize,self.hintColor) + else: + text = self.font.render(self.text,self.fontSize,self.afg) + x = 10 + if text.size[0] >= self.surface.size[0]-20: + x = self.surface.size[0]-text.size[0]-10 + self.surface.blit(text,(x,self.surface.size[1]/2-text.size[1]/2)) + for i in _k.getPressed().items(): + if i[1]: + if i[0] not in self.wclk: + if len(i[0]) == 1: + self.insert(i[0]) + elif i[0] == 'backspace': + self.delete() + elif i[0] == 'enter': + self.focus = False + elif i[0] == 'space': + self.insert(' ') + self.wclk.append(i[0]) + else: + if i[0] in self.wclk: + self.wclk.remove(i[0]) + self.tick += 1 + if self.tick >= 60: + if self.text != '': + points = [[x+text.size[0],self.surface.size[1]/2-text.size[1]/2], + [x+text.size[0],self.surface.size[1]/2-text.size[1]/2+self.surface.size[1]-10]] + self.surface.draw.line(self.lineColor,points[0],points[1],3) + if self.tick == 120: + self.tick = 0 + else: + self.surface.draw.rect(self.bg,_r(self.borderWidth,self.borderWidth, + self.surface.size[0]-self.borderWidth*2, + self.surface.size[1]-self.borderWidth*2)) + if self.text == '': + text = self.font.render(self.hint,self.fontSize,self.hintColor) + else: + text = self.font.render(self.text,self.fontSize,self.fg) + x = self.surface.size[0]/2-text.size[0]/2 + if text.size[0] >= self.surface.size[0]-20: + x = self.surface.size[0]-text.size[0]-10 + self.surface.blit(text,(x,self.surface.size[1]/2-text.size[1]/2)) + + if position != None: + if self.surface.rect(position[0], + position[1]).contains(_m.getPosition()[0], + _m.getPosition()[1]): + if not self.wcl: + _m.setCursor(_pg.SYSTEM_CURSOR_HAND) + else: + if not self.ws: + _m.setCursor(_pg.SYSTEM_CURSOR_ARROW) + self.ws = True + if _m.isPressed('left'): + if not self.wcl: + self.focus=self.focus==0 + self.wcl = True + else: + self.wcl = False + else: + _m.setCursor(_pg.SYSTEM_CURSOR_ARROW) + if _m.isPressed('left'): + self.focus = False + def insert(self,text): + if _ct.WinDLL("User32.dll").GetKeyState(0x14): + text = text.upper() + if hex(getattr(_ct.windll.LoadLibrary("user32.dll"), "GetKeyboardLayout")(0))=='0x4190419': + text = text.translate(dict(zip(map(ord, '''qwertyuiop[]asdfghjkl;'zxcvbnm,./`QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?~'''), + '''йцукенгшщзхъфывапролджэячсмитьбю.ёЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,Ё'''))) + self.text += text + def delete(self,symbols=1): + self.text = self.text[:0-symbols] + def draw(self, win, pos): + self._generate(pos) + win.blit(self.surface,pos) +class image(widget): + def __init__(self, path): + self.surface = _l(path) +class loadingBar(widget): + def __init__(self,width, + height=50, + length=100, + bg=(70,70,70), + loadedColor=(50,200,50), + borderColor=(50,50,50), + borderWidth=5): + self.surface = _s((width,height)) + self.surface.fill(bg) + self.length = length + self.bg = bg + self.loadedColor = loadedColor + self.loaded = 0 + self.borderColor = borderColor + self.borderWidth = borderWidth + def step(self,count=1): + self.loaded += 1 + if self.loaded > self.length: + self.loaded = self.length + def reset(self): + self.loaded = 0 + def draw(self, win, pos): + self.surface.fill(self.borderColor) + self.surface.draw.rect(self.bg,_r(5,5, + self.surface.size[0]-10, + self.surface.size[1]-10)) + self.surface.draw.rect(self.loadedColor,_r(self.borderWidth,self.borderWidth, + (self.surface.size[0]/self.length*self.loaded)-self.borderWidth*2, + self.surface.size[1]-self.borderWidth*2)) + win.blit(self.surface, pos) +class scrollBar(widget): + def __init__(self,width, + bg=(70,70,70), + fg=(50,200,50), + horizontal=True): + self.horizontal = horizontal + self.width = width + self.bg = bg + self.fg = fg + self.x = 0 + self._generate(None) + def _generate(self, pos): + if self.horizontal: + self.surface = _s((self.width,50)) + self.surface.draw.line(self.bg,[5,25],[self.width-5,25],10) + self.surface.draw.circle(self.bg,[5,26],5) + self.surface.draw.circle(self.bg,[self.width-5,26],5) + self.surface.draw.line(self.fg,[self.x+5,11],[self.x+5,39],6) + else: + self.surface = _s((50,self.width)) + self.surface.draw.line(self.bg,[25,5],[25,self.width-5],10) + self.surface.draw.circle(self.bg,[26,5],5) + self.surface.draw.circle(self.bg,[26,self.width-5],5) + self.surface.draw.line(self.fg,[11,self.x+5],[39,self.x+5],6) + if pos != None: + if _m.isPressed('left'): + if self.horizontal: + rect = _r(pos[0]+5,pos[1], + self.surface.size[0]-10, + self.surface.size[1]) + if rect.contains(_m.getPosition()[0], + _m.getPosition()[1]): + self.x = _m.getPosition()[0]-pos[0]-5 + else: + rect = _r(pos[0],pos[1]+5, + self.surface.size[0], + self.surface.size[1]-10) + if rect.contains(_m.getPosition()[0], + _m.getPosition()[1]): + self.x = _m.getPosition()[1]-pos[1]-5 + def get(self): + return int(self.x/(self.width-10)*101) + def set(self, x): + self.x = x/101*(self.width-10) + def draw(self, win, pos): + self._generate(pos) + win.blit(self.surface, pos) +class checkBox(widget): + def __init__(self,width=50,bg=(180,180,180), + fg=(50,180,50),afg=(70,200,70), + abg=(120,120,120),borderColor=(220,220,220), + borderWidth=5): + self.width = width + self.bg = bg + self.fg = fg + self.afg = afg + self.abg = abg + self.cl0 = False + self.cl1 = False + self.nc0 = True + self.x = False + self.borderColor = borderColor + self.borderWidth = borderWidth + self._generate() + def set(self, x): + self.x = x + def get(self): + return self.x + def _generate(self, position=None): + self.surface = _s((self.width,self.width)) + if position != None: + contains = self.surface.rect(position[0], position[1]).contains( + _m.getPosition()[0], _m.getPosition()[1]) + cacm = contains and _m.isPressed('left') + else: + contains = False + cacm = False + if contains and not self.cl0: + _m.setCursor(_pg.SYSTEM_CURSOR_HAND) + self.cl0 = True + self.nc0 = True + elif not contains: + if self.nc0: + _m.setCursor(_pg.SYSTEM_CURSOR_ARROW) + self.nc0 = False + self.cl0 = False + if cacm and not self.cl1: + self.x=self.x==0 + self.cl1 = True + elif not cacm: + self.cl1 = False + self.surface.fill(self.borderColor) + if cacm: + self.surface.draw.rect(self.abg,_r(self.borderWidth,self.borderWidth, + self.surface.size[0]-self.borderWidth*2, + self.surface.size[1]-self.borderWidth*2)) + if self.x: + self.surface.draw.line(self.afg,[5,self.width/2+5],[self.width/2,self.width-5],7) + self.surface.draw.line(self.afg,[self.width/2,self.width-5],[self.width-5,5],7) + else: + self.surface.draw.rect(self.bg,_r(self.borderWidth,self.borderWidth, + self.surface.size[0]-self.borderWidth*2, + self.surface.size[1]-self.borderWidth*2)) + if self.x: + self.surface.draw.line(self.fg,[5,self.width/2+5],[self.width/2,self.width-5],7) + self.surface.draw.line(self.fg,[self.width/2,self.width-5],[self.width-5,5],7) + def draw(self, win, pos): + self._generate(pos) + win.blit(self.surface,pos) +class menu: + def __init__(self, win, bg=(128,128,128)): + self._widgets = {} + self._bg = bg + self._win = win + self._page = 0 + def draw(self): + self._win.fill(self._bg) + for i in self._widgets[self._page]: + if i[0].power: + i[0].draw(self._win, i[1]) + if i[0].destroyed: + self._widgets[self._page].remove(i) + def put(self, widget, pos, page=0): + if page not in self._widgets: + self._widgets.update({page:[]}) + self._widgets[page].append([widget, pos]) + def selectPage(self, page): + self._page = page + def getPage(self): + return self._page + def getWidgets(self, page=0): + return self._widgets[page]