This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/js-framework/demo.ts

161 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-07-24 19:58:30 +08:00
import { StackConfig, ViewHolder, VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
2019-07-24 19:26:25 +08:00
2019-07-24 16:38:34 +08:00
interface CountModel {
count: number
}
2019-07-18 20:11:01 +08:00
2019-07-24 19:26:25 +08:00
class CounterView extends ViewHolder {
number = new Text
counter = new Text
build(root: Group) {
2019-07-24 16:38:34 +08:00
const vlayout = new VLayout
2019-07-24 19:26:25 +08:00
this.number.textSize = 40
this.number.layoutConfig = {
2019-07-24 16:38:34 +08:00
alignment: new Gravity().center()
2019-07-23 13:01:45 +08:00
}
2019-07-24 19:26:25 +08:00
this.counter = new Text
this.counter.text = "点击计数"
this.counter.textSize = 20
2019-07-24 11:43:38 +08:00
2019-07-24 16:38:34 +08:00
vlayout.space = 20
vlayout.layoutConfig = {
alignment: new Gravity().center()
2019-07-24 11:43:38 +08:00
}
2019-07-24 19:26:25 +08:00
vlayout.addChild(this.number)
vlayout.addChild(this.counter)
2019-07-24 17:24:00 +08:00
2019-07-24 16:38:34 +08:00
root.addChild(vlayout)
2019-07-24 19:26:25 +08:00
}
setNumber(n: number) {
this.number.text = n.toString()
}
2019-07-24 11:43:38 +08:00
2019-07-24 19:26:25 +08:00
setCounter(v: Function) {
this.counter.onClick = v
}
}
2019-07-24 10:14:17 +08:00
2019-07-24 19:26:25 +08:00
class CounterVM extends ViewModel<CountModel, CounterView> {
2019-07-24 20:40:25 +08:00
2019-07-24 19:26:25 +08:00
binding(v: CounterView, model: CountModel): void {
v.setNumber(model.count)
2019-07-24 20:40:25 +08:00
v.setCounter(() => {
this.getModel().count++
})
2019-07-24 16:38:34 +08:00
}
}
2019-07-24 19:26:25 +08:00
class MyPage extends VMPanel<CountModel, CounterView>{
2019-07-24 16:38:34 +08:00
2019-07-24 19:26:25 +08:00
getVMClass() {
return CounterVM
}
getModel() {
return {
2019-07-24 17:24:00 +08:00
count: 0,
add: function () {
this.count++
2019-07-24 19:26:25 +08:00
},
}
2019-07-18 16:29:24 +08:00
}
2019-07-22 14:36:32 +08:00
2019-07-24 19:26:25 +08:00
getViewHolder() {
return new CounterView
}
2019-07-22 13:41:38 +08:00
@NativeCall
2019-07-18 16:29:24 +08:00
log() {
2019-07-22 13:41:38 +08:00
log("Hello.HEGO")
logw("Hello.HEGO")
loge("Hello.HEGO")
2019-07-18 16:29:24 +08:00
}
2019-07-24 19:58:30 +08:00
}
2019-07-24 20:40:25 +08:00
type Postion = { x: number, y: number }
2019-07-24 19:58:30 +08:00
2019-07-24 20:40:25 +08:00
enum Location {
left = 0,
right = 1,
top = 2,
bottom = 3,
}
2019-07-24 19:58:30 +08:00
class Snake {
2019-07-24 20:40:25 +08:00
direction: "left" | "right" | "top" | "bottom" = "right"
length: number = 1
width: number = 1
height: number = 1
head: Postion = { x: 1, y: 0 }
body: Location[] = []
crash() {
}
step() {
switch (this.direction) {
case "left":
const head = this.body[0]
if (head.x - 1 < 0) {
this.crash()
}
head.x -= 1
this.body.reduce((pre, cur) => {
return cur
})
break
case "right":
break
case "top":
break
case "bottom":
break
}
}
2019-07-24 19:58:30 +08:00
}
class SnakeView extends ViewHolder {
build(root: Group): void {
root.bgColor = Color.parse('#000000')
const title = new Text
title.text = "Snake"
title.textSize = 20
title.textColor = Color.parse("#ffffff")
title.layoutConfig = {
alignment: new Gravity().centerX().top(),
margin: {
top: 20
}
} as StackConfig
root.addChild(title)
}
}
class SnakeVM extends ViewModel<Snake, SnakeView>{
binding(v: SnakeView, model: Snake) {
}
}
@Entry
class SnakePanel extends VMPanel<Snake, SnakeView>{
getVMClass() {
return SnakeVM
}
getModel() {
return new Snake
}
getViewHolder() {
return new SnakeView
}
2019-07-18 16:29:24 +08:00
}