Re: Gotta Have It Programming Contest #1
- Posted by mattlewis (admin) Jun 17, 2009
- 983 views
euphoric said...
mattlewis said...
What do you mean by "select triangles"?
I just meant that given an XY screen coordinate (such as for the mouse/cursor), indicate somehow what triangle it's on. For instance, in your hexes demo, you indicate the "selected" triangle in the title bar. I don't know what grid system you use for triangles. I figure there have to be three components to it though... right?
Ok, I modified it so that it not only shows the triangle in the title bar, but you can click on them. Hold down shift and click to multi-select. Requires eu 4.0 and the bleeding edge release of wxEuphoria (though it wouldn't be too difficult to use earlier versions of both).
-- triangles.exw -- by Matt Lewis without warning include wxeud.e integer width, height width = 400 height = 400 integer old_width, old_height old_width = width old_height = height constant frame = create( wxFrame, {0, -1, "Triangle Test", 100, 100, width, height} ), win = frame, --create( wxPanel, frame ), BrightWhite = create( wxColour, {255, 255, 255}), Black = create( wxColour, {0,0,0}), Grey = create( wxColour, {"Grey"}), white_pen = create( wxPen, {BrightWhite, 1, wxSOLID}), black_pen = create( wxPen, {Black, 1, wxSOLID}), grey_brush = create( wxBrush, Grey), trans_brush = create( wxBrush, BrightWhite), control_frame = create( wxFrame, {frame, -1, "Controls", 500, 300, 240, 240} ), control = create( wxPanel, control_frame ), zoom = create( wxSpinCtrl, {control, -1, "120", 10, 10, 50, 25, wxSP_ARROW_KEYS, 1, 200, 50} ), zoom_lbl = create( wxStaticText, {control, -1, "Size", 70,13, 40, 25}), XRatio = create( wxSpinCtrl,{ control, -1, "70", 10, 70, 50, 25, wxSP_ARROW_KEYS, 1, 200, 112} ), XRatio_lbl = create( wxStaticText, {control, -1, "Horz. Aspect", 70,73, 100, 25}), font = create( wxFont, {10,wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,0, "Courier"}), YRatio = create( wxSpinCtrl, { control, -1, "60", 10, 130, 50, 25, wxSP_ARROW_KEYS, 1, 60, 200 } ), YRatio_lbl = create( wxStaticText, {control, -1, "Vert. Aspect", 70,133, 100, 25 }) atom dc, pix pix = create( wxBitmap, {BM_IN_MEMORY, width, height} ) dc = create( wxMemoryDC, pix ) set_font( dc, font ) integer dx, dy atom slope integer columns, rows dx=0 dy=0 slope=0 sequence selection = {} procedure draw_triangles() if not dy then return else sequence shape = repeat( 0, 8 ) set_pen( dc, white_pen ) draw_polygon( dc, {0, 0, width, 0, width, height, 0, height }, 0, 0, wxODDEVEN_RULE ) set_pen( dc, black_pen ) integer _2dx = dx * 2, _1_2dx = floor( dx / 2 ), _1_2dy = floor( dy / 2 ), temp = - _1_2dx, whichX, row, column, x1, y1, x2, y2, x3 sequence offX for i = 1 to columns do -- the starting point offX = { temp, temp - _1_2dx } temp += dx whichX = 2 y1 = 0 for j = 1 to rows do -- alternate between 1 and 2 whichX = 3 - whichX x1 = offX[whichX] x2 = x1 + _1_2dx y2 = y1 + dy x3 = x1 + dx shape = { x1, y1, x2, y2, x3, y1, x1, y1 } for pt = 1 to 5 by 2 do draw_line( dc, shape[pt..pt+3] ) end for row = j column = i - 1 sequence cr = {column * 2 - not and_bits( j, 1 ), row} if find( cr, selection ) then set_brush( dc, grey_brush ) draw_polygon( dc, shape, 0, 0, wxODDEVEN_RULE ) set_brush( dc, trans_brush ) end if sequence ext = get_text_extent( win, sprintf("(%d,%d)", cr), font) wx_printf( {0, x1 + _1_2dx - floor(ext[1] / 2), y1 + _1_2dy - floor( ext[2] ), dc}, "(%d,%d)",cr) cr = {column * 2 + and_bits( j, 1 ), row} if find( cr, selection ) then set_brush( dc, grey_brush ) draw_polygon( dc, {x2, y2, x3, y1, x2 + dx, y2 }, 0, 0, wxODDEVEN_RULE ) set_brush( dc, trans_brush ) end if ext = get_text_extent( win, sprintf("(%d,%d)", cr), font) wx_printf( {0, x1 + dx - floor(ext[1] / 2), y1 + _1_2dy - 0*floor( ext[2] / 2 ), dc}, "(%d,%d)",cr) y1 += dy end for end for atom client_dc = create( wxClientDC, win ) begin_drawing( client_dc ) blit( client_dc, 0, 0, dc, 0, 0, width, height, wxCOPY ) end_drawing( client_dc ) delete_instance( client_dc ) end if end procedure procedure calc_rc() columns = floor(width/dx) + 2 slope = dy / dx rows = floor(height / dy ) * 2 end procedure procedure zoom_triangles() atom z z = get_spin_value( zoom ) dx = floor( z * get_spin_value(XRatio) / 100 ) dy = floor( z * get_spin_value(YRatio) / 100 ) if dx = 0 or dy = 0 then return end if calc_rc() draw_triangles() end procedure procedure on_zoom( atom this, atom event_type, atom id, atom event ) zoom_triangles() end procedure set_event_handler( zoom, -1, wxEVT_COMMAND_SPINCTRL_UPDATED, routine_id("on_zoom") ) set_event_handler( XRatio, -1, wxEVT_COMMAND_SPINCTRL_UPDATED, routine_id("on_zoom") ) set_event_handler( YRatio, -1, wxEVT_COMMAND_SPINCTRL_UPDATED, routine_id("on_zoom") ) procedure on_paint( atom this, atom event_type, atom id, atom event ) atom pdc pdc = create( wxPaintDC, this ) blit( pdc, 0, 0, dc, 0, 0, width, height, wxCOPY ) delete_instance( pdc ) end procedure set_event_handler( win, get_id(win), wxEVT_PAINT, routine_id("on_paint")) procedure on_erase( atom this, atom event_type, atom id, atom event ) end procedure set_event_handler( win, -1, wxEVT_ERASE_BACKGROUND, routine_id("on_erase")) procedure on_resize( atom this, atom event_type, atom id, atom event ) sequence lExt lExt = get_rect(this) width = lExt[3] height = lExt[4] delete_instance( dc ) delete_instance( pix ) pix = create( wxBitmap, {BM_IN_MEMORY, width + 100, height + 100}) dc = create( wxMemoryDC, pix ) set_font( dc, font ) on_zoom(0,0,0,0) end procedure set_event_handler( win, -1, wxEVT_SIZE, routine_id("on_resize")) procedure win_activate( atom this, atom event_type, atom id, atom event ) draw_triangles() show_window( control_frame, 1 ) end procedure set_event_handler( win, -1, wxEVT_SHOW, routine_id("win_activate")) procedure win_paint( atom this, atom event_type, atom id, atom event ) atom paint paint = create( wxPaintDC, this ) blit( paint, 0, 0, dc, 0, 0, width, height, wxCOPY ) delete_instance( paint ) end procedure set_event_handler( win, get_id(win), wxEVT_PAINT, routine_id("win_paint")) function xy_to_triangle( sequence xy ) integer x = xy[1] integer y = xy[2] integer row = floor(y / dy) + 1 -- Row 1: starts like / -- Row 2: starts like \ -- etc... integer _1_2dx = floor( dx / 2 ) integer starts_up = and_bits( row, 1 ) integer column = floor( x / dx ) * 2 + starts_up integer rx = remainder( x, dx ) integer ry = remainder( y, dy ) atom slope = 2 * dy / dx if starts_up then if rx < _1_2dx then if ry < dy - slope * rx then column -= 1 end if else if ry < slope * (rx - _1_2dx) then column += 1 end if end if else if rx < _1_2dx then if ry < slope * rx then column += 1 end if else column += 1 if ry > dy - slope * (rx - _1_2dx) then column += 1 end if end if end if return { column, row } end function procedure win_scrollwheel(atom this, atom event_type, atom id, atom event ) integer scroll scroll = floor( get_wheel_rotation(event)/120 ) set_spin_value( zoom, get_spin_value(zoom) + scroll ) zoom_triangles() end procedure set_event_handler( win, -1, wxEVT_MOUSEWHEEL, routine_id("win_scrollwheel")) procedure win_mouse( atom this, atom event_type, atom id, atom event ) set_title( frame, sprintf("Triangle Test: ( %d, %d ) ( %d, %d)", xy_to_triangle( mouse_event_position( event )) & mouse_event_position(event) ) ) end procedure set_event_handler( win, -1, wxEVT_MOTION, routine_id("win_mouse")) procedure win_click( atom this, atom event_type, atom id, atom event ) sequence triangle = xy_to_triangle( mouse_event_position( event ) ) if mouse_event_shiftdown( event ) then integer ix = find( triangle, selection ) if ix then selection = remove( selection, ix ) else selection = append( selection, triangle ) end if else selection = { triangle } end if if not find( triangle, selection ) then selection = append( selection, triangle ) end if draw_triangles() refresh_window( win ) end procedure set_event_handler( win, -1, wxEVT_LEFT_DOWN, routine_id("win_click")) wxMain( frame )