【按钮状态操作】

This commit is contained in:
weizhihong 2023-08-17 17:14:47 +08:00
parent c44a6a5db2
commit 6f601c8d4a
4 changed files with 376 additions and 5 deletions

15
components/button.go Normal file
View File

@ -0,0 +1,15 @@
package components
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/state"
)
// 按钮状态组件
var ButtonState = ecs.NewComponentType[state.ButtonState]()
// 按钮操作组件
var ButtonPressOperating = ecs.NewComponentType[state.ButtonPressOperating]()
// 按钮确认组件
var ButtonConfirmOperating = ecs.NewComponentType[state.ButtonConfirmOperating]()

View File

@ -134,4 +134,30 @@ message PsdCellOperating{
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//
message ButtonState {
//
bool pressDown = 1;
}
//
message ButtonPressOperating {
//
bool start = 1;
// (true)(false)
bool down = 2;
//
int64 operateTime = 3;
//
bool needConfirm = 4;
}
//
message ButtonConfirmOperating {
//
bool confirm = 1;
//
bool cancel = 2;
}

View File

@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.31.0
// protoc-gen-go v1.28.1
// protoc v4.23.1
// source: status.proto
@ -751,6 +751,190 @@ func (x *PsdCellOperating) GetOperateTime() int64 {
return 0
}
// //////////////////////////////////////////////////////
// 按钮状态
type ButtonState struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// 是否按下
PressDown bool `protobuf:"varint,1,opt,name=pressDown,proto3" json:"pressDown,omitempty"`
}
func (x *ButtonState) Reset() {
*x = ButtonState{}
if protoimpl.UnsafeEnabled {
mi := &file_status_proto_msgTypes[10]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ButtonState) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ButtonState) ProtoMessage() {}
func (x *ButtonState) ProtoReflect() protoreflect.Message {
mi := &file_status_proto_msgTypes[10]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ButtonState.ProtoReflect.Descriptor instead.
func (*ButtonState) Descriptor() ([]byte, []int) {
return file_status_proto_rawDescGZIP(), []int{10}
}
func (x *ButtonState) GetPressDown() bool {
if x != nil {
return x.PressDown
}
return false
}
// 按钮按下、抬起操作
type ButtonPressOperating struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// 开始操作
Start bool `protobuf:"varint,1,opt,name=start,proto3" json:"start,omitempty"`
// 是否按下(true)、抬起(false)
Down bool `protobuf:"varint,2,opt,name=down,proto3" json:"down,omitempty"`
// 倒计时
OperateTime int64 `protobuf:"varint,3,opt,name=operateTime,proto3" json:"operateTime,omitempty"`
// 需确认
NeedConfirm bool `protobuf:"varint,4,opt,name=needConfirm,proto3" json:"needConfirm,omitempty"`
}
func (x *ButtonPressOperating) Reset() {
*x = ButtonPressOperating{}
if protoimpl.UnsafeEnabled {
mi := &file_status_proto_msgTypes[11]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ButtonPressOperating) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ButtonPressOperating) ProtoMessage() {}
func (x *ButtonPressOperating) ProtoReflect() protoreflect.Message {
mi := &file_status_proto_msgTypes[11]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ButtonPressOperating.ProtoReflect.Descriptor instead.
func (*ButtonPressOperating) Descriptor() ([]byte, []int) {
return file_status_proto_rawDescGZIP(), []int{11}
}
func (x *ButtonPressOperating) GetStart() bool {
if x != nil {
return x.Start
}
return false
}
func (x *ButtonPressOperating) GetDown() bool {
if x != nil {
return x.Down
}
return false
}
func (x *ButtonPressOperating) GetOperateTime() int64 {
if x != nil {
return x.OperateTime
}
return 0
}
func (x *ButtonPressOperating) GetNeedConfirm() bool {
if x != nil {
return x.NeedConfirm
}
return false
}
// 按钮确认操作
type ButtonConfirmOperating struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// 确认操作
Confirm bool `protobuf:"varint,1,opt,name=confirm,proto3" json:"confirm,omitempty"`
// 取消
Cancel bool `protobuf:"varint,2,opt,name=cancel,proto3" json:"cancel,omitempty"`
}
func (x *ButtonConfirmOperating) Reset() {
*x = ButtonConfirmOperating{}
if protoimpl.UnsafeEnabled {
mi := &file_status_proto_msgTypes[12]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ButtonConfirmOperating) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ButtonConfirmOperating) ProtoMessage() {}
func (x *ButtonConfirmOperating) ProtoReflect() protoreflect.Message {
mi := &file_status_proto_msgTypes[12]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ButtonConfirmOperating.ProtoReflect.Descriptor instead.
func (*ButtonConfirmOperating) Descriptor() ([]byte, []int) {
return file_status_proto_rawDescGZIP(), []int{12}
}
func (x *ButtonConfirmOperating) GetConfirm() bool {
if x != nil {
return x.Confirm
}
return false
}
func (x *ButtonConfirmOperating) GetCancel() bool {
if x != nil {
return x.Cancel
}
return false
}
var File_status_proto protoreflect.FileDescriptor
var file_status_proto_rawDesc = []byte{
@ -817,7 +1001,23 @@ var file_status_proto_rawDesc = []byte{
0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x6d, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01,
0x28, 0x03, 0x52, 0x07, 0x73, 0x75, 0x6d, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x6f,
0x70, 0x65, 0x72, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03,
0x52, 0x0b, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x2a, 0x81, 0x01,
0x52, 0x0b, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x22, 0x2b, 0x0a,
0x0b, 0x42, 0x75, 0x74, 0x74, 0x6f, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x1c, 0x0a, 0x09,
0x70, 0x72, 0x65, 0x73, 0x73, 0x44, 0x6f, 0x77, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52,
0x09, 0x70, 0x72, 0x65, 0x73, 0x73, 0x44, 0x6f, 0x77, 0x6e, 0x22, 0x84, 0x01, 0x0a, 0x14, 0x42,
0x75, 0x74, 0x74, 0x6f, 0x6e, 0x50, 0x72, 0x65, 0x73, 0x73, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74,
0x69, 0x6e, 0x67, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x01, 0x20, 0x01,
0x28, 0x08, 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x6f, 0x77,
0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x64, 0x6f, 0x77, 0x6e, 0x12, 0x20, 0x0a,
0x0b, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01,
0x28, 0x03, 0x52, 0x0b, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12,
0x20, 0x0a, 0x0b, 0x6e, 0x65, 0x65, 0x64, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x18, 0x04,
0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x6e, 0x65, 0x65, 0x64, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x72,
0x6d, 0x22, 0x4a, 0x0a, 0x16, 0x42, 0x75, 0x74, 0x74, 0x6f, 0x6e, 0x43, 0x6f, 0x6e, 0x66, 0x69,
0x72, 0x6d, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x12, 0x18, 0x0a, 0x07, 0x63,
0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x63, 0x6f,
0x6e, 0x66, 0x69, 0x72, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x18,
0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x63, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x2a, 0x81, 0x01,
0x0a, 0x0c, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x41, 0x73, 0x70, 0x65, 0x63, 0x74, 0x12, 0x06,
0x0a, 0x02, 0x4e, 0x6f, 0x10, 0x00, 0x12, 0x05, 0x0a, 0x01, 0x52, 0x10, 0x01, 0x12, 0x05, 0x0a,
0x01, 0x47, 0x10, 0x02, 0x12, 0x05, 0x0a, 0x01, 0x59, 0x10, 0x03, 0x12, 0x05, 0x0a, 0x01, 0x57,
@ -843,7 +1043,7 @@ func file_status_proto_rawDescGZIP() []byte {
}
var file_status_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_status_proto_msgTypes = make([]protoimpl.MessageInfo, 10)
var file_status_proto_msgTypes = make([]protoimpl.MessageInfo, 13)
var file_status_proto_goTypes = []interface{}{
(SignalAspect)(0), // 0: state.SignalAspect
(*DeviceIdentity)(nil), // 1: state.DeviceIdentity
@ -856,6 +1056,9 @@ var file_status_proto_goTypes = []interface{}{
(*PsdCellState)(nil), // 8: state.PsdCellState
(*PsdState)(nil), // 9: state.PsdState
(*PsdCellOperating)(nil), // 10: state.PsdCellOperating
(*ButtonState)(nil), // 11: state.ButtonState
(*ButtonPressOperating)(nil), // 12: state.ButtonPressOperating
(*ButtonConfirmOperating)(nil), // 13: state.ButtonConfirmOperating
}
var file_status_proto_depIdxs = []int32{
0, // 0: state.SignalState.display:type_name -> state.SignalAspect
@ -993,6 +1196,42 @@ func file_status_proto_init() {
return nil
}
}
file_status_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ButtonState); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_status_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ButtonPressOperating); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_status_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ButtonConfirmOperating); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
@ -1000,7 +1239,7 @@ func file_status_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_status_proto_rawDesc,
NumEnums: 1,
NumMessages: 10,
NumMessages: 13,
NumExtensions: 0,
NumServices: 0,
},

