Lines overwrite Buttons ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
GWS
User
User
Posts: 23
Joined: Tue Sep 27, 2005 9:51 pm
Location: Staffs. UK

Lines overwrite Buttons ?

Post by GWS »

Hi,

Here's a small program which draws patterns in a window whenever the 'Next' button is pressed.
I quite like these pretty pattern programs - you never know what will appear next .. :)

At line 120, is a the statement:
SetGadgetText(#B_Exit, "Exit")

If you comment out this line, you'll see that the lines drawn each time 'Next' is pressed, overwrite the 'Exit' button - but strangely, not the 'Next' button.

With line 120 active, all seems to work correctly.

Code: Select all

; Patterns
; GWS 2013  -  (a modification of MovingLines by Davetea 2004)

EnableExplicit

Define.w Event,run
Global.w wW,wH,d,draw
Global Dim Dir.w(1)
Global Dim x.w(3)                    ; x-coordinate 
Global Dim y.w(3)                    ; y-coordinate
Global Dim dx.w(3)                   ; x-displacement 
Global Dim dy.w(3)                   ; y-displacement 

Enumeration
#w
#B_Exit
#B_Next
EndEnumeration

draw = #w
Declare DrawLines(draw)

#Center = 1
#Fixed = $A0000                       ; #SysMenu|#Min

wW = 1024
wH = 768
run = #True

OpenWindow(#w, 0, 0, wW, wH, "Lines in Space",#Fixed|#Center)
SetWindowColor(#w, RGB(0,0,30))

ButtonGadget(#B_Exit, (wW-70)*0.8, wH*0.88, 70, 25, "Exit",#BS_FLAT)
ButtonGadget(#B_Next, (wW-70)*0.2, wH*0.88, 70, 25, "Next",#BS_FLAT)

d = 3                                 ; initial distance between line endpoints (pixels)							
dir(0) = d
dir(1) = -d

DrawLines(draw)


Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_CloseWindow
      run = #False
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #B_Exit
          run = #False
        Case #B_Next
          StartDrawing(WindowOutput(draw))
            Box(0,0,wW,wH,RGB(0,0,30))
          StopDrawing()
          DrawLines(draw)
      EndSelect
  EndSelect
Until run = #False


Procedure DrawLines(draw)
; routine to draw a new set of lines ..
Define.w i,j,k,n,r,g,b

d = Random(10) + 2									; distance between the line endpoints (pixels)

For i = 0 To 2 Step 2
	x(i) = Random(wW)
	x(i+1) = Random(wW)  
  y(i) = Random(wH)
  y(i+1) = Random(wH)

	j = Random(1): k = Random(d-1): dx(i) = dir(j) - k
  j = Random(1): k = Random(d-1): dy(i) = dir(j) - k
  j = Random(1): k = Random(d-1): dx(i+1) = dir(j) - k
  j = Random(1): k = Random(d-1): dy(i+1) = dir(j) - k
Next i

For n = 1 To (Random(500) + 100)

	If (n%10 = 0)
		r = Random(255)
		g = Random(255)
		b = Random(255)
	EndIf

  For i = 0 To 2 Step 2

		x(i) + dx(i)										        ; Coordinates changed by dx/dy 
		y(i) + dy(i)
		x(i+1) + dx(i+1)
    y(i+1) + dy(i+1)

		If (x(i) <= 50) Or (x(i) >= wW-50)			; Change direction when nearing the screen edge
			dx(i) = - dx(i)
    EndIf 
		If (y(i) <= 50) Or (y(i) >= wH-50) 
			dy(i) = - dy(i)
    EndIf
    If (x(i+1) <= 50) Or (x(i+1) >= wW-50)
      dx(i+1) = - dx(i+1)
    EndIf 
    If (y(i+1) <= 50) Or (y(i+1) >= wH-50)
      dy(i+1) = - dy(i+1)
    EndIf

    StartDrawing(WindowOutput(draw))

   		LineXY (x(i),y(i),x(i+1),y(i+1),RGB(r,g,b))
  		LineXY (wW-x(i),wH-y(i),wW-x(i+1),wH-y(i+1),RGB(r,g,b))
  		LineXY (x(i),wH-y(i),x(i+1),wH-y(i+1),RGB(r,g,b))
  		LineXY (wW-x(i),y(i),wW-x(i+1),y(i+1),RGB(r,g,b))
      
    StopDrawing()

; Note: without the following statement, lines will be drawn over the Exit button ..
; Why this happens I've no idea ..

    SetGadgetText(#B_Exit, "Exit")

    
	Next i 	

Next n

EndProcedure

End
Does anyone have any idea why this happens ?

Best wishes, :)

Graham
Tomorrow may be too late ..
User avatar
spikey
Enthusiast
Enthusiast
Posts: 771
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Lines overwrite Buttons ?

Post by spikey »

Drawing on the window directly isn't "supported" in the sense that the window doesn't have any persistence memory for what gets drawn on the window "unofficially" so that it can be automatically redrawn if invalidation occurs - unlike the contents of a stringgadget for example. So if you do it the consequences become your problem rather than the operating system's.

You're invalidating the window's official content by drawing on it - but you aren't providing a repaint routine to take care of things. Strange things will occur as a result. The surviving button is being redrawn because something else is causing this to happen (not sure what exactly).

The easiest way to fix it is to cause the buttons to be redrawn in the Case #B_Next

Code: Select all

...
DrawLines(draw)

HideGadget(#B_Exit, #True)
HideGadget(#B_Exit, #False)
          
HideGadget(#B_Next, #True)
HideGadget(#B_Next, #False)
A more elegant solution would be to use the relevant API calls to tell the OS that the buttons need updating.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Lines overwrite Buttons ?

Post by IdeasVacuum »

on WinXP X86 (PB5.11Beta, Unicode, threadsafe, XP Skin) the Exit button does not get lines drawn on it, they go underneath it. Possibly the Next button is spared the issue on your machine because it is the active gadget?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
GWS
User
User
Posts: 23
Joined: Tue Sep 27, 2005 9:51 pm
Location: Staffs. UK

Re: Lines overwrite Buttons ?

Post by GWS »

Thanks Spikey .. Hide it - Show it .. that works for me .. :)

I suppose what my makeshift method of re-writing the 'Exit' button text was doing, was to make it active - a sort of SetFocus.

@IdeasVacuum .. Oh no! you're on 5.11 already ?
'.. the Exit button does not get lines drawn on it, they go underneath it.' - yes, that was what I was hoping for.

Anyway, my little application is working merrily now - thanks all ..

By the way, I tried drawing on an image, but with the Procedure DrawLines activated by the 'Next' button, my image stayed obstinately black .. :shock: Couldn't get it to work at all .. I must have been doing something wrong somewhere .. :?

best wishes, :)

Graham
Tomorrow may be too late ..
Post Reply