import löve user interface
This commit is contained in:
		
							parent
							
								
									bb078d798b
								
							
						
					
					
						commit
						0624b43b82
					
				
							
								
								
									
										33
									
								
								main.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								main.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,33 @@ | |||||||
|  | require "ui.globals" | ||||||
|  | require "ui.config" | ||||||
|  | require "ui.colors" | ||||||
|  | require "ui.init" | ||||||
|  | require "ui.draw" | ||||||
|  | require "ui.keyboard" | ||||||
|  | require "ui.mouse" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | function love.load() | ||||||
|  |     love.window.setTitle("Sudoku!") | ||||||
|  |     love.window.setMode(0, 0) | ||||||
|  |     love.mouse.setGrabbed(false) | ||||||
|  |     love.keyboard.setKeyRepeat(true) | ||||||
|  |     math.randomseed(os.time()) | ||||||
|  |     initSmallNumbers() | ||||||
|  |     initGrid() | ||||||
|  |     messages[1] = "Game Info" | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function love.update(dt) | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function love.draw() | ||||||
|  |     -- love.graphics.setBackgroundColor(colors.backGround) | ||||||
|  |     setBackgroundColor(colors.backGround) | ||||||
|  |     drawGrid() | ||||||
|  |     drawSmallNumbers() | ||||||
|  |     drawCursor() | ||||||
|  |     drawMessages() | ||||||
|  |     love.graphics.setFont(fonts.bigNum) | ||||||
|  |     love.graphics.print("5", grid[5][5].x1+25, grid[5][5].y1+10) | ||||||
|  | end | ||||||
							
								
								
									
										29
									
								
								ui/colors.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								ui/colors.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | |||||||
|  | require "ui.globals" | ||||||
|  | 
 | ||||||
|  | local cached_colors = {} | ||||||
|  | function cHex(rgb) | ||||||
|  |     if cached_colors[rgb] then return unpack(cached_colors[rgb]) end | ||||||
|  |     r = tonumber(string.sub(rgb, 1, 2), 16) / 255 | ||||||
|  |     g = tonumber(string.sub(rgb, 3, 4), 16) / 255 | ||||||
|  |     b = tonumber(string.sub(rgb, 5, 6), 16) / 255 | ||||||
|  |     cached_colors[rgb] = {r, g, b} | ||||||
|  |     return r, g, b | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function setBackgroundColor(c) | ||||||
|  |     love.graphics.setBackgroundColor(cHex(c)) | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function setColor(c) | ||||||
|  |     love.graphics.setColor(cHex(c)) | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | -- colors.smallNumbersDisabled = cHex("EEEEEE") | ||||||
|  | -- colors.smallNumbersEnabled = cHex("0000FF") | ||||||
|  | -- colors.boardCursor = cHex("0000FF") | ||||||
|  | -- colors.backGround = cHex("FFFFFF") | ||||||
|  | -- colors.messagesText = cHex("000000") | ||||||
|  | -- colors.boardThinLines = cHex("808080") | ||||||
|  | -- colors.boardThikLines = cHex("000000") | ||||||
|  | 
 | ||||||
							
								
								
									
										24
									
								
								ui/config.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								ui/config.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | |||||||
|  | require "ui.globals" | ||||||
|  | 
 | ||||||
|  | grid.x1 = 300 | ||||||
|  | grid.y1 = 150 | ||||||
|  | 
 | ||||||
|  | cell.width = 90 | ||||||
|  | cell.height = 90 | ||||||
|  | cell.mx = 10             -- margin x | ||||||
|  | cell.my = 5              -- margin y | ||||||
|  | 
 | ||||||
|  | fonts.bigNum = love.graphics.newFont(64) | ||||||
|  | fonts.smallNum = love.graphics.newFont(24) | ||||||
|  | fonts.info = love.graphics.newFont(24) | ||||||
|  | 
 | ||||||
