RE: Hex Grid Window
- Posted by Matt Lewis <matthewwalkerlewis at yahoo.com> Sep 05, 2003
- 421 views
C. K. Lester wrote: > > > This one is for the Win32Lib pros... > > I need to create a floating (child?) window for my main app that > will display a hex-grid and allow the user to zoom in/out on > that grid, click hexes, etc... I will also need to display > graphics within each hex, indicating position and facing. > > Can somebody make this for me? :) Or point me in the right > direction... Oooh...Neat. Try this: -- hex.exw without warning include win32lib.ew constant win = create( Window, "Hex Test", 0, 100, 100, 400, 400, 0 ), bmp = create( Bitmap, "", win, 0, 0, 400, 400, 0 ), pix = create( Pixmap, "", win, 0, 0, 400, 400, 0 ), control = create( Window, "Controls", 0, 500, 300, 140, 60, 0 ), zoom = create( EditText, "50", control, 10, 10, 50, 25, 0 ), zoom_ud = create( UpDown, "", control, 60, 10, 10, 10, or_all( {UDS_AUTOBUDDY,UDS_ALIGNRIGHT,UDS_SETBUDDYINT,UDS_ARROWKEYS})) constant width = 400, height = 400 integer dx, dy atom slope procedure draw_hexes() integer x, y sequence hex, ext setPenColor( pix, White ) drawPolygon( pix, True, {{0,0},{400,0},{400,400},{0,400}}) for i = 1 to floor(width/dx) + 1 do for j = 1 to floor(height / (dy * 3) ) + 1 do if remainder(j,2) then x = ((i * 2) - 1) * dx else x = (i-1) * 2 * dx end if y = (j-1) * 3 * dy drawLines( pix, { Black, {x, y} & {x + dx, y + dy}, {x + dx, y + 3 * dy}, {x, y + 4 * dy}, {x - dx, y + 3 * dy}, {x - dx, y + dy}, {x, y}}) hex = sprintf("%d,%d",{i,j}) ext = getTextExtent( pix, hex ) setPenPos( pix, x - floor(ext[1]/2), y + 2 * dy - floor(ext[2]/2) ) wPuts( pix, hex ) end for end for copyBlt( bmp, 0, 0, pix ) end procedure procedure on_zoom( integer self, atom event, sequence params ) atom z z = getNumber( zoom ) dx = floor( z * 112 / 100 ) -- should be 50, but due to monitor -- aspect ratio, 60 looks better dy = floor( z * 60 / 100 ) slope = dy / dx draw_hexes() end procedure setHandler( zoom, w32HChange, routine_id("on_zoom")) procedure win_activate( integer self, atom event, sequence params ) setScrollRange( zoom_ud, 1, 100 ) setText( zoom, "50" ) draw_hexes() openWindow( control, Normal ) end procedure setHandler( win, w32HActivate, routine_id("win_activate")) procedure win_paint( integer self, atom event, sequence params ) copyBlt( bmp, 0, 0, pix ) end procedure setHandler( win, w32HPaint, routine_id("win_paint")) function xy_to_hex( sequence xy ) integer x, y, gx, gy, rx, ry, hx, hy atom mxb x = xy[1] y = xy[2] gx = floor(x/dx) gy = floor(y/dy) rx = remainder(x, dx ) ry = remainder(y,dy) hy = floor( gy/3 + 0.7 ) if remainder(gy,3) < 1 then mxb = slope * rx -- need to check the slope if remainder(hy,2) then if remainder( gx, 2 ) then mxb = dy - mxb end if else if not remainder( gx, 2 ) then mxb = dy - mxb end if end if if ry > mxb then hy += 1 end if end if if remainder( hy, 2 ) then hx = floor(gx / 2 ) + 1 else hx = floor((gx+1) / 2 ) + 1 end if return {hx,hy} end function procedure win_mouse( integer self, atom event, sequence params ) integer x, y, gx, gy, r, rx, ry if params[1] = MouseMove then setText( win, sprintf("Hex Test: ( %d, %d )", xy_to_hex(params[2..3]))) end if end procedure setHandler( win, w32HMouse, routine_id("win_mouse")) WinMain( win, Normal )