-- code generated by Win32Lib IDE v0.20.1 include Win32Lib.ew without warning include graphex.ew -------------------------------------------------------------------------------- -- Window Window1 constant Window1 = createEx( Window, "Window1", 0, Default, Default, 400, 300, 0, 0 ) constant StatusB = createEx( StatusBar, "StatusBar4", Window1, 0, 0, 0, 0, 0, 0 ) constant LText6 = createEx( LText, "Drawing Area:", Window1, 0, 0, 108, 20, 0, 0 ) constant LText25 = createEx( RText, "Pen: ", Window1, 120, 0, 44, 20, 0, 0 ) constant btnTogglePen = createEx( PushButton, "---", Window1, 164, 0, 32, 20, 0, 0 ) constant LText7 = createEx( LText, "Line list:", Window1, 204, 0, 144, 20, 0, 0 ) constant btnBack = createEx( PushButton, "<--", Window1, 348, 0, 44, 20, 0, 0 ) constant lstLines = createEx( List, "List5", Window1, 200, 24, 188, 100, 0, 0 ) constant LText9 = createEx( LText, "GridSnap:", Window1, 204, 124, 56, 20, 0, 0 ) constant LText12 = createEx( LText, "X:", Window1, 260, 124, 16, 20, 0, 0 ) constant txtGridX = createEx( EditText, "1", Window1, 276, 124, 48, 20, 0, 0 ) constant LText13 = createEx( LText, "Y:", Window1, 324, 124, 20, 20, 0, 0 ) constant txtGridY = createEx( EditText, "1", Window1, 344, 124, 48, 20, 0, 0 ) constant chkGrid = createEx( CheckBox, "draw grid", Window1, 212, 148, 64, 20, 0, 0 ) constant LText18 = createEx( LText, "Scale:", Window1, 288, 148, 40, 20, 0, 0 ) constant List22 = createEx( DropDownList, "List22", Window1, 328, 148, 64, 20*6, 0, 0 ) constant MleText14 = createEx( MleText, "mleCode", Window1, 204, 176, 188, 76, 0, 0 ) constant LText21 = createEx( LText, "20 x 20", Window1, 0, 228, 108, 20, 0, 0 ) constant LText16 = createEx( RText, "Shape \"code\"-->", Window1, 116, 228, 88, 20, 0, 0 ) setHint( btnBack,"Remove line") --------------------------------------------------------- -------------------------------------------------------------------------------- with trace sequence thegfx, tmpgfx --placeholder sequence linelist -- list of line segments linelist = {} sequence pen_colour -- colours to indicate pen status pen_colour = {Red, Green} -- {up_colour, dn_colour} integer pen_status constant PEN_UP = 0, PEN_DN = 1 pen_status = PEN_UP sequence start_coords, end_coords start_coords = {} -- indicates drawing in progress end_coords = {} -- indicates end of draw integer scale -- currently selected scale sequence zooms -- divide by constant and consequential resolution -- assuming Bitmap3 is 200x200! zooms = { {1, "200x200"}, {2, "100x100"}, {4, "50x50"}, {5, "40x40"}, {8, "25x25"}, {10, "20x20"}, {20, "10x10"} } procedure make_gfxs() thegfx = newGfx(Window1, 0, 20, 200, 200) tmpgfx = newGfx(Window1, 0, 20, 200, 200) end procedure function round_scale(sequence s) sequence tmp tmp = {} for i = 1 to length(s) do if s[i]/scale - floor(s[i]/scale) < 0.5 then tmp = append(tmp, floor(s[i]/scale) - 100/scale) else tmp = append(tmp, floor(s[i]/scale) + 1 - 100/scale) end if end for return tmp end function -- return the mouse coordinates relative to the placeholder bitmap -- adjusted for zoom level function get_canvas_pos() sequence xy, ret integer zoomidx xy = getPointerRelPos(gfxControl(thegfx)) zoomidx = getIndex(List22) ret = round_scale(xy) -- floor doesn't cut it return ret end function -- draw what we want to see procedure draw() sequence shape -- start again from scratch each time -- slow but simple gfxClear(thegfx, Black) -- draw dark grid if draw_grid is checked if isChecked(chkGrid) and scale != 1 then for j = 0 to gfxWidth(thegfx) by scale do for i = 0 to gfxHeight(thegfx) by scale do gfx_draw(thegfx, "line", {64,64,64}, {0, j, gfxWidth(thegfx), j}) gfx_draw(thegfx, "line", {64,64,64}, {i, 0, i, gfxHeight(thegfx)}) end for end for end if shape = {} for i = 1 to length(linelist) do --gfx_draw(thegfx, "line", pen_colour[linelist[i][3]+1], linelist[i]) shape = append(shape, linelist[i]) end for gfx_draw(thegfx, "shape-test", {Red, BrightGreen}, {100,100, gfxScaleShape(shape,scale)}) end procedure procedure pen_toggle() if pen_status = PEN_UP then pen_status = PEN_DN setText(btnTogglePen, "/") setText(StatusB, "PEN IS DOWN") else pen_status = PEN_UP setText(btnTogglePen, "---") setText(StatusB, "PEN IS UP") end if end procedure function penstr(integer updn) if updn then return "DOWN" else return "UP" end if end function procedure update_line_lists() sequence str str = "" VOID = deleteItem(lstLines, -1) for i = 1 to length(linelist) do addItem(lstLines, sprintf("%d, %d, %s", {linelist[i][1], linelist[i][2], penstr(linelist[i][3])})) str = str & sprintf("{%d, %d, %d}, ", {linelist[i][1], linelist[i][2], linelist[i][3]}) end for setText(MleText14, str) end procedure procedure add_line(sequence start_coords, sequence end_coords, integer pen_status) sequence delta delta = end_coords - start_coords delta = append(delta, pen_status) linelist = append(linelist, delta) update_line_lists() end procedure -- work out the current start coordinates function calc_end_coords() sequence start start = {0, 0} for i = 1 to length(linelist) do start = start + {linelist[i][1], linelist[i][2]} end for return start end function -- go back one procedure del_line() linelist = linelist[1 .. length(linelist)-1] start_coords = calc_end_coords() update_line_lists() draw() end procedure procedure update_info_display() setText(LText21, zooms[getIndex(List22)][2]) end procedure procedure init_zoom_level() for i = 1 to length(zooms) do addItem(List22, zooms[i][2]) end for setIndex(List22, floor(getCount(List22)/2)) scale = zooms[floor(getCount(List22)/2)][1] end procedure procedure thegfx_onMouse(integer self, integer event, sequence params)--params is ( int event, int x, int y, int shift, int wheelmove ) sequence coords integer evt evt = params[1] draw() coords = get_canvas_pos() -- 0,0 grid offset adjusted -- highlight griddiness gfx_draw(thegfx, "ellipse", Red, {(coords[1] + 100/scale) * scale - 3 , (coords[2] + 100/scale) * scale -3, (coords[1] + 100/scale) * scale +3, (coords[2] + 100/scale) * scale + 3}) end_coords = coords gfx_draw(thegfx, "line", White, ((start_coords & end_coords) + 100/scale) * scale) if evt = LeftDown then add_line(start_coords, end_coords, pen_status) start_coords = end_coords end if setText(StatusB, sprintf("x:%d, y:%d", coords)) if evt = RightDown then -- short cut pen_toggle() end if doGfxPaint(Window1) end procedure -- main make_gfxs() gfxAttachHandler(thegfx, w32HMouse, routine_id("thegfx_onMouse")) init_zoom_level() setGfxAutoPaint(0) update_info_display() start_coords = {0,0} -- all shapes are defined from 0, 0 -------------------------------------------------------------------------------- procedure Window1_onActivate (integer self, integer event, sequence params)--params is () gfxClear(thegfx, Black) doGfxPaint(Window1) end procedure setHandler( Window1, w32HActivate, routine_id("Window1_onActivate")) -------------------------------------------------------------------------------- procedure btnTogglePen_onClick (integer self, integer event, sequence params)--params is () pen_toggle() end procedure setHandler( btnTogglePen, w32HClick, routine_id("btnTogglePen_onClick")) -------------------------------------------------------------------------------- procedure btnBack_onClick (integer self, integer event, sequence params)--params is () del_line() -- go back one on the list end procedure setHandler( btnBack, w32HClick, routine_id("btnBack_onClick")) -------------------------------------------------------------------------------- procedure chkGrid_onClick (integer self, integer event, sequence params)--params is () setText(StatusB, "Grid draw toggled") update_info_display() doGfxPaint(Window1) end procedure setHandler( chkGrid, w32HClick, routine_id("chkGrid_onClick")) -------------------------------------------------------------------------------- procedure List22_onChange (integer self, integer event, sequence params)--params is () setText(StatusB, "Zoom level changed to " & zooms[getIndex(List22)][2]) update_info_display() scale = zooms[getIndex(List22)][1] doGfxPaint(Window1) end procedure setHandler( List22, w32HChange, routine_id("List22_onChange")) WinMain( Window1,Normal )