feat:adjust anchor style
This commit is contained in:
parent
a459e0440d
commit
1b7952d1ae
@ -1,5 +1,5 @@
|
|||||||
import { Stack, Group, Color, stack, layoutConfig, LayoutSpec, vlayout, IVLayout, Text, ViewHolder, ViewModel, VMPanel, scroller } from "doric";
|
import { Stack, hlayout, Group, Color, stack, layoutConfig, LayoutSpec, vlayout, IVLayout, Text, ViewHolder, ViewModel, VMPanel, scroller, modal, text, gravity, Gravity, IHLayout, takeNonNull } from "doric";
|
||||||
import { title } from "./utils";
|
import { colors } from "./utils";
|
||||||
|
|
||||||
|
|
||||||
const lineColor = Color.BLACK
|
const lineColor = Color.BLACK
|
||||||
@ -39,13 +39,17 @@ interface GoBangState {
|
|||||||
gap: number
|
gap: number
|
||||||
role: "white" | "black"
|
role: "white" | "black"
|
||||||
matrix: Map<number, State>
|
matrix: Map<number, State>
|
||||||
|
anchor?: { x: number, y: number }
|
||||||
}
|
}
|
||||||
|
|
||||||
class GoBangVH extends ViewHolder {
|
class GoBangVH extends ViewHolder {
|
||||||
pieces!: Stack
|
pieces!: Stack
|
||||||
root!: Group
|
root!: Group
|
||||||
gap = 0
|
gap = 0
|
||||||
|
currentRole!: Text
|
||||||
|
result!: Text
|
||||||
onPieceDown?: (x: number, y: number) => void
|
onPieceDown?: (x: number, y: number) => void
|
||||||
|
onAnchorDown?: (x: number, y: number) => void
|
||||||
build(root: Group): void {
|
build(root: Group): void {
|
||||||
this.root = root
|
this.root = root
|
||||||
}
|
}
|
||||||
@ -53,11 +57,18 @@ class GoBangVH extends ViewHolder {
|
|||||||
const boardSize = state.gap * (state.count - 1)
|
const boardSize = state.gap * (state.count - 1)
|
||||||
const gap = state.gap
|
const gap = state.gap
|
||||||
const borderWidth = gap
|
const borderWidth = gap
|
||||||
let hintText: Text
|
|
||||||
this.gap = state.gap
|
this.gap = state.gap
|
||||||
scroller(
|
scroller(
|
||||||
vlayout([
|
vlayout([
|
||||||
title("GoBang"),
|
text({
|
||||||
|
text: "五子棋",
|
||||||
|
layoutConfig: layoutConfig().configWidth(LayoutSpec.MOST),
|
||||||
|
textSize: 30,
|
||||||
|
textColor: Color.WHITE,
|
||||||
|
backgroundColor: colors[0],
|
||||||
|
textAlignment: gravity().center(),
|
||||||
|
height: 50,
|
||||||
|
}),
|
||||||
stack([
|
stack([
|
||||||
stack([
|
stack([
|
||||||
...(new Array(count - 2)).fill(0).map((_, idx) => {
|
...(new Array(count - 2)).fill(0).map((_, idx) => {
|
||||||
@ -91,9 +102,8 @@ class GoBangVH extends ViewHolder {
|
|||||||
v.top = (row - 0.5) * gap + borderWidth
|
v.top = (row - 0.5) * gap + borderWidth
|
||||||
v.left = (colum - 0.5) * gap + borderWidth
|
v.left = (colum - 0.5) * gap + borderWidth
|
||||||
v.onClick = () => {
|
v.onClick = () => {
|
||||||
hintText.text = `row:${row},colum:${colum}`
|
if (this.onAnchorDown) {
|
||||||
if (this.onPieceDown) {
|
this.onAnchorDown(colum, row)
|
||||||
this.onPieceDown(colum, row)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -107,7 +117,26 @@ class GoBangVH extends ViewHolder {
|
|||||||
height: boardSize + 2 * borderWidth,
|
height: boardSize + 2 * borderWidth,
|
||||||
backgroundColor: Color.parse("#E6B080"),
|
backgroundColor: Color.parse("#E6B080"),
|
||||||
}),
|
}),
|
||||||
hintText = title('Hint'),
|
hlayout([
|
||||||
|
this.currentRole = text({
|
||||||
|
text: "当前:",
|
||||||
|
textSize: 20,
|
||||||
|
textColor: Color.WHITE,
|
||||||
|
layoutConfig: layoutConfig().just().configWeight(1),
|
||||||
|
height: 50,
|
||||||
|
backgroundColor: colors[1],
|
||||||
|
}),
|
||||||
|
this.result = text({
|
||||||
|
text: "获胜方:",
|
||||||
|
textSize: 20,
|
||||||
|
textColor: Color.WHITE,
|
||||||
|
layoutConfig: layoutConfig().just().configWeight(1),
|
||||||
|
height: 50,
|
||||||
|
backgroundColor: colors[2],
|
||||||
|
}),
|
||||||
|
]).apply({
|
||||||
|
layoutConfig: layoutConfig().fit().configWidth(LayoutSpec.MOST),
|
||||||
|
} as IHLayout),
|
||||||
])
|
])
|
||||||
.apply({
|
.apply({
|
||||||
layoutConfig: layoutConfig().fit(),
|
layoutConfig: layoutConfig().fit(),
|
||||||
@ -120,7 +149,7 @@ class GoBangVH extends ViewHolder {
|
|||||||
const x = Math.floor(pos / count)
|
const x = Math.floor(pos / count)
|
||||||
const y = pos % count
|
const y = pos % count
|
||||||
const piece = (new Stack).also(v => {
|
const piece = (new Stack).also(v => {
|
||||||
v.width = v.height = 30
|
v.width = v.height = this.gap
|
||||||
v.corners = 15
|
v.corners = 15
|
||||||
v.backgroundColor = role === 'black' ? Color.BLACK : Color.WHITE
|
v.backgroundColor = role === 'black' ? Color.BLACK : Color.WHITE
|
||||||
})
|
})
|
||||||
@ -128,20 +157,56 @@ class GoBangVH extends ViewHolder {
|
|||||||
piece.centerY = (y + 1) * this.gap
|
piece.centerY = (y + 1) * this.gap
|
||||||
this.pieces.addChild(piece)
|
this.pieces.addChild(piece)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addAnchor(x: number, y: number) {
|
||||||
|
const piece = (new Stack).also(v => {
|
||||||
|
v.width = v.height = 30
|
||||||
|
v.border = {
|
||||||
|
color: Color.RED,
|
||||||
|
width: 1,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
piece.centerX = (x + 1) * this.gap
|
||||||
|
piece.centerY = (y + 1) * this.gap
|
||||||
|
piece.onClick = () => {
|
||||||
|
if (this.onPieceDown) {
|
||||||
|
this.onPieceDown(x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.pieces.addChild(piece)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GoBangVM extends ViewModel<GoBangState, GoBangVH>{
|
class GoBangVM extends ViewModel<GoBangState, GoBangVH>{
|
||||||
onAttached(state: GoBangState, vh: GoBangVH) {
|
onAttached(state: GoBangState, vh: GoBangVH) {
|
||||||
vh.actualBuild(state)
|
vh.actualBuild(state)
|
||||||
|
vh.onAnchorDown = (x, y) => {
|
||||||
|
const pos = x * count + y
|
||||||
|
if (state.matrix.get(pos) == State.BLACK
|
||||||
|
|| state.matrix.get(pos) == State.WHITE) {
|
||||||
|
modal(context).toast('This position had been token.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this.updateState(it => {
|
||||||
|
it.anchor = { x, y }
|
||||||
|
})
|
||||||
|
}
|
||||||
vh.onPieceDown = (x, y) => {
|
vh.onPieceDown = (x, y) => {
|
||||||
|
const pos = x * count + y
|
||||||
|
if (state.matrix.get(pos) == State.BLACK
|
||||||
|
|| state.matrix.get(pos) == State.WHITE) {
|
||||||
|
modal(context).toast('This position had been token.')
|
||||||
|
return
|
||||||
|
}
|
||||||
this.updateState(it => {
|
this.updateState(it => {
|
||||||
if (it.role === 'black') {
|
if (it.role === 'black') {
|
||||||
it.matrix.set(x * count + y, State.BLACK)
|
it.matrix.set(pos, State.BLACK)
|
||||||
it.role = 'white'
|
it.role = 'white'
|
||||||
} else {
|
} else {
|
||||||
it.matrix.set(x * count + y, State.WHITE)
|
it.matrix.set(pos, State.WHITE)
|
||||||
it.role = 'black'
|
it.role = 'black'
|
||||||
}
|
}
|
||||||
|
it.anchor = undefined
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -162,6 +227,10 @@ class GoBangVM extends ViewModel<GoBangState, GoBangVH>{
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
takeNonNull(state.anchor)(it => {
|
||||||
|
vh.addAnchor(it.x, it.y)
|
||||||
|
})
|
||||||
|
vh.currentRole.text = `当前: ${(state.role === 'black') ? "黑方" : "白方"}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,7 +242,7 @@ class Gobang extends VMPanel<GoBangState, GoBangVH> {
|
|||||||
getState(): GoBangState {
|
getState(): GoBangState {
|
||||||
return {
|
return {
|
||||||
count,
|
count,
|
||||||
gap: 40,
|
gap: this.getRootView().width / 14,
|
||||||
role: "black",
|
role: "black",
|
||||||
matrix: new Map
|
matrix: new Map
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user