From ed2e2c50063e9d2f36828008592885a6238779ad Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 20 May 2020 20:18:52 +0930 Subject: [PATCH] Drawing works(?) --- scripts/GameField.gd | 95 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 5 deletions(-) diff --git a/scripts/GameField.gd b/scripts/GameField.gd index 3b6d6cc..d709f20 100644 --- a/scripts/GameField.gd +++ b/scripts/GameField.gd @@ -28,8 +28,11 @@ func draw_string_centered(font, position, string, color := Color.white): draw_string(font, Vector2(position.x - font.get_string_size(string).x/2.0, position.y + font.get_ascent()), string, color) const font := preload('res://dynamicfont.tres') -const COLORS = [null, Color.lightgreen, Color.lightyellow, Color.lightblue, Color.lightpink] +const COLORS = [null, Color.lightgreen, Color.lightyellow, Color.lightblue, Color.purple] +const N_COLORS = 5 +var grid_0 := Vector2(0.0, 0.0) +var grid_space := Vector2(1.0, 1.0) var h0 := 0.0 var v0 := 0.0 var h_space := 1.0 @@ -43,15 +46,20 @@ func update_grid_spacing() -> void: v_space = h_space h0 = h_space * h_margin v0 = v_space * v_margin + grid_0 = Vector2(h0, v0) + grid_space = Vector2(h_space, v_space) func grid_corner(row, col) -> Vector2: - return Vector2(h0+col*h_space, v0+row*v_space) + return (Vector2(col, row) * grid_space) + grid_0 + +func to_grid_space(position: Vector2) -> Vector2: + return (position - grid_0)/grid_space func _draw() -> void: if not READY: return update_grid_spacing() - font.set_size(v_space-5) + font.set_size(int(ceil(v_space-5))) # Colors for row in rows: @@ -91,12 +99,14 @@ func _draw() -> void: else: draw_line(grid_corner(row, cols), grid_corner(row+1, cols), Color.white) + # Arc pencil marks + # Numbers for row in rows: for col in cols: var num = cell_numbers.get_cell(row, col) if num < 4: - draw_string_centered(font, Vector2(h0+(col+0.5)*h_space, v0+row*v_space), str(num), Color.black) + draw_string_centered(font, grid_corner(row, col+0.5), str(num), Color.black) # COORDS for row in rows: @@ -162,7 +172,7 @@ func load_puzzle(filename: String): -1: corner_marks.set_flag(row, col, CornerMark.X_DOWN) col += 1 - 3: # hor lines + 3: # horiz lines var line = file.get_csv_line(' ') var col = 0 for numstr in line: @@ -179,4 +189,79 @@ func load_puzzle(filename: String): file.close() READY = true +enum DragAction {TEST_LINE, DRAW_LINE, REMOVE_LINE, DRAW_X, REMOVE_X, DRAW_COLOR} +var drag_action = 0 +var drag_color := 0 +func _input(event: InputEvent) -> void: + if event is InputEventMouse: + var gridpos = to_grid_space(event.position) + var dx = fmod(gridpos.x, 1.0) + var dy = fmod(gridpos.y, 1.0) + var adx = 0.5 - abs(dx-0.5) + var ady = 0.5 - abs(dy-0.5) + var threshold = 0.2 + var sel_col + var sel_row + + if event is InputEventMouseButton and event.pressed: + if adx < threshold: + if ady < threshold: + drag_action = DragAction.TEST_LINE + else: + sel_row = int(gridpos.y) + sel_col = int(round(gridpos.x)) + if corner_marks.get_flag(sel_row, sel_col, CornerMark.X_DOWN): + drag_action = DragAction.REMOVE_X + corner_marks.clear_flag(sel_row, sel_col, CornerMark.X_DOWN) + else: + drag_action = DragAction.DRAW_X + corner_marks.set_flag(sel_row, sel_col, CornerMark.X_DOWN) + else: + if ady < threshold: + sel_row = int(round(gridpos.y)) + sel_col = int(gridpos.x) + if corner_marks.get_flag(sel_row, sel_col, CornerMark.X_RIGHT): + drag_action = DragAction.REMOVE_X + corner_marks.clear_flag(sel_row, sel_col, CornerMark.X_RIGHT) + else: + drag_action = DragAction.DRAW_X + corner_marks.set_flag(sel_row, sel_col, CornerMark.X_RIGHT) + else: + sel_row = int(gridpos.y) + sel_col = int(gridpos.x) + drag_action = DragAction.DRAW_COLOR + drag_color = posmod(cell_colors.get_cell(sel_row, sel_col) + (1 if event.button_index==BUTTON_LEFT else -1), N_COLORS) + cell_colors.set_cell(sel_row, sel_col, drag_color) + + elif event is InputEventMouseMotion and event.button_mask: + if drag_action == DragAction.TEST_LINE: + if adx < threshold and ady > threshold: + drag_action = DragAction.REMOVE_LINE if corner_marks.get_flag(int(gridpos.y), int(round(gridpos.x)), CornerMark.L_DOWN) else DragAction.DRAW_LINE + elif adx > threshold and ady < threshold: + drag_action = DragAction.REMOVE_LINE if corner_marks.get_flag(int(round(gridpos.y)), int(gridpos.x), CornerMark.L_RIGHT) else DragAction.DRAW_LINE + match drag_action: + DragAction.DRAW_LINE: + if adx < threshold and ady > threshold: + corner_marks.set_flag(int(gridpos.y), int(round(gridpos.x)), CornerMark.L_DOWN) + elif adx > threshold and ady < threshold: + corner_marks.set_flag(int(round(gridpos.y)), int(gridpos.x), CornerMark.L_RIGHT) + DragAction.REMOVE_LINE: + if adx < threshold and ady > threshold: + corner_marks.clear_flag(int(gridpos.y), int(round(gridpos.x)), CornerMark.L_DOWN) + elif adx > threshold and ady < threshold: + corner_marks.clear_flag(int(round(gridpos.y)), int(gridpos.x), CornerMark.L_RIGHT) + DragAction.DRAW_X: + if adx < threshold and ady > threshold: + corner_marks.set_flag(int(gridpos.y), int(round(gridpos.x)), CornerMark.X_DOWN) + elif adx > threshold and ady < threshold: + corner_marks.set_flag(int(round(gridpos.y)), int(gridpos.x), CornerMark.X_RIGHT) + DragAction.REMOVE_X: + if adx < threshold and ady > threshold: + corner_marks.clear_flag(int(gridpos.y), int(round(gridpos.x)), CornerMark.X_DOWN) + elif adx > threshold and ady < threshold: + corner_marks.clear_flag(int(round(gridpos.y)), int(gridpos.x), CornerMark.X_RIGHT) + DragAction.DRAW_COLOR: + sel_row = int(gridpos.y) + sel_col = int(gridpos.x) + cell_colors.set_cell(sel_row, sel_col, drag_color)