; ; ------------------------------------------------------------ ; ; SpiderBasic - CanvasGadget example file ; ; (c) Fantaisie Software ; ; ------------------------------------------------------------ ; Enumeration 10 #IMAGE_Content ; stores the previous CanvasGadget content while the mouse is down #IMAGE_Color EndEnumeration Enumeration #GADGET_Canvas #GADGET_Color #GADGET_Brush #GADGET_Line #GADGET_Box #GADGET_Circle #GADGET_Clear EndEnumeration Global CurrentColor, CurrentMode, StartX, StartY ; Draw the mouse action result depending on the currently selected mode and event type ; Procedure DrawAction(x, y, EventType) If StartDrawing(CanvasOutput(#GADGET_Canvas)) Select CurrentMode Case #GADGET_Brush If EventType = #PB_EventType_LeftButtonDown Or EventType = #PB_EventType_MouseMove Circle(x, y, 5, CurrentColor) EndIf Case #GADGET_Line DrawImage(ImageID(#IMAGE_Content), 0, 0) LineXY(StartX, StartY, x, y, CurrentColor) Case #GADGET_Box DrawImage(ImageID(#IMAGE_Content), 0, 0) Box(StartX, StartY, x-StartX, y-StartY, CurrentColor) Case #GADGET_Circle DrawImage(ImageID(#IMAGE_Content), 0, 0) If x > StartX rx = x - StartX Else rx = StartX - x EndIf If y > StartY ry = y - StartY Else ry = StartY - y EndIf If GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Modifiers) & #PB_Canvas_Control ry = rx EndIf Ellipse(StartX, StartY, rx, ry, CurrentColor) EndSelect StopDrawing() EndIf EndProcedure CurrentMode = #GADGET_Brush CreateImage(#IMAGE_Color, 55, 35, 24) CreateImage(#IMAGE_Content, 380, 380, 24) Procedure GenerateRandomColor() CurrentColor = RGB(Random(255), Random(255), Random(255)) If StartDrawing(ImageOutput(#IMAGE_Color)) Box(0, 0, ImageWidth(#IMAGE_Color), ImageHeight(#IMAGE_Color), CurrentColor) StopDrawing() EndIf EndProcedure Procedure ClearCanvas() If StartDrawing(CanvasOutput(#GADGET_Canvas)) Box(0, 0, GadgetWidth(#GADGET_Canvas), GadgetHeight(#GADGET_Canvas), $FFFFFF) StopDrawing() EndIf EndProcedure Procedure GadgetEvent() Select EventGadget() Case #GADGET_Canvas X = GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_MouseX) Y = GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_MouseY) Type = EventType() Select EventType() Case #PB_EventType_LeftButtonDown ; ; This stores the current content of the CanvasGadget in #IMAGE_Content, ; so it can be re-drawn while the mouse moves ; If StartDrawing(ImageOutput(#IMAGE_Content)) DrawImage(GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Image), 0, 0) StopDrawing() EndIf StartX = X StartY = Y DrawAction(X, Y, EventType()) Case #PB_EventType_LeftButtonUp DrawAction(X, Y, EventType()) Case #PB_EventType_MouseMove If GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton DrawAction(X, Y, EventType()) EndIf EndSelect Case #GADGET_Color GenerateRandomColor() SetGadgetAttribute(#GADGET_Color, #PB_Button_Image, ImageID(#IMAGE_Color)) Case #GADGET_Brush, #GADGET_Line, #GADGET_Box, #GADGET_Circle EventGadget = EventGadget() For Gadget = #GADGET_Brush To #GADGET_Circle If Gadget = EventGadget SetGadgetState(Gadget, 1) Else SetGadgetState(Gadget, 0) ; unset the state of all other gadgets EndIf Next Gadget CurrentMode = EventGadget Case #GADGET_Clear ClearCanvas() EndSelect EndProcedure If OpenWindow(0, 0, 0, 480, 400, "CanvasGadget", #PB_Window_TitleBar | #PB_Window_ScreenCentered) CanvasGadget(#GADGET_Canvas, 10, 10, 380, 380, #PB_Canvas_Border) ClearCanvas() GenerateRandomColor() ButtonImageGadget(#GADGET_Color, 400, 10, 70, 50, ImageID(#IMAGE_Color)) ButtonGadget(#GADGET_Brush, 400, 100, 70, 25, "Brush", #PB_Button_Toggle) ButtonGadget(#GADGET_Line, 400, 130, 70, 25, "Line", #PB_Button_Toggle) ButtonGadget(#GADGET_Box, 400, 160, 70, 25, "Box", #PB_Button_Toggle) ButtonGadget(#GADGET_Circle, 400, 190, 70, 25, "Circle", #PB_Button_Toggle) ButtonGadget(#GADGET_Clear, 400, 280, 70, 25, "Clear") SetGadgetState(#GADGET_Brush, 1) SetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Cursor, #PB_Cursor_Cross) BindEvent(#PB_Event_Gadget, @GadgetEvent()) CompilerIf #PB_Compiler_OS <> #PB_OS_Web Repeat Event = WaitWindowEvent() Until Event = #PB_Event_CloseWindow ; If the user has pressed on the window close button CompilerEndIf EndIf