From: John L. <jr...@us...> - 2006-11-22 21:14:01
|
Update of /cvsroot/wxlua/wxLua/samples In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv19437/wxLua/samples Modified Files: calculator.wx.lua scribble.wx.lua wxluasudoku.wx.lua Log Message: add more wxcomap26 checks cleanup and simplify samples Index: wxluasudoku.wx.lua =================================================================== RCS file: /cvsroot/wxlua/wxLua/samples/wxluasudoku.wx.lua,v retrieving revision 1.58 retrieving revision 1.59 diff -C2 -d -r1.58 -r1.59 *** wxluasudoku.wx.lua 22 Nov 2006 06:08:33 -0000 1.58 --- wxluasudoku.wx.lua 22 Nov 2006 21:13:56 -0000 1.59 *************** *** 540,549 **** local value = sudoku.GetCellValue(cell, sudokuTable) ! if not sudokuTable.row_values[row][value] then sudokuTable.row_values[row][value] = {} end ! if not sudokuTable.col_values[col][value] then sudokuTable.col_values[col][value] = {} end ! if not sudokuTable.block_values[block][value] then sudokuTable.block_values[block][value] = {} end ! sudokuTable.row_values[row][value][cell] = cell ! sudokuTable.col_values[col][value][cell] = cell ! sudokuTable.block_values[block][value][cell] = cell end end --- 540,558 ---- local value = sudoku.GetCellValue(cell, sudokuTable) ! if not sudokuTable.row_values[row][value] then ! sudokuTable.row_values[row][value] = {[cell] = cell} ! else ! sudokuTable.row_values[row][value][cell] = cell ! end ! if not sudokuTable.col_values[col][value] then ! sudokuTable.col_values[col][value] = {[cell] = cell} ! else ! sudokuTable.col_values[col][value][cell] = cell ! end ! if not sudokuTable.block_values[block][value] then ! sudokuTable.block_values[block][value] = {[cell] = cell} ! else ! sudokuTable.block_values[block][value][cell] = cell ! end end end *************** *** 798,804 **** local function add_possible(atable, rcb_key, key, cell) ! if not atable[rcb_key] then atable[rcb_key] = {} end ! if not atable[rcb_key][key] then atable[rcb_key][key] = {} end ! table.insert(atable[rcb_key][key], cell) end --- 807,817 ---- local function add_possible(atable, rcb_key, key, cell) ! if not atable[rcb_key] then ! atable[rcb_key] = { [key] = {cell} } ! elseif not atable[rcb_key][key] then ! atable[rcb_key][key] = {cell} ! else ! table.insert(atable[rcb_key][key], cell) ! end end *************** *** 1101,1105 **** function sudoku.SolveScanSingles(sudokuTable) sudokuTable = sudoku.CalcAllPossible(sudokuTable) ! local changed_cells = nil for row = 1, 9 do --- 1114,1118 ---- function sudoku.SolveScanSingles(sudokuTable) sudokuTable = sudoku.CalcAllPossible(sudokuTable) ! local changed_cells = {} for row = 1, 9 do *************** *** 1116,1120 **** sudokuTable = sudoku.SetValue(row, col, value, sudokuTable) sudokuTable = sudoku.RemovePossibleAll(row, col, value, sudokuTable) - if not changed_cells then changed_cells = {} end changed_cells[sudoku.RowColToCell(row, col)] = value end --- 1129,1132 ---- *************** *** 1123,1126 **** --- 1135,1140 ---- end + if TableIsEmpty(changed_cells) then changed_cells = nil end -- reset if not used + return sudokuTable, changed_cells end *************** *** 1136,1140 **** function sudoku.SolveScanRowsCols(scan_rows, sudokuTable) sudokuTable = sudoku.CalcAllPossible(sudokuTable) ! local changed_cells = nil for row_col1 = 1, 9 do --- 1150,1154 ---- function sudoku.SolveScanRowsCols(scan_rows, sudokuTable) sudokuTable = sudoku.CalcAllPossible(sudokuTable) ! local changed_cells = {} for row_col1 = 1, 9 do *************** *** 1165,1169 **** sudokuTable = sudoku.SetValue(r, c, value, sudokuTable) sudokuTable = sudoku.RemovePossibleAll(r, c, value, sudokuTable) - if not changed_cells then changed_cells = {} end changed_cells[cell] = value end --- 1179,1182 ---- *************** *** 1171,1174 **** --- 1184,1189 ---- end + if TableIsEmpty(changed_cells) then changed_cells = nil end -- reset if not used + return sudokuTable, changed_cells end *************** *** 1177,1181 **** function sudoku.SolveScanBlocks(sudokuTable) sudokuTable = sudoku.CalcAllPossible(sudokuTable) ! local changed_cells = nil for block = 1, 9 do --- 1192,1196 ---- function sudoku.SolveScanBlocks(sudokuTable) sudokuTable = sudoku.CalcAllPossible(sudokuTable) ! local changed_cells = {} for block = 1, 9 do *************** *** 1204,1208 **** sudokuTable = sudoku.SetValue(row, col, value, sudokuTable) sudokuTable = sudoku.RemovePossibleAll(row, col, value, sudokuTable) - if not changed_cells then changed_cells = {} end changed_cells[cell] = value end --- 1219,1222 ---- *************** *** 1210,1213 **** --- 1224,1229 ---- end + if TableIsEmpty(changed_cells) then changed_cells = nil end -- reset if not used + return sudokuTable, changed_cells end *************** *** 1243,1247 **** end ! if TableCount(changed_cells) == 0 then changed_cells = nil end -- nothing done return sudokuTable, count, changed_cells --- 1259,1263 ---- end ! if TableIsEmpty(changed_cells) then changed_cells = nil end -- nothing done return sudokuTable, count, changed_cells Index: scribble.wx.lua =================================================================== RCS file: /cvsroot/wxlua/wxLua/samples/scribble.wx.lua,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** scribble.wx.lua 21 Nov 2006 23:20:37 -0000 1.12 --- scribble.wx.lua 22 Nov 2006 21:13:56 -0000 1.13 *************** *** 15,22 **** mouseDown = false -- left mouse button is down pointsList = {} -- list of the points added to the drawing itemColour = 1 -- current pen colour - isRunning = true -- only false when we're about to exit isModified = false -- has the drawing been modified redrawRequired = true -- redraw the image fileName = "" -- filename to save to ID_SAVEBITMAP = wx.wxID_HIGHEST + 1 --- 15,23 ---- mouseDown = false -- left mouse button is down pointsList = {} -- list of the points added to the drawing + -- pointsList[segment] = {penColour = index, [n] = {x = x_pos, y = x_pos}} itemColour = 1 -- current pen colour isModified = false -- has the drawing been modified redrawRequired = true -- redraw the image + lastDrawn = 0 -- last point that was drawn or 0 to redraw all fileName = "" -- filename to save to ID_SAVEBITMAP = wx.wxID_HIGHEST + 1 *************** *** 27,70 **** bitmap = wx.wxEmptyBitmap(screenWidth, screenHeight) ! for key, value in pairs(penColours) do ! table.insert(pens, wx.wxPenFromColourName(value, 3, wx.wxSOLID)) end function DrawPoints(drawDC) ! for listIndex = 1, #pointsList do ! local listValue = pointsList[listIndex] drawDC:SetPen(pens[listValue.penColour]) ! local pointsValue = listValue[1] ! local lastX, lastY = pointsValue.x, pointsValue.y ! for pointsIndex = 2, #listValue do ! pointsValue = listValue[pointsIndex] ! drawDC:DrawLine(lastX, lastY, pointsValue.x, pointsValue.y) ! lastX, lastY = pointsValue.x, pointsValue.y end end drawDC:SetPen(wx.wxNullPen) end function DrawBitmap(bmp) ! local bitmapDC = wx.wxMemoryDC() -- create off screen dc to draw on ! bitmapDC:SelectObject(bmp) -- select our bitmap to draw into ! bitmapDC:Clear() ! DrawPoints(bitmapDC) ! bitmapDC:SelectObject(wx.wxNullBitmap) -- release our bitmap ! bitmapDC:Delete() -- ALWAYS Delete() any wxDCs created when done end function OnPaint(event) local dc = wx.wxPaintDC(panel) ! if redrawRequired then ! if isRunning then ! DrawBitmap(bitmap) ! end ! redrawRequired = false end ! if isRunning then dc:DrawBitmap(bitmap, 0, 0, false) end --- 28,79 ---- bitmap = wx.wxEmptyBitmap(screenWidth, screenHeight) ! for n = 1, #penColours do ! table.insert(pens, wx.wxPenFromColourName(penColours[n], 3, wx.wxSOLID)) end function DrawPoints(drawDC) ! if lastDrawn == 0 then ! drawDC:Clear() ! end ! ! local start_index = 1 ! if lastDrawn > 1 then start_index = lastDrawn end ! ! for list_index = start_index, #pointsList do ! local listValue = pointsList[list_index] drawDC:SetPen(pens[listValue.penColour]) ! local point = listValue[1] ! local last_x, last_y = point.x, point.y ! for point_index = 2, #listValue do ! point = listValue[point_index] ! drawDC:DrawLine(last_x, last_y, point.x, point.y) ! last_x, last_y = point.x, point.y end end + + lastDrawn = #pointsList drawDC:SetPen(wx.wxNullPen) end function DrawBitmap(bmp) ! local memDC = wx.wxMemoryDC() -- create off screen dc to draw on ! memDC:SelectObject(bmp) -- select our bitmap to draw into ! DrawPoints(memDC) ! memDC:SelectObject(wx.wxNullBitmap) -- always release bitmap ! memDC:Delete() -- ALWAYS Delete() any wxDCs created when done end function OnPaint(event) + -- ALWAYS create wxPaintDC in wxEVT_PAINT handler, even if unused local dc = wx.wxPaintDC(panel) ! if redrawRequired and bitmap and bitmap:Ok() then ! DrawBitmap(bitmap) end ! redrawRequired = false -- reset since we at least tried to redraw it ! ! if bitmap and bitmap:Ok() then dc:DrawBitmap(bitmap, 0, 0, false) end *************** *** 74,81 **** function OnLeftDown(event) - local pointItem = {} - pointItem.penColour = itemColour frame:SetStatusText(penColours[itemColour], 1) itemColour = itemColour + 1 if penColours[itemColour] == nil then -- have colours wrap around --- 83,91 ---- function OnLeftDown(event) frame:SetStatusText(penColours[itemColour], 1) + local pointItem = {penColour = itemColour, {x = event:GetX(), y = event:GetY()}} + table.insert(pointsList, pointItem) + itemColour = itemColour + 1 if penColours[itemColour] == nil then -- have colours wrap around *************** *** 83,90 **** end - local points = { x = event:GetX(), y = event:GetY() } - table.insert(pointItem, points) - table.insert(pointsList, pointItem) - if (not panel:HasCapture()) then panel:CaptureMouse() end mouseDown = true --- 93,96 ---- *************** *** 96,101 **** function OnLeftUp(event) if mouseDown then ! local points = { x = event:GetX(), y = event:GetY() } ! table.insert(pointsList[#pointsList], points) if panel:HasCapture() then panel:ReleaseMouse() end --- 102,112 ---- function OnLeftUp(event) if mouseDown then ! -- only add point if the mouse moved since DrawLine(1,2,1,2) won't draw anyway ! if (#pointsList[#pointsList] > 1) then ! local point = { x = event:GetX(), y = event:GetY() } ! table.insert(pointsList[#pointsList], point) ! else ! pointsList[#pointsList] = nil ! end if panel:HasCapture() then panel:ReleaseMouse() end *************** *** 112,117 **** if event:LeftIsDown() then ! local points = { x = event:GetX(), y = event:GetY() } ! table.insert(pointsList[#pointsList], points) mouseDown = true --- 123,128 ---- if event:LeftIsDown() then ! local point = { x = event:GetX(), y = event:GetY() } ! table.insert(pointsList[#pointsList], point) mouseDown = true *************** *** 139,144 **** end ! function LoadScribbles() pointsList = {} return ((pcall(dofile, fileName)) ~= nil) end --- 150,156 ---- end ! function LoadScribbles(fileName) pointsList = {} + lastDrawn = 0 return ((pcall(dofile, fileName)) ~= nil) end *************** *** 172,182 **** fh:close() return true end - - wx.wxMessageBox("Unable to save file:'"..fileName.."'.\n"..msg, - "wxLua Scribble Save error", - wx.wxOK + wx.wxICON_ERROR, - frame) - return false end --- 184,194 ---- fh:close() return true + else + wx.wxMessageBox("Unable to save file:'"..fileName.."'.\n"..msg, + "wxLua Scribble Save error", + wx.wxOK + wx.wxICON_ERROR, + frame) + return false end end *************** *** 191,195 **** if fileDialog:ShowModal() == wx.wxID_OK then fileName = fileDialog:GetPath() ! result = LoadScribbles() if result then frame:SetTitle("wxLua Scribble - " .. fileName) --- 203,207 ---- if fileDialog:ShowModal() == wx.wxID_OK then fileName = fileDialog:GetPath() ! result = LoadScribbles(fileName) if result then frame:SetTitle("wxLua Scribble - " .. fileName) *************** *** 220,230 **** function SaveChanges() ! local isOKToContinue = false if fileName == "" then ! isOKToContinue = SaveAs() else ! isOKToContinue = SaveScribbles() end ! return isOKToContinue end --- 232,242 ---- function SaveChanges() ! local saved = false if fileName == "" then ! saved = SaveAs() else ! saved = SaveScribbles() end ! return saved end *************** *** 235,248 **** wx.wxDEFAULT_FRAME_STYLE ) ! panel = wx.wxPanel(frame, wx.wxID_ANY) ! ! panel:Connect(wx.wxEVT_PAINT, OnPaint) ! panel:Connect(wx.wxEVT_ERASE_BACKGROUND, function(event) end) -- do nothing ! ! ! panel:Connect(wx.wxEVT_LEFT_DOWN, OnLeftDown ) ! panel:Connect(wx.wxEVT_LEFT_UP, OnLeftUp ) ! panel:Connect(wx.wxEVT_MOTION, OnMotion ) ! local fileMenu = wx.wxMenu() fileMenu:Append(wx.wxID_NEW, "&New", "Begin a new drawing") --- 247,251 ---- wx.wxDEFAULT_FRAME_STYLE ) ! -- Create the menubar local fileMenu = wx.wxMenu() fileMenu:Append(wx.wxID_NEW, "&New", "Begin a new drawing") *************** *** 261,272 **** menuBar:Append(fileMenu, "&File") menuBar:Append(helpMenu, "&Help") - frame:SetMenuBar(menuBar) local statusBar = frame:CreateStatusBar(3) local status_width = statusBar:GetTextExtent("88888, 88888") frame:SetStatusWidths({ -1, status_width, status_width }) frame:SetStatusText("Welcome to wxLua Scribble.") frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) --- 264,286 ---- menuBar:Append(fileMenu, "&File") menuBar:Append(helpMenu, "&Help") frame:SetMenuBar(menuBar) + -- Create the statusbar local statusBar = frame:CreateStatusBar(3) local status_width = statusBar:GetTextExtent("88888, 88888") frame:SetStatusWidths({ -1, status_width, status_width }) frame:SetStatusText("Welcome to wxLua Scribble.") + frame:SetStatusText(penColours[itemColour], 1) + -- Create a wxPanel to draw on, always a good idea, it will fill the frame + panel = wx.wxPanel(frame, wx.wxID_ANY) + + panel:Connect(wx.wxEVT_PAINT, OnPaint) + panel:Connect(wx.wxEVT_ERASE_BACKGROUND, function(event) end) -- do nothing + panel:Connect(wx.wxEVT_LEFT_DOWN, OnLeftDown ) + panel:Connect(wx.wxEVT_LEFT_UP, OnLeftUp ) + panel:Connect(wx.wxEVT_MOTION, OnMotion ) + + -- Connect menu events frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) *************** *** 281,286 **** fileName = "" ! frame:SetTitle( "wxLua Scribble") pointsList = {} redrawRequired = true isModified = false --- 295,301 ---- fileName = "" ! frame:SetTitle("wxLua Scribble") pointsList = {} + lastDrawn = 0 redrawRequired = true isModified = false *************** *** 303,313 **** frame:Connect(wx.wxID_SAVE, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) ! local isSaved = false if fileName == "" then ! isSaved = SaveAs() else ! isSaved = SaveScribbles() end ! if isSaved then isModified = false end --- 318,328 ---- frame:Connect(wx.wxID_SAVE, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) ! local saved = false if fileName == "" then ! saved = SaveAs() else ! saved = SaveScribbles() end ! if saved then isModified = false end *************** *** 320,330 **** end end ) frame:Connect(ID_SAVEBITMAP, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) - local w, h = panel:GetClientSize() - local bmp = wx.wxEmptyBitmap(w, h) - DrawBitmap(bmp) - local img = bmp:ConvertToImage() - local fileDialog = wx.wxFileDialog(frame, "Save wxLua scribble file as", --- 335,341 ---- end end ) + frame:Connect(ID_SAVEBITMAP, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) local fileDialog = wx.wxFileDialog(frame, "Save wxLua scribble file as", *************** *** 334,343 **** wx.wxSAVE + wx.wxOVERWRITE_PROMPT) if fileDialog:ShowModal() == wx.wxID_OK then ! img:SaveFile(fileDialog:GetPath()) end fileDialog:Destroy() - bmp:Delete() - img:Delete() end ) --- 345,367 ---- wx.wxSAVE + wx.wxOVERWRITE_PROMPT) if fileDialog:ShowModal() == wx.wxID_OK then ! local w, h = panel:GetClientSize() ! local bmp = wx.wxEmptyBitmap(w, h) ! lastDrawn = 0 -- force redrawing all points ! DrawBitmap(bmp) ! lastDrawn = 0 -- force redrawing all points ! ! local img = bmp:ConvertToImage() ! if not img:SaveFile(fileDialog:GetPath()) then ! wx.wxMessageBox("There was a problem saving the image file\n"..fileDialog:GetPath(), ! "Error saving image", ! wx.wxOK + wx.wxICON_ERROR, ! frame ) ! end ! ! bmp:Delete() ! img:Delete() end fileDialog:Destroy() end ) *************** *** 366,375 **** end if isOkToClose then ! -- prevent paint events using the bitmapDC after we ! -- have deleted them ! isRunning = false bitmap:Delete() ! -- ensure the event is passed on so ! -- that it is handled event:Skip() end --- 390,397 ---- end if isOkToClose then ! -- prevent paint events using the memDC during closing bitmap:Delete() ! bitmap = nil ! -- ensure the event is passed on so that it is handled event:Skip() end Index: calculator.wx.lua =================================================================== RCS file: /cvsroot/wxlua/wxLua/samples/calculator.wx.lua,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** calculator.wx.lua 26 Sep 2006 22:22:00 -0000 1.7 --- calculator.wx.lua 22 Nov 2006 21:13:56 -0000 1.8 *************** *** 200,203 **** --- 200,206 ---- -- try to load the resource and ask for path to it if not found while not xmlResource:Load(xrcFilename) do + -- must unload the file before we try again + xmlResource:Unload(xrcFilename) + wx.wxMessageBox("Error loading xrc resources, please choose the path to 'calculator.xrc'.", "Calculator", |