91
system/button_system.go Normal file
View File

@ -0,0 +1,91 @@
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
)
// 按钮系统
type ButtonSystem struct {
}
// 按钮组件
var btnIdCom, btnStaCom, btnPreOperCom, btnConOperCom = components.ComDeviceIdentity, components.ButtonState, components.ButtonPressOperating, components.ButtonConfirmOperating
var btnIdOperQuery = ecs.NewQuery(filter.Contains(btnIdCom, btnPreOperCom, btnConOperCom))
var btnUpdateQuery = ecs.NewQuery(filter.Contains(btnStaCom, btnPreOperCom, btnConOperCom))
func NewButtonSystem() *ButtonSystem {
return &ButtonSystem{}
}
// 按钮操作
func PressDownAndUpButton(w ecs.World, btnId string, down bool) bool {
buttonEntry := queryButton(w, btnId)
if buttonEntry == nil {
return false
}
t, b := getButtonBaseInfo(btnId)
preOper := btnPreOperCom.Get(buttonEntry)
preOper.Start, preOper.Down, preOper.NeedConfirm, preOper.OperateTime = true, down, b, t
return true
}
// 确认操作
func ConfirmButton(w ecs.World, btnId string, confirm bool) bool {
buttonEntry := queryButton(w, btnId)
if buttonEntry == nil {
return false
}
conOper := btnConOperCom.Get(buttonEntry)
conOper.Confirm, conOper.Cancel = confirm, !confirm
return true
}
// 更新按钮状态
func (bs *ButtonSystem) Update(w ecs.World) {
btnUpdateQuery.Each(w, func(e *ecs.Entry) {
btnPreOper := btnPreOperCom.Get(e)
if btnPreOper.Start { // 按钮开始操作
if btnPreOper.OperateTime <= 0 { // 按钮操作时间大于0
btnPreOper.Start, btnPreOper.OperateTime = false, 0
} else {
btnPreOper.OperateTime -= int64(w.Tick())
if btnPreOper.NeedConfirm {
conOper := btnConOperCom.Get(e)
if conOper.Cancel || conOper.Confirm {
if conOper.Confirm {
btnSta := btnStaCom.Get(e)
btnSta.PressDown = btnPreOper.Down
}
btnPreOper.Start, btnPreOper.OperateTime = false, 0
conOper.Cancel, conOper.Confirm = false, false
}
} else {
btnSta := btnStaCom.Get(e)
btnSta.PressDown = btnPreOper.Down
btnPreOper.Start, btnPreOper.OperateTime = false, 0
}
}
}
})
}
// 查找按钮
func queryButton(w ecs.World, btnId string) *ecs.Entry {
var buttonEntry *ecs.Entry = nil
btnIdOperQuery.Each(w, func(e *ecs.Entry) {
if id := btnIdCom.Get(e).Id; id == btnId {
buttonEntry = e
}
})
return buttonEntry
}
// 获取按钮设置信息
func getButtonBaseInfo(btnId string) (int64, bool) {
return 15, false
}