rui_orig/canvasView.go

108 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
// 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 {
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
2021-09-07 17:36:50 +03:00
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 {
2024-11-13 12:56:39 +03:00
if fn, ok := value.(func(Canvas)); ok {
canvasView.setRaw(DrawFunction, fn)
2021-09-07 17:36:50 +03:00
} else {
notCompatibleType(tag, value)
2024-11-13 12:56:39 +03:00
return nil
}
canvasView.Redraw()
2024-11-13 12:56:39 +03:00
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
}
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 {
if drawer, ok := value.(func(Canvas)); ok {
drawer(canvas)
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()
}
}