Advice on selecting an area of the window.

Just starting out? Need help? Post your questions and find answers here.
Rich Dersheimer
New User
New User
Posts: 7
Joined: Mon Oct 14, 2019 4:40 pm

Advice on selecting an area of the window.

Post by Rich Dersheimer »

Hi all. I'm currently working on a "seating chart" program, and I need to be able to select a bunch of seats at a time. What I have so far is:

The seats are drawn on the window, at predetermined locations.

A left click or right click event on the window returns the x and y values for the mouse.

I then loop through the seats and if the mouse x/y fall within the boundaries of the seat location, the seat is selected (left click) or deselected (right click)

I would like to be able to click and drag a selection box over the seats, and use the coords of the box to select or deselect all seats inside the box.

I'm thinking I'll need to draw all the seats and then draw a semi-transparent box over them, in a loop. I can test to see if the user has chosen "single seat" or "multi seat" selection mode. If in multi seat mode, a left button down would return the mouse x1,y1, and the selection box would be drawn from there to the mouse, until a left button up is detected, at which point I'll have an x2,y2 to check all the seats inside the selection area.

So far I haven't been able to do this, but I'll keep plugging away at it.

I would be grateful for any advice on this type of thing, as I am new to the event driven looping in PB.

Rich
User avatar
kenmo
Addict
Addict
Posts: 2069
Joined: Tue Dec 23, 2003 3:54 am

Re: Advice on selecting an area of the window.

Post by kenmo »

If you're not already using it, I suggest the CanvasGadget - it is both an area for drawing and also provides all sorts of mouse and keyboard events natively (no OS API).

I even wrote an includefile called "CanvasDrag" or similar, that simplifies exactly what you're trying to do... tracking click-and-drag boxes... I'll look when I'm home.
Rich Dersheimer
New User
New User
Posts: 7
Joined: Mon Oct 14, 2019 4:40 pm

Re: Advice on selecting an area of the window.

Post by Rich Dersheimer »

That sounds very cool, I'd love to see it!

Using your advice as a starting place, I came up with this...

Code: Select all

wFlags = #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget |
         #PB_Window_SizeGadget | #PB_Window_ScreenCentered

OpenWindow(0, 0, 0, 1024, 780, "Dragging Selection", wFlags)
CanvasGadget(0, 10, 10, 1024, 780)
BoxColor = RGB(255,0,0)
StartDrawing(CanvasOutput(0)) 
Box(0,0,1024,768,RGB(255,255,255))
Box(412,284,200,200,BoxColor)  
StopDrawing()

Repeat
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Gadget And EventGadget() = 0 
    
    If EventType() = #PB_EventType_LeftButtonDown
      x1 = GetGadgetAttribute(0, #PB_Canvas_MouseX)
      y1 = GetGadgetAttribute(0, #PB_Canvas_MouseY)
    EndIf
    
    If (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
      StartDrawing(CanvasOutput(0))              
      Box(0,0,1024,768,RGB(255,255,255))      
      Box(412,284,200,200,BoxColor)
      x2 = GetGadgetAttribute(0, #PB_Canvas_MouseX)        
      y2 = GetGadgetAttribute(0, #PB_Canvas_MouseY)
      LineXY(x1,y1,x2,y1,0)
      LineXY(x2,y1,x2,y2,0)
      LineXY(x2,y2,x1,y2,0)
      LineXY(x1,y2,x1,y1,0)
      StopDrawing()
    EndIf
    
    If EventType() = #PB_EventType_LeftButtonUp
      StartDrawing(CanvasOutput(0))    
      Box(0,0,1024,768,RGB(255,255,255)) 
      
      If X1 < X2
        Swap X1,X2
      EndIf
      If Y1 < Y2
          Swap Y1,Y2
      EndIf
        
      If X1 >= 412 And X2 <= 612 And Y1 >= 284 And y2 <= 484
        BoxColor = RGB(Random(255),Random(255),Random(255))      
      EndIf
      Box(412,284,200,200,BoxColor)      
      StopDrawing()      
    EndIf      
  EndIf    
  
Until Event = #PB_Event_CloseWindow


  
  
This will let me do what I want, but I would still love to see your code.

Thanks,
Rich

*edit to include showing the seat selected
User avatar
kenmo
Addict
Addict
Posts: 2069
Joined: Tue Dec 23, 2003 3:54 am

Re: Advice on selecting an area of the window.

Post by kenmo »

Looks like you basically figured it out!

I posted my CanvasDrag.pbi here... it tries to simplify this to a few procedure calls and lots of helper functions.
https://github.com/kenmo-pb/includes/bl ... asDrag.pbi

But looking at it again, it needs a few things! Mostly, I should add StartDrag and StopDrag events that get posted, so you don't need to poll IsCanvasDragging().
Post Reply