|  | colors.smallNumbersDisabled = "EEEEEE" | ||||||
|  | colors.smallNumbersEnabled = "0000FF" | ||||||
|  | colors.boardCursor = "0000FF" | ||||||
|  | colors.backGround = "FFFFFF" | ||||||
|  | colors.messagesText = "000000" | ||||||
|  | colors.boardThinLines = "808080" | ||||||
|  | colors.boardThikLines = "000000" | ||||||
|  | 
 | ||||||
|  | cursor.x = 1 | ||||||
|  | cursor.y = 1 | ||||||
							
								
								
									
										51
									
								
								ui/draw.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								ui/draw.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,51 @@ | |||||||
|  | require "ui.globals" | ||||||
|  | require "ui.colors" | ||||||
|  | 
 | ||||||
|  | function drawGrid() | ||||||
|  |     love.graphics.setLineWidth(1) | ||||||
|  |     setColor(colors.boardThinLines) | ||||||
|  |     for i = 1, 6 do | ||||||
|  |         love.graphics.line(grid.vline[i].x1, grid.vline[i].y1, grid.vline[i].x2, grid.vline[i].y2) | ||||||
|  |         love.graphics.line(grid.hline[i].x1, grid.hline[i].y1, grid.hline[i].x2, grid.hline[i].y2) | ||||||
|  |     end | ||||||
|  |     love.graphics.setLineWidth(2) | ||||||
|  |     setColor(colors.boardThikLines) | ||||||
|  |     for i = 1, 4 do | ||||||
|  |         love.graphics.line(grid.vbline[i].x1, grid.vbline[i].y1, grid.vbline[i].x2, grid.vbline[i].y2) | ||||||
|  |         love.graphics.line(grid.hbline[i].x1, grid.hbline[i].y1, grid.hbline[i].x2, grid.hbline[i].y2) | ||||||
|  |     end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function drawSmallNumbers() | ||||||
|  |     love.graphics.setFont(fonts.smallNum) | ||||||
|  |     for x = 1, 9 do | ||||||
|  |         for y = 1, 9 do | ||||||
|  |             for i = 1, 9 do | ||||||
|  |                 local x1 = smallNumbers[x][y][i].x | ||||||
|  |                 local y1 = smallNumbers[x][y][i].y | ||||||
|  |                 if smallNumbersVal[x][y][i] then | ||||||
|  |                     setColor(colors.smallNumbersEnabled) | ||||||
|  |                 else | ||||||
|  |                     setColor(colors.smallNumbersDisabled) | ||||||
|  |                 end | ||||||
|  |                 love.graphics.print(tostring(i), x1, y1) | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function drawCursor() | ||||||
|  |     setColor(colors.boardCursor) | ||||||
|  |     local x = grid[cursor.x][cursor.y].x1 | ||||||
|  |     local y = grid[cursor.x][cursor.y].y1 | ||||||
|  |     love.graphics.setLineWidth(3) | ||||||
|  |     love.graphics.rectangle("line", x+3, y+3, cell.width-6, cell.height-6, 10, 10) | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function drawMessages() | ||||||
|  |     setColor(colors.messagesText) | ||||||
|  |     love.graphics.setFont(fonts.info) | ||||||
|  |     for i, msg in ipairs(messages) do | ||||||
|  |         love.graphics.print(msg, 25, 150 + 24 * (i-1)) | ||||||
|  |     end | ||||||
|  | end | ||||||
							
								
								
									
										9
									
								
								ui/globals.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								ui/globals.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | |||||||
|  | grid = {} | ||||||
|  | cell = {} | ||||||
|  | fonts = {} | ||||||
|  | colors = {} | ||||||
|  | messages = {} | ||||||
|  | cursor = {} | ||||||
|  | 
 | ||||||
|  | smallNumbers = {} | ||||||
|  | smallNumbersVal = {} | ||||||
							
								
								
									
										71
									
								
								ui/init.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								ui/init.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,71 @@ | |||||||
