rui_orig/canvasView.go

149 lines
3.6 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
import "reflect"
// DrawFunction is the constant for "draw-function" property tag.
//
// Used by `CanvasView`.
// Property sets the draw function of `CanvasView`.
//
// Supported types: `func(Canvas)`.
2024-11-13 12:56:39 +03:00
const DrawFunction PropertyName = "draw-function"
2021-09-07 17:36:50 +03:00
// CanvasView interface of a custom draw view
type CanvasView interface {
View
// Redraw forces CanvasView to redraw its content
2021-09-07 17:36:50 +03:00
Redraw()
}
type canvasViewData struct {
viewData
}
// NewCanvasView creates the new custom draw view
func NewCanvasView(session Session, params Params) CanvasView {
2026-11-05 13:31:38 +03:00
if session == nil {
ErrorLog(`the Session argument cannot be nil`)
return nil
}
2021-09-07 17:36:50 +03:00
view := new(canvasViewData)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newCanvasView(session Session) View {
2024-11-13 12:56:39 +03:00
return new(canvasViewData)
2021-09-07 17:36:50 +03:00
}
// Init initialize fields of ViewsContainer by default values
func (canvasView *canvasViewData) init(session Session) {
canvasView.viewData.init(session)
2021-09-07 17:36:50 +03:00
canvasView.tag = "CanvasView"
2024-11-13 12:56:39 +03:00
canvasView.normalize = normalizeCanvasViewTag
canvasView.set = canvasView.setFunc
canvasView.remove = canvasView.removeFunc
2024-12-10 18:23:04 +03:00
canvasView.changed = canvasView.propertyChanged
2022-05-22 12:54:02 +03:00
}
2024-11-13 12:56:39 +03:00
func normalizeCanvasViewTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
2021-09-07 17:36:50 +03:00
switch tag {
case "draw-func":
tag = DrawFunction
}
return tag
}
func (canvasView *canvasViewData) removeFunc(tag PropertyName) []PropertyName {
2021-09-07 17:36:50 +03:00
if tag == DrawFunction {
if canvasView.getRaw(DrawFunction) != nil {
canvasView.setRaw(DrawFunction, nil)
//canvasView.Redraw()
2024-11-13 12:56:39 +03:00
return []PropertyName{DrawFunction}
}
return []PropertyName{}
2021-09-07 17:36:50 +03:00
}
return canvasView.viewData.removeFunc(tag)
2021-09-07 17:36:50 +03:00
}
func (canvasView *canvasViewData) setFunc(tag PropertyName, value any) []PropertyName {
2021-09-07 17:36:50 +03:00
if tag == DrawFunction {
switch value := value.(type) {
case func(Canvas):
canvasView.setRaw(DrawFunction, value)
case string:
canvasView.setRaw(DrawFunction, value)
default:
2021-09-07 17:36:50 +03:00
notCompatibleType(tag, value)
2024-11-13 12:56:39 +03:00
return nil
}
return []PropertyName{DrawFunction}
2021-09-07 17:36:50 +03:00
}
return canvasView.viewData.setFunc(tag, value)
2021-09-07 17:36:50 +03:00
}
2024-12-10 18:23:04 +03:00
func (canvasView *canvasViewData) propertyChanged(tag PropertyName) {
if tag == DrawFunction {
canvasView.Redraw()
} else {
canvasView.viewData.propertyChanged(tag)
}
}
2021-09-07 17:36:50 +03:00
func (canvasView *canvasViewData) htmlTag() string {
return "canvas"
}
func (canvasView *canvasViewData) Redraw() {
2024-11-13 12:56:39 +03:00
canvas := newCanvas(canvasView)
canvas.ClearRect(0, 0, canvasView.frame.Width, canvasView.frame.Height)
if value := canvasView.getRaw(DrawFunction); value != nil {
switch drawer := value.(type) {
case func(Canvas):
2024-11-13 12:56:39 +03:00
drawer(canvas)
case string:
bind := canvasView.binding()
if bind == nil {
ErrorLogF(`There is no a binding object for call "%s"`, drawer)
break
}
val := reflect.ValueOf(bind)
method := val.MethodByName(drawer)
if !method.IsValid() {
ErrorLogF(`The "%s" method is not valid`, drawer)
break
}
methodType := method.Type()
if methodType.NumIn() == 1 && equalType(methodType.In(0), reflect.TypeOf(canvas)) {
method.Call([]reflect.Value{reflect.ValueOf(canvas)})
} else {
ErrorLogF(`Unsupported prototype of "%s" method`, drawer)
}
2021-09-07 17:36:50 +03:00
}
}
2024-11-13 12:56:39 +03:00
canvas.finishDraw()
2021-09-07 17:36:50 +03:00
}
func (canvasView *canvasViewData) onResize(self View, x, y, width, height float64) {
canvasView.viewData.onResize(self, x, y, width, height)
canvasView.Redraw()
}
// RedrawCanvasView finds CanvasView with canvasViewID and redraws it
func RedrawCanvasView(rootView View, canvasViewID string) {
if canvas := CanvasViewByID(rootView, canvasViewID); canvas != nil {
canvas.Redraw()
}
}