From 5fcdad61eef7facfc61f3ec7ed8e147407114ffa Mon Sep 17 00:00:00 2001 From: Phyks Date: Tue, 24 Dec 2013 18:50:34 +0100 Subject: [PATCH] Restarting from scratch using a code with wxpython --- main | Bin 11059 -> 0 bytes main.c | 208 ----------------------------------------------------- makefile | 4 -- piboard.py | 187 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+), 212 deletions(-) delete mode 100755 main delete mode 100644 main.c delete mode 100644 makefile create mode 100755 piboard.py diff --git a/main b/main deleted file mode 100755 index ed6a93b942dc222adcd07692fa578e20969a8b2b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 11059 zcmcIq4{%h+dEdK3ApYr&KQ=PZaubUb*+B-!5J-r9LVEDToUj6%OC}~yhwg;VIsNh7 zI}nMXnx1iaa}m+BnU-YS8QPGB>CiEgP>|gWNWw)*%d{fF9h@cqt z_wC!gd-rtLGM(w(-23+XeSdcM+kLz5?b|<6{9Bw32b0Oko@d03&h~N0yfSvGlCuKa zz{;7A{XSd9W&$b0-^V3H&77nI!nLI3LeB*{gYr%RIDKYD(B&q_B$YKJO6BE~Ph?Gc zWwFUh%9xh}WJh%!74p%=%^a7sU({nn)<@V8OLiGyml1Z7-X_OLlFIf`+vvYs_*?Gu zfg|2Wgw^wFp7K}#Yx+iMWi*7KQYWbgXto{5H5 zMQ=2>2U4;)exw{8z`*D@Yna8x#hh5C}@6F(Ty{w_28;$J22 z*S)dq>GJP*?%p98>B#sW9rQZb4BuSB9>@O){5LD_-TH@L{^@IDukK#n-+QNG<8M;S z$A9+g3W!RkA3(COh;#ukZxMU|STX%qO5pcO;9JpqlzYB}zNdu#Cg^+dx2KN)6syl?OZaJmzSr&q{FkBcVU_IYlI8+wl;cmZ z<2Jlm;CB54tQWGm?0C7az|RKgFTg01`f}WV%=IgnzOYcA;eOaz?_a&P=2^N4)-bJM zSECjR$HMy}aXlQ{)mRtp>rnRDY?x8_eBhb|qj%|(#e_NuF zE{aQ+-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%~pu#UFd&0u%vcAS=q;dCDq!)e-g;BC_0UN3Mb!`t2*A9DG$4q3w2*l<Br3yaaxk`&qz77W8lkbIp>u+S5OCiFV`%* zXdItD-d+J-?7RVeCl-$)1&;oh#`cu(^EKce+(*Z4<PTbaZzzVpdcm)0VJ$H|t|wYsk=3+qbvrbF!_Hke+mb&4qv`Eh3!Q<9GkLDX0%=;P zQ`;Bq+#8H)A-yve*Mf;|hL`!SXjl)2YSw>l?M8SmqQsA(n41JR7^24xGILuYlxS~1 z2onp(&&w33(h(D092UQg$7cmrgf8kn;QF|Gc~+iM#e>Y!zj@S{9T+v_QiwJ-a;IMSE(e|VDLAN;cPdT= zkwycZ$ccybrbu@=PB1JC39^yolC?A{|;H9!6~Qk)qgg-TXl&e(Z7>G@Y8no4$st0gyW}t~Jln- -#include -#include - -#define WIDTH_CONTROL 100 - -#ifndef min - #define min(a, b) ((a < b) ? a : b) -#endif - -void setPixel(SDL_Surface *affichage, int x, int y, Uint16 coul) -{ - *((Uint16**)(affichage->pixels) + x + y * affichage->w) = coul; -} - -void echangerEntiers(int* x, int* y) -{ - int t = *x; - *x = *y; - *y = t; -} - -void DrawLine(SDL_Surface *affichage, int x1, int y1, int x2, int y2, Uint32 coul) //Draw line using Bresenham algorithm -{ - int d, dx, dy, aincr, bincr, xincr, yincr, x, y; - - if(x1 == x2 || y1 == y2) //If horizontal or vertical, use the SDL library optimized function - { - SDL_Rect r; - - r.x = min(x1, x2); - r.y = min(y1, y2); - r.w = (x1 == x2) ? 1 : abs(x1 - x2); - r.h = (y1 == y2) ? 1 : abs(y1 - y2); - - SDL_FillRect(affichage, &r, coul); - } - else if (abs(x2 - x1) < abs(y2 - y1)) //Go through vertical axis - { - if (y1 > y2) - { - echangerEntiers(&x1, &x2); - echangerEntiers(&y1, &y2); - } - - xincr = x2 > x1 ? 1 : -1; - dy = y2 - y1; - dx = abs(x2 - x1); - d = 2 * dx - dy; - aincr = 2 * (dx - dy); - bincr = 2 * dx; - x = x1; - y = y1; - - setPixel(affichage, x, y, coul); - - for (y = y1+1; y <= y2; ++y) - { - if (d >= 0) - { - x += xincr; - d += aincr; - } - else - d += bincr; - - setPixel(affichage, x, y, coul); - } - } - else //Go through horizontal axis - { - - if (x1 > x2) - { - echangerEntiers(&x1, &x2); - echangerEntiers(&y1, &y2); - } - - yincr = y2 > y1 ? 1 : -1; - dx = x2 - x1; - dy = abs(y2 - y1); - d = 2 * dy - dx; - aincr = 2 * (dy - dx); - bincr = 2 * dy; - x = x1; - y = y1; - - setPixel(affichage, x, y, coul); - - for (x = x1+1; x <= x2; ++x) - { - if (d >= 0) - { - y+= yincr; - d += aincr; - } - else - d += bincr; - setPixel(affichage, x, y, coul); - } - } -} - - -int main(int argc, char *argv[]) -{ - int continuer = 1; - int x_old = -1, y_old = -1; - int left = 0; - SDL_Event event; - SDL_Surface *ecran = NULL, *controles = NULL; - - if(-1 == SDL_Init(SDL_INIT_VIDEO)) - { - fprintf(stderr, "Erreur de chargement de la SDL : %s\n", SDL_GetError()); - exit(EXIT_FAILURE); - } - - const SDL_VideoInfo* video_info = SDL_GetVideoInfo(); - int width = video_info->current_w; - int height = video_info->current_h; - int bpp = 16; //video_info ->vfmt->BitsPerPixel; - - SDL_Rect position_controles = {width - WIDTH_CONTROL, 0}; - - ecran = SDL_SetVideoMode(width, height, bpp, SDL_HWSURFACE | SDL_FULLSCREEN); //Raspi has only a 16 bpp framebuffer - controles = SDL_CreateRGBSurface(SDL_HWSURFACE, WIDTH_CONTROL, height, bpp, 0, 0, 0, 0); - - if(NULL == ecran || NULL == controles || NULL == ecran) - { - fprintf(stderr, "Erreur d'ouverture de la SDL : %s\n", SDL_GetError()); - exit(EXIT_FAILURE); - } - - SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255)); - SDL_FillRect(controles, NULL, SDL_MapRGB(controles->format, 100, 100, 100)); - SDL_BlitSurface(controles, NULL, ecran, &position_controles); - - SDL_Flip(ecran); - - while(continuer) - { - SDL_WaitEvent(&event); - - switch(event.type) - { - case SDL_MOUSEBUTTONDOWN: - if(event.button.x < width - WIDTH_CONTROL) - { - if(SDL_BUTTON_LEFT == event.button.button) - { - left = 1; - x_old = event.button.x; - y_old = event.button.y; - } - else if(SDL_BUTTON_RIGHT == event.button.button) - { - left = 0; - x_old = event.button.x; - y_old = event.button.y; - } - } - break; - - case SDL_MOUSEMOTION: - if(event.motion.x < width - WIDTH_CONTROL) - { - if(x_old > 0 && y_old > 0) - { - if(left) - DrawLine(ecran, x_old, y_old, event.motion.x, event.motion.y, SDL_MapRGB(ecran->format, 0, 0, 0)); - else - DrawLine(ecran, x_old, y_old, event.motion.x, event.motion.y, SDL_MapRGB(ecran->format, 255, 255, 255)); - - SDL_Flip(ecran); - x_old = event.motion.x; - y_old = event.motion.y; - } - } - else if(x_old > 0 && y_old > 0) - { - x_old = width - WIDTH_CONTROL; - y_old = event.motion.y;; - } - break; - - case SDL_MOUSEBUTTONUP: - if(SDL_BUTTON_LEFT == event.button.button || SDL_BUTTON_RIGHT == event.button.button) - { - x_old = -1; - y_old = -1; - } - break; - - case SDL_KEYDOWN: - continuer = 0; - break; - - case SDL_QUIT: - continuer = 0; - break; - } - } - - SDL_FreeSurface(controles); - SDL_Quit(); - return EXIT_SUCCESS; -} diff --git a/makefile b/makefile deleted file mode 100644 index 3222e30..0000000 --- a/makefile +++ /dev/null @@ -1,4 +0,0 @@ -all : main - -main : main.c - gcc -o main main.c `sdl-config --cflags --libs` diff --git a/piboard.py b/piboard.py new file mode 100755 index 0000000..e0400ea --- /dev/null +++ b/piboard.py @@ -0,0 +1,187 @@ +#!/usr/bin/env python2 +# -*- coding: utf8 -*- + +# Written by: QuantumPhysics +# http://compsci.ca/v3/viewtopic.php?t=32343 +# Date Written: 10/06/12 + +import wx + +class PaintWindow(wx.Window): + # array of colors available to draw + colours = ['Black', 'Yellow', 'Red', 'Green', 'Blue', 'Purple', + 'Brown', 'Aquamarine', 'Forest Green', 'Light Blue', 'Goldenrod', + 'Cyan', 'Orange', 'Navy', 'Light Grey', 'ClearScreen'] + # color quantity + thicknesses = [1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128] + + def __init__(self, parent): + # creates paint window + super(PaintWindow, self).__init__(parent, + style=wx.NO_FULL_REPAINT_ON_RESIZE) + self.initDrawing() + self.makeMenu() + # binds key_down and up events + self.bindEvents() + self.initBuffer() + + def initDrawing(self): + self.SetBackgroundColour('WHITE') + self.currentThickness = self.thicknesses[0] + self.currentColour = self.colours[0] + self.lines = [] + self.previousPosition = (0, 0) + + def bindEvents(self): + for event, handler in [ + # creates bind event for left button down + (wx.EVT_LEFT_DOWN, self.onLeftDown), + # creates bind event once left button is released + (wx.EVT_LEFT_UP, self.onLeftUp), + # creates bind event for drawing (moving cursor) + (wx.EVT_MOTION, self.onMotion), + # creates bind event for right button up + (wx.EVT_RIGHT_UP, self.onRightUp), + (wx.EVT_SIZE, self.onSize), + (wx.EVT_IDLE, self.onIdle), + (wx.EVT_PAINT, self.onPaint), + # creates event to close window after 'x' is selected + (wx.EVT_WINDOW_DESTROY, self.cleanup)]: + self.Bind(event, handler) + + def initBuffer(self): + # gets size of window + size = self.GetClientSize() + # sets window to empty canvas .bmp x-width, y-height + self.buffer = wx.EmptyBitmap(size.width, size.height) + dc = wx.BufferedDC(None, self.buffer) + dc.SetBackground(wx.Brush(self.GetBackgroundColour())) + dc.Clear() + self.drawLines(dc, *self.lines) + self.reInitBuffer = False + + def makeMenu(self): + # creates menu on right click + self.menu = wx.Menu() + # sets selection for items (colors) in the runtime window on right click + self.idToColourMap = self.addCheckableMenuItems(self.menu, + self.colours) + # updates screen once a new color is selected + self.bindMenuEvents(menuHandler=self.onMenuSetColour, + updateUIHandler=self.onCheckMenuColours, + ids=self.idToColourMap.keys()) + self.menu.Break() + self.idToThicknessMap = self.addCheckableMenuItems(self.menu, + self.thicknesses) + self.bindMenuEvents(menuHandler=self.onMenuSetThickness, + updateUIHandler=self.onCheckMenuThickness, + ids=self.idToThicknessMap.keys()) + + # adds a static surpressor to conditions + @staticmethod + def addCheckableMenuItems(menu, items): + + idToItemMapping = {} + for item in items: + menuId = wx.NewId() + idToItemMapping[menuId] = item + menu.Append(menuId, str(item), kind=wx.ITEM_CHECK) + return idToItemMapping + + def bindMenuEvents(self, menuHandler, updateUIHandler, ids): + + sortedIds = sorted(ids) + firstId, lastId = sortedIds[0], sortedIds[-1] + for event, handler in \ + [(wx.EVT_MENU_RANGE, menuHandler), + (wx.EVT_UPDATE_UI_RANGE, updateUIHandler)]: + self.Bind(event, handler, id=firstId, id2=lastId) + + def onLeftDown(self, event): + # draw line + self.currentLine = [] + self.previousPosition = event.GetPositionTuple() + self.CaptureMouse() + + def onLeftUp(self, event): + # close motion, stop drawing, wait for event + if self.HasCapture(): + self.lines.append((self.currentColour, self.currentThickness, + self.currentLine)) + self.currentLine = [] + self.ReleaseMouse() + + def onRightUp(self, event): + # if right button is clicked then make wx.menu to select colors + self.PopupMenu(self.menu) + + def onMotion(self, event): + + if event.Dragging() and event.LeftIsDown(): + dc = wx.BufferedDC(wx.ClientDC(self), self.buffer) + currentPosition = event.GetPositionTuple() + lineSegment = self.previousPosition + currentPosition + self.drawLines(dc, (self.currentColour, self.currentThickness, + [lineSegment])) + self.currentLine.append(lineSegment) + self.previousPosition = currentPosition + + def onSize(self, event): + + self.reInitBuffer = True + + def onIdle(self, event): + + if self.reInitBuffer: + self.initBuffer() + self.Refresh(False) + + def onPaint(self, event): + + + dc = wx.BufferedPaintDC(self, self.buffer) + + def cleanup(self, event): + if hasattr(self, "menu"): + self.menu.Destroy() + del self.menu + + def onCheckMenuColours(self, event): + colour = self.idToColourMap[event.GetId()] + event.Check(colour == self.currentColour) + + def onCheckMenuThickness(self, event): + thickness = self.idToThicknessMap[event.GetId()] + event.Check(thickness == self.currentThickness) + + def onMenuSetColour(self, event): + self.currentColour = self.idToColourMap[event.GetId()] + + def onMenuSetThickness(self, event): + self.currentThickness = self.idToThicknessMap[event.GetId()] + + @staticmethod + def drawLines(dc, *lines): + dc.BeginDrawing() + for colour, thickness, lineSegments in lines: + pen = wx.Pen(wx.NamedColour(colour), thickness, wx.SOLID) + dc.SetPen(pen) + for lineSegment in lineSegments: + dc.DrawLine(*lineSegment) + dc.EndDrawing() + + +class PaintFrame(wx.Frame): + def __init__(self, parent=None): + size = wx.GetDisplaySize() + super(PaintFrame, self).__init__(parent, title="Black PiBoard", + size=size, + style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE) + doodle = PaintWindow(self) + + +if __name__ == '__main__': + app = wx.App() + frame = PaintFrame() + frame.ShowFullScreen(True, wx.FULLSCREEN_ALL) + app.MainLoop()