|  | function initSmallNumbers() | ||||||
|  |     local dx = math.floor((cell.width - 2 * cell.mx) / 3) + 2 | ||||||
|  |     local dy = math.floor((cell.width - 2 * cell.my) / 3) | ||||||
|  |     for x = 1, 9 do | ||||||
|  |         smallNumbers[x] = {} | ||||||
|  |         smallNumbersVal[x] = {} | ||||||
|  |         for y = 1, 9 do | ||||||
|  |             smallNumbers[x][y] = {} | ||||||
|  |             smallNumbersVal[x][y] = {} | ||||||
|  |             local x1 = grid.x1 + (x - 1) * cell.width | ||||||
|  |             local y1 = grid.y1 + (y - 1) * cell.height | ||||||
|  |             local n = 1 | ||||||
|  |             for j = 1, 3 do | ||||||
|  |                 for i = 1, 3 do | ||||||
|  |                     local x2 = x1 + cell.mx + (i-1) * dx | ||||||
|  |                     local y2 = y1 + cell.my + (j-1) * dy | ||||||
|  |                     smallNumbers[x][y][n] = {x=x2, y=y2} | ||||||
|  |                     smallNumbersVal[x][y][n] = false | ||||||
|  |                     n = n + 1 | ||||||
|  |                 end | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     smallNumbersVal[8][5][4] = true | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function initGrid() | ||||||
|  |     grid.x2 = grid.x1 + 9 * cell.width | ||||||
|  |     grid.y2 = grid.y1 + 9 * cell.height | ||||||
|  |     for i = 1, 9 do | ||||||
|  |         grid[i] = {} | ||||||
|  |         for j = 1, 9 do | ||||||
|  |             local x = grid.x1 + (i - 1) * cell.width | ||||||
|  |             local y = grid.y1 + (j - 1) * cell.height | ||||||
|  |             grid[i][j] = {x1=x, y1=y, x2=x+cell.width, y2=y+cell.height} | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     local n = 0 | ||||||
|  |     local m = 0 | ||||||
|  |     grid.vline = {} | ||||||
|  |     for x = grid.x1, grid.x1 + 9 * cell.width, cell.width do | ||||||
|  |         n = n + 1 | ||||||
|  |         if n % 3 ~= 1 then | ||||||
|  |             m = m + 1 | ||||||
|  |             grid.vline[m] = {x1=x, y1=grid.y1, x2=x, y2=grid.y1+9*cell.height} | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     n = 0 | ||||||
|  |     m = 0 | ||||||
|  |     grid.hline = {} | ||||||
|  |     for y = grid.y1, grid.y1 + 9 * cell.height, cell.height do | ||||||
|  |         n = n + 1 | ||||||
|  |         if n % 3 ~= 1 then | ||||||
|  |             m = m + 1 | ||||||
|  |             grid.hline[m] = {x1=grid.x1, y1=y, x2=grid.x1+9*cell.width, y2=y} | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     n = 0 | ||||||
|  |     grid.vbline = {} | ||||||
|  |     for x = grid.x1, grid.x1+9 * cell.width, cell.width * 3 do | ||||||
|  |         n = n + 1 | ||||||
|  |         grid.vbline[n] = {x1=x, y1=grid.y1, x2=x, y2=grid.y1+9*cell.height} | ||||||
|  |     end | ||||||
|  |     n = 0 | ||||||
|  |     grid.hbline = {} | ||||||
|  |     for y = grid.y1, grid.y1+9*cell.height, cell.height * 3 do | ||||||
|  |         n = n + 1 | ||||||
|  |         grid.hbline[n] = {x1=grid.x1, y1=y, x2=grid.x1+9*cell.width, y2=y} | ||||||
|  |     end | ||||||
|  | end | ||||||
|  | 
 | ||||||
							
								
								
									
										40
									
								
								ui/keyboard.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								ui/keyboard.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,40 @@ | |||||||
|  | function love.keypressed(key) | ||||||
|  |     if key == "escape" or key == "q" then | ||||||
|  |         love.event.quit() | ||||||
|  |     elseif key == "right" or key == "l" then | ||||||
|  |         cursor.x = cursor.x + 1 | ||||||
|  |         if cursor.x > 9 then cursor.x = 1 end | ||||||
|  |     elseif key == "left" or key == "h" then | ||||||
|  |         cursor.x = cursor.x - 1 | ||||||
|  |         if cursor.x < 1 then cursor.x = 9 end | ||||||
|  |     elseif key == "down" or key == "j" then | ||||||
|  |         cursor.y = cursor.y + 1 | ||||||
|  |         if cursor.y > 9 then cursor.y = 1 end | ||||||
|  |     elseif key == "up" or key == "k" then | ||||||
|  |         cursor.y = cursor.y - 1 | ||||||
|  |         if cursor.y < 1 then cursor.y = 9 end | ||||||
|  |     -- elseif key == "n" then | ||||||
|  |     --     cursor.x = cursor.x - 1 | ||||||
|  |     --     if cursor.x < 1 then cursor.x = 9 end | ||||||
|  |     --     cursor.y = cursor.y + 1 | ||||||
|  |     --     if cursor.y > 9 then cursor.y = 1 end | ||||||
|  |     elseif key == "1" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][1] = not smallNumbersVal[cursor.x][cursor.y][1] | ||||||
|  |     elseif key == "2" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][2] = not smallNumbersVal[cursor.x][cursor.y][2] | ||||||
|  |     elseif key == "3" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][3] = not smallNumbersVal[cursor.x][cursor.y][3] | ||||||
|  |     elseif key == "4" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][4] = not smallNumbersVal[cursor.x][cursor.y][4] | ||||||
|  |     elseif key == "5" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][5] = not smallNumbersVal[cursor.x][cursor.y][5] | ||||||
|  |     elseif key == "6" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][6] = not smallNumbersVal[cursor.x][cursor.y][6] | ||||||
|  |     elseif key == "7" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][7] = not smallNumbersVal[cursor.x][cursor.y][7] | ||||||
|  |     elseif key == "8" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][8] = not smallNumbersVal[cursor.x][cursor.y][8] | ||||||
|  |     elseif key == "9" then | ||||||
|  |         smallNumbersVal[cursor.x][cursor.y][9] = not smallNumbersVal[cursor.x][cursor.y][9] | ||||||
|  |     end | ||||||
|  | end | ||||||
							
								
								
									
										29
									
								
								ui/mouse.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								ui/mouse.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | |||||||
|  | function love.mousepressed(x, y, button, istouch) | ||||||
|  |     local inBoard = false | ||||||
|  |     local dx = math.floor(cell.width / 3) | ||||||
|  |     local dy = math.floor(cell.height / 3) | ||||||
|  |     local cx = 0 | ||||||
|  |     local cy = 0 | ||||||
|  |     local mx = 0 | ||||||
|  |     local my = 0 | ||||||
|  |     local n = 0 | ||||||
|  |     if x > grid.x1 and x < grid.x2 and y > grid.y1 and y < grid.y2 then | ||||||
|  |         inBoard = true | ||||||
|  |         cx = math.floor((x - grid.x1) / cell.width) + 1 | ||||||
|  |         cy = math.floor((y - grid.y1) / cell.height) + 1 | ||||||
|  |         mx = math.floor((x - grid[cx][cy].x1) / dx) + 1 | ||||||
|  |         my = math.floor((y - grid[cx][cy].y1) / dy) + 1 | ||||||
|  |         n = mx + 3 * (my - 1) | ||||||
|  |     end | ||||||
|  |     if button == 1 then | ||||||
|  |         if cursor.x == cx and cursor.y == cy then | ||||||
|  |             smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n] | ||||||
|  |         else | ||||||
|  |             cursor.x = cx | ||||||
|  |             cursor.y = cy | ||||||
|  |         end | ||||||
|  |     elseif button == 2 then | ||||||
|  |         smallNumbersVal[cx][cy][n] = not smallNumbersVal[cx][cy][n] | ||||||
|  |     end | ||||||
|  | end | ||||||
|  | 
 | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user