Home   Archive   Permalink



getting values from gui and using them in functions

I'm slowly going mad trying to do something I'm sure is really easy: I modified the simple 3d box drawing program in the tutorial to show a window first that asked for coordinates of the corners and colours for the sides. I've got it to show that window without any trouble, and as long as i leave the values for the box hard-coded, when i press the 'draw box' button, it does that.
    
However, I'm confused as to how to get the values from the input to use as parameters fot drawing the box. Although I've assigned a name to the color-picker, for instance, i can 'print get-face c10' in the code, and it prints the RGB value. but there's no 'to-color' or 'to-rgb' that i can tell from the internal help, and i just can't seem to get the fill command to understand that c10 is supposed to be a color.
    
Similarly, I've tried to enter separate x and y coordinates for the corners, and then concatenate them into the XxY format for coordinates, but it won't accept the variable names, having tried several options including 'to-integer', get-face and so-on.
    
FYI, my background is BASIC programmer as a youth, then 15 years as a professional RPG programmer on IBM s/38 and AS/400, writing transaction-processing programs. virtually no PC or graphics programming, but exploring Rebol on Android. Here is the program in its. Im sure the solution is simple, but either I misunderstood or couldn't find the relevant documentation. Any help appreciated
    
R E B O L [title: "here"]
do %r3-gui.r3
bck: make image! 400x220
view [ c1: field 75 "top left x"
    c2: field 75 "top left y"
    c3: field 75 "top right x"
    c4: field 75 "top right y"
    c5: field 75 "top front x"
    c6: field 75 "top front y"
    c7: field 75 "top back x"
    c8: field 75 "top back y"
    c9: field 75 "height"
    c10: color-picker
;    c11: color-picker
;    c12: color-picker
    button "make box" on-action [
print get-face c10
view/no-wait [image bck]
draw bck to-draw [
    t1: rejoin [to-integer get-face c1, "x", to-integer get-face c2]
    t2: join [c3, "x", c4]
    t3: join [c5, "x", c6]
    t4: join [c7, "x", c8]
    b1: join [c1, "x", (c2 +c9)]
    fill-pen c10
    polygon t1 t2 t3 t4
    fill-pen c10
    polygon 20x40 200x50 200x200 20x100
    fill-pen c10
    polygon 200x50 380x40 380x100 200x200]
copy []
do-events
]
]


posted by:   Mike Hersee       30-Mar-2014/4:27:18-7:00



Hi Mike,
    
If you type this into the R3 console:
    
     type? red     ; or any other color
    
You'll see the response is:
    
     == tuple!
    
If you type:
    
     help tuple
    
You'll see that one of the choices is:
    
     to-tuple        function! Converts to tuple! value.
    
That's the function you need to convert to a color data type. Now try typing:
    
     help pair
    
You'll see that there are 'as-pair and 'to-pair functions. Try looking at the source to see what the function arguments are:
    
     >> source as-pair
    
     as-pair: make native! [[
         "Combine X and Y values into a pair."
         x [number!]
         y [number!]
     ]]
    
That's the function you need to convert your number input into a coordinate (pair) value. Notice in the definition above that the 'x and 'y arguments must be number! values:
    
     >> as-pair "10" "23"
     ** Script error: as-pair does not allow string! for its x argument
    
     >> as-pair to-integer "10" to-integer "23"
     == 10x23
    
You can usually find the answers to any documentation questions in the interpreter console, once you know where to look. Using 'what, 'help, 'source, and exploring the built in system objects is an important part of being able to find answers. I wrote some basics about it:
    
     http://business-programming.com/business_programming.html#section-13.1 (R2)
     http://learnrebol.com/rebol3_book.html#section-17.1 (R3)

posted by:   Nick       30-Mar-2014/9:11-7:00



This should do the job:
    
R E B O L [title: "here"]
do %r3-gui.r3
bck: make image! 400x220
view [
     c1: field 75 "top left x"
     c2: field 75 "top left y"
     c3: field 75 "top right x"
     c4: field 75 "top right y"
     c5: field 75 "top front x"
     c6: field 75 "top front y"
     c7: field 75 "top back x"
     c8: field 75 "top back y"
     c9: field 75 "height"
     c10: color-picker
     button "make box" on-action [
         color1: to-tuple get-face c10
         t1: as-pair to-integer get-face c1 to-integer get-face c2
         t2: as-pair to-integer get-face c3 to-integer get-face c4
         t3: as-pair to-integer get-face c5 to-integer get-face c6
         t4: as-pair to-integer get-face c7 to-integer get-face c8
         b1: as-pair to-integer get-face c1 ((to-integer get-face c2) + (to-integer get-face c9))
         view/no-wait [image bck]
         draw bck to-draw [
             fill-pen color1
             polygon t1 t2 t3 t4
             fill-pen color1
             polygon 20x40 200x50 200x200 20x100
             fill-pen color1
             polygon 200x50 380x40 380x100 200x200]
         copy []
         do-events
     ]
]

posted by:   Nick       30-Mar-2014/9:16:28-7:00



Doing it this way would be my preference:
    
R E B O L [title: "here"]
do %r3-gui.r3
bck: make image! 400x220
view [
     text "Top Left:" c1: field 75 "20x40"
     text "Top Back:" c2: field 75 "200x20"
     text "Top Right:" c3: field 75 "380x40"
     text "Top Front:" c4: field 75 "200x80"
     text "Height" c9: field 75 "30"
     c10: color-picker
     button "make box" on-action [
         color1: to-tuple get-face c10
         height: as-pair 0 (to-integer get-face c9)
         t1: to-pair get-face c1
         t2: to-pair get-face c2
         t3: to-pair get-face c3
         t4: (to-pair get-face c4) - height
         view/no-wait [image bck]
         draw bck to-draw compose [
             fill-pen color1
             polygon t1 t2 t3 t4
             fill-pen color1
             polygon 20x40 (200x80 - height) 200x200 20x100
             fill-pen color1
             polygon (200x80 - height) 380x40 380x100 200x200
         ]
         copy []
         do-events
     ]
]

posted by:   Nick       30-Mar-2014/22:48:34-7:00



I'd add some validation to ensure users don't enter invalid data:
    
R E B O L [title: "here"]
do %r3-gui.r3
bck: make image! 400x220
btndo: [if error? try [to-pair arg] [alert "Please enter a coordinate pair (i.e., 20x40)"]]
view [
     text "Top Left:" c1: field 75 "20x40" on-action btndo
     text "Top Back:" c2: field 75 "200x20" on-action btndo
     text "Top Right:" c3: field 75 "380x40" on-action btndo
     text "Top Front:" c4: field 75 "200x80" on-action btndo
     text "Height" c9: field 75 "30"
     c10: color-picker
     button "make box" on-action [
         color1: to-tuple get-face c10
         height: as-pair 0 (to-integer get-face c9)
         t1: to-pair get-face c1
         t2: to-pair get-face c2
         t3: to-pair get-face c3
         t4: (to-pair get-face c4) - height
         view/no-wait [image bck]
         draw bck to-draw compose [
             fill-pen color1
             polygon t1 t2 t3 t4
             fill-pen color1
             polygon 20x40 (200x80 - height) 200x200 20x100
             fill-pen color1
             polygon (200x80 - height) 380x40 380x100 200x200
         ]
         copy []
         do-events
     ]
]
    
(Notice that the 'height' calculation is included in the code above)

posted by:   Nick       30-Mar-2014/23:01:10-7:00



Gosh, thank you Nick for that copious help. Will try it out at the earliest opportunity. Actually, I did try 'tuple', but i wasn't sure if it applied only to ip addresses, and i didn't have the rest of the syntax correct so i was led astray. i specifically set it to ask for x and y separately just to test myself joining the values together. I agree, for that simple purpose, it's easier to input the combined coordinate as one field


posted by:   Mike Hersee       31-Mar-2014/14:56:41-7:00