Canvas gadget keyboard handling?

Just starting out? Need help? Post your questions and find answers here.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Canvas gadget keyboard handling?

Post by J@ckWhiteIII »

Hey B.P.,
I don't know why, but all I see when compiling the modified code, is a white Canvas Gadget. Also, I cannot exit the window. That's not what it's supposed to do, I think...
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Canvas gadget keyboard handling?

Post by BasicallyPure »

Hmmm...
I have no idea why it doesn't work for you.
I thought I had something you could really use.

B.P.

edit: I'm running with Linux, but that shouldn't matter.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5011
Joined: Sun Apr 12, 2009 6:27 am

Re: Canvas gadget keyboard handling?

Post by RASHAD »

Work around

Code: Select all

;---------------------------------------------------------------------------------------------------
;- GLOBALS
;---------------------------------------------------------------------------------------------------
Global Game_Window_Event.i

Global MousePosX.w
Global MousePosY.w

Global GameTextWidth.w
Global GameTextHeight.w

Global GameText.s

Global GameTextFont.i = 4
Global GameBackgroundImage.i = 5
Global GameTextImage.i = 6

;---------------------------------------------------------------------------------------------------
;- LOAD FONTS
;---------------------------------------------------------------------------------------------------
   LoadFont(GameTextFont, "Arial", 48, #PB_Font_HighQuality)

;---------------------------------------------------------------------------------------------------
;- PRECREATE IMAGE
;---------------------------------------------------------------------------------------------------
   CreateImage(GameBackgroundImage, 800, 600, 32)
      StartDrawing(ImageOutput(GameBackgroundImage))
         DrawingMode(#PB_2DDrawing_Default)
         Box(0, 0, 800, 600, RGB(43, 144, 232))
         DrawingMode(#PB_2DDrawing_Transparent)
         DrawingFont(FontID(GameTextFont))
         DrawText(100, 100, "PVG Canvas Test", RGB(255, 255, 255))
      StopDrawing()

;---------------------------------------------------------------------------------------------------
   CreateImage(GameTextImage, 600, 100, 32)
      StartDrawing(ImageOutput(GameTextImage))
         DrawingFont(FontID(GameTextFont))
         GameText = "PVG Canvas Test"
         GameTextWidth = TextWidth(GameText) + 10
         GameTextHeight = TextHeight(GameText) + 10

         DrawingMode(#PB_2DDrawing_Default)
         Box(0, 0, GameTextWidth, GameTextHeight, RGB(0, 0, 0))

         DrawingMode(#PB_2DDrawing_Transparent)
         DrawText(5, 5, GameText, RGB(255, 255, 255))

;- FOR FULLY TRANSPARENT TEXT (CUT OUT) USE THIS
;         DrawingMode(#PB_2DDrawing_AlphaChannel)
;         DrawText(5, 5, GameText, RGBA(0, 0, 0, 0))

      StopDrawing()

;---------------------------------------------------------------------------------------------------
;- CREATE CANVAS GAMING WINDOW
;---------------------------------------------------------------------------------------------------
   If OpenWindow(0, 0, 0, 800, 600, "PVG - Canvas Game Test - v001", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
      CanvasGadget(1, 0, 0, 800, 600, #PB_Canvas_Keyboard)

;---------------------------------------------------------------------------------------------------
;- DO LOOP
;---------------------------------------------------------------------------------------------------
      Repeat
         Game_Window_Event = WaitWindowEvent()
            Select Game_Window_Event
               Case #PB_Event_Gadget
                  Select EventGadget()
                     Case 1            ; Canvas gadget
                        Select EventType()
                           Case #PB_EventType_MouseEnter
                                 mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
                                 mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)
                                                            
                           Case #PB_EventType_KeyUp
                               If GetGadgetAttribute (1,#PB_Canvas_Key) = 68
                                 Debug "Works"
                               EndIf 

                             
                           Case #PB_EventType_LeftButtonDown
                                 If Run > 0
                                   StartDrawing(ImageOutput(GameBackgroundImage))
                                   DrawingMode(#PB_2DDrawing_Default)
                                   DrawAlphaImage(ImageID(GameTextImage), MousePosX, MousePosY, 50)
                                   StopDrawing()
                                 EndIf
                                 Run + 1

                           Case #PB_EventType_MouseMove
                               StartDrawing(CanvasOutput(1))
                                 DrawingMode(#PB_2DDrawing_Default)
;                                 Box(0, 0, 800, 600, RGB(43, 144, 232))
                                 DrawImage(ImageID(GameBackgroundImage), 0, 0, 800, 600)

                                 MousePosX = GetGadgetAttribute(1, #PB_Canvas_MouseX)
                                 MousePosY = GetGadgetAttribute(1, #PB_Canvas_MouseY)
                                 DrawingMode(#PB_2DDrawing_AlphaBlend)
;                                 Circle(MousePosX, MousePosY, 10, RGB(100, 100, 100))
                                 DrawAlphaImage(ImageID(GameTextImage), MousePosX, MousePosY, 50)   ;255)   ;100)
                               StopDrawing()
                              
                      EndSelect       

                  EndSelect
            EndSelect
      Until Game_Window_Event = #PB_Event_CloseWindow
   EndIf

;---------------------------------------------------------------------------------------------------
Einde:
   End

;---------------------------------------------------------------------------------------------------

Egypt my love
marc_256
Addict
Addict
Posts: 859
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: Canvas gadget keyboard handling?

Post by marc_256 »

Hi J@ckWhiteIII,

This is an key extra of my example ...
Z-Q-S-D keys added

Code: Select all

;---------------------------------------------------------------------------------------------------
;- GLOBALS
;---------------------------------------------------------------------------------------------------
Global Game_Window_Event.i

Global MousePosX.w
Global MousePosY.w

Global GameTextWidth.w
Global GameTextHeight.w

Global GameText.s

Global GameTextFont.i = 4
Global GameBackgroundImage.i = 5
Global GameTextImage.i = 6

;---------------------------------------------------------------------------------------------------
;- LOAD FONTS
;---------------------------------------------------------------------------------------------------
	LoadFont(GameTextFont, "Arial", 48, #PB_Font_HighQuality)

;---------------------------------------------------------------------------------------------------
;- PRECREATE IMAGE
;---------------------------------------------------------------------------------------------------
	CreateImage(GameBackgroundImage, 800, 600, 32)
		StartDrawing(ImageOutput(GameBackgroundImage))
			DrawingMode(#PB_2DDrawing_Default)
			Box(0, 0, 800, 600, RGB(43, 144, 232))
			DrawingMode(#PB_2DDrawing_Transparent)
			DrawingFont(FontID(GameTextFont))
			DrawText(100, 100, "PVG Canvas Test", RGB(255, 255, 255))
		StopDrawing()

;---------------------------------------------------------------------------------------------------
	CreateImage(GameTextImage, 600, 100, 32)
		StartDrawing(ImageOutput(GameTextImage))
			DrawingFont(FontID(GameTextFont))
			GameText = "PVG Canvas Test"
			GameTextWidth = TextWidth(GameText) + 10
			GameTextHeight = TextHeight(GameText) + 10

			DrawingMode(#PB_2DDrawing_Default)
			Box(0, 0, GameTextWidth, GameTextHeight, RGB(0, 0, 0))

;			DrawingMode(#PB_2DDrawing_Transparent)
			DrawingMode(#PB_2DDrawing_AlphaChannel)
;			DrawText(5, 5, GameText, RGBA(255, 255, 255))
			DrawText(5, 5, GameText, RGBA(0, 0, 0, 0))

		StopDrawing()

;---------------------------------------------------------------------------------------------------
;- CREATE CANVAS GAMING WINDOW
;---------------------------------------------------------------------------------------------------
	If OpenWindow(0, 0, 0, 800, 600, "PVG - Canvas Game Test - v001", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
		CanvasGadget(1, 0, 0, 800, 600, #PB_Canvas_Keyboard | #PB_Canvas_DrawFocus)

;---------------------------------------------------------------------------------------------------
;- DO LOOP
;---------------------------------------------------------------------------------------------------
		Repeat
			Game_Window_Event = WindowEvent()
				Select Game_Window_Event
					Case #PB_Event_Gadget
						Select EventGadget()
							Case 1				; Canvas gadget
								Select EventType()
									Case #PB_EventType_MouseEnter
										SetActiveGadget(1)

									Case #PB_EventType_MouseLeave

									Case #PB_EventType_LeftButtonDown
										If StartDrawing(ImageOutput(GameBackgroundImage))
											DrawingMode(#PB_2DDrawing_Default)
											DrawAlphaImage(ImageID(GameTextImage), MousePosX, MousePosY, 50)
											StopDrawing()
										EndIf

									Case #PB_EventType_MouseMove
										If StartDrawing(CanvasOutput(1))
											DrawingMode(#PB_2DDrawing_Default)
;											Box(0, 0, 800, 600, RGB(43, 144, 232))
											DrawImage(ImageID(GameBackgroundImage), 0, 0, 800, 600)

											MousePosX = GetGadgetAttribute(1, #PB_Canvas_MouseX)
											MousePosY = GetGadgetAttribute(1, #PB_Canvas_MouseY)
											DrawingMode(#PB_2DDrawing_AlphaBlend)
;											Circle(MousePosX, MousePosY, 10, RGB(100, 100, 100))
											DrawAlphaImage(ImageID(GameTextImage), MousePosX, MousePosY, 50)	;255)	;100)

											StopDrawing()
										EndIf

									Case #PB_EventType_KeyDown
										If GetGadgetAttribute (1, #PB_Canvas_Key) = #PB_Shortcut_Z
											Debug "Z - Forward moving key pushed"
										ElseIf GetGadgetAttribute (1, #PB_Canvas_Key) = #PB_Shortcut_S
											Debug "S - Back moving key pushed"
										ElseIf GetGadgetAttribute (1, #PB_Canvas_Key) = #PB_Shortcut_Q
											Debug "Q - Left moving key pushed"
										ElseIf GetGadgetAttribute (1, #PB_Canvas_Key) = #PB_Shortcut_D
											Debug "D - Right moving key pushed"
										EndIf
								EndSelect
						EndSelect
				EndSelect
		Until Game_Window_Event = #PB_Event_CloseWindow
	EndIf

;---------------------------------------------------------------------------------------------------
Einde:
	End

;---------------------------------------------------------------------------------------------------

You must use the shortcut values of the keys for canvas gadget.

Marc,
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Post